 Okay, so let's make a genetic algorithm. The first thing we're going to need is a function that we're going to try to optimize. Okay, so we're going to have a function of foo that takes x, y, and z. Then we're going to return, let's see, something that's kind of hard. Let's see x to the power of 3 plus y to the power of 2. And let's just do 90 times z. And let's multiply this by 9 just for fun. Okay, perfect. So basically we have a function that's going to do this calculation and it's going to return us a value. And what we want is we want to find the x, y, and z that satisfies this equation when it equals, let's see, when it equals, yeah, let's do 25. Okay, so we have to find a value where x, y, and z, when we do this calculation here, it's going to give us a 25. And to do that, we're going to basically set it to zero. So we're going to subtract 25 from this expression here so that when we get the result back we can basically say, okay, whatever is closer to zero is more fit than what is not. Okay, if that doesn't make sense, just keep watching, you'll see what I'm doing. So then we have to define a fitness function and this fitness function is going to take the parameters that we passed into our foo. So x, y, and z. And what it's going to do is it's going to run our function up here, our foo function. It's going to pass the x, y, and z into that. Then what it's going to do is it's going to check if the answer is equal to zero, then we're going to return a really big number. So let's just say we're going to return 99,000. And if that's not the case, we're going to return the absolute value of 1 divided by ans. Okay, so if you don't understand what I'm doing here, basically we need a way of ranking our basically parameters. So when we pass something into this fitness function, we're asking Python, hey, how good do these parameters do against this function? How close did we get to zero? And if we got exactly to zero, we say, hey, this is 99,000, this is like really good, this is a really good solution. If not, then what we're going to do is we're going to take 1 and then we're going to divide it by ans. And the reason for that is because the smaller your denominator is, the bigger the result you'll get. 1 divided by 0.0001 is going to be a pretty big number. So if you don't really understand what I'm doing, so let's say I have a Python and I divide 1 by 0.001, I get 10,000 from that. So the smaller the result is, the bigger it ranks. So later on when we sort our results, we are able to get a high number based on how close this is going to be to zero. Also we are taking the absolute value because if we take the absolute value of this, then we are able to basically, the absolute value lets us have a negative answer. And even if it's negative, we can still treat it as a positive. So I don't care if it's a negative and really close to zero or if it's a positive and really close to zero, we can treat it the exact same way. So let's actually get back to it. So next we are going to generate our solutions. So this step is really basic. All you're doing is you're making a bunch of random solutions to this problem. So I'm going to say 4s in range and I'm going to generate 1000 solutions just to keep it simple. So actually I have to define a new list. So let's say in here we are going to say solutions.append and we're going to append a new tuple into the solution. So this new tuple is going to be our random number, so I forgot to import random. So we're going to use a random number and this is going to be a uniform. And it's going to be between the sizes of 0 and 10,000. So this is the x and then the next one is going to be another uniform and this one is going to be a value from 0 to 10,000. All of these have to be different from each other. You can't have the same uniform for every parameter. So and then finally we're going to have another uniform and this is going to be again 0 to 10,000. Okay. I think I have to clean this up a little bit. Okay, perfect. What's the problem here? I forgot to close this. So basically our solution is going to be a tuple and this tuple is going to have three random numbers. That's essentially what it is. Now what we're going to do here is we're just going to print the top five solutions just to see what we get. So we're going to print the solutions, we're going to print the first five. So let's run this. So let's see. As you can see I'm getting a tuple here with three numbers, I'm getting a tuple here with three numbers, I'm getting a tuple here with three numbers. So that's good. So what we're trying to do is working. So the next step is to actually do the genetic algorithm and this genetic algorithm is going to run in steps. So it's going to run once, it's going to generate a generation, then it's going to run again. So we have to do this in the loop. So I'm going to say 4i in range and this is where we define the maximum math times that we want to do this. And let's say I'm going to do this 10,000 times. You can set this lower or bigger, it's really up to you. So now what we're going to do is we're going to say ranked solutions and we're going to make this into a list and we are going to go for every element in our solution for us in solutions. And what we're going to do is we're going to append to ranked solutions. We're going to basically append the fitness function value that we get from these tuples basically. We're going to append the fitness value. So this is the fitness and then we are going to also append here the S. So what we want is we want the solution plus we want the tuple that we use to get that solution. So now we're going to close this here and we're going to close this here. This is going to be a tuple. So as you can see, just to make it clear, this is a tuple and this tuple contains the ranking of the solution that we got from these values that we passed into the fitness function plus the tuple of the parameters. Okay, so now that we have that, what I'm going to do is I am going to rank solutions.sort. I'm going to sort this and then we are going to reverse this, we're going to reverse this list because it's going to go from the smallest, the biggest one we want is we want the biggest first then the smallest. Okay, so now what we're going to do is we're going to print and then we are going to use an f-string and we are going to print gen i best solutions. Okay, so this is, yeah, okay, so this is taking the best, we're basically getting the best rank solutions here and then down here we are getting, we're basically printing out the best solutions. So let's actually just print a couple of them. I'm going to print the top one, actually not all of them. So solutions, not solutions, ranked solutions. And I am going to print the, actually I'm just going to get the first one. So it's going to be random because every iteration of this loop, we're not really doing anything else, it's just going to give you the same thing. So I can run this, but it's basically going to loop over the same solution over and over again. So as you can see, we got a ranking of nine for this, no, actually it's, no, it's actually a pretty small ranking, but yeah, we are using these values to check against our fitness function and our fitness function is telling us these values give us the ranking of this. Okay, let's actually close that. Okay, so what's the next step? The next step is going to be to combine the best solutions and make a new solution from those combined solutions. So let's say we are going to go, let's actually, no, I'm going to keep this actually, okay. So the next step is going to be, we are going to combine the best solutions in our solution set and we're going to make a new solution based on those old solutions that were good enough in our opinion. So what we're going to do is we're going to say, okay, best solutions, then we're going to say this is equal to ranked solutions and we are going to take the top 100. So the top 100 ranked solution is going to assign to the list best solutions. And what we're going to do is we are going to go into a loop and we're going to basically extract every solution. So let's say elements and this elements list is basically going to hold all of the best solutions in the, not the best solutions, but the best parameters for the solution. So for example, for elements, element in elements, what we're going to do is we are going to append, no, not the elements, okay, for best solutions, for element in best solutions, actually, let's just say s, for s in best solutions, we are going to append, let's see, elements dot append, then we are going to append s and then we're going to say the first index and then we're going to say zero. So basically we're extracting all of the parameters we used for our function here. And when we do that, we are going to have basically a big list of, a big list of parameters that we used. And from this, we are going to, we're going to combine new solutions. So now what we're going to do is we're going to say new generation is equal to a new list. And then for, put an underscore because I don't really care about this, I don't care about this induction value here, what we're going to do is we are going to basically take a random element. So let's say element one is equal to random dot choice, and then we are going to go into our elements and we are going to just take a random one and then element two is going to be random dot choice elements. And then element three is going to be random dot choice elements. Okay, and then we're going to make a new solution here by saying, okay, new gen dot append, and then we're going to add the solution as a tuple. So it's going to be e1, e2, and e3, okay, perfect. And then down here, we are going to, okay, let's see, solutions, rank solutions, okay, so I guess down here, we're going to assign ranked solutions to new gen, okay, and that should work, I think, okay, let's actually try this. Now it didn't work, what did I miss? Oh yeah, I have to, okay, my bad. So another thing you have to do is you have to basically mutate the solution. So we're going to say random dot unique form, and we are going to, we're going to mutate this by 2%. So we're going to mutate all of our solutions by 2%, just to give them some diversity. Okay, let's actually get this running. Okay, that doesn't work. Why not? Oh no, okay, we have to assign this to solutions. Okay, let's stop that, and let's run it again. Perfect, perfect, okay, let's see. So it's slowly finding the optimal solution. Let's see how long it's going to take, it's probably going to take a minute or so. Oh, oh, do you see how the rank is getting higher? Do you see how basically, oh, actually, whoops. Okay, the thing I missed here is if we find something that's good enough, we want to basically stop this loop. So let's say, so if this rank solution, if this rank solution is good enough, if the top solution is good enough for us, we are basically going to stop. So if this is greater than 999, well actually 999, this is good enough. Okay, so what we're going to do is we're going to break out of this because we basically found something that's good enough for us. We don't want to find the perfect solution. We want to find something that's good enough. So let's run this. Okay, so we're going back to our solutions. We're on generation 200, and yeah, it's slowly, it's slowly, okay, let's see. So we found this solution, and this solution is, okay, let's see if this works. So let's copy this, and let's look at our function. So our function looks like this. Okay, so Python 3, and then let's take our values here, and let's check them against this function. So let's see, 6 times x, and then we are going to cube that, and then add that to 9 times y, and we are going to square that, and then we're going to add that to 90 times the solution. Hit Enter, and then we get 24.999. You know what? For me, that's good enough, and I hope you enjoyed watching this video, and thanks for watching.