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 output 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>

A set of allowed mutation methods for evolution. If unset a random mutation method from all possible mutation methods will be chosen when mutation occurs.

Example
      Copy
      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:

Members

(inner) inputs

To-do:

new Neat(population) - leave out for now new Neat(input, output) new Neat(population, options) - leave out for now new Neat(population, dataset) - leave out for now new Neat(input, output, dataset) new Neat(population, dataset, options) - leave out for now new Neat(input, output, dataset, options)

Source:

Methods

(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 (networkopt, sizeopt) → {Array.<Network>} alpha

Creates a new population

Parameters:
Name Type Default Description
network Network <optional> options.template

Template network used to create population - other networks will be "identical twins" - will use options.template, if network is not defined

size number <optional> 50

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

Returns:

Returns an array of networks each a member of the population

Source:

(static) evaluate (datasetopt) → {Array.<Network>} open an issue

Evaluates the current population, basically sets their .score property

Parameters:
Name Type Description
dataset Array.<Object> <optional>
Returns:

Return the population networks

Source:

(static) evolve (evolve_datasetopt, pickGenomeopt, adjustGenomeopt) → {Array.<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 function that takes a genome as a parameter and returns true "marking" it for adjustment - invoked pick(network, index, population)

adjustGenome function <optional>

A function that takes a marked genome as a parameter, makes changes, and returns it - invoked transform(network, index, population)

Returns:

An evolved population

Example
      Copy
      // 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()

// optional parameters left out
let pick = function pickGenome(genome) {

 // Select genomes with more than 100 nodes
 return genome.nodes.length > 100 ? true : false

}

// optional parameters left out
let transform = function transformGenome(genome) {

 genome.clear() // Adjust by cleaning the genome state
 return genome // Return the genome

}

neat.evolve(null, pick, transform) // First param is usually dataset, but this uses originalSet instead
    
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) → {Array.<Network>} 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.

Returns:

An array of mutated networks (a new population)

Source:

(static) replace (populationopt, pickopt, transformopt) → {Array.<Network>} alpha

Replaces all networks that match the pick function - picked networks are sent through the transform function

Allows networks (genomes) in a population to be picked and transformed with a custom user-defined function, used within Neat.evolve to allow for custom mutation before and after evolution

Parameters:
Name Type Description
population Array.<Network> <optional>

An array (population) of genomes (networks)

pick number | Network | function <optional>

An index, network, or function used to pick out replaceable genome(s) from the population - invoked pick(network, index, population)

transform Network | function <optional>

A network used to replace picked genomes or a function used to mutate picked genomes - invoked transform(network, index, population)

Returns:

Returns the replaced genomes (population)

Example
      Copy
      let neat = new Neat()

let pick = function pickGenome(genome, index, population) {

 return genome.nodes.length > 100 ? true : false // Pick genomes with >100 nodes

}

let transform = function transformGenome(genome, index, population) {

 genome.clear() // Adjust by cleaning the genome state
 return genome // Return the genome

}

neat.population = neat.replace(neat.population, pick, transform)
    
Source:

(static) resize (update) alpha

Resizes the population and adjusts the population_size

Parameters:
Name Type Description
update Array.<Network> | number

An array of new networks to add to the existing population or a new size for the population.

Example
      Copy
      let neat = new Neat() // default population_size = 50

neat.resize(51) // Adds 1 new network to make the 51 population members

let neat2 = new Neat()

neat.resize(neat2.population) // Adds neat2 population to neat, neat now has 101 networks

console.log(neat.population_size) // 101
    
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: