Documentation ¶
Overview ¶
Package cgp implements Cartesian Genetic Programming in Go.
Index ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type CGP ¶
type CGP struct { Options CGPOptions Population []Individual NumEvaluations int // The number of evaluations so far }
func New ¶
func New(options CGPOptions) *CGP
New takes CGPOptions and returns a new CGP object. It panics when a necessary precondition is violated, e.g. when the number of genes is negative.
func (*CGP) RunGeneration ¶
func (cgp *CGP) RunGeneration()
RunGeneration creates offspring from the current parent via mutation, evaluates the offspring using the CGP object's Evaluator and selects the new parent for the following generation.
type CGPFunction ¶
A CGPFunction is a function that is usable in a Genetic Program. It takes zero or more parameters and outputs a single result. For example a CGPFunction could implement binary AND or floating point multiplication.
type CGPOptions ¶
type CGPOptions struct { PopSize int // Population Size NumGenes int // Number of Genes MutationRate float64 // Mutation Rate NumInputs int // The number of Inputs NumOutputs int // The number of Outputs MaxArity int // The maximum Arity of the CGPFunctions in FunctionList FunctionList []CGPFunction // The functions used in evolution RandConst RndConstFunction // The function supplying constants Evaluator EvalFunction // The evaluator that assigns a fitness to an individual Rand *rand.Rand // An instance of rand.Rand that is used throughout cgp to make runs repeatable }
CGPOptions is a struct describing the options of a CGP run.
type EvalFunction ¶
type EvalFunction func(Individual) float64
The EvalFunction takes one Individual and returns its fitness value.
type Individual ¶
type Individual struct { Genes []gene // The function genes Outputs []int // The output genes Options *CGPOptions // A pointer to the CGPOptions. Necessary to retrieve e.g. the mutation rate. Fitness float64 // The fitness of the individual // contains filtered or unexported fields }
An Individual represents the genetic code of an evolved program. It contains function genes and output genes and can hold the fitness of the evolved program.
func NewIndividual ¶
func NewIndividual(options *CGPOptions) (ind Individual)
NewIndividual creates a random valid program with the options as specified.
func (*Individual) CacheID ¶
func (ind *Individual) CacheID() string
CacheID returns the functional ID of ind. Two individuals that have the same CacheID are guaranteed to compute the same function. Note that individuals that differ in their inactive genes but are identical in their active genes will have the same CacheID.
func (Individual) Mutate ¶
func (ind Individual) Mutate() (mutant Individual)
Mutate returns a mutated copy of the Individual.
func (Individual) Run ¶
func (ind Individual) Run(input []float64) []float64
Run executes the evolved program with the given input.
type RndConstFunction ¶
RndConstFunction takes a PRNG as input and outputs a random number that is used as a constant in the evolved program. This allows you to set the range and type (integers vs. floating point) of constants used during evolution. For example, if you are evolving programs that create RGB images you might constrain the RndConstFunction to return integer values between 0 and 255.