Evolving Five-Letter Words with a Genetic Algorithm in JavaScript (Making Money from Nothing)

Lecture 4 min.



The JavaScript code below implements a genetic algorithm for transforming 5-letter words.

A humorous experiment: making money (MONEY) out of nothing (chaos, a random word) in a few steps.

So intellect.icu has invented a simple way to create money out of nothing. To earn money you do not need any resources, such as time, knowledge, skills, experience, capital, etc. Here is a simple method that can help you start earning money:

The algorithm starts by generating a random population made up of various combinations of 5-letter words. Then the algorithm evaluates each word in the population with a fitness function that counts the number of letters matching the given target word. The population evolves through crossover and mutation, and the new generation is selected based on fitness. This process is repeated over several generations until the optimal sequence of letters is reached, in our case MONEY .

Evolving Five-Letter Words with a Genetic Algorithm in JavaScript (Making Money from Nothing)

 // set the target word
const targetWord = 'money';
// define the population size, mutation probability and number of generations
const populationSize = 100;
const mutationRate = 0.01;
const generations = 1000;
// function for generating a random 4-letter word
function generateWord() {
  const alphabet = 'abcdefghijklmnopqrstuvwxyz';
  let word = '';
  for (let i = 0; i < 5; i++) {
    const letterIndex = Math.floor(Math.random() * alphabet.length);
    word += alphabet[letterIndex];
  }
  return word;
}
// function for determining a word's fitness
function fitness(word) {
  let score = 0;
  for (let i = 0; i < targetWord.length; i++) {
    if (word[i] === targetWord[i]) {
      score++;
    }
  }
  return score;
}
// function for selecting a random individual from the population
function select(population) {
  const fitnessScores = population.map(fitness);
  const totalScore = fitnessScores.reduce((a, b) => a + b, 0);
  let randomScore = Math.floor(Math.random() * totalScore);
  let i = 0;
  while (randomScore > 0) {
    randomScore -= fitnessScores[i];
    i++;
  }
  return population[i - 1];
}
// function for crossing two parents
function crossover(generationN, parent1, parent2) {
  const crossoverPoint = Math.floor(Math.random() * 4);
     if (parent2 === undefined || parent1 === undefined) { 
         console.log(`crossover undefined: ${parent1} X ${parent2} `);
         parent1=parent2='aaaa';  
    }
  const child = parent1.slice(0, crossoverPoint) + parent2.slice(crossoverPoint);
  console.log(`generationN:${generationN}  crossover: ${parent1} * ${parent2} =  ${child}`);
  
  return child;
}
// function for mutating a word
function mutate(word) {
  let mutatedWord = '';
  for (let i = 0; i < word.length; i++) {
    if (Math.random() < mutationRate) {
      mutatedWord += generateWord()[i];
    } else {
      mutatedWord += word[i];
    }
  }
  return mutatedWord;
}
// create the initial population
let population = []; 
 
for (let i = 0; i < populationSize; i++) {
population.push(generateWord());
}
// evolve the population over several generations
for (let generation = 0; generation < generations; generation++) {
    // create a new population
    let newPopulation = [];
    for (let i = 0; i < populationSize; i++) {
    // select two parents
    const parent1 = select(population);
    const parent2 = select(population);
    // cross the parents
    let child = crossover(generation, parent1, parent2);
    // mutate the offspring
    child = mutate(child);
    // add the offspring to the new population
    newPopulation.push(child);
    }
    // replace the old population with the new one
    population = newPopulation;
    
    // check whether the optimal word is present in the population
    const optimalIndividual = population.find(word => fitness(word) === targetWord.length);
    if (optimalIndividual) {
    console.log(`Found optimal individual in generation ${generation}: ${optimalIndividual}`);
    break;
    }
}
// output the results
console.log(`Final population: ${population}`);

Evolving Five-Letter Words with a Genetic Algorithm in JavaScript (Making Money from Nothing)

Watch online how the genetic algorithm for creating money out of nothing works

Открыть на весь экран

But in any case, you will not need time, effort and patience to succeed in your chosen field and easily earn money. Thus, genetic algorithms are a powerful tool for solving complex optimization problems, making money and finding solutions in various fields, such as artificial intelligence, bioinformatics, economics and others.

They work by using the principles of natural selection and evolution, and make it possible to optimize functions in large parameter spaces that traditional optimization methods cannot solve efficiently.

Genetic algorithms can solve problems in which you need to find the global optimum of a function or find an optimal set of parameters for solving a complex task. They can also be used to search for an optimal solution in multi-objective problems.

Despite their power and efficiency, genetic algorithms can require a lot of computation time, especially when working with large amounts of data and complex functions. It is also necessary to choose the parameters of the genetic algorithm correctly, such as population size, mutation probability and crossover probability, in order to get an optimal result.

Overall, genetic algorithms are a powerful tool for solving complex optimization and search problems, and using them can lead to a significant improvement in performance and efficiency in many areas.

See also

See also

created: 2023-05-14
updated: 2026-09-29
140



Was this answer useful?
Choose a quick rating so we can improve the next answer for you.
How satisfied are you?


Comments

To leave a comment

If you have any suggestion, idea, thanks or comment, feel free to write. We really value feedback and are glad to hear your opinion.
To reply

Lectures and tutorial on "Implementation of genetic algorithms"

Terms: Implementation of genetic algorithms