genetic algorithm in javascript

Here's an example implementation of a basic genetic algorithm in JavaScript:

index.tsx
// Define genetic algorithm parameters
const POPULATION_SIZE = 100;
const CHROMOSOME_LENGTH = 5;
const MUTATION_RATE = 0.01;
const ELITISM_PERCENTAGE = 0.1;
const MAX_GENERATIONS = 100;

// Generate initial population
function initializePopulation() {
  const population = [];
  for (let i = 0; i < POPULATION_SIZE; i++) {
    const chromosome = [];
    for (let j = 0; j < CHROMOSOME_LENGTH; j++) {
      chromosome.push(Math.random());
    }
    population.push(chromosome);
  }
  return population;
}

// Evaluate fitness of each chromosome
function evaluateFitness(population) {
  for (let i = 0; i < POPULATION_SIZE; i++) {
    const chromosome = population[i];
    const fitness = fitnessFunction(chromosome);
    chromosome.fitness = fitness;
  }
}

// Select parents for reproduction
function selectParents(population) {
  const parents = [];
  const sortedPopulation = population.sort((a, b) => b.fitness - a.fitness);
  const elitismCount = Math.floor(ELITISM_PERCENTAGE * POPULATION_SIZE);
  for (let i = 0; i < elitismCount; i++) {
    parents.push(sortedPopulation[i]);
  }
  while (parents.length < 2) {
    const rouletteWheel = [];
    let fitnessSum = 0;
    for (let i = 0; i < POPULATION_SIZE; i++) {
      fitnessSum += population[i].fitness;
    }
    for (let i = 0; i < POPULATION_SIZE; i++) {
      const chromosome = population[i];
      const fitnessRatio = chromosome.fitness / fitnessSum;
      for (let j = 0; j < fitnessRatio; j++) {
        rouletteWheel.push(chromosome);
      }
    }
    const parent1 = rouletteWheel[Math.floor(Math.random() * rouletteWheel.length)];
    const parent2 = rouletteWheel[Math.floor(Math.random() * rouletteWheel.length)];
    parents.push(parent1, parent2);
  }
  return parents;
}

// Perform crossover and mutation to generate new offspring
function generateOffspring(parents) {
  const offspring = [];
  while (offspring.length < POPULATION_SIZE - 1) {
    const parent1 = parents[Math.floor(Math.random() * parents.length)];
    const parent2 = parents[Math.floor(Math.random() * parents.length)];
    const crossoverPoint = Math.floor(Math.random() * (CHROMOSOME_LENGTH - 1)) + 1;
    const child1 = parent1.slice(0, crossoverPoint).concat(parent2.slice(crossoverPoint));
    const child2 = parent2.slice(0, crossoverPoint).concat(parent1.slice(crossoverPoint));
    offspring.push(child1, child2);
  }
  // Add one more offspring to round out the population
  const parent = parents[Math.floor(Math.random() * parents.length)];
  const mutationIndex = Math.floor(Math.random() * CHROMOSOME_LENGTH);
  const mutant = parent.slice();
  mutant[mutationIndex] += (Math.random() - 0.5) * MUTATION_RATE;
  offspring.push(mutant);
  return offspring;
}

// Run the genetic algorithm
function runGeneticAlgorithm() {
  let population = initializePopulation();
  for (let i = 0; i < MAX_GENERATIONS; i++) {
    evaluateFitness(population);
    const parents = selectParents(population);
    const offspring = generateOffspring(parents);
    population = parents.concat(offspring);
  }
  return population.sort((a, b) => b.fitness - a.fitness)[0];
}
3112 chars
89 lines

This code includes functions for generating an initial population, evaluating fitness, selecting parents, and generating offspring using crossover and mutation. You would also need to define a fitness function specific to your problem. To use this code, simply call the runGeneticAlgorithm() function. It returns the fittest chromosome found in the final population by sorting the population by fitness and selecting the first chromosome.

gistlibby LogSnag