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

bookChallenge: Build a Genetic Algorithm

Taak

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.

Oplossing

Was alles duidelijk?

Hoe kunnen we het verbeteren?

Bedankt voor je feedback!

Sectie 2. Hoofdstuk 4
single

single

Vraag AI

expand

Vraag AI

ChatGPT

Vraag wat u wilt of probeer een van de voorgestelde vragen om onze chat te starten.

Suggested prompts:

Can you explain that in simpler terms?

What are the main benefits of this approach?

Are there any common mistakes to avoid with this?

close

Awesome!

Completion rate improved to 6.25

bookChallenge: Build a Genetic Algorithm

Veeg om het menu te tonen

Taak

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.

Oplossing

Switch to desktopSchakel over naar desktop voor praktijkervaringGa verder vanaf waar je bent met een van de onderstaande opties
Was alles duidelijk?

Hoe kunnen we het verbeteren?

Bedankt voor je feedback!

Sectie 2. Hoofdstuk 4
single

single

some-alt