Hello and welcome to this lecture, where we are going to finally run the genetic algorithm and then
we will put all the cores together in this part of the code, the.
We just did all these step-by-step implementations to see the partial results.
But now that you have understood all the functions and all the classes we implemented, we can implement
a new function to boards, all the codes together and to run the final genetic algorithm.
We will create another function again in the genetic algorithm class.
Then they will be so and it will receive some parameters.
The first one, the probability of applying mutation, the number of generations, which is related
to this stopping Darian and like in space, is occupied by the products, the prices and also the limits
of the drug.
Now we need to implement all this flow here.
The first part, as you already know, is to create the initial population so we can just grow our function,
initialize population.
Let's send as a parameter spaces, prices and limits these parameters you can see here.
The next step of the algorithm is to evaluate the initial population, and in order to do that, we
can implement a for loop for individual in cell thought population.
And let's just go in there, though.
But fitness, when we run these codes, each one of the individuals will have ice.
Now we need to order the population self, but our population put the best solutions at the low positions
of the list and then we can call our new function visualize generation.
So we will get the best individual and threatens the values now that we are going to implement this
stopping criterion.
That, as I said before, is based on the number of generations you want to create.
We have this parameter here.
Number of generations so we can implement a for loop for generation and range number of generations.
If you define 102 generations, we will run the genetic operators 100 times.
This is the implementation of this thought being carried, Daryll.
Now we need to select the parents to combine dams to generate that new generation applying crossover
and the mutation to select the individuals we needs to stem the violence of the generation.
So we are going to call the function some evaluations.
Let's create the new population and at the least, and now we need to apply the genetic operators.
We will implement a major for look for new individuals and range from zero to this size of the population.
Population size and we will increments by Chu as we implement as before.
For example, if we have 20 individuals, we need to any new individuals and not for the new individuals.
Now we can apply this function.
Select parents.
Let's select parents.
Number one equals self.
Select parent.
I will send this some parents.
Sure.
Equals self select parent.
Again, we send this sum after selecting the parents, we will have a list of children so copulation
parents one and let's apply the cross over functional self calculation.
Parents to this code is quite similar to what we implement as before.
Now we will define this new population lots up bands, children in position zero.
Let's apply the mutation.
We'll send here the probability new population, but that bands children boys issue one.
Let's once again the mutation probability and after the far loop we need to discard the old population
should do that is quite simple.
We just axis copulation, which is these are terrible at the EU.
The list of the first population, it will receive the new individuals that were generated, so it will
be equal short list new population.
This list is just to make sure to have the date that in the correct format.
So now we have all these parts already implemented with select parents applied crossover mutation.
We discard discarded the older population and now we need to evaluate the population in quite similar
to the process we implement as before.
So we are going to repeat the codes we implemented here, and it is important to emphasize that this
is only the initial population.
And inside this far loop, we are evaluating the new population that is being generated so we can implement
the for loop for our individual in self thoughts population.
Let's go.
The fit in this function to get this score for each one of them.
We can also visualise the new generation.
We can get that passed in the low self population position zero and then let's go our function based
individual standing as a barometer.
The best one.
Just to make sure that we are going to star in this attribute here, the best individual, all three,
all the population and all the generations after the far loop.
After we go through each one of the generations we can bring into the result, I will copy this code
from the visualizes generation.
And let's face it here, we just needs to make sure that the Bryans is outside the far loop.
We can both hear the final message.
I will put these symbols just to be easier to visualize.
Best solution and the results of the best solution of all generations will be in this attribute here,
so we will just change here.
Self Thoughts Best solution score evaluation.
This space.
Self Thoughts Best solution using space.
And finally, let us visualize the chromosome self based Social Thoughts chromosome.
And finally, we can return the chromosome self docked best solution, but chromosome.
After running the algorithm for a specified number of generations, we will return to the chromosome
of the best solution.
Let's recreate this function again and here at.
After the initial tests, we will open a new Baxter cell holding all the coal together and just to be
easier to understand.
I will based our call here.
We have the products list, the refrigerator, the cell phone and so one with the fine, the spaces.
The price is the names, one list for spaces, one list for price is and one list for our names.
We have the limits the population size, mutation probability and also number of generations equals
100.
You can make some tests using 50, 100 200 and see the results.
Now that's great.
The genetic algorithm and let's sans the only parameter, which is the population size, we will build
a genetic algorithm composed of 20 solutions per generation or 20 individuals, population software
and individuals.
And the way we were only called 100 times, which means the number of generations we can get the results
equals genetic algorithm.
Let's call our new function solve and sense the parameters.
The first one mutational probability number of generations.
This space is the price and the limits.
We can't print the results.
Let's run these codes.
There is one a horror in this part two here.
Best equals self population size.
Here we can specify only the population and not population size.
See that we are getting the messages regarding mutation before and after so we can use that messages
in comments.
So let's go up to the individual glass and put both lines here in command.
Creates the individual class again.
Now we just need to correct the genetic algorithm class.
This part here population creates the class again.
Now let's move back to this part of code and run again, and we can see the results, for example,
in generation zero.
The total price was eighteen thousand and the user space is two point fifty eight.
Here we can see information about the chromosome zero.
If we are not going to load the product and when if we are going to load the product, this prints here
are related.
Should these function visualize the generation, we are visualizing the best of the 20 individuals in
each one of the populations.
See here.
That's after running generation one.
The total price is less than the first generation.
Just a reminder that's generation zero is randomly initialized, and the all other generations use cross
over and mutation to create the new population.
See that in generation number one.
This base used was only zero point eighty two.
This is a very bad results so we can keep moving to all other generations.
For example, Generational Florida one, the total price is twenty two thousand.
And after some other generations, the values starts to be bad.
For example, from seventy choo choo 79 80, the total price has a very good result.
So you bands two finds bad results in the last two generations.
But this is not always true because as we are working with random initialization, we can get the badder
solution here as the first generation.
But maybe.
It will be hard to happen at the hands of the algorithm, we can see the last sprint here.
The best solution was found on generation 100s.
See, that's the total price twenty two thousand eight hundred and forty for this base, two point sixty
eight.
And the way it can take a look at it chromosome and every time you run these codes, you may get different
results.
Let's see here.
The first generation 16000 and the final result here was also found on the last generation generation
one hundred and a total value twenty four thousand.
And there is a small typo in the schools because when you run this code, the best solution will also
be sad to hear Generation 100.
So we need to perform a small change in this last spring statement.
See here that we are accessing population zero, which is the last population that was generated, so
we will always have this same result here.
So we need just change here based solution thought generation.
Now we can recreate this function.
We are accessing the attributes a year to get to the result.
And now let's run it again.
Now we can see that's the best solution was found on Generation 27.
So it doesn't means that you will get always the best results in the last two generations, so you'll
need to pass this code and see in what generation you are going to find the best results.
Let's run it again now.
The best solution was found on generation number 20 and in total value twenty four thousand.
When working with four genetic algorithms, you need to run the algorithm a couple of times and see
if you can get bad and bad results.
Then you can choose this solution here.
When you are satisfied with the results, let's suppose you are also turns off at Albany and you will
give to the company just this final solution here.
You will be responsible for running the algorithm and giving to the manager.
The list of products that they show, the loads on the truck and you see bad results.
We can implement an additional quota here for I in range length product list.
We are going through each one of the products.
If results position, I equals one.
It means we are going to load the product.
So let's sprint to hear the name of the product.
Let's access products list.
Was you sure?
I thought.
Name and we can also brand the price of the product.
Let's access product list.
I thought Price.
Let's run this genetic algorithm again.
We can see the values regarding each one of the generations, and the final results is here.
For example, the total price is twenty four thousand and you are going to Sand's to the manager.
This list of products the cell phones, TV, 55 TV, 50 notebook, microwave, refrigerator and so on.
And we can see that's the algorithm is performing well since the ventilator has a very low price.
If we compare to the other products and it occupies a large space on the truck, so the algorithm pans
to not select the ventilator because our goal is to maximize this sales price and then the next lecture
we are going to generate a graph to bed through visualizes is or.
Suits, see you there.