Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Apprendre Challenge: Build a Genetic Algorithm | Genetic Algorithms
Bio-Inspired Algorithms

bookChallenge: Build a Genetic Algorithm

Tâche

Swipe to start coding

You are tasked with implementing the main loop of a genetic algorithm to find the maximum value of a mathematical function. The function to optimize is a simple parabola: f(x) = -(x - 3)² + 10. This function has a clear peak at x = 3, where its value is 10.

All the helper functions (init_population, fitness_function, tournament_selection, arithmetic_crossover, mutate) and the main loop structure are provided for you.

Your task is to fill in the core logic of the evolutionary process:

  1. Inside the main for loop, you must first evaluate the entire population by applying the fitness_function to each individual. Store these scores in the fitness list.
  2. Find the index of the best-performing individual in the current generation and store it in gen_best_idx. (Hint: np.argmax() is useful here).
  3. Inside the while len(new_population) < POP_SIZE: loop, you must create a new individual by:
    • Selecting parent1 using the tournament_selection function.
    • Selecting parent2 using the tournament_selection function.
    • Creating a child by combining the parents with the arithmetic_crossover function.
    • Applying variation to the child using the mutate function.
  4. After the while loop, replace the old population with the new_population to complete the generation.

Solution

Tout était clair ?

Comment pouvons-nous l'améliorer ?

Merci pour vos commentaires !

Section 2. Chapitre 4
single

single

Demandez à l'IA

expand

Demandez à l'IA

ChatGPT

Posez n'importe quelle question ou essayez l'une des questions suggérées pour commencer notre discussion

close

Awesome!

Completion rate improved to 6.25

bookChallenge: Build a Genetic Algorithm

Glissez pour afficher le menu

Tâche

Swipe to start coding

You are tasked with implementing the main loop of a genetic algorithm to find the maximum value of a mathematical function. The function to optimize is a simple parabola: f(x) = -(x - 3)² + 10. This function has a clear peak at x = 3, where its value is 10.

All the helper functions (init_population, fitness_function, tournament_selection, arithmetic_crossover, mutate) and the main loop structure are provided for you.

Your task is to fill in the core logic of the evolutionary process:

  1. Inside the main for loop, you must first evaluate the entire population by applying the fitness_function to each individual. Store these scores in the fitness list.
  2. Find the index of the best-performing individual in the current generation and store it in gen_best_idx. (Hint: np.argmax() is useful here).
  3. Inside the while len(new_population) < POP_SIZE: loop, you must create a new individual by:
    • Selecting parent1 using the tournament_selection function.
    • Selecting parent2 using the tournament_selection function.
    • Creating a child by combining the parents with the arithmetic_crossover function.
    • Applying variation to the child using the mutate function.
  4. After the while loop, replace the old population with the new_population to complete the generation.

Solution

Switch to desktopPassez à un bureau pour une pratique réelleContinuez d'où vous êtes en utilisant l'une des options ci-dessous
Tout était clair ?

Comment pouvons-nous l'améliorer ?

Merci pour vos commentaires !

Section 2. Chapitre 4
single

single

some-alt