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 |
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
options |
Configuration Options Properties
|
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 |
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)
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 |
(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 |
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
(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
(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 |
|
adjustGenome |
function <optional> | A function that takes a marked genome as a parameter, makes changes, and returns it - invoked |
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
(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. |
(static) getAverage () → {number} open an issue
Returns the average fitness of the current population
Returns:
Average fitness of the current population
(static) getFittest () → {Network} open an issue
Returns the fittest genome of the current population
Returns:
Current population's fittest genome
(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
(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
(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)
(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 |
transform |
Network | function <optional> | A network used to replace picked genomes or a function used to mutate picked genomes - invoked |
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)
(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
(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.