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

bookChallenge: Build a Genetic Algorithm

Uppgift

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.

Lösning

Var allt tydligt?

Hur kan vi förbättra det?

Tack för dina kommentarer!

Avsnitt 2. Kapitel 4
single

single

Fråga AI

expand

Fråga AI

ChatGPT

Fråga vad du vill eller prova någon av de föreslagna frågorna för att starta vårt samtal

close

Awesome!

Completion rate improved to 6.25

bookChallenge: Build a Genetic Algorithm

Svep för att visa menyn

Uppgift

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.

Lösning

Switch to desktopByt till skrivbordet för praktisk övningFortsätt där du är med ett av alternativen nedan
Var allt tydligt?

Hur kan vi förbättra det?

Tack för dina kommentarer!

Avsnitt 2. Kapitel 4
single

single

some-alt