本文整理匯總了Golang中github.com/handcraftsman/GeneticGo.Solver.GetBestUsingHillClimbing方法的典型用法代碼示例。如果您正苦於以下問題:Golang Solver.GetBestUsingHillClimbing方法的具體用法?Golang Solver.GetBestUsingHillClimbing怎麽用?Golang Solver.GetBestUsingHillClimbing使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類github.com/handcraftsman/GeneticGo.Solver
的用法示例。
在下文中一共展示了Solver.GetBestUsingHillClimbing方法的5個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Golang代碼示例。
示例1: main
func main() {
resources := []resource{
{name: "Bark", value: 3000, weight: 0.3, volume: .025},
{name: "Herb", value: 1800, weight: 0.2, volume: .015},
{name: "Root", value: 2500, weight: 2.0, volume: .002},
}
const maxWeight = 25.0
const maxVolume = .25
geneSet := "0123456789ABCDEFGH"
calc := func(candidate string) int {
decoded := decodeGenes(candidate, resources, geneSet)
return getFitness(decoded, maxWeight, maxVolume)
}
start := time.Now()
disp := func(candidate string) {
decoded := decodeGenes(candidate, resources, geneSet)
fitness := getFitness(decoded, maxWeight, maxVolume)
display(decoded, fitness, time.Since(start))
}
var solver = new(genetic.Solver)
solver.MaxSecondsToRunWithoutImprovement = .1
solver.MaxRoundsWithoutImprovement = 2
var best = solver.GetBestUsingHillClimbing(calc, disp, geneSet, 10, 2, math.MaxInt32)
fmt.Println("\nFinal:")
disp(best)
}
示例2: main
func main() {
startX := fieldWidth / 2
startY := fieldHeight / 2
calc := func(candidate string) int {
field, program := evaluate(candidate, startX, startY)
fitness := getFitness(field.numberOfSquaresMowed, program.numberOfInstructions())
return fitness
}
start := time.Now()
disp := func(candidate string) {
field, program := evaluate(candidate, startX, startY)
fitness := getFitness(field.numberOfSquaresMowed, program.numberOfInstructions())
display(field, program, fitness, startX, startY, time.Since(start))
}
var solver = new(genetic.Solver)
solver.MaxSecondsToRunWithoutImprovement = 1
solver.MaxRoundsWithoutImprovement = 10
var best = solver.GetBestUsingHillClimbing(calc, disp, geneSet, maxMowerActions, 1, maxFitness)
fmt.Print("\nFinal: ")
disp(best)
}
示例3: main
func main() {
flag.Parse()
if flag.NArg() != 1 {
fmt.Println("Usage: go run standard.go RESOURCEFILEPATH")
return
}
var resourceFileName = flag.Arg(0)
if !File.Exists(resourceFileName) {
fmt.Println("file " + resourceFileName + " does not exist.")
return
}
fmt.Println("using resource file: " + resourceFileName)
resources, maxWeight, solution := loadResources(resourceFileName)
optimalFitness := 0
for resource, count := range solution {
optimalFitness += resource.value * count
}
calc := func(candidate string) int {
decoded := decodeGenes(candidate, resources)
return getFitness(decoded, maxWeight, optimalFitness)
}
start := time.Now()
disp := func(candidate string) {
decoded := decodeGenes(candidate, resources)
fitness := getFitness(decoded, maxWeight, optimalFitness)
display(decoded, fitness, time.Since(start), true)
}
var solver = new(genetic.Solver)
solver.MaxSecondsToRunWithoutImprovement = 5
solver.MaxRoundsWithoutImprovement = 3
var best = solver.GetBestUsingHillClimbing(calc, disp, hexLookup, 10, numberOfGenesPerChromosome, optimalFitness)
fmt.Print("\nFinal: ")
decoded := decodeGenes(best, resources)
fitness := getFitness(decoded, maxWeight, optimalFitness)
display(decoded, fitness, time.Since(start), false)
if fitness == optimalFitness {
fmt.Println("-- that's the optimal solution!")
} else {
percentOptimal := float32(100) * float32(fitness) / float32(optimalFitness)
fmt.Printf("-- that's %f%% optimal\n", percentOptimal)
}
}
示例4: main
func main() {
wanted := []string{"AL", "AK", "AS", "AZ", "AR"}
unwanted := []string{"AA"}
geneSet := getUniqueCharacters(wanted) + regexSpecials
calc := func(candidate string) int {
return calculate(wanted, unwanted, geneSet, candidate)
}
start := time.Now()
disp := func(candidate string) {
fmt.Println(candidate,
"\t",
calc(candidate),
"\t",
time.Since(start))
}
var solver = new(genetic.Solver)
solver.MaxSecondsToRunWithoutImprovement = .5
solver.MaxRoundsWithoutImprovement = 3
var best = solver.GetBestUsingHillClimbing(calc, disp, geneSet, 10, 1, math.MaxInt32)
matches, misses := getMatchResults(wanted, unwanted, geneSet, best)
if matches == len(wanted) && misses == 0 {
fmt.Println("\nsolved with: " + best)
} else {
fmt.Println("\nfailed to find a solution")
fmt.Println("consider increasing the following:")
fmt.Println("\tsolver.MaxSecondsToRunWithoutImprovement")
fmt.Println("\tsolver.MaxRoundsWithoutImprovement")
}
fmt.Print("Total time: ")
fmt.Println(time.Since(start))
}
示例5: main
func main() {
clearImages()
startX := fieldWidth / 2
startY := fieldHeight / 2
flowerPoints := createFlowerPoints()
calc := func(candidate string) int {
field := NewField(fieldWidth, fieldHeight, flowerPoints)
bee := NewBee(startX, startY)
program := evaluate(candidate, bee, field, startX, startY)
fitness := getFitness(field.numberOfFlowersFound, program.numberOfInstructions())
return fitness
}
start := time.Now()
disp := func(candidate string) {
field := NewField(fieldWidth, fieldHeight, flowerPoints)
bee := NewBee(startX, startY)
program := evaluate(candidate, bee, field, startX, startY)
fitness := getFitness(field.numberOfFlowersFound, program.numberOfInstructions())
display(bee, flowerPoints, program, fitness, startX, startY, time.Since(start))
}
var solver = new(genetic.Solver)
solver.MaxSecondsToRunWithoutImprovement = 3
solver.MaxRoundsWithoutImprovement = 3
solver.PrintDiagnosticInfo = true
solver.NumberOfConcurrentEvolvers = 1 // 3
// solver.MaxProcs = 12
var best = solver.GetBestUsingHillClimbing(calc, disp, geneSet, maxBeeActions, 4, maxFitness)
fmt.Print("\nFinal: ")
disp(best)
}