This genetic solver adaptively adjusts the strategies being used to those that are successful at improving results.
GeneticGo is compatible with Go 1. Add it to your package repository:
go get "github.com/handcraftsman/GeneticGo"
then use it in your program:
import "github.com/handcraftsman/GeneticGo"
solver := new(genetic.Solver)
solver.MaxSecondsToRunWithoutImprovement = 20 // you decide
solver.LowerFitnessesAreBetter = true // you decide
// create a fitness function
getFitness := func(candidate string) int {
return ?? // evaluate the candidate and return a fitness value
}
// create a display function
display := func(genes string) {
println(??) // provide some output to the user if desired
}
// each gene is a single character
geneSet := "abc123..." // you decide the set of valid genes
numberOfGenesInAChromosome := 1 // you decide
if your problem can be solved with a fixed number of genes:
numberOfChromosomes := 10 // you decide
var result = solver.GetBest(getFitness, display, geneSet, numberOfChromosomes, numberOfGenesInAChromosome)
alternatively, if you want the gene sequence to grow as necessary:
solver.MaxRoundsWithoutImprovement = 10 // you decide
bestPossibleFitness := 0 // you decide
maxNumberOfChromosomes := 50 // you decide
var result = solver.GetBestUsingHillClimbing(getFitness, display, geneSet, maxNumberOfChromosomes, numberOfGenesInAChromosome, bestPossibleFitness)
-
string_duplication.go - duplicates a string, see related blog post
go run samples/string_duplication/string_duplication.go
-
8queens.go - solves the 8 Queens Puzzle, see related blog post
go run samples/8queens/8queens.go
-
tsp.go - travelling salesperson problem solver. See related blog post
prerequisite: go get "github.com/handcraftsman/File"
go run samples/tsp/tsp.go samples/data/tsp/eil51.tsp
-
regex.go - genetically builds a regular expression. See related blog post
go run samples/regex/regex.go