Skip to content

cashlionjp/JeneticS

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

67 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

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

<script src="path/to/JeneticS.js"></script>
<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.

let evolve = "A string to evolve."; // Trivial example
Agent.prototype.live = function () {  // Example: Live function
    for (let i = 0; i < this.dna.length; i++) {
        if (this.dna[i] === evolve[i]) {
            this.score++;
        }
    }
};

Agent.prototype.mutate = function (rate) {  // Example: Mutate function
    for (let i = 0; i < this.dna.length; i++) {
        if (Math.random() < rate) {
            // MUTATE DNA
        }
    }
    // OR
    if (Math.random() < rate) {
        // MUTATE AGENT
    }
};

Create a function to define random Agents

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

Create a Genetic Algorithm instance and innoculate the culture:

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

Simulate a generation

geneticAlgorithm.run().generation();

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

best = geneticAlgorithm.culture.best;

allIndividuals = geneticAlgorithm.culture.citizens;

let i = 2;
byIndex = geneticAlgorithm.culture.citizen(i);

Options

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

TODO

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