本文整理匯總了Golang中github.com/handcraftsman/GeneticGo.Solver.GetBest方法的典型用法代碼示例。如果您正苦於以下問題:Golang Solver.GetBest方法的具體用法?Golang Solver.GetBest怎麽用?Golang Solver.GetBest使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類github.com/handcraftsman/GeneticGo.Solver
的用法示例。
在下文中一共展示了Solver.GetBest方法的4個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Golang代碼示例。
示例1: main
func main() {
const genes = " abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ!."
target := "Not all those who wander are lost."
calc := func(candidate string) int {
return calculate(target, candidate)
}
start := time.Now()
disp := func(candidate string) {
fmt.Print(candidate)
fmt.Print("\t")
fmt.Print(calc(candidate))
fmt.Print("\t")
fmt.Println(time.Since(start))
}
var solver = new(genetic.Solver)
solver.MaxSecondsToRunWithoutImprovement = 1
var best = solver.GetBest(calc, disp, genes, len(target), 1)
fmt.Println()
fmt.Println(best)
fmt.Print("Total time: ")
fmt.Println(time.Since(start))
}
示例2: main
func main() {
genes := ""
for i := 0; i < boardWidthHeight; i++ {
genes += strconv.Itoa(i)
}
start := time.Now()
calc := func(candidate string) int {
return getFitness(candidate, boardWidthHeight)
}
disp := func(candidate string) {
display(candidate, boardWidthHeight)
fmt.Print(candidate)
fmt.Print("\t")
fmt.Print(getFitness(candidate, boardWidthHeight))
fmt.Print("\t")
fmt.Println(time.Since(start))
}
var solver = new(genetic.Solver)
solver.MaxSecondsToRunWithoutImprovement = 1
var best = solver.GetBest(calc, disp, genes, boardWidthHeight, 2)
disp(best)
fmt.Print("Total time: ")
fmt.Println(time.Since(start))
}
示例3: main
func main() {
flag.Parse()
if flag.NArg() != 1 {
fmt.Println("Usage: go run samples/tsp.go ROUTEFILEPATH")
return
}
var routeFileName = flag.Arg(0)
if !File.Exists(routeFileName) {
fmt.Println("file " + routeFileName + " does not exist.")
return
}
fmt.Println("using route file: " + routeFileName)
idToPointLookup := readPoints(routeFileName)
fmt.Println("read " + strconv.Itoa(len(idToPointLookup)) + " points...")
calc := func(candidate string) int {
return getFitness(candidate, idToPointLookup)
}
if File.Exists(routeFileName + ".opt.tour") {
fmt.Println("found optimal solution file: " + routeFileName + ".opt")
optimalRoute := readOptimalRoute(routeFileName+".opt.tour", len(idToPointLookup))
fmt.Println("read " + strconv.Itoa(len(optimalRoute)) + " segments in the optimal route")
points := getPointsInOptimalOrder(idToPointLookup, optimalRoute)
genes := genericGeneSet[0:len(idToPointLookup)]
idToPointLookup = make(map[string]Point, len(idToPointLookup))
for i, v := range points {
idToPointLookup[genericGeneSet[i:i+1]] = v
}
fmt.Print("optimal route: " + genes)
fmt.Print("\t")
fmt.Println(getFitness(genes, idToPointLookup))
}
geneSet := genericGeneSet[0:len(idToPointLookup)]
start := time.Now()
disp := func(candidate string) {
fmt.Print(candidate)
fmt.Print("\t")
fmt.Print(getFitness(candidate, idToPointLookup))
fmt.Print("\t")
fmt.Println(time.Since(start))
}
var solver = new(genetic.Solver)
solver.MaxSecondsToRunWithoutImprovement = 20
solver.LowerFitnessesAreBetter = true
var best = solver.GetBest(calc, disp, geneSet, len(idToPointLookup), 1)
fmt.Println()
fmt.Println(best, "\t", getFitness(best, idToPointLookup))
fmt.Print("Total time: ")
fmt.Println(time.Since(start))
}
示例4: main
func main() {
var lengthTable = flag.String("lengthTable", "", "Source length table (2 columns, name<TAB>length)")
var targetLength = flag.Int("targetLength", 1000, "Target length for bins")
var maxBins = flag.Int("maxBins", 10, "Try and have fewer bins than this")
var batchSize = flag.Int("batchSize", 40, "Batch N items at a time. MUST be <90")
var slop = flag.Int("slop", 100, "Allow a certain amount of slop.")
var patience = flag.Int("patience", 0, "Integer 0-5, with the max being Dalai-Lama-level patience")
flag.Parse()
resources := []resource{}
content, err := ioutil.ReadFile(*lengthTable)
if err != nil {
//Do something
panic(err)
}
lines := strings.Split(string(content), "\n")
for _, line := range lines {
data := strings.Split(line, "\t")
if len(data) == 2 {
length, _ := strconv.Atoi(data[1])
resources = append(
resources,
*&resource{
name: data[0],
length: length,
},
)
}
}
geneSet := "qwer[email protected]#$%^&*()<>?|{}[];:',./\\"[0:*batchSize]
fmt.Printf("# Round IDX\tBin Idx\tSum\tFeature IDs\n")
for i := 0; i <= len(resources) / *batchSize; i++ {
min_bound := i * (*batchSize)
max_bound := (i + 1) * (*batchSize)
max_bound = int(math.Min(float64(max_bound), float64(len(resources))))
localResources := resources[min_bound:max_bound]
log.Info(fmt.Sprintf("Processing %d items", len(localResources)))
calc := func(candidate string) int {
decoded := decodeGenes(candidate, localResources, geneSet)
return getFitness(localResources, decoded, *targetLength, *maxBins, *slop)
}
start := time.Now()
disp := func(candidate string) {
decoded := decodeGenes(candidate, localResources, geneSet)
fitness := getFitness(localResources, decoded, *targetLength, *maxBins, *slop)
display(localResources, decoded, fitness, time.Since(start), i, false)
}
var solver = new(genetic.Solver)
solver.MaxSecondsToRunWithoutImprovement = 1 + float64(*patience)*20
solver.MaxRoundsWithoutImprovement = 10 + (*patience)*50
var best = solver.GetBest(calc, disp, geneSet, *maxBins, 32)
log.Info("Final:")
decoded := decodeGenes(best, localResources, geneSet)
fitness := getFitness(localResources, decoded, *targetLength, *maxBins, *slop)
display(localResources, decoded, fitness, time.Since(start), i, true)
}
}