Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up

TinyNEAT is a simple and extensible NEAT (NeuroEvolution of Augmenting Topologies) implementation in TypeScript.

License

NotificationsYou must be signed in to change notification settings

We-Gold/tinyneat

Repository files navigation

GitHub package.json version (branch)npm bundle sizecontributions welcomeCIViewCountLicense: MITNPM Downloads

What is TinyNEAT?

TinyNEAT is a simple and extensible NEAT (NeuroEvolution of Augmenting Topologies) implementation in JavaScript.

Skip To Documentation

Motivation

With many websites and web-based games being written in JavaScript, having a solid starting place for using NEAT to train AI agents seems pretty useful.

The goal is that this library is able to fit into any particular use case, and that the implementation of the environment is entirely up to the user.

Features

  • Tiny Size (less than 20 KB!)
  • Plugin System
    • Neural Networks
    • Logging
  • Examples Provided
  • Fully Configurable
  • Complete TypeScript Support

Basic Example

Installation

npm i tinyneat

or

<!-- Import using CDN, no types included --><scripttype="module"src="https://cdn.jsdelivr.net/npm/tinyneat/dist/index.js"></script>

Example Usage (inspired by OpenAI gym)

import{TinyNEAT}from"tinyneat"consttn=TinyNEAT(config)// Loop until the configured fitness or generation threshold has been met.// Will never end if neither is configured.while(!tn.complete()){constpopulationIndexed=tn.getPopulationIndexed()// Reset the environment for a new generationenv.reset(populationIndexed.length)// Environment loop for one generationfor(letstep=0;step<maxGenerationSteps;step++){for(const[i,genome]ofpopulationIndexed){constinputs=env.getInputs(i)constoutputs=genome.process(inputs)// Environment either supports outputs or takes argmax actionconststepFitness=env.receiveAgentAction(i,outputs)// Fitness is either calculated at the end of the generation// or iteratively throughout.genome.fitness+=stepFitness}}// Run evolution to produce the next generationtn.evolve()}// Reset the environment for a testing roundenv.reset(1)// Test the best genome in a roundconstbestGenome=tn.getBestGenomes()[0]for(letstep=0;step<maxGenerationSteps;step++){// Get the inputs for the singular agentconstinputs=env.getInputs(0)constoutputs=bestGenome.process(inputs)bestGenome.fitness+=env.receiveAgentAction(i,outputs)}console.log(`Best fitness:${tn.getBestGenomes()[0].fitness}`)

NEAT Demos

Racing: An adaptation of a very old project of mine that trains agents to drive around race tracks.

Environment Recommendations

Environments are not managed by TinyNEAT. We do recommend one of two environment structures. One, is an OpenAI gym-type environment, where each agent has their own environment. The other is an environment that manages each agent individually inside a single environment.

Environments are expected to iterate automatically, manage agents, and calculate fitness values. After each generation, TinyNEAT will handle the rest, as long as fitness values have been calculated for each genome (they are initialized to 0). TinyNEAT will build neural networks and provide evaluate methods.

Documentation

See our reference documentationhere.

Configuration

The following is the default configuration of TinyNEAT. For any changes, for example the number of inputs to the networks, pass an object with these updated properties to theTinyNEAT method.

// Closely based on parameters of the original NEAT paperexportconstdefaultConfig={initialPopulationSize:50,// Number of networks in the populationtargetSpecies:10,// Desired number of species to maintainmaxGenerations:100,// Stopping point for evolutionmaximumStagnation:15,// Maximum number of generations a species is allowed to stay the same fitness before it is removedexcessCoefficient:2.0,// Coefficient representing how important excess genes are in measuring compatibilitydisjointCoefficient:2.0,// Coefficient for disjoint genesweightDifferenceCoefficient:1.0,// Coefficient for average weight difference (highly recommended for tuning)compatibilityThreshold:6.0,// Threshold for speciation (highly recommended for tuning)compatibilityModifier:0.3,// Rate to change the compatibility threshold at when target species count is not metsurvivalThreshold:0.2,// Percentage of each species allowed to reproducemutateOnlyProbability:0.25,// Probability that a reproduction will only result from mutation and not crossovermateOnlyProbability:0.2,// Probability an offspring will be created only through crossover without mutationaddNodeProbability:0.03,// Probability a new node gene will be added to the genomeaddLinkProbability:0.05,// Probability a new connection will be addedmutateWeightProbability:0.3,// Probability a weight will be mutatedinterspeciesMatingRate:0.01,// Percentage of crossovers allowed to occur between parents of different speciesmateByChoosingProbability:0.6,// Probability that genes will be chosen one at a time from either parent during crossovermateByAveragingProbability:0.4,// Probability that matching genes will be averaged during crossoverreenableConnectionProbability:0.01,// Probability that a connection is randomly reenabled during crossoverfitnessSort:"max",largeNetworkSize:20,// A network with this many genes is considered to be largeminimumSpeciesSize:1,// The minimum number of offspring a species can havehallOfFameSize:10,// The number of top-performing individuals to storeinputSize:1,// The number of inputs to each neural networkoutputSize:1,// The number of outputs of each neural network// Plugin for the specific type of neural network (ANN, RNN, etc)nnPlugin:ANNPlugin(),// Plugins for logging dataloggingPlugins:[ConsoleLogger()],}

Usage vs. Plugin Development

TinyNEAT supports both standard usage and plugins that add custom functionality. Certain plugins are built-in, such as the standard feedforward neural network and a few default loggers.

There are only two external entry points to the library. See below:

import{TinyNEAT,plugins}from"tinyneat"

TheTinyNEAT method (with configuration) creates and manages the population of NEAT individuals and provides all necessary methods. SeeBasic Example for what this looks like.

This was done so that multiple NEAT instances can be created with no conflicts.

Besides theTinyNEAT method andplugins, nearly all other items in the documentation are internal. These are only applicable for building plugins.

Plugin Development

TinyNEAT currently supports two types of plugins: Neural Network Plugins and Logging Plugins.

Neural Network Plugins

These plugins extend TinyNEAT's capabilities, enabling any type of neural network desired. While a only a standard ANNplugin is provided, additional plugins can be developed. See the documentation (NNPlugin) for the proper interface, and see the configuration section for how to include a Neural Network Plugin.

A plugin could even use Tensorflow.js, Brain.js, or another neural network library, as long as it works in conjunction with NEAT.

Logging Plugins

These plugins enable custom logging features. TinyNEAT emits two events, one when the initial population is created, and one on each evolution. For more specific information, see theLogger interface in the documentation.

Also, seeconsolelogger.ts for a basic example.

Library Development

If you are interested in contributing to the library, or have public plugin code you would like linked on this page, create an issue or pull request.

For local installation, simply clone the repository and runnpm i to install all of the development dependencies.

All scripts are listed inpackage.json.


[8]ページ先頭

©2009-2025 Movatter.jp