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 |
Methods
evolve (evolve_datasetopt, pickGenome, filterGenome) → {Object} 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 | ||
filterGenome |
function |
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()
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)
(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> | Template network used to create population - other networks will be "identical twins" - will use |
|
size |
number <optional> |
50
|
Number of network in created population - how many identical twins created in new population |
Returns:
Returns an array of networks
(static) evaluate (datasetopt, optionsopt) → {Object} open an issue
Evaluates the current population, basically sets their .score
property
Parameters:
Name | Type | Description | ||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
dataset |
Array.<Object> <optional> | |||||||||||||
options |
Properties
|
Returns:
Return the performance metrics/benchmarks of the networks - returns networks iff options.networks === true
(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) 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. |
(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
(static) replace (populationopt, filteropt, transformopt) → {Array.<Network>} 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> <optional> | An array (population) of genomes (networks) |
filter |
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 filtered genomes or a function used to mutate filtered genomes - invoked |
Returns:
Returns the new genome
(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.