neat

package
v4.2.0 Latest Latest
Warning

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

Go to latest
Published: Aug 30, 2024 License: MIT Imports: 14 Imported by: 3

Documentation

Overview

Package neat implements the NeuroEvolution of Augmenting Topologies (NEAT) method, which can be used to evolve specific Artificial Neural Networks from scratch using genetic algorithms.

Index

Constants

View Source
const NumTraitParams = 8

NumTraitParams The number of parameters used in neurons that learn through habituation, sensitization or Hebbian-type processes

Variables

View Source
var (
	// LogLevel The current log level of the context
	LogLevel LoggerLevel

	// DebugLog The logger to output all messages
	DebugLog = func(message string) {
		if acceptLogLevel(LogLevel, LogLevelDebug) {
			_ = loggerDebug.Output(2, message)
		}
	}
	// InfoLog The logger to output messages with Info and up level
	InfoLog = func(message string) {
		if acceptLogLevel(LogLevel, LogLevelInfo) {
			_ = loggerInfo.Output(2, message)
		}
	}
	// WarnLog The logger to output messages with Warn and up level
	WarnLog = func(message string) {
		if acceptLogLevel(LogLevel, LogLevelWarning) {
			_ = loggerWarn.Output(2, message)
		}
	}
	// ErrorLog The logger to output messages with Error and up level
	ErrorLog = func(message string) {
		if acceptLogLevel(LogLevel, LogLevelError) {
			_ = loggerError.Output(2, message)
		}
	}
)
View Source
var (
	ErrNoActivatorsRegistered                = errors.New("no node activators registered with NEAT options, please assign at least one to NodeActivators")
	ErrActivatorsProbabilitiesNumberMismatch = errors.New("number of node activator probabilities doesn't match number of activators")
)
View Source
var ErrNEATOptionsNotFound = errors.New("NEAT options not found in the context")
View Source
var (
	ErrTraitsParametersCountMismatch = errors.New("traits parameters number mismatch")
)

Functions

func InitLogger

func InitLogger(level string) error

InitLogger is to initialize logger

func NewContext

func NewContext(ctx context.Context, opts *Options) context.Context

NewContext returns a new Context that carries value of NEAT options.

Types

type EpochExecutorType

type EpochExecutorType string

EpochExecutorType is to define the type of epoch evaluator

const (
	EpochExecutorTypeSequential EpochExecutorType = "sequential"
	EpochExecutorTypeParallel   EpochExecutorType = "parallel"
)

func (EpochExecutorType) Validate

func (e EpochExecutorType) Validate() error

Validate is to check is this executor type is supported by algorithm

type GenomeCompatibilityMethod

type GenomeCompatibilityMethod string

GenomeCompatibilityMethod defines the method to calculate genomes compatibility

const (
	GenomeCompatibilityMethodLinear GenomeCompatibilityMethod = "linear"
	GenomeCompatibilityMethodFast   GenomeCompatibilityMethod = "fast"
)

func (GenomeCompatibilityMethod) Validate

func (g GenomeCompatibilityMethod) Validate() error

Validate is to check if this genome compatibility method supported by algorithm

type LoggerLevel

type LoggerLevel string

LoggerLevel type to specify logger output level

const (
	// LogLevelDebug The Debug log level
	LogLevelDebug LoggerLevel = "debug"
	// LogLevelInfo The Info log level
	LogLevelInfo LoggerLevel = "info"
	// LogLevelWarning The Warning log level
	LogLevelWarning LoggerLevel = "warn"
	// LogLevelError The Error log level
	LogLevelError LoggerLevel = "error"
)

type Options

type Options struct {
	// Probability of mutating a single trait param
	TraitParamMutProb float64 `yaml:"trait_param_mut_prob"`
	// Power of mutation on a single trait param
	TraitMutationPower float64 `yaml:"trait_mutation_power"`
	// The power of a link weight mutation
	WeightMutPower float64 `yaml:"weight_mut_power"`

	// These 3 global coefficients are used to determine the formula for
	// computing the compatibility between 2 genomes.  The formula is:
	// disjoint_coeff * pdg + excess_coeff * peg + mutdiff_coeff * mdmg.
	// See the compatibility method in the Genome class for more info
	// They can be thought of as the importance of disjoint Genes,
	// excess Genes, and parametric difference between Genes of the
	// same function, respectively.
	DisjointCoeff float64 `yaml:"disjoint_coeff"`
	ExcessCoeff   float64 `yaml:"excess_coeff"`
	MutdiffCoeff  float64 `yaml:"mutdiff_coeff"`

	// This global tells compatibility threshold under which
	// two Genomes are considered the same species
	CompatThreshold float64 `yaml:"compat_threshold"`

	// How much does age matter? Gives a fitness boost up to some young age (niching).
	// If it is 1, then young species get no fitness boost.
	AgeSignificance float64 `yaml:"age_significance"`
	// Percent of average fitness for survival, how many get to reproduce based on survival_thresh * pop_size
	SurvivalThresh float64 `yaml:"survival_thresh"`

	// Probabilities of a non-mating reproduction
	MutateOnlyProb         float64 `yaml:"mutate_only_prob"`
	MutateRandomTraitProb  float64 `yaml:"mutate_random_trait_prob"`
	MutateLinkTraitProb    float64 `yaml:"mutate_link_trait_prob"`
	MutateNodeTraitProb    float64 `yaml:"mutate_node_trait_prob"`
	MutateLinkWeightsProb  float64 `yaml:"mutate_link_weights_prob"`
	MutateToggleEnableProb float64 `yaml:"mutate_toggle_enable_prob"`
	MutateGeneReenableProb float64 `yaml:"mutate_gene_reenable_prob"`
	MutateAddNodeProb      float64 `yaml:"mutate_add_node_prob"`
	MutateAddLinkProb      float64 `yaml:"mutate_add_link_prob"`
	// probability of mutation involving disconnected inputs connection
	MutateConnectSensors float64 `yaml:"mutate_connect_sensors"`

	// Probabilities of a mate being outside species
	InterspeciesMateRate  float64 `yaml:"interspecies_mate_rate"`
	MateMultipointProb    float64 `yaml:"mate_multipoint_prob"`
	MateMultipointAvgProb float64 `yaml:"mate_multipoint_avg_prob"`
	MateSinglepointProb   float64 `yaml:"mate_singlepoint_prob"`

	// Prob. of mating without mutation
	MateOnlyProb float64 `yaml:"mate_only_prob"`
	// Probability of forcing selection of ONLY links that are naturally recurrent
	RecurOnlyProb float64 `yaml:"recur_only_prob"`

	// Size of population
	PopSize int `yaml:"pop_size"`
	// Age when Species starts to be penalized
	DropOffAge int `yaml:"dropoff_age"`
	// Number of tries mutate_add_link will attempt to find an open link
	NewLinkTries int `yaml:"newlink_tries"`

	// Tells to print population to file every n generations
	PrintEvery int `yaml:"print_every"`

	// The number of babies to stolen off to the champions
	BabiesStolen int `yaml:"babies_stolen"`

	// The number of runs to average over in an experiment
	NumRuns int `yaml:"num_runs"`

	// The number of epochs (generations) to execute training
	NumGenerations int `yaml:"num_generations"`

	// The epoch's executor type to apply (sequential, parallel)
	EpochExecutorType EpochExecutorType `yaml:"epoch_executor"`
	// The genome compatibility testing method to use (linear, fast (make sense for large genomes))
	GenCompatMethod GenomeCompatibilityMethod `yaml:"genome_compat_method"`

	// The neuron nodes activation functions list to choose from
	NodeActivators []math.NodeActivationType `yaml:"-"`
	// The probabilities of selection of the specific node activator function
	NodeActivatorsProb []float64 `yaml:"-"`

	// NodeActivatorsWithProbs the list of supported node activation with probability of each one
	NodeActivatorsWithProbs []string `yaml:"node_activators"`

	// LogLevel the log output details level
	LogLevel string `yaml:"log_level"`
}

Options The NEAT algorithm options.

func FromContext

func FromContext(ctx context.Context) (*Options, bool)

FromContext returns the NEAT Options value stored in ctx, if any.

func LoadNeatOptions

func LoadNeatOptions(r io.Reader) (*Options, error)

LoadNeatOptions Loads NEAT options configuration from provided reader encode in plain text format (.neat)

func LoadYAMLOptions

func LoadYAMLOptions(r io.Reader) (*Options, error)

LoadYAMLOptions is to load NEAT options encoded as YAML file

func ReadNeatOptionsFromFile

func ReadNeatOptionsFromFile(configFilePath string) (*Options, error)

ReadNeatOptionsFromFile reads NEAT options from specified configFilePath automatically resolving config file encoding.

func (*Options) NeatContext

func (c *Options) NeatContext() context.Context

NeatContext is to get Context which carries NEAT options inside to be propagated

func (*Options) RandomNodeActivationType

func (c *Options) RandomNodeActivationType() (math.NodeActivationType, error)

RandomNodeActivationType Returns next random node activation type among registered with this context

func (*Options) Validate

func (c *Options) Validate() error

Validate is to validate that this options has valid values

type Trait

type Trait struct {
	// The trait ID
	Id int `yaml:"id"`
	// The learned trait parameters
	Params []float64 `yaml:"params"`
}

Trait is a group of parameters that can be expressed as a group more than one time. Traits save a genetic algorithm from having to search vast parameter landscapes on every node. Instead, each node can simply point to a trait and those traits can evolve on their own.

func NewTrait

func NewTrait() *Trait

NewTrait is to create empty trait with default parameters number (see: NumTraitParams above)

func NewTraitAvrg

func NewTraitAvrg(t1, t2 *Trait) (*Trait, error)

NewTraitAvrg Special Constructor creates a new Trait which is the average of two existing traits passed in

func NewTraitCopy

func NewTraitCopy(t *Trait) *Trait

NewTraitCopy The copy constructor

func (*Trait) Mutate

func (t *Trait) Mutate(traitMutationPower, traitParamMutProb float64)

Mutate perturb the trait parameters slightly

func (*Trait) String

func (t *Trait) String() string

Directories

Path Synopsis
Package genetics holds data holders and helper utilities used to implement genetic evolution algorithm
Package genetics holds data holders and helper utilities used to implement genetic evolution algorithm
Package math defines standard mathematical primitives used by the NEAT algorithm as well as utility functions
Package math defines standard mathematical primitives used by the NEAT algorithm as well as utility functions
Package network provides data structures and utilities to describe Artificial Neural Network and network solvers.
Package network provides data structures and utilities to describe Artificial Neural Network and network solvers.
formats
Package formats defines the serialization formats which can be used for network graph persistence
Package formats defines the serialization formats which can be used for network graph persistence

Jump to

Keyboard shortcuts

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