Genetic Algorithm In Python Super Basic Example

preview_player
Показать описание
Genetic Algorithms are a family of evolutionary algorithms which can be implemented in any language (including python) they solve problems which have no clear solution by generating random solutions and picking the best results then applying a crossover and a mutation to the best solutions before starting the process again.
Due to the "evolving" nature of this algorithm, the search space is greatly reduced in comparison to exhaustive search.

To learn more about Genetic Algorithms:

Give me money:

Below are affiliate links, I may earn something if you purchase the mentioned product or service linked.

📚 Recommended Books

💵 Get $100 in credits from Vultr with this link

#python #algorithm
Рекомендации по теме
Комментарии
Автор

I found an improvement that made it literally 100 times faster. There, in line 34, when you append all three elements to the same group you're mixing the x, y and z elements. So when you use these elements to create new tupple most of the newGen solutions are mixed, using a good y as x, a good x as z and so on. To fix it, instead of just one group "elements" create three groups ("elements_0", "elements_1" and "elements_2"). Then instead of "elements.append(s[1][0])" type "elements_0.append(s[1][0])", "elements_1.append(s[1][1])" and "elements_2.append(s[1][2])". Finally, when creating the newGen, comand "e1 = random.choice(elemets_0)" and the same for the other two groups. This way you're always taking a good x as x, y as y and z as z, no more scrumble. Congrats for the nice class Top Shelf, the best I could find so far!

sidneyw.mathiasdeoliveira
Автор

That was amazing. I just managed to make my first neural network and wanted to learn the NEAT algorithm.
You made it easier for me to understand the application of the genetic algorithm.

Much thanks!

victor
Автор

Nice Video.
Small tip: you can sort a list in descending order by using the parameter 'reverse' (like: list.sort(reverse=True)) instead of sorting and then reversing the list.

tim-vs
Автор

Slow, steady and perfect video thank you so much !!

theodoregiannilias
Автор

I think you rushed in the explanation too much in the important part, which is at the end when the new contestants mutate from the previous bests. Still it is a really good example and without pedantic, too complex, paraphernalic, stack overflowish code, and I respect that.

javierurena
Автор

Thanks for the video, helping me through my AI assignments. A little trick I found is to make the mutation rate adjust depending on fitness, if fitness was exponential, and mutation rate being the inverse (of course capped at 0.02 though) It helps with being super precise

taffox
Автор

thanks sir! the best yet simple explanation for AG ive seen so far... more power!

datastako
Автор

I subscribed cause you teach clearly and do not step out of anything. Thank you, sir.

DenizTurkben
Автор

hi, thanks for your impressive explaination! Something confuses me is that, I notice that in the array 'element', you just put all the param_x, param_y, param_z into the 'element' array and then randomly choose the new x, y, z value and mutate it. But why? From my perspective, it will cause that the param_x in this iteration will come from the prarm_y in previous iteration, won't it fall into chaos?

ZizhaoMo
Автор

You can make this much much faster by changing the fixed mutation to a time based mutation, i used a 1 - 0.2exp(-i/10), 1 + 0.2exp(-i10). Thank you for this very informative video nonetheless it gives a very clear idea of what genetic algorithms actually are.

megumin-
Автор

Hey! I suppose you did this for learning purposes, but all for loops you wrote could be replaced by list comprehensions. Also, the reason why you had to manually add a second convergence interval break was because your approximation rate (or learning rate) was too big for it to converge properly! Really nice video

rzimmerdev
Автор

Hello! Nice tutorial to explain the key/abstract elements of GA. But allow me to mention a couple of points. Someone raised it in the comments (12:30 or thereabouts). I think it is misleading to append the three parameters into one list then selecting at random from that one list. That translates to generating a gene for the 'arms' from a 'gene' for the 'legs' for example. A proper GA code would separate them and randomize around the same gene.

This 'mistake' will cause a faster convergence since it allows the population to try more solutions beyond 1% from the previous population, but also makes it hard to get to the final solution when you are near the correct values, since the mutation could re-start from a completely unrelated, 'far' value.

timelapsecoder
Автор

I'm a bit confused on why at 12:30 do we scramble all the parameters together into a big list, because aren't a, b and c independent values, like in the correct solution "a" could be 10 and "b" could be 100 so it wouldn't make sense to put them all into the same list because in the next generation the value of "b" from the last generation (let's say 95) could be put in the "a" place (which should be much smaller). Could you explain this if you understood what I meant, thanks

bucketfisher
Автор

thank you for your amazing video, I have question about how we can save best result (last ranked solutions ) in a list?

aligazal
Автор

Wait isn't there a slight inperfection (may be just me not understanding it). We're adding the best x, y, z values in the list 'elements', without specifying which are x, y and z. Then we take random three values into a tuple and add it to newGen and make it them the new solutions. Doesn't this kind of mix x, y and z values together. And from the result we can see that x, y and z are very close to each other.

onddu
Автор

I am fairly new to GA, and was able to kind of grasp the whole. So the first part is the choice of the fittest solutions (first 100). Then comes the crossover, which is randomly choosing from the pool of fittest solutions (please correct me if I am wrong). Then comes the multiplication by a random probability range, which is where I am confused a little. I would like to know why a multiplication by a random probability is a mutation in this scenario; specifically why do you multiply the chosen elements to obtain a mutation ? In the classical scenario, a mutation would be the swapping of 1 to 0 or 0 to 1 and I would like to know why in this case it represents multiplication.

dakirmossadeq
Автор

Hi, thank you for the video. If we want to add constraints in this model how can we do that? Do you have any example code to share about that?

ilaydacolakoglu
Автор

Am I missing something, why loop 10000 in line 21, when in line 16 you only loop 1000 to make 1000 random solutions?

pallenda
Автор

this is so awesome thank you! This really helped distill my understanding of genetic iteration.

axaide
Автор

It would great to show the different parts of a GA, variation, fitness, etc.

nosuchthing