Genetic algorithm knapsack problem

  • Comp Sci
  • Thread starter shivajikobardan
  • Start date
  • Tags
    Algorithm
In summary: Or you can stop when you have reached a certain number of generations, or when you have reached a certain fitness value. These are all parameters that need to be decided when implementing the genetic algorithm.
  • #1
shivajikobardan
674
54
Homework Statement
want 2 iterations solved by hand
Relevant Equations
genetic algorithm
kluAEmyiH63Z4BsXKT5Xr7X7mLxOwbR1L0y3ttoLitS6bOnwz-.png


Here is a solution, part of which doesn’t make any sense to me-:


->Why did we select c3,c1 and c3,c2 for 2nd generation?
-> why did we select os1,os2,c3,c2 for 3rd generation? (And what the heck is happening here? We are already in 3rd generation when even 2nd one is not complete?
-> then we mutate. Why are we mutating c1 lol. It doesn’t make any sense. We need to mutate one of the os1,os2,c3 or c2 according to my logic.

Anyway i have tried solving 2 iterations of it. If you can help me with 2 iterations of this problem, please do so.
Also test out my 2 iterations of this algorithm. What is right and wrong. I have used 1 point crossover.
AVBciQqBMkom-eP6coHtimC9hbzpfWkovo5U9W1pjGfcNncNNM.png

pHHSJyNapyFQWvhYGXTBs9tklfEX-0IwFg8O9xnOzEVA59dp0s.png

7ABdhqdHlCbBA67Qshvybt6WAEAM8ZHM13OX0Hj9BvREEaCwDy.png

CybhNAD643FTzy4f2b7Za1tFcQV2r9srNDZafZ69cr2c4EaCUn.png


I just want 2 iterations of this and know when to stop and check if I am right or wrong..And understand that first solved example video that I shared above.. this is getting confusing more and more I see it. Is this something where I can do anything randomly and get marks in exam lol...I feel like so looking at it. And the internet. Internet sucks. 1 easy thing then there are 5 lakhs of posts explaining the same thing. 1 hard thing and suddenly there is no one even remotely trying to explain it. Few explanations are there but they are very contradictory to each other.
 
Physics news on Phys.org
  • #2
shivajikobardan said:
->Why did we select c3,c1 and c3,c2 for 2nd generation?
The selection is random with a probability based on the relative fitness (roulette wheel).

shivajikobardan said:
-> why did we select os1,os2,c3,c2 for 3rd generation? (And what the heck is happening here? We are already in 3rd generation when even 2nd one is not complete?
It is a very bad explanation. Normally, you would call it a generation after the offspring have been generated.

shivajikobardan said:
-> then we mutate. Why are we mutating c1 lol. It doesn’t make any sense. We need to mutate one of the os1,os2,c3 or c2 according to my logic.
I think this was just to show an example of a mutation.

shivajikobardan said:
Anyway i have tried solving 2 iterations of it. If you can help me with 2 iterations of this problem, please do so.
It is hard to follow all you are doing, but you seem to have too little randomness. Choosing to mate the fittest individuals is not efficient.

There are different parameters that need to be decided in advance. For instance, do any individuals survive to the next generation, or what is the mutation rate.

If some individuals survive to the next generation, you can either take the fittest, or use a roulette wheel. Then, you create offspring, choosing the parents by roulette wheel, and pick a crossover point randomly. Then, when you have your pool of individuals for the next generation, you determine for each one if there will be a mutation (randomly chosen following the mutation rate). You now have your new generation.

When to stop is also a choice. You can for instance stop when there has been a certain number of generations with no increase in fitness.
 

FAQ: Genetic algorithm knapsack problem

What is a genetic algorithm?

A genetic algorithm is a type of optimization algorithm that is inspired by the process of natural selection. It uses principles of genetics and evolution to find the best solution to a problem.

What is the knapsack problem?

The knapsack problem is a classic optimization problem in computer science. It involves finding the most valuable combination of items that can fit into a limited space, such as a backpack or knapsack.

How does a genetic algorithm solve the knapsack problem?

In the context of the knapsack problem, a genetic algorithm works by creating a population of potential solutions (or chromosomes) and using genetic operations such as crossover and mutation to evolve and improve these solutions over multiple generations. The algorithm then selects the best solution from the final population as the optimal solution to the problem.

What are the advantages of using a genetic algorithm for the knapsack problem?

One of the main advantages of using a genetic algorithm for the knapsack problem is that it can handle a large number of variables and constraints, making it suitable for complex problems. It also has the ability to find near-optimal solutions in a relatively short amount of time, making it a useful tool for real-world applications.

Are there any limitations to using a genetic algorithm for the knapsack problem?

While genetic algorithms can be effective in solving the knapsack problem, they are not guaranteed to find the optimal solution every time. Additionally, the performance of the algorithm can be affected by the choice of parameters and the structure of the problem itself. It is important to carefully tune and test the algorithm for each specific problem to achieve the best results.

Similar threads

Replies
1
Views
1K
Replies
1
Views
1K
Replies
1
Views
2K
Replies
9
Views
1K
Replies
3
Views
1K
Replies
2
Views
1K
Replies
3
Views
1K
Back
Top