Hello and welcome to this lecture, where we are going to implement a different function to select the
parents in this part here off the flow of the genetic algorithm.
And we will simulate this rule methods where the idea is to is being, they will in order to select
some of the individuals, as we saw in last lecture.
We have a higher probability of selecting goods solutions.
However, there is a small probability off select the not that good solutions, as we can see here.
For example, Ace three moving back to our Google collab fail, where we implement a new function in
our genetic algorithm class.
Let's define the function that select variants and it will receive as a parameter self, which is the
default parameter.
And also this some evaluation.
The results of these function we implement is before it will return this some of this class of all individuals
in the population.
First, we will implement that, this function and then we will perform some tests.
So it will be easier to understand how we can simulate the roulette methods.
First, we will create a variable called Barings, which will be equal to minus one.
These variable that we are going through returns, as we can see here, we have a list of individuals
that are parts of the population.
So the idea is to select one of these individuals here to apply crossover mutation to generate the new
generation.
So we will select a specific index of this list in our implementation.
We create is a population composed of 20 individuals, so we will return an index which will be associate
this to the population.
This variable parents will be initialized with this value, which means that we are not to return any
solo show because we know that indexes in Python starts at zero.
So we need to select a number from zero to 19.
Considering our population composed of 20 individuals, we will update this variable.
Then we will create a new variable.
Random value equals random and we will multiply by some evaluation the parameter that we are going to
send just to perform some tests.
We can go down here see that this evaluation equals one hundred and ninety six.
So we are selecting a random number in this range when we run this code.
We will get and the number in the range from zero to one and we can multiply by this sum.
We will get our value in this range every time we run this code.
We Gad's value one hundred sixty six to seven six to one, one hundred and thirteen.
And so one, we will use this value to simulate that roulette.
It will be this simulation when we spill the will to get one of the solutions.
Let's go back to our glass and we will create another variable sum equals zero and I equals zero.
The variable y will be used to control the loop.
We are going to use a while loop while I less than length self population.
It will guarantee that we are going to run these codes based on this size of the population.
Will we implement a model condition and.
Some less than they own, don't value, it will be easier to understand this condition when we passed
this gold in some minutes, then we will increment this variable.
Some blurs the equal cell population position.
I thought score evaluation.
We are doing something very similar what we implemented here.
We are just adding these values.
Then we will increments the parents plus equal one.
And also I B+ equal one.
Just a reminder that in this variable, we will return the index that was just selects.
Or in other words, we will extracts an individual off these list here.
Then we can just return after the while loop parents.
And to better understand these codes, we can put some prints brands.
Let's put some marks here to be easier to see the value random value when we run these codes.
The first step is to generate a random value between zero and this sum evaluation.
So let's put here the random value and inside the way you look.
Let's variants first variable I and then this sum.
And let's put here this sum will run again.
This code should create the glass.
Let's move back to this part where we are creating the new population.
We can see the results here.
The best in the video is in position zero.
Then we can just run these codes to get the best in the video.
We can prints this sum of évaluations equals eighteen thousand and just see the results.
Let's create parents.
One equals genetic algorithm.
Let's call our new function select parents and we'll send the here this some less visualize parents.
One.
Run this code!
See, that's the random value that was generated was one hundred and sixty seven.
So we are in if the random value is less than this sum.
As we can see here, these numbers here are equivalents to this sum of each one of the solutions.
Take a look here that we keep odd the the values and then when I equals four, then we can take a look
here.
That's this.
Some of the evaluations is less than the random value that was selected.
So we stop is like, you think the codes and the results of the function is 14, which is the value
of parents one variable.
We can take a look here that we are selecting these individual words score is six thousands.
Take a look here that we have bad their solutions.
Should the initial parts of the list.
But as I say adds in the theoretical lecture, the idea is to apply a random function to simulate that
rule.
Its methods.
So we are not going to select only the best individuals.
There is a chance of select the disordered individuals here that are not so good as these other ones
here.
But it is important to create diversity in this genetic algorithm, and the probability of selecting
these last individuals are less than.
Selecting these or other individuals in the initial positions off the list, because as we already know
in the initial positions, we can see the best individuals, we can try to run this code again.
See, that's another number was selected.
Now the random value is 77 and doing the while loop, we are adding the scores of the individuals in
the first position.
That's conditional.
Employment is in the way Loop is through.
So we select this individual number five and then we can take a look here.
12 thousands.
And every time you run this code, you will get a different individual and it is based on this random
value that is generated.
Let's create another variable variance two equals genetic algorithm.
Select variants.
I will send again this sum.
Let's visualize the index that was selected.
See, that's in the first is equation with selected individual number four, and the SEC one is a commercial
with selected individual number 12.
So we can use both individuals to apply crossover and mutation to generate the new generation.
So now we have implemented this select variants function.
We will also select two random variants and then we are going to apply the genetic operators.
That's what we are going to implement in the next lectures.
So there.