Hello and welcome to this lecture, where we are going to implement and fast they cross over functional,
according to the theoretical lecturer.
And in order to implement crossover, we need two individuals.
And in this part of the codes, we define only individual number one.
So I will copy all these codes, open a new cell based here.
Or we'll just put this first sprints in comments.
Puts a higher tune and less great individual.
No, true.
We will sans the same parameters.
And here we will just change individual.
Sure.
Let's supply the fitness function to individual.
Let's change here.
Individual chore and individual virtue.
Let's run these schools and we have a new individual are Neal solution.
For example, this car is twenty two thousand and this base two point forty one.
And regarding that other solution said that this car was only one since this space occupied by this
products are over the limit so we can run it again.
And Etienne's we can just implements and larger prints.
Choose C D chromosome.
Let's Typekit here in this video.
Once that chromosome.
Copy and paste it here in the video.
Sure.
Let's run this code again.
We have these solution and the chromosome.
Let's run here again.
Another solution.
Twelve thousand and only 10000.
So this second solution is better than the first one.
Now we need to combine these chromosome with these all other chromosome here.
To do that, we can move back to our individual class.
Let's create a new function death cross over self, which is the standard parameter, and we need to
sand's the order in the video because we are going to combine these chromosome here with the chromosome
of these other individual that we are going to stand as a parameter.
The first step is to define the cutoff point, according to these is like the here, we need to randomly
select one of these points.
We can type again random.
It will generate a number in the range from zero to one.
And now we'll multiply by the length of the chromosome.
For example, if our chromosome has four theme positions, it will generate random number from zero
to 14.
If our chromosome has 20 positions or 20 products, when we multiply both values, we're we'll select
a number from zero to 20 just to perform at best.
We can copy and paste this code here, but we don't have the information about the chromosome.
Let's multiply by 14.
See here.
That's every time we run this code.
We have a different number.
So we will use this number to select one of these cutoff points.
We can also rounds this value to have an integer number every time we run.
We get an integer number in the range from zero to 13.
And lots from zero to 14.
Because indexes in Python.
It starts at zero.
So if we have 14 positions, we need to select a number between zero and 13.
We can rounds this number here and after dance, we can generate child.
One equals let's axis all other individual dots chromosome.
And now we will select from all z shows zero.
Sure, they cut off points.
In other words, we are selecting only this part here of the first berries one, zero and one, according.
What we have here was zero zero.
Should the cut off was the usual and then we need to combine with self that chromosome from the cut
off point should the and off the list.
Because of that, we use these two symbols.
So in other words, we are selecting from the parents chew from the cut off position until demands of
the list.
So child one is the composition of the first part of parents one.
And the second part of parents chew.
Now we need to create a child's number.
Sure.
Now we will do something very similar.
Child chew.
Let's access the inverse self chromosome from position zero to the cutoff point.
And how will concatenate with the other individual?
Let's access the chromosome from the cut off points until the end of the list.
We can prints they they off points just to perform the first test and also grants child one and child's
chew.
Let's recreate the individual class.
Now we need to run this code again.
We have another in the video see here that the score equals one.
So let's run it again.
11000.
Let's run in the video.
Sure.
17000.
Now we can combine both individuals in the video one dot crossover, and now we will stand as a barometer
in the video, too.
Let's run this code.
See, that's the cutoff point is 11.
For example, we can take a look at the positions.
Zero one two three four five six seven eight nine 11.
So we are selecting these good off points.
We can take a look at the first child's.
See, that's it is the first part of the chromosome.
And instead of have one, zero and one.
Now we have zero one zero because we are selecting the 11 parts.
So we are combining the first part of this chromosome with the last parts of this order chromosome.
And regarding child number two, we can take a look at the initial part see that these values are exactly
the same, but the and's is different.
We are selecting the first parts of individual one and we are combining with the last parts of individual
two.
We can run this code again.
The cutoff point is this section gene should be easier to see the results we can run again.
The cutoff point now is number nine.
We can see zero one two three four five six seven eight nine.
So now we have the first part of the chromosome.
This part here is combined with the last part of the first individual, as we can see here.
If you want to see more results, you can run this code and check the values.
Finally, we need to go back to our function and we'll put this Bryants in comments.
I will create another variable.
Children equals in the form of a list.
We have only the chromosomes here, but we need to generate anew individual because each individual
must to have all these information here so we can now create a new individual object.
And let's sans added parameters, self thought spaces, self thought prices, self thoughts, space
limits and finally, self thought generation plus one.
So we are defining the next generation when we create the new generation, which sets the initial value
to zero when we create a new individual.
We increment the number of generations and now we can define exactly the same for the other individual.
I can just copy and paste the codes.
It will be exactly the same.
And now we need to fill this parameter here.
The chromosome for each one of the individuals as we have a buy on the list here, we can access each
one of the positions.
Children positions zero.
Let's specify the chromosome, which will be equal to child one.
I will access children in position one.
This is position zero.
And this is position one.
Let's access the chromosome.
It will be equal to child and number two.
And finally, we will return the children then list with the new individuals.
Let's recreate that in the video class.
And now we can perform one last test.
I will create a new in the video, see that the value is a sad thing.
The limits are we will run it again.
Let's create in the video.
Number.
We need to run it again because they value exactly the available space in the truck run again.
Now we can combine them, see that it was returns, at least in the video one and in the video to just
shoot fast.
We can create the children variable.
And now we can access each one of the positions, for example, sample position.
Zero.
And in order to get the values, we can access this core evaluation.
See that this variable is not defined since there is a typo here in the definition of the glass.
I will just change here and create again.
Now we can run this code again.
And we also needs to correct the here.
Let's create the glass again when we run these other codes.
There's a typo here because we need to correct his quiet evaluation.
This code?
We also needs to correct a score evaluation.
Let's generate the children now we can see that this quiet equals zero because we first needs to evaluate
so we can access children zero thoughts fitness, let's call this function see that the value is for
the thousands.
We can also take a look.
That's the solution.
Children zero dot chromosome run these codes we can see here the results.
We can just brands.
Both lines should see the violence run again.
Now we can copy and paste this code.
Let's change to children, no virtual and now we can compare the results.
This is one combination and this is another combination.
We will use the cross over function to generate a new population later on in the implementations and
in the next lecture, you are going to implement the new facial, genetic or breakthrough.
See you there!