Documentation ¶
Index ¶
Constants ¶
const (
ChromosomeLength = 9 // ChromosomeLength is the number of Genes contained in each Chromosome.
)
const (
GeneLength = 4 // GeneLength is the number of bits that represent a gene string.
)
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type ASTSolver ¶
type ASTSolver struct{}
Solver implementation which parses the expression into an Abstract Syntax Tree and solves using recursion.
func NewASTSolver ¶
func NewASTSolver() *ASTSolver
NewASTSolver initializes and returns a new ASTSolver.
type ByteGene ¶
type ByteGene struct {
Encoded byte
}
ByteGene represents a single gene that has an encoded byte representation.
func NewByteGene ¶
NewByteGene initializes and returns a new ByteGene from a byte.
func (*ByteGene) IsOperator ¶
IsOperator returns true if the gene is an operator.
type GeneDecoder ¶
GeneDecoder defines necessary functions for decoding a Gene.
type GeneDecoderMutator ¶
type GeneDecoderMutator interface { GeneDecoder GeneMutator }
GeneDecoderMutator defines necessary functions for decoding and mutating a Gene.
type GeneMutator ¶
type GeneMutator interface {
Mutate(r float64)
}
GeneMutator defines necessary functions for mutating a Gene.
type Population ¶
type Population struct { Generations uint32 // Generations in the current population // contains filtered or unexported fields }
Population contains the members of a population and the age of the population in generations.
func NewPopulation ¶
func NewPopulation(size int, solver Solver) *Population
NewPopulation initializes and returns a new population with the given size from random data.
func (*Population) Evolve ¶
func (p *Population) Evolve(t int)
Evolve evolves the population by applying selection, crossover, and mutation using the given target, crossover rate, and mutation rate.
func (*Population) Fittest ¶
func (p *Population) Fittest(t int) *StringGenotype
Fittest returns the fittest member of the population for the given target.
func (*Population) Solution ¶
func (p *Population) Solution(t int) (*StringGenotype, bool)
Solution returns the first solution found in the population for the given target and a boolean indicating whether a solution was found.
func (*Population) String ¶
func (p *Population) String() string
String is the fmt.Stringer implementation for Population. Returns the population members as a formatted string.
type Solver ¶
Solver interface defines the necessary functions for solving the equation defined by a decoded genotype.
type StringGene ¶
type StringGene struct {
Encoded string
}
StringGene represents a single gene that has an encoded string representation.
func NewStringGene ¶
func NewStringGene(s string) *StringGene
NewStringGene initializes and returns a new StringGene from a string.
func NewStringGeneFromByte ¶
func NewStringGeneFromByte(b byte) *StringGene
NewStringGeneFromByte initializes and returns a new StringGene from a byte.
func (*StringGene) Crossover ¶
func (g *StringGene) Crossover(o *StringGene, i int) *StringGene
Crossover this StringGene with another StringGene at the provided index to create a new StringGene.
func (*StringGene) Decode ¶
func (g *StringGene) Decode() string
Decode gets the decoded equation part of the gene's encoded string.
func (*StringGene) IsNumeric ¶
func (g *StringGene) IsNumeric() bool
IsNumeric returns true if the gene is a numeric value.
func (*StringGene) IsOperator ¶
func (g *StringGene) IsOperator() bool
IsOperator returns true if the gene is an operator.
func (*StringGene) Mutate ¶
func (g *StringGene) Mutate(r float64)
Mutate the StringGene by iterating over the string "bits" and randomly inverting them according to the given rate.
func (*StringGene) String ¶
func (g *StringGene) String() string
String implementation of the Stringer interface for StringGene.
type StringGenotype ¶
type StringGenotype struct {
Chromosome [ChromosomeLength]*StringGene
}
StringGenotype contains a Chromosome made up of StringGenes - a list of genes that make up a potential solution to a problem.
func NewStringGenotype ¶
func NewStringGenotype() *StringGenotype
NewStringGenotype initializes and returns a new StringGenotype with random data.
func (*StringGenotype) Crossover ¶
func (g *StringGenotype) Crossover(o *StringGenotype, i int) *StringGenotype
Crossover this Genotype with another Genotype at the provided index to create a new Genotype.
func (*StringGenotype) Decoded ¶
func (g *StringGenotype) Decoded() string
ToDecodedString converts the Genotype to a decoded string of values.
func (*StringGenotype) Formula ¶
func (g *StringGenotype) Formula() string
Formula converts the Genotype to a proper formula after discarding nonsensical data.
func (*StringGenotype) Mutate ¶
func (g *StringGenotype) Mutate(r float64) *StringGenotype
Mutate the Genotype by iterating over all bits of the EncodedString and randomly flipping bits according to the mutationRate.
func (*StringGenotype) String ¶
func (g *StringGenotype) String() string
String converts the Genotype to an encoded string of bits.