Genetic algorithm knapsack problem

  • Topic: Comp Sci 
  • Thread starter shivajikobardan
  • Start date
  • Tags Tags
    Algorithm
1 reply · 2K views · last reply
shivajikobardan
Messages
637
Reaction score
54
Homework Statement
want 2 iterations solved by hand
Relevant Equations
genetic algorithm
Slide titled Knapsack Problem by using Genetic Algorithm with a table of four items: A 5 kg $12, B 3 kg $5, C 7 kg $10, D 2 kg $7. A knapsack with capacity K = 12 Kg is shown beside it.


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.
Handwritten notes on lined paper: a genetic algorithm flowchart (Initial random population, Selection, Crossover, Mutation, stopping condition met, yes/no, end), a four-cell chromosome diagram labelled A to D, chromosome bit strings C1 to C4, and a knapsack table with weight and value columns.

Handwritten notes titled Selection #2 showing genetic algorithm steps: selecting c3 and c1, crossover of 5-bit strings, offspring os1 to os4, next generation bit tables, and mutation of os1 and os2 at bit 3.

Handwritten genetic algorithm worksheet showing chromosome bit strings, a second iteration table with fitness and weight values, selection, a one-point crossover of c1 and c3, and offspring os1 and os2.

Handwritten genetic algorithm notes: parent chromosomes c1 = 1 1 1 0 and c2 = 0 1 0 1 with a crossover point, offspring os3 = 1 1 1 1 and os4 = 0 1 0 0, then mutation of os3's first bit giving c1 = 1111, c2 = 1100, c3 = 0111, cu = 0100, and the question 'when to stop?'


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.
 
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.