Genetic Algorithm In Python Super Basic Example

Genetic Algorithm In Python Super Basic Example

The Builder

3 года назад

121,501 Просмотров

Ссылки и html тэги не поддерживаются


Комментарии:

The Builder
The Builder - 17.03.2022 03:01

Subscribe for more

Ответить
nosuchthing8
nosuchthing8 - 06.08.2023 07:32

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

Ответить
nosuchthing8
nosuchthing8 - 06.08.2023 07:22

Wow, could you make it any slower?

Ответить
Seba Contreras
Seba Contreras - 06.05.2023 07:51

Java was my first language and the lack of types in Python is making my head hurt 😂

Ответить
Ayushi Dixit
Ayushi Dixit - 09.04.2023 23:18

Can I get the code

Ответить
prateek.
prateek. - 30.03.2023 06:20

what if i want to do this for any variable type function
...i m havung problem in substiting the variables in the funtion.

Ответить
Master Ed
Master Ed - 18.03.2023 20:11

finally, something I can understand

Ответить
Matheus
Matheus - 03.03.2023 15:39

Thank you! It helped me a ton!

Ответить
TafFox
TafFox - 18.02.2023 07:14

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

Ответить
Ziad Ismaili Alaoui
Ziad Ismaili Alaoui - 11.02.2023 23:11

The reason you didn't get a result close to 0 at the end was merely due to the fact that you forgot to subtract 25.

Ответить
WORLD OF ISS🚀
WORLD OF ISS🚀 - 19.01.2023 16:04

Sir how to implement multiple traveling salesman problem using NSGA-2 in python.

Ответить
K
K - 12.01.2023 22:55

Line 29 print (f"=== Gen..........).
The world f not declared. How this is working !?
Thank you

Ответить
Louis V
Louis V - 02.12.2022 23:13

Use a list comprehension rather than that for loop.

Ответить
Deniz
Deniz - 29.11.2022 20:27

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

Ответить
pallenda
pallenda - 04.11.2022 02:59

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

Ответить
Yong CH
Yong CH - 17.10.2022 17:29

Can I know how to use GA for optimising an array? Example of given an array of [1, 2, 3, 5, 7, 9] and partition it into two. So my objective is to minimise the difference between these two…

Ответить
Salkogii
Salkogii - 02.10.2022 16:09

Why didn't you write like this? (I'm not good at ENG)
At first, I thought this is better than your code. Because my code can distinguish x, y, z. but it wasn't better than yours. I wonder why.
(my guess is that: In my code, difference between x, y, z are large. So it is hard to reach to appropriate value. But in your code, x, y and z are almost same. So it's easy to x,y,z to reach to appropriate value.)

elements1 = []
elements2 = []
elements3 = []

for s in bestsolutions:
elements1.append(s[1][0])
elements2.append(s[1][1])
elements3.append(s[1][2])

newGen = []
for _ in range(1000):
e1 = random.choice(elements1) * random.uniform(0.99, 1.01)
e2 = random.choice(elements2) * random.uniform(0.99, 1.01)
e3 = random.choice(elements3) * random.uniform(0.99, 1.01)

Ответить
ReputationHemp
ReputationHemp - 25.08.2022 18:45

this was so confusing

Ответить
Taiwo Ridwan
Taiwo Ridwan - 19.07.2022 13:58

Thanks for the video.

Please, how can one optimize the coefficients of a Logistic Regression Model using a Genetic Algorithm?

Ответить
Ankush Biniwale
Ankush Biniwale - 10.07.2022 01:25

Hello! I want to use multi-objective genetic algorithm for data imputation. How can I do it? Can you help me with the code?

Ответить
Shamim Ahamed
Shamim Ahamed - 20.06.2022 05:44

Code:
import random
def foo(x, y, z):
return 6*x**3 + 9*y**2 + 90*z - 25
def fitness(x,y,z):
ans = foo(x,y,z)
if ans == 0:
return 99999
else:
return abs(1/ans)
solutions = []
for s in range(1000):
solutions.append( (random.uniform(0, 10000), random.uniform(0, 10000), random.uniform(0, 10000)))
for i in range(10000):
rankedsolutions = []
for s in solutions:
rankedsolutions.append( (fitness(s[0], s[1], s[2]), s) )
rankedsolutions.sort()
rankedsolutions.reverse()
print(f'=== Gen {i} best solutions ===')
print(rankedsolutions[0])
if rankedsolutions[0][0] > 999:
break

bestsolutions = rankedsolutions[:100]
elements = []
for s in bestsolutions:
elements.append(s[1][0])
elements.append(s[1][1])
elements.append(s[1][2])
newGen = []
for _ in range(1000):
e1 = random.choice(elements) * random.uniform(0.99, 1.01)
e2 = random.choice(elements) * random.uniform(0.99, 1.01)
e3 = random.choice(elements) * random.uniform(0.99, 1.01)
newGen.append((e1, e2, e3))
solutions = newGen

Ответить
SidneyW. Mathias de Oliveira
SidneyW. Mathias de Oliveira - 17.06.2022 22:07

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!

Ответить
Usman Yousaf
Usman Yousaf - 25.05.2022 11:25

source code>

Ответить
Javier Ureña
Javier Ureña - 12.05.2022 11:08

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.

Ответить
Dakir Mossadeq
Dakir Mossadeq - 29.04.2022 20:56

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.

Ответить
tim 1
tim 1 - 29.04.2022 00:26

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.

Ответить
Andrew Frost
Andrew Frost - 19.04.2022 18:48

Excellent! very informative and understandable.

Ответить
Alviya Isieva
Alviya Isieva - 18.04.2022 13:05

Superb video, thank you very much! If I need x to be in a certain range, how could I define that in the code?

Ответить
Dibyojyoti Bhattacherjee
Dibyojyoti Bhattacherjee - 18.04.2022 09:57

one in c++ please...

Ответить
Joe Jacob
Joe Jacob - 02.04.2022 07:49

Is this really genetic algorithm. Where is selection, mutation and crossover implemented in this code? This is just generation some random numbers and iterating to get the result.

Ответить
Kovács Kolos
Kovács Kolos - 02.04.2022 00:06

I feel like the intent behind this video is good, but you should know the basics by hearth before trying to teach genetic algos and stuff. Also, the production quality is.....mediocre at best.

Ответить
Thomas Jarland
Thomas Jarland - 21.03.2022 20:43

Good evening, thank you for this video. I'm trying to reproduce this code to solve a second equation at the same time with 3 unknowns (6 unknowns to be determined) and I'm having a bit of trouble, do you have any advice?

Ответить
The Ashmedai
The Ashmedai - 07.03.2022 18:08

It's just sorting and taking the first 100 ?
Did I missed anything ?

Ответить
DataStako
DataStako - 26.02.2022 11:59

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

Ответить
FractAlkemist
FractAlkemist - 24.02.2022 01:03

Hey thanks for posting this! I couldn't solve my previous issue; that line just prints scrolling numbers and generations; so I wrote my own code to do that - works just as well, looks even better.

I am able to follow the code through, but I couldn't have written it myself from scratch (yet); the narrator seemed to be blowing it off the top of his head on the fly - I got a long way to go before I am that proficient!

I have adapted it to solve other math problems. You get a different (still correct) answer each time! I suppose there is a way to calculate exactly how many solutions there are to equations like that, or maybe if you are shooting for a "close, good enough" solution, there may be an infinite number, depending on how close you set the break-out. I don't know.

It uses MUTATION, and I found by increasing from 2%->10% it solves much quicker and shorter!

Also, as I study the theory of GA, there is something called "crossover". I know what it is, but I don't see it.
Is that used here? Or am I missing it? (be gentle, I am a newbie with GA).

Ответить
FractAlkemist
FractAlkemist - 20.02.2022 08:08

When I get to the line: print(f"=== Gen {i} best solutions === ") I get a "invalid syntax" error, and the cursor is placed between the final ") characters, suggesting the problem is there. Anyone else get this? I am using python 3.5.4 in Fedora Linux.

Ответить
Eugenio Polanski
Eugenio Polanski - 20.01.2022 02:34

would be amazing if you could do an implementation of the ES Hyperneat algorithm, but not using the typical google enviroments

Ответить
ghazal ghazal abdollahzadeh
ghazal ghazal abdollahzadeh - 29.12.2021 23:23

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

Ответить
Kudi Maysey
Kudi Maysey - 25.12.2021 16:53

Thank you

Ответить
Chris
Chris - 15.12.2021 07:56

In line10 you should change the returned value from an arbitrarily large value to 1/ machine epsilon. Your currently selected value is too small, and there is no way for a computer to determine the difference between zero and zero +- anything smaller than machine epsilon.

Ответить
Antoine Grand
Antoine Grand - 14.12.2021 14:03

I just can't understand the need to evaluate the cells as you do. Why bother the if ==0 and the 1/ans ? Instead of just ranking the evaluation the other way around ?

Ответить
AlphaBeta
AlphaBeta - 30.11.2021 21:58

Awesome video man! Your good in explainning! The end of the video is so funny :-D

Ответить
Jakub Červinka
Jakub Červinka - 24.11.2021 12:18

Idea great, but this is not how you write python :D

Ответить
Tecnolaundry AGS
Tecnolaundry AGS - 29.10.2021 09:03

Great great vídeo, i have a question.
Suppose You have polynomial roots ( -4,-2,-3,-1,4) and You are asked to find the 6 coeffients
How would You do it ?

Ответить
Pranjal Prasad
Pranjal Prasad - 29.09.2021 15:24

Thank You!

Ответить
A Dimas Cahyaning
A Dimas Cahyaning - 17.09.2021 13:25

This is really great, easy to follow

Ответить
sourcelocation
sourcelocation - 26.08.2021 00:50

Great video! Thank you very much

Ответить
Mouli Duddupudi
Mouli Duddupudi - 15.08.2021 18:30

I did the same thing as you did but didn't get any output. I am always getting error at sort() and reverse().

Ответить