neat

package module
v0.4.3 Latest Latest
Warning

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

Go to latest
Published: Apr 21, 2024 License: MIT Imports: 10 Imported by: 0

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

Constants

This section is empty.

Variables

This section is empty.

Functions

func AddRandomNeuron added in v0.4.3

func AddRandomNeuron(g *Genotype, counter *goevo.Counter, activations ...goevo.Activation) bool

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

func AddRandomSynapse(g *Genotype, counter *goevo.Counter, weightStd float64, recurrent bool) bool

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

func MutateRandomSynapse(g *Genotype, std float64) bool

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

func RemoveRandomSynapse(g *Genotype) bool

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

func ResetRandomSynapse(g *Genotype) bool

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) Build

func (g *Genotype) Build() goevo.Forwarder

Build a NEATPhenotype from a NEATGenotype.

func (*Genotype) Clone

func (g *Genotype) Clone() any

Clone returns a new genotype that is an exact copy of this genotype.

func (*Genotype) MarshalJSON

func (g *Genotype) MarshalJSON() ([]byte, error)

MarshalJSON implements json.Marshaler, allowing the genotype to be marshalled to JSON.

func (*Genotype) NumHiddenNeurons

func (g *Genotype) NumHiddenNeurons() int

NumHiddenNeurons returns the number of hidden neurons in the genotype.

func (*Genotype) NumInputNeurons

func (g *Genotype) NumInputNeurons() int

NumInputNeurons returns the number of input neurons in the genotype.

func (*Genotype) NumNeurons

func (g *Genotype) NumNeurons() int

NumNeurons returns the total number of neurons in the genotype.

func (*Genotype) NumOutputNeurons

func (g *Genotype) NumOutputNeurons() int

NumOutputNeurons returns the number of output neurons in the genotype.

func (*Genotype) NumSynapses

func (g *Genotype) NumSynapses() int

NumSynapses returns the total number of synapses in the genotype.

func (*Genotype) RenderDot

func (g *Genotype) RenderDot(width, height float64) string

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

func (g *Genotype) RenderImage(width, height float64) image.Image

RenderImage returns an image of the genotype using graphviz.

func (*Genotype) UnmarshalJSON

func (g *Genotype) UnmarshalJSON(bs []byte) error

UnmarshalJSON implements json.Unmarshaler, allowing the genotype to be unmarshalled from JSON.

TODO(Needs more validation)

func (*Genotype) Validate

func (g *Genotype) Validate() error

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 NeuronID

type NeuronID int

NeuronID is the unique identifier for a neuron in a NEATGenotype

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.

func (*Phenotype) Forward

func (p *Phenotype) Forward(x []float64) []float64

Forward propagate inputs through the network, returning the resulting outputs.

func (*Phenotype) Reset

func (p *Phenotype) Reset()

Reset will clear the recurrent memories of the phenotype.

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.

type SynapseEP

type SynapseEP struct {
	From NeuronID
	To   NeuronID
}

SynapseEP is the endpoints of a synapse in a NEATGenotype

type SynapseID

type SynapseID int

SynapseID is the unique identifier for a synapse in a NEATGenotype

Jump to

Keyboard shortcuts

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