Skip to content

handcraftsman/GeneticGo

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

40 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

GeneticGo - genetic algorithm library written in Go

This genetic solver adaptively adjusts the strategies being used to those that are successful at improving results.

Usage

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
numberOfChromosomes := 10 // you decide
numberOfGenesInAChromosome := 1 // you decide

if your problem can be solved with a fixed number of genes:

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
maxNumberOfGenes := 50 // you decide

var result = solver.GetBestUsingHillClimbing(getFitness, display, geneSet, maxNumberOfGenes, numberOfGenesInAChromosome, bestPossibleFitness)

Sample programs (in order of genetic complexity)

  • string_duplication.go - duplicates a string, see related blog post

    go run samples/string_duplication.go

  • 8queens.go - solves the 8 Queens Puzzle, see related blog post

    go run samples/8queens.go

  • tsp.go - travelling salesperson problem solver. See related blog post

    prerequisite: go get "github.com/handcraftsman/File"

    go run samples/tsp.go samples/data/tsp/eil51.tsp

  • regex.go - genetically builds a regular expression. See related blog post

    go run samples/regex.go

License

MIT License

About

Genetic problem solver written in Go

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Contributors 3

  •  
  •  
  •  

Languages