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

bookChallenge: Build a Genetic Algorithm

Oppgave

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

Alt var klart?

Hvordan kan vi forbedre det?

Takk for tilbakemeldingene dine!

Seksjon 2. Kapittel 4
single

single

Spør AI

expand

Spør AI

ChatGPT

Spør om hva du vil, eller prøv ett av de foreslåtte spørsmålene for å starte chatten vår

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

Sveip for å vise menyen

Oppgave

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 desktopBytt til skrivebordet for virkelighetspraksisFortsett der du er med et av alternativene nedenfor
Alt var klart?

Hvordan kan vi forbedre det?

Takk for tilbakemeldingene dine!

Seksjon 2. Kapittel 4
single

single

some-alt