import "github.com/rrs/goga"
func Evolve(
popSize, nGenes, nRuns int,
initialiser Initialiser,
evaluator Evaluator,
selecter Selector,
mater Mater,
printPop bool)
func Print(p Population)
prints out each individual
type Crosser interface {
Cross(father, mother *Individual) (*Individual, *Individual)
}Crosser defines a cross function between two individuals returns two children
func NewSinglePointCrossover(chance float64, nGenes int) Crosser
func NewUniformCrossover(chance float64, nGenes int) Crosser
type Evaluator interface {
Evaluate(p Population) (Population, bool)
}handles problem specific evaluation returns a bool value to alert the caller to succesfully achieving the termination condition and retuns Population to allow the use of elitism (see allones example)
type Gene interface {
Clone() Gene
Mutate()
String() string
}Clone: return a deep copy Mutate: handles gene specific mutation String: return a printable version of the data
type Individual struct {
Genes []Gene
Fitness int
}holds a slice of genes and a fitness value it is possible to contain any number of types of genes
func (individual Individual) String() string
printable version of the individual
type Initialiser interface {
Init(p Population, nGenes int)
}handles problem specific initialisation takes an empty population and fills it with initialised *Individual
type Mater interface {
Mate(Population, Population) Population
}Combines the crossover and mutation steps
func NewParallelMater(crosser Crosser, mutator Mutator) Mater
func NewProceduralMater(crosser Crosser, mutator Mutator) Mater
type Mutator interface {
Mutate(*Individual)
}
func NewSimpleMutator(chance float64) Mutator
type ParallelMater struct {
// contains filtered or unexported fields
}uses crossover and mutation in a parallel manner
func (mp *ParallelMater) Mate(pop, parents Population) Population
type Population []*Individual
Population has utility methods which are useful on a slice of type *Individual
func (p Population) Contains(individual *Individual) bool
returns true if individual is contained within Population p
func (p Population) Delete(individual *Individual)
removes the individual if it exists in the Population p
func (p Population) DeleteI(i int)
removes the individual at index i and shortens the slice
type ProceduralMater struct {
// contains filtered or unexported fields
}uses crossover and mutation in a procedural manner
func (mp *ProceduralMater) Mate(pop, parents Population) Population
type RouletteSelection struct {
// contains filtered or unexported fields
}
func (rs *RouletteSelection) Select(pop Population) Population
type Selector interface {
Select(population Population) Population
}A Selecter is used for the selection process, select returns a group of individuals selected by some probabilistic method giving a higher chance to individuals with a higher fitness
func NewRouletteSelection(nParents int) Selector
func NewTournamentSelection(nParents, tounamentSize int) Selector
type SimpleMutator struct {
// contains filtered or unexported fields
}
func (sm *SimpleMutator) Mutate(individual *Individual)
type SinglePointCrossover struct {
// contains filtered or unexported fields
}Simple single point crossover will with probability chance randomly pick a point between 1 and nGenes - 1 to cross the individuals
func (spc *SinglePointCrossover) Cross(father, mother *Individual) (*Individual, *Individual)
type TournamentSelection struct {
// contains filtered or unexported fields
}Tounament selection selects a group of individuals of size tournamentSize completely at random from the population, then the fittest individual is selected from that group, the lower the group size the higher the selection pressure on weak individuals, this can be quite desirable.
func (ts *TournamentSelection) Select(pop Population) Population
type UniformCrossover struct {
// contains filtered or unexported fields
}Uniform crossover will cross each gene posistion in the two individuals with probability chance
func (uc *UniformCrossover) Cross(father, mother *Individual) (*Individual, *Individual)
mater.go ga.go crossover.go population.go mutator.go selector.go individual.go evolve.go