Neat

new Neat (inputsopt, outputsopt, datasetopt, options) open an issue

Runs the NEAT algorithm on group of neural networks.

Parameters:
Name Type Default Description
inputs number <optional> 1

Size of input layer of the networks in the population

outputs number <optional> 1

Size of input layer of the networks in the population

dataset Array.<{Array.<inputs:number>, Array.<outputs:number>}> <optional>

Dataset used to train networks in the population at first - other sets of data can be passed to neat.evolve() after constuction

options

Configuration Options

Properties
Name Type Default Description
population_size number <optional> 50

Population size of each generation.

elitism number <optional> 1

Elitism of every evolution loop. Elitism in genetic algortihtms.

provenance number <optional> 0

Number of genomes inserted the original network template (Network(input,output)) per evolution.

mutation_rate number <optional> 0.4

Sets the mutation rate. If set to 0.3, 30% of the new population will be mutated. Default is 0.4.

mutation_amount number <optional> 1

If mutation occurs (randomNumber < mutation_rate), sets amount of times a mutation method will be applied to the network.

cost cost <optional> cost.MSE

Specify the cost function for the evolution, this tells a genome in the population how well it's performing. Default: methods.cost.MSE (recommended).

equal boolean <optional> false

When true crossover parent genomes are assumed to be equally fit and offspring are built with a random amount of neurons within the range of parents' number of neurons. Set to false to select the "fittest" parent as the neuron amount template.

clear number <optional> false

Clear the context of the population's nodes, basically reverting them to 'new' neurons. Useful for predicting timeseries with LSTM's.

growth number <optional> 0.0001

Set the penalty for large networks. Penalty calculation: penalty = (genome.nodes.length + genome.connectoins.length + genome.gates.length) * growth; This penalty will get added on top of the error. Your growth should be a very small number.

amount number <optional> 1

Set the amount of times to test the trainingset on a genome each generation. Useful for timeseries. Do not use for regular feedfoward problems.

fitnessPopulation boolean <optional> false

Flag to return the fitness of a population of genomes. Set this to false to evaluate each genome inidividually.

fitness function <optional>

A fitness function to evaluate the networks. Takes a dataset and a genome i.e. a network or a population i.e. an array of networks and sets the genome .score property

selection string <optional> FITNESS_PROPORTIONATE

Selection method for evolution (e.g. Selection.FITNESS_PROPORTIONATE).

crossover Array <optional>

Sets allowed crossover methods for evolution.

network Network <optional> false

Network to start evolution from

maxNodes number <optional> Infinity

Maximum nodes for a potential network

maxConns number <optional> Infinity

Maximum connections for a potential network

maxGates number <optional> Infinity

Maximum gates for a potential network

mutation Array.<mutation> <optional>

Sets allowed mutation methods for evolution, a random mutation method will be chosen from the array when mutation occurs. Optional, but default methods are non-recurrent

Example
const { Neat } = require("@liquid-carrot/carrot");

// new Neat()
let neat = new Neat()

// new Neat(options)
let neat = new Neat({ population_size: 100 })

// new Neat(dataset)
let neat = new Neat([
  { input: [0, 0], output: [0] },
  { input: [0, 1], output: [1] },
  { input: [1, 0], output: [1] },
  { input: [1, 1], output: [0] }
])

// new Neat(input, output)
let neat = new Neat(64, 10)

// new Neat(dataset, options)
let neat = new Neat([
  { input: [0, 0], output: [0] },
  { input: [0, 1], output: [1] },
  { input: [1, 0], output: [1] },
  { input: [1, 1], output: [0] }
], { population_size: 100 })

// new Neat(input, output, options)
let neat = new Neat(64, 10, { population_size: 100 })

// new Neat(input, output, dataset)
let neat = new Neat(2, 1, [
  { input: [0, 0], output: [0] },
  { input: [0, 1], output: [1] },
  { input: [1, 0], output: [1] },
  { input: [1, 1], output: [0] }
])

// new Neat(input, output, dataset, options)
let neat = new Neat(2, 1, [
  { input: [0, 0], output: [0] },
  { input: [0, 1], output: [1] },
  { input: [1, 0], output: [1] },
  { input: [1, 1], output: [0] }
], { population_size: 100 })
Properties:
Name Type Description
generation number

A count of the generations

population Array.<Network>

The current population for the neat instance. Accessible through neat.population

Source:

Methods

evolve (evolve_datasetopt, pickGenomeopt, adjustGenomeopt) → {Network} open an issue

Evaluates, selects, breeds and mutates population

Parameters:
Name Type Default Description
evolve_dataset Array.<{Array.<input:number>, Array.<output:number>}> <optional> dataset

A set to be used for evolving the population, if none is provided the dataset passed to Neat on creation will be used.

pickGenome function <optional>

A custom selection function to pick out unwanted genomes. Accepts a network as a parameter and returns true for selection.

adjustGenome function <optional> this.template

Accepts a network, modifies it, and returns it. Used to modify unwanted genomes returned by pickGenome and reincorporate them into the population. If left unset, unwanted genomes will be replaced with the template Network. Will only run when pickGenome is defined.

Returns:

Fittest network

Example
// original
let originalSet = [
 { input: [0,0], output: [0] },
 { input: [0,1], output: [1] },
 { input: [1,0], output: [1] },
 { input: [1,1], output: [0] },
]

let neat = new Neat(originalSet, {
 input: 1,
 output: 2
});

// special set to be used when evolving
let evolve_dataset = [
 { input: [0], output: [1] },
 { input: [1], output: [0] }
]

// evolves using evolve_dataset INSTEAD of originalSet
neat.evolve(evolve_dataset)

// evolves using originalSet
neat.evolve()

let pick = function pickGenome(genome) return genome.nodes.length > 100 ? true : false // Remove genomes with more than 100 nodes

let adjust = function adjustGenome(genome) return genome.clear() // clear the nodes

// evolves using originalSet
neat.evolve(null, filter, adjust)
Source:

(static) createPool (network, population_size) deprecated

Create the initial pool of genomes

Parameters:
Name Type Description
network Network
population_size Number

the number of types the genome of network will be copied to make the pool

Source:

(static) createPopulation (network, size) → {Array.<Network>} alpha

Creates a new population

Parameters:
Name Type Description
network Network

Template network used to create population - other networks will be "identical twins"

size number

Number of network in created population - how many identical twins created in new population

Returns:

Returns an array of networks

Source:

(static) evalute () → {Network} open an issue

Evaluates the current population, basically sets their .score property

Returns:

Fittest Network

Source:

(static) fromJSON (json) open an issue

Imports population from a json. Must be an array of networks converted to JSON objects.

Parameters:
Name Type Description
json Array.<object>

set of genomes (a population) represented as JSON objects.

Source:

(static) getAverage () → {number} open an issue

Returns the average fitness of the current population

Returns:

Average fitness of the current population

Source:

(static) getFittest () → {Network} open an issue

Returns the fittest genome of the current population

Returns:

Current population's fittest genome

Source:

(static) getOffspring () → {Network} open an issue

Selects two genomes from the population with getParent(), and returns the offspring from those parents. NOTE: Population MUST be sorted

Returns:

Child network

Source:

(static) getParent () → {Network} open an issue

Returns a genome for recombination (crossover) based on one of the selection methods provided.

Should be called after evaluate()

Returns:

Selected genome for offspring generation

Source:

(static) mutate (methodopt) open an issue

Mutates the given (or current) population

Parameters:
Name Type Description
method mutation <optional>

A mutation method to mutate the population with. When not specified will pick a random mutation from the set allowed mutations.

Source:

(static) mutateRandom (genome, allowedMutations) → {mutation} beta

Selects a random mutation method for a genome and mutates it

Parameters:
Name Type Description
genome Network

Network to test for possible mutations

allowedMutations Array.<mutation>

An array of allowed mutations to pick from

Returns:

Selected mutation

Source:

(static) replace (population, new_networkopt, selectopt, transformopt) open an issue

Replaces all networks that match the select function - if transform is provided networks will be transformed before being filtered out

Parameters:
Name Type Description
population Array.<network>

An array (population) of genomes (networks)

new_network network <optional>

Replaces networks from

select function <optional>
transform function <optional>

A function to change genomes with, takes a genome as a parameter

Source:

(static) toJSON () → {Array.<object>} open an issue

Export the current population to a JSON object

Can be used later with fromJSON(json) to reload the population

Returns:

A set of genomes (a population) represented as JSON objects.

Source: