I need a solved numerical example on genetic algorithm for 1 iteration

In summary, the conversation focused on finding a solved example for a genetic algorithm to solve the knapsack problem. The individual seeking help searched for resources online, specifically on YouTube, but was unable to find what they were looking for. They expressed the need for a step-by-step explanation that is easy to understand and can be used for their exam. Suggestions were made to download Python code examples and use a debugger to better understand the code. The individual also shared the requirements for their exam, which involve explaining the steps of a genetic algorithm and its operators, listing the steps with an example, and performing one round of a genetic algorithm using a given fitness function. It was suggested that studying the provided code and commentary would be beneficial in meeting these requirements.
  • #1
shivajikobardan
674
54
Homework Statement
genetic algorithm
Relevant Equations
none
Really need this. Tried googling but not many. 1 or 2 are there. I want this algorithm solved by hand to some problem. IDK what kinds of problems exists. but one is knapsack problem. there is analytics vidya's tutorial but I want something else, more direct, more clear...Any resource you can show to me? I really need it. This is important for my exam. Asked almost 70% of time.

if you search "genetic algorithm-smita tiwari" in youtube, that is exactly kind of answer I want. step by step 1 iteration of genetic algorithm..clearly written so that I can understand it easily.
 
Last edited by a moderator:
Physics news on Phys.org
  • #2
I did a search for "genetic algorithm knapsack problem" and got lots of hits. What I would do, rather than look at a video, is to download one of the many Python code examples out there, and run it, preferably using a debugger to step through the code a line at a time.

If you don't know how to use a debugger, there's a simple one called pdb that comes with Python distros. I wrote a two-part Insights article a few years ago with some tips about debugging Python code. See https://www.physicsforums.com/insights/simple-python-debugging-pdb-part-1/ and https://www.physicsforums.com/insights/simple-python-debugging-pdb-part-2/
 
  • Informative
  • Like
Likes sysprog and berkeman
  • #3
Mark44 said:
I did a search for "genetic algorithm knapsack problem" and got lots of hits. What I would do, rather than look at a video, is to download one of the many Python code examples out there, and run it, preferably using a debugger to step through the code a line at a time.

If you don't know how to use a debugger, there's a simple one called pdb that comes with Python distros. I wrote a two-part Insights article a few years ago with some tips about debugging Python code. See https://www.physicsforums.com/insights/simple-python-debugging-pdb-part-1/ and https://www.physicsforums.com/insights/simple-python-debugging-pdb-part-2/
I don't find any codes for it that can be downloaded tbh. Most are libraries...
 
  • #5
Mark44 said:
I'm not sure that's true. Here's one that looks like it's just one Python file: https://github.com/Pantzan/KnapsackGA
that code doesn't give any information to me. tells solution found in 1 generation.
 
  • #6
shivajikobardan said:
that code doesn't give any information to me. tells solution found in 1 generation.
It looks to me like what @Mark44 linked to was a very good source for what you said you were seeking. I think that your response is maybe too hasty and dismissive. Have you read and understood that code and commentary?
 
  • Love
Likes Mark44
  • #7
shivajikobardan said:
that code doesn't give any information to me. tells solution found in 1 generation.
I just ran the code, and it says that the opt. solution was found in 7 generations.
 
  • Like
Likes sysprog and berkeman
  • #8
sysprog said:
It looks to me like what @Mark44 linked to was a very good source for what you said you were seeking. I think that your response is maybe too hasty and dismissive. Have you read and understood that code and commentary?
Hmm no I haven't understood much of that code..I currently need a solved example. That kind of code is beyond the level I need in my exams.
 
  • #9
shivajikobardan said:
Hmm no I haven't understood much of that code..I currently need a solved example. That kind of code is beyond the level I need in my exams.
Then can you please elaborate on what the exam requirements are? What you specified, i.e. a genetic algorithm e.g. for solving the knapsack problem, is not 'elementary'. That code is a "solved example", even though it's not presented in the 'worked example' academic form. I think that if you study it well, you will come to understand it well.
 
Last edited:
  • #10
sysprog said:
Then can you please elaborate on what the exam requirements are? What you specified, i.e. a genetic algorithm e.g. for solving the knapsack problem, is not 'elementary'.
1) Explain all the steps in genetic algorithm with block diagram and operators-8 marks out of 80 marks
2) List down steps involved in genetic algorithm with example-4 marks out of 80 marks
3) x=abcdefgh is the form of chromosome.
f(x)=a+b-(c+d)+e+f-(g+h) is fitness function do 1 round of genetic algorithm in it
 
  • #11
shivajikobardan said:
1) Explain all the steps in genetic algorithm with block diagram and operators-8 marks out of 80 marks
2) List down steps involved in genetic algorithm with example-4 marks out of 80 marks
3) x=abcdefgh is the form of chromosome.
f(x)=a+b-(c+d)+e+f-(g+h) is fitness function do 1 round of genetic algorithm in it
In my apprehension, this detailing serves to reinforce my impression that your reading and studying and understanding the code and commentary at the link that @Mark44 provided would be appropriate for your stated purposes. That code and commentary does meet a reasonable definition of a "solved example", even though it's not presented in exactly the conventional 'worked example' format, and it's precisely relevant. I think that you would do well to give it some more attention.
 
  • #12
Mark44 said:
I just ran the code, and it says that the opt. solution was found in 7 generations.
I think due to randomization, some solutions are being given in 1 generations, some in 7 some in 2.
 

1. What is a genetic algorithm?

A genetic algorithm is a type of optimization algorithm that is inspired by the process of natural selection. It is used to solve complex problems by mimicking the process of evolution and improving solutions over multiple iterations.

2. How does a genetic algorithm work?

A genetic algorithm starts with a population of potential solutions to a problem, represented as a set of binary strings. These solutions undergo a process of selection, crossover, and mutation, similar to how genes are passed down and mutated in natural selection. The fittest solutions are then selected and used to create a new population, which is iteratively improved upon until a satisfactory solution is found.

3. What is a numerical example of a genetic algorithm?

A numerical example of a genetic algorithm could be finding the shortest path between multiple cities. The algorithm would start with a population of potential routes, represented as binary strings, and use selection, crossover, and mutation to improve the routes until the shortest path is found.

4. How many iterations does it take for a genetic algorithm to find a solution?

The number of iterations required for a genetic algorithm to find a solution varies depending on the complexity of the problem and the parameters set for the algorithm. In general, the more complex the problem, the more iterations it will take to find a satisfactory solution.

5. What are the advantages of using a genetic algorithm?

Genetic algorithms have several advantages, including the ability to find solutions to complex problems that may be difficult for traditional algorithms to solve. They can also handle a wide range of problem types and can provide multiple solutions to a problem. Additionally, they can continue to improve solutions over time, making them useful for long-term optimization problems.

Similar threads

  • Engineering and Comp Sci Homework Help
Replies
1
Views
1K
  • Programming and Computer Science
Replies
1
Views
727
  • Engineering and Comp Sci Homework Help
Replies
9
Views
1K
  • General Math
Replies
5
Views
848
  • Programming and Computer Science
Replies
1
Views
678
  • Engineering and Comp Sci Homework Help
Replies
9
Views
1K
  • Programming and Computer Science
Replies
1
Views
960
Replies
20
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
2
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
7
Views
907
Back
Top