项目作者: cashlionjp

项目描述 :
Genetic Algorithm Framework in JS
高级语言: JavaScript
项目地址: git://github.com/cashlionjp/JeneticS.git
创建时间: 2018-04-27T22:26:22Z
项目社区:https://github.com/cashlionjp/JeneticS

开源协议:MIT License

下载


JeneticS

Genetic Algorithm Library in JS

A genetic algorithm is more or less a universal function approximator. After an initial (usually randomized) population is created, the algorithm loops through the following processes:

  • Fitness - Assessing how well an indiviual performs.
  • Crossover ( reproduction ) - The fittest individuals have a higher probability to pass on DNA.
  • Mutation - Random mutations supply the necessary entropy to navigate the search space.

Such that the population evolves towards some optimal solution.

More Reading Material Here

Installation

Include Jenetics.js or Jenetics.min.js from dist

  1. <script src="path/to/JeneticS.js"></script>
  2. <script src="path/to/your/app.js"></script>

Node specific usage coming soon.

Usage

Examples

Define a live (fitness) and mutate functions for your Agent.

  1. let evolve = "A string to evolve."; // Trivial example
  2. Agent.prototype.live = function () { // Example: Live function
  3. for (let i = 0; i < this.dna.length; i++) {
  4. if (this.dna[i] === evolve[i]) {
  5. this.score++;
  6. }
  7. }
  8. };
  9. Agent.prototype.mutate = function (rate) { // Example: Mutate function
  10. for (let i = 0; i < this.dna.length; i++) {
  11. if (Math.random() < rate) {
  12. // MUTATE DNA
  13. }
  14. }
  15. // OR
  16. if (Math.random() < rate) {
  17. // MUTATE AGENT
  18. }
  19. };

Create a function to define random Agents

  1. function createAgent(index) {
  2. // Create an agent
  3. let agent = new Agent();
  4. agent.dna = [/* Populate according to your needs */]
  5. return agent;
  6. }

Create a Genetic Algorithm instance and innoculate the culture:

  1. let geneticAlgorithm = new JeneticS();
  2. geneticAlgorithm.innoculate(createAgent); // Pass in a function to create a random Agent

Simulate a generation

  1. geneticAlgorithm.run().generation();

Access the fittest individual, the entire population, or by index.

  1. best = geneticAlgorithm.culture.best;
  2. allIndividuals = geneticAlgorithm.culture.citizens;
  3. let i = 2;
  4. byIndex = geneticAlgorithm.culture.citizen(i);

Options

  1. let geneticAlgorithm = new JeneticS({
  2. mutationRate: 0.01, // Rate of mutation
  3. population: 500, // Population of Agents in Culture
  4. crossoverMethod: "all", // "all" "half" "alternate"
  5. elitism: 0.1, // Percentage of additional mutated elites
  6. eliteMutationMultiplier: 5 // Multiplier for elite mutation rate
  7. });

TODO

  • Add more examples
    • TSP
  • Add continuous evolution mode
  • Neuro Evolution: Tensorflow.js integration