Hello and welcome to this lecture, where we are going to work with the deep library to solve the problem
about the transport of products.
The good point is that we will be able to compare the results of the library with the results of our
implementation from scratch.
If you want to know more detail about this library, you can follow this link here to get more information.
We can see a brief description of the library and as we can see here, it includes the following features
genetic algorithms, which is the focus of this class.
However, we can see more features, for example, Evolution Strategies mode, the objective optimization
cooperative and the competitive evolution, and many others.
As we saw in the introduction lecture, the genetic algorithms are just a part of the evolutionary algorithms.
So if you want to know more detail about this topic, I recommend that you go deep into these documentation
here to see and to learn more about all other evolutionary algorithms.
Now let's install the library PIP.
Install the App Room.
This sale of codes we can see here that it is downloading the app version one point three point one.
So we are using this version of the library.
In case you have a different version, you can install these.
A specific one should not have problems with different versions.
You can just type PIP install.
The app equals one point three point one, so the codes we are going to implement will be compatible
with this version here.
Now we need to define the class of products.
We'll just paste this code to here.
This code we implement as before and I will just paste.
It should be more organized than we need to create a list of variables.
I will open a new set of codes and based these other codes we implement, as before, the list of products
we are adding in this list before parting products off our case study.
Then we create this basis prices and names lists.
We have bands the values to each one of then this base limit, population size, mutation probability
and number of generations.
Now we run this so of codes to create these variables.
The next step is to eat birds.
The deep library the first one in part no bite.
This is a scientific library in Python to work with matrices and vectors.
We will use it later.
F-words Random to generate random numbers from deep import base from the imports.
Create third from the import algorithms and finally from the deep library.
Let's import the tools package.
We are going to use them in some means run these codes.
No error was returns, so it means that the installation was made successfully.
Now we need to create the fit in this function, which will be exactly the same.
We implemented before.
So when working with libraries, you must implement the fitness function.
Just a reminder that we have this list here.
Zero means that we are not going to load the product on the truck, and one means that we are going
to load the product in the truck.
We can create this function.
Death Fitness.
It will receive.
As a barometer, a solution or a chromosome, now we can create the cost variable.
We are going to return to this variable, which will be equal to this sum of all prizes.
Lest lecturer, we used a different terminology to represent this variable.
But it's important to know that when you work with genetic algorithms, it is common to see these word
cost, which means the results of the fitness function.
Let's create some space equals zero.
Now we need to go through each one of the zeros and ones off the solution.
For I in range length solution, we are going through each one of the zeros and ones.
Let's check if Solution Position II equals one.
Then let's increment the cost plus equal prices position, i.e. we are accessing this list here.
Then we need to add space.
Some space plus equal spaces position i.
It is this list of spaces here.
Then when these short verify after default, look, if some space greater than the limits, the cost
will be equal to one.
And then let's return the cost.
Something different when implement the genetic algorithms using deep library is that we needs to put
this comma here in the next set of calls.
You will understand why we need it.
Let's create the fitness function and now to create some objects related to the deep library.
The first one, let's create the two blocks variable, which will be equal to Bayes dot two blocks.
We need to create two blocks from the base package to initialize the genetic algorithm.
Then let's go creator.
We imported this package here.
Dots create as we have a maximization problem.
We need supports here.
Fit in this max bayes dots fitness.
It is related to the fitness function and now there is another parameter quote weights equals one and
comma here.
See that we have this comma after the value, according should this part here.
When you work with the app, you can return more than one value.
For example, if you had another variable here that you once to optimize your code returns, show variables
here.
For this reason, we need this goal Mahere in this particular case study.
We are returning only the cost, which is the sum of the total price, and this cost is associated with
this value.
One means that the results will be in the range from zero to one.
Then we need to create the individual creator thoughts create and I will type here the name of the glass.
It is related to the deep library.
In the video, we want to generate a list of individuals and now let's type.
Fitness equals creator again thought fitness max, which means that was to maximize the profit or maximize
the cost.
We can run this sale of codes.
Now we needs to register some other objects.
Let's access our toolbox variable, but register now it needs to specify the type of our soldiers.
Washing or that type of the chromosome, let's type A, B, D, R, which means attributes.
Boom.
We are defining a Boolean attributes, which means this solution r d chromosome is composed only of
zeros and ones.
Then we need to specify how we are going to initialize the chromosome.
We will use the transient function as we implement as before, and this solution is composed only of
zeros and ones.
This part here means that this solution will be generated this way here only zeros and ones.
Let's register another component.
The box, but registered the next one is the individual.
Those thoughts it needs.
Repeat it.
The means that we are going to generate random solutions.
Creator but individual.
It is the name of this glass here.
Then we need to specify the box a our bool and this size and equals 14 because there are 14 products
in our case study.
These two lines of code are registering the individuals which are composed of Boolean attributes.
Now we can register the population.
Let's type here, population.
Those dots init repeats to generate a random population, then type the population will be a list.
And now we need to specify the barometer in the video.
So we are creating a population associated with this glass here.
The next step, the box that register evaluates how we are going to evaluate the population using the
fitness function we defined the year, then we need to specify how we are going to apply the crossover.
It is related should the maid's parameter, and then we can access those C x worm lines.
It is the crossover we implement as before, when we have only one cutoff point.
We can see here more options if you was to go deep into this library.
You can see these c x at the beginning means different types of cross over that you can apply.
Let's register another parameter.
It will be the mutation.
Those dots must flip the bit.
It is the mutation we implemented from scratch.
When the number is zero, it is changing the G1 and when the number is one, it is changes to zero.
And the last parameter AI and beep beep beep beep means probability.
We will specify when persons of probability to execute the mutation and the last parameter, the box
thought register how we are going to select the individuals are the best in the videos.
Those dots sale roulette, which is the one we implement as before.
You can see here more options if you want to go deep into genetic algorithms theory.
Let's select the roulette methods.
Now we can read these codes.
There is one error here just correct this typo run again, and the basic structure of the genetic algorithm
is already implemented, and in the next lecture, we are going to finish the implementation.
See you there!