Documentation ¶
Overview ¶
Package neat provides a genotype for a neural network using the NEAT algorithm. This does not provide the NEAT population, that is in pop/neatpop
Index ¶
- func AddRandomNeuron(g *Genotype, counter *goevo.Counter, activations ...goevo.Activation) bool
- func AddRandomSynapse(g *Genotype, counter *goevo.Counter, weightStd float64, recurrent bool) bool
- func MutateRandomActivation(g *Genotype, activations ...goevo.Activation) bool
- func MutateRandomSynapse(g *Genotype, std float64) bool
- func RemoveRandomSynapse(g *Genotype) bool
- func ResetRandomSynapse(g *Genotype) bool
- type AsexualCrossoverStrategy
- type Genotype
- func (g *Genotype) Build() goevo.Forwarder
- func (g *Genotype) Clone() any
- func (g *Genotype) MarshalJSON() ([]byte, error)
- func (g *Genotype) NumHiddenNeurons() int
- func (g *Genotype) NumInputNeurons() int
- func (g *Genotype) NumNeurons() int
- func (g *Genotype) NumOutputNeurons() int
- func (g *Genotype) NumSynapses() int
- func (g *Genotype) RenderDot(width, height float64) string
- func (g *Genotype) RenderImage(width, height float64) image.Image
- func (g *Genotype) UnmarshalJSON(bs []byte) error
- func (g *Genotype) Validate() error
- type NeuronID
- type Phenotype
- type SimpleCrossoverStrategy
- type StdMutation
- type SynapseEP
- type SynapseID
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func AddRandomNeuron ¶ added in v0.4.3
AddRandomNeuron adds a new neuron to the genotype on a random forward synapse. It will return false if there are no forward synapses to add to. The new neuron will have a random activation function from the given list of activations.
func AddRandomSynapse ¶ added in v0.4.3
AddRandomSynapse adds a new synapse to the genotype between two nodes. It will return false if it failed to find a place to put the synapse after 10 tries. The synapse will have a random weight from a normal distribution with the given standard deviation. If recurrent is true, the synapse will be recurrent, otherwise it will not.
func MutateRandomActivation ¶ added in v0.4.3
func MutateRandomActivation(g *Genotype, activations ...goevo.Activation) bool
MutateRandomActivation will change the activation function of a random hidden neuron to a random activation function from the given list of activations. It will return false if there are no hidden neurons to mutate.
func MutateRandomSynapse ¶ added in v0.4.3
MutateRandomSynapse will change the weight of a random synapse by a random amount from a normal distribution with the given standard deviation. It will return false if there are no synapses to mutate.
func RemoveRandomSynapse ¶ added in v0.4.3
RemoveRandomSynapse will remove a random synapse from the genotype. It will return false if there are no synapses to remove.
func ResetRandomSynapse ¶ added in v0.4.3
ResetRandomSynapse will reset the weight of a random synapse to 0. It will return false if there are no synapses to reset.
Types ¶
type AsexualCrossoverStrategy ¶ added in v0.4.3
type AsexualCrossoverStrategy struct{}
func (*AsexualCrossoverStrategy) Crossover ¶ added in v0.4.3
func (s *AsexualCrossoverStrategy) Crossover(gs []*Genotype) *Genotype
func (*AsexualCrossoverStrategy) NumParents ¶ added in v0.4.3
func (s *AsexualCrossoverStrategy) NumParents() int
type Genotype ¶
type Genotype struct {
// contains filtered or unexported fields
}
Genotype is a genotype for a neural network using the NEAT algorithm. It is conceptually similar to the DNA of an organism: it encodes how to build a neural network, but is not the neural network itself. This means if you want to actually run the neural network, you need to use the Genotype.Build method to create a Phenotype.
func NewGenotype ¶
func NewGenotype(counter *goevo.Counter, inputs, outputs int, outputActivation goevo.Activation) *Genotype
NewGenotype creates a new NEATGenotype with the given number of inputs and outputs, and the given output activation function. All output neurons will have the same activation function, and all input neurons will have the linear activation function. The genotype will have no synapses.
func (*Genotype) MarshalJSON ¶
MarshalJSON implements json.Marshaler, allowing the genotype to be marshalled to JSON.
func (*Genotype) NumHiddenNeurons ¶
NumHiddenNeurons returns the number of hidden neurons in the genotype.
func (*Genotype) NumInputNeurons ¶
NumInputNeurons returns the number of input neurons in the genotype.
func (*Genotype) NumNeurons ¶
NumNeurons returns the total number of neurons in the genotype.
func (*Genotype) NumOutputNeurons ¶
NumOutputNeurons returns the number of output neurons in the genotype.
func (*Genotype) NumSynapses ¶
NumSynapses returns the total number of synapses in the genotype.
func (*Genotype) RenderDot ¶
RenderDot returns a string in the DOT language that represents the genotype. This DOT code cannot be use to recreate the genotype, but can be used to visualise it using Graphviz.
func (*Genotype) RenderImage ¶
RenderImage returns an image of the genotype using graphviz.
func (*Genotype) UnmarshalJSON ¶
UnmarshalJSON implements json.Unmarshaler, allowing the genotype to be unmarshalled from JSON.
TODO(Needs more validation)
func (*Genotype) Validate ¶
Validate runs as many checks as possible to check the genotype is valid. It is really only designed to be used as part of a test suite to catch errors with the package. This should never throw an error, but if it does either there is a bug in the package, or the user has somehow invalidated the genotype.
type Phenotype ¶
type Phenotype struct {
// contains filtered or unexported fields
}
Phenotype is a phenotype for a NEAT genotype. It conceptually represents a neural network, built according to the instructions in the NEATGenotype (DNA). Once built, the Phenotype can be used to forward propagate inputs through the network, but it cannot be modified though mutation or corss-over.
type SimpleCrossoverStrategy ¶ added in v0.4.3
type SimpleCrossoverStrategy struct{}
func (*SimpleCrossoverStrategy) Crossover ¶ added in v0.4.3
func (s *SimpleCrossoverStrategy) Crossover(gs []*Genotype) *Genotype
Crossover implements goevo.CrossoverStrategy.
func (*SimpleCrossoverStrategy) NumParents ¶ added in v0.4.3
func (s *SimpleCrossoverStrategy) NumParents() int
NumParents implements goevo.CrossoverStrategy.
type StdMutation ¶ added in v0.4.3
type StdMutation struct { // The standard deviation for the number of new synapses StdNumNewSynapses float64 // The standard deviation for the number of new recurrent synapses StdNumNewRecurrentSynapses float64 // The standard deviation for the number of new neurons StdNumNewNeurons float64 // The standard deviation for the number of synapses to mutate StdNumMutateSynapses float64 // The standard deviation for the number of synapses to prune StdNumPruneSynapses float64 // The standard deviation for the number of activations to mutate StdNumMutateActivations float64 // The standard deviation for the weight of new synapses StdNewSynapseWeight float64 // The standard deviation for the weight of mutated synapses StdMutateSynapseWeight float64 // The maximum number of hidden neurons this mutation can add MaxHiddenNeurons int // The counter to use for new synapse IDs Counter *goevo.Counter // The possible activations to use for new neurons PossibleActivations []goevo.Activation }
StdMutation is a reproduction strategy that uses a standard deviation for the number of mutations in each category. The standard deviation is not scaled by the size of the network, meaning that larger networks will tend to have more mutations than smaller networks.
func (*StdMutation) Mutate ¶ added in v0.4.3
func (r *StdMutation) Mutate(g *Genotype)
Reproduce creates a new genotype by crossing over and mutating the given genotypes.