evolution

package
v0.0.0-...-1f0185d Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jan 23, 2024 License: MIT Imports: 7 Imported by: 0

Documentation

Index

Constants

View Source
const (
	ChromosomeLength = 9 // ChromosomeLength is the number of Genes contained in each Chromosome.
)
View Source
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.

func (*ASTSolver) Solve

func (s *ASTSolver) Solve(e string) (int, error)

SolveExpression takes a string that represents a mathematical expression and returns the solution. Supported operators are +, -, *, and /.

type ByteGene

type ByteGene struct {
	Encoded byte
}

ByteGene represents a single gene that has an encoded byte representation.

func NewByteGene

func NewByteGene(b byte) *ByteGene

NewByteGene initializes and returns a new ByteGene from a byte.

func (*ByteGene) Decode

func (g *ByteGene) Decode() string

Decoded gets the decoded equation part of the gene's encoded byte.

func (*ByteGene) IsNumeric

func (g *ByteGene) IsNumeric() bool

IsNumeric returns true if the gene is a numeric value.

func (*ByteGene) IsOperator

func (g *ByteGene) IsOperator() bool

IsOperator returns true if the gene is an operator.

func (*ByteGene) Mutate

func (g *ByteGene) Mutate(r float64)

Mutate the ByteGene by creating a bitmask and applying it to the encoded byte with an XOR operation.

func (*ByteGene) String

func (g *ByteGene) String() string

String implementation of the Stringer interface for ByteGene.

type GeneDecoder

type GeneDecoder interface {
	fmt.Stringer
	Decode() string
	IsNumeric() bool
	IsOperator() bool
}

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

type Solver interface {
	Solve(string) (int, error)
}

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.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL