Hello and welcome to this lecture, where you are going to learn about one of the main concepts of genetic
algorithms, which is the individual, as you can read here.
They represent solutions to the problem.
Let's assume that are banned individuals in the algorithm.
Each one of them will have a different solution for the problem.
For example, considering the problem of transporting products, the first individual may indicate that
we should only carry refrigerators.
This section one may indicate that we should carry televisions and notebooks, while the 10th individual
may indicate that we should only carry cell phones.
Therefore, a set of individuals forms a population.
We have another concept that is called chromosome, which actually represents a solution to better understand
it.
Let's consider this image that contains the four theme products for our case study and the sets of zeros
and ones.
Then number one indicates that we are going to take the products, and the number zero indicates that
we are not going to take the product.
For example, in this solution, we will load on the drug, this cell phone, this televisual here,
the notebook, the microwave, the refrigerator and these all other notable Cahir, all these other
products, for example.
These are other refrigerator.
There's two devices here the ventilator.
This two microwaves designed the refrigerator and the last the note.
The broker here, once loaded on the truck, the chromosome is.
This sets off zeros and ones, which indicates whether the product will be loaded.
Let's go back to our Google CoLab file.
I will open a new cell of codes and we are going to create the individual class in order to represent
this solutions.
Let's state Glass in the video, and again, we need to define the constructor of the glass.
The in-need function self is the standard parameter and it will receive this basis.
The prices this base limits and also another concept generation equals one.
This is the initial value for this parameter.
Later on, we'll discuss more about the generations we will sand's to this glass.
The information about the products, for example, we have the prices of the products and also this
base each product occupies in the truck.
This is the list of prices and the list of spaces.
And also we will sand's this base limits, which is related to this information.
In our case study, the maximum capacity of the truck is three meters.
Now we can create the attributes of the class, so basis equals basis.
The new attributes we are defining now will be equal to this basis parameter self that prices equals
prices.
This parameter here sells space limits equals space limits.
Self generation equals the parameter generation.
And finally, let's define the list of solutions.
Are the lists of zeros and ones we will define the naming chromosome equals open and closed.
Brackets, it means that we are defining an the list, and the next step is to initialize the chromosome
with a random solution, as we saw here.
And we need a list composed of 14 positions and in each position we are going to set one.
If we are going to load the products and zero, if we are not going to load the product, of course,
later on, the genetic algorithm will put ones should the best products in order to maximize the profits.
But in this initial step, we need a totally random initialization so we can implement a for loop.
For I in range length basis, for example, if we have four denes basis, we need to build a far loop
to go through.
Each one of the spaces are to go through each one of the products.
Now we can access a functional from by them, which is the random function.
I will open a new cell of code tree bark.
This function from random, in part random.
Let's run this cell and just shoot past it.
We can just type random, run this code, see that every time we run these codes, we have four different
random number and it will generate numbers from zero to one.
And we can use these random function to define if the value of the chromosome will be zero or one.
So we can apply the following if random, less than 0.5.
We can access the chromosome attributes and lets up bands zero here or otherwise.
If the random number that was generated is greater are equal to zero point five, then we will axis
the chromosome and we will the bands number one we are doing.
It's because there is a probability of 50 percent off.
Select the R number over 0.5 and also there is a 50 percent probability of selecting a number under
0.5.
So we are using this random function to choose if we are going to load or if we are not going to load
the product in the truck.
And as I said before, this is just the initialization of the individual.
Now we can create this class and to perform the initial best, we can create three new variables.
The first one spaces and the list, this section one crisis and the list and names again and empty list.
In this order list here, see that we have the name of the product, the price and this space, and
we are defining three new lists just to be easier to implement the codes.
As the names of the variable suggests, we will use each one of this lists to store the information
regarding the products so we can now go through each one of the products.
I will define that this new variable for products in products lists.
We are going through each one of the products and let's suggest the values, spaces, dots are bands,
products, but space prices.
But the bands, products, lots, price and finally names.
But the bands product but name I will also define the limit.
Variable, which will be equal should three it the means that the maximum capacity of the truck is only
three cubic meters.
We can create this variable and see the value.
Let's sprint spaces.
We can see the information regarding this space of each one of the products.
We can also.
Brent's prices run this codes.
We have the corresponding prices.
And finally, Bryant's names, and here we can see the name of the products.
We can also access specific positions, for example, names in position.
Five.
We can see Notebook A to see the price.
We can access the list of prices in the same position.
We can see here more than $2000.
And finally, we can take a look at this space.
Accessing this through variable.
Zero point zero zero thirty five.
As I said before, we just separated our original list in three new lists.
So the access will be easier now.
We can create the first in the feedlot and we'll create the new object.
The individual one equals let's call the name of the glass just to remind that we need to sense the
original basis of all products.
Vaporizes.
Space limits.
And when we create the object, we will assume the individual will be part of generation number one.
Later, we will see more details about this parameter.
Now let's sends the list off space is the list of prices and the limit.
It's important to emphasize that each one of these parameters will be qubits.
Should this part the here and then we create the attributes of the glass and we use lamp spaces just
to implement this for loop here.
For example, we can type LEM spaces.
The result is 14, which means that there are 14 products.
So we need to use this number here to generate 14 values.
Each one of the values will be related to each one of the products.
After creating the object, we can print some information.
The first one?
Spaces we can Typekit here in the video, one thoughts spaces.
This section.
Information the list of prices of all products in the world.
Thought prices brands.
The next information.
The chromosome individual.
One dots chromosome, let's run this cell of code we can see here this basis, the price and the chromosome.
What is important to say is that every time you run this codes, you will have a different chromosome.
As you can see here, because we are using this random function.
So this is a totally random initialization.
Now we can associate this chromosome to the products.
For example, no one here means that we are going to select refrigerator.
A zero means that we are not going to select this cell phone.
One means that we are going to select this for these other one.
Here means that we are going to also select this order TV.
Now we can implement codes to get the names of the products so we can associate ones with the names.
So let's implement a for loop.
For I in range land.
Product lists, we are going to be at this for loop for 14 times the same amount of the products, and
we can also print to hear the individual one dots chromosome position high.
Let's run this code.
We can see here each one of the files.
Of course, every time we run this code, we will get different results.
Now, it's quite easy to associate the ones with the names of the products.
I will implement an if statement if individual chromosome was the issue, I.
Equals one, then we can just prints the name of the product and we can access our product lists, position
pi dots, name, we need to tie the dots name here because remember that we have a list of products
in each position of the list.
There is one product.
Now we can run this code again.
Take a look here.
That's in this solution.
We are going to select Refrigerator A that cell phone, TV 55, 50, 50, TV 42 and so on.
Let's run this code again.
We will get another list of products, and that's how we implement the first step of the genetic algorithm,
which is the definition and representation of the solutions.
And in the next lecture, we will continue the genetic algorithm.
See you there!