本文整理匯總了Golang中github.com/handcraftsman/GeneticGo.Solver.LowerFitnessesAreBetter方法的典型用法代碼示例。如果您正苦於以下問題:Golang Solver.LowerFitnessesAreBetter方法的具體用法?Golang Solver.LowerFitnessesAreBetter怎麽用?Golang Solver.LowerFitnessesAreBetter使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類github.com/handcraftsman/GeneticGo.Solver
的用法示例。
在下文中一共展示了Solver.LowerFitnessesAreBetter方法的1個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Golang代碼示例。
示例1: 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))
}