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

bookChallenge: Build a Genetic Algorithm

Aufgabe

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ösung

War alles klar?

Wie können wir es verbessern?

Danke für Ihr Feedback!

Abschnitt 2. Kapitel 4
single

single

Fragen Sie AI

expand

Fragen Sie AI

ChatGPT

Fragen Sie alles oder probieren Sie eine der vorgeschlagenen Fragen, um unser Gespräch zu beginnen

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

Swipe um das Menü anzuzeigen

Aufgabe

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ösung

Switch to desktopWechseln Sie zum Desktop, um in der realen Welt zu übenFahren Sie dort fort, wo Sie sind, indem Sie eine der folgenden Optionen verwenden
War alles klar?

Wie können wir es verbessern?

Danke für Ihr Feedback!

Abschnitt 2. Kapitel 4
single

single

some-alt