-
Notifications
You must be signed in to change notification settings - Fork 3.9k
Completed Genetic algorithm in search.py #345 #347
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
@articuno12 Is our implementation of genetic algorithms complete? I am not sure if the test is guaranteed to pass - is there a (rare and unfortunate) case where the code terminates without generating a perfect solution? If there isn't, then this is perfect, but otherwise it may make sense to have a different test case than the current one. |
@kaivalyar As stated in the pseudo code in fig 4.8 : "Run until some individual is fit enough, or enough time has elapsed return the best individual in population , according to FITNESS -FN" , so genetic algorithm will terminate if the solution is found , otherwise it has to be stopped after some time.In second case , it will obviously not return a perfect solution.And some time bound has to be given otherwise the algorithm can keep on running infinitely. @norvig Am I right? |
Hi @articuno12, there are now some conflicts with your PR. May you resolve them? |
#501 implements GA. This PR can be closed. |
Mr. Norvig took a break for a while, and this PR must have slipped through the cracks. It's unfortunate, but stuff like this happens. |
@articuno12 I'm sorry it was a misunderstanding on my part. I thought this PR was made to work with |
Personally I feel It would be interesting though to have the algorithm solve problems given by the problem classes in the module, even though that's not exactly needed due to the nature of the algorithm. Your |
Perhaps we could open an issue on GitHub to discuss whether or not to use |
@kaivalyar: Since this PR deals with the topic, I think it would be best if we discussed it here to keep things as much in order as possible. Personally I feel we don't need Since the genes are in string form though (thus are simple), it would be best to simply store them as strings and then have the functions of mutation and reproduction as stand-alone functions. This will not only keep things simpler, but will also keep the code closer to the book. In the book, the aforementioned functions are not properties of the individual states, so they shouldn't be in the code either. |
My feeling is that we don't need GAState -- when in doubt, keep it simple. I think that individuals should be sequences (not necessarily strings). I apologize for being away from this project while I attended to other duties, and not resolving this issue earlier. |
I think there is some merit to keeping Of course, as @norvig mentioned, this does have the flipside of slightly more complicated code - but I think this can be overcome by documenting |
I agree with @kaivalyar that a str is too restrictive a type. But I think that it is ok to assume that each individual is a mutable sequence. Then there would be two operations on these sequences: mate(a, b) returns a[i:] + b[i:]; and mutate(a, values) does a[i] = random.choice(values). So for most problems, individuals are lists, and everything works fine; the code is simple. But if you want to define a custom class, you just need to define getitem and add methods, and you can do whatever you want. Do you think that works? |
Yes, this seems like a simple and extendable solution - better than using strings or Perhaps an example demonstrating how to define a custom class would be helpful in the Jupyter notebook, and the simplified |
I agree with individuals as lists. When writing the code, this is where I wanted it to go, but didn't get a chance to make it happen at the time. I think the code needs very few modifications to go from strings to lists. I'm not entirely convinced though that we need a class for it. Is it possible to give us an example of when a class would be better than a list? |
I don't think we need to have a custom class defined beforehand - it's just that using lists keeps that option potentially open. There may be cases where this helps, I am unsure of exactly where, but it is hard to think that lists would cover all use cases students would ever want to explore Genetic Algorithms with. As for the GA implementation within the code for this project, using the built-in Python lists seems to be the simplest and best solution. Its just that others studying aima-code may choose to extend our code, and will have the option to do so. |
I made the to-list changes on #523. |
#345
work done :
3.Implemented mutate function . Passed one more argument "gene_bound" to know the range of genes to choose randomly from for mutation.
4.Fixed bug in genetic algorithm. Same iterator was used for nested 'for' loops.
5.As stated in pseudo code : algorithm should stop either after some time lapse or when a particular utility is reached.Added optimal value to the algorithm.If the value of an individual reached optimal value then the algorithm will stop.
6.Test for genetic algorithm.