Hello and welcome to this lecture, where you are going to learn how to initialize the population for
the genetic algorithm.
Population is a set of several individuals.
For example, here we can see individual one in the video out show an individual tree and each one has
its own solution.
A set of zeros and ones and the ones means that we are going to load those products on the truck.
So instead of creating just one or two individuals, as we did before, we need to define the number
of individuals that our population will have.
For example, 50 individuals are 100 individuals.
You need to perform some tests in order to see the best sets of individuals that you need to create
will finally implement the first part of the graph, as you can see here, which is great initial population.
So far, we implemented functions related to the individuals, such as the fitness function, the crossover
and mutation.
Now that those functions are ready, we can create the initial population or post off several individuals.
Now let's move back to our Google collab file, which we started the implementation.
And so far we have created the product glass and also there in the video glass.
Now we will create a new glass to represent the genetic algorithm.
Let's open a new set of codes and we'll create the new glass genetic algorithm.
And let's define the init function, which means the constructor of the glass every time you create
an object of this glass.
This function will be executed.
It's let's first specified the default parameter self and we'll specify another parameter, which is
the size of the population.
Now let's create the attributes.
The first one population size equals should the parameter population size the next one, we will create
population.
It will be at the beginning and empty list.
And here we are, going to start each one of the individual objects in each position of this list,
there will be an individual objects.
Another parameter self generation equals zero.
We will start the algorithm with this value.
It's similar to the generation attribute, which defines a year for each in the individual class.
Another parameter, self thought based solution at the beginning, it will be equal to no one in these
attributes.
We will start the best solution considering all generations.
We will see more details later on and also how we will define another attribute list of solutions at
the beginning.
It will be an empty list.
We will use both attributes later on.
Now, let's define a differential to initialize the population, initialize population self.
This standard parameter and it will receive this basis, the prices and also this base limit.
This same parameters we discussed before should generate annual population is quite easy.
We just need to implement a for loop.
For I in range self thoughts, population size.
We run this far loop according to this parameter that we are going to use sands to the class.
Now let's access our population.
Lots of bands.
It means we will have bands, objects should this list here and now we can create the individual objects
and I will send spaces, prices and space limits.
Just to recap, we are creating objects of this glass here and we are going to sans these parameters.
Take a look here.
That's when we run this in its metals.
We are initialize each one of these attributes, and we are also running this code here to initialize
the chromosome with at random solution.
And after initially using the population, we can access the attributes best solution.
It will be equal to cell population in position zero.
We are going to see more details about IDs in the next lecture, and we need to specify which is the
best in the video that was generated and we are sitting at position number zero off the list.
Of course, this is just to initialize this attribute and we will see more details later.
Let's create this class.
Now we can go back here.
I will open a new cell of code.
I will create a new variable population size, for example, 20 individuals.
Let's create the genetic algorithm quali the glass genetic algorithm, and I will set here the population
size variable we defined here.
It means that we will initialize the algorithm using 20 in these blocks.
Now we can call our function genetic algorithm initialize population, and let's send here this basis
if prices and limits.
Let's run this code.
Now we can access our genetic algorithm.
The population tributes run this code.
See that in each one of the positions, we can find an individual object.
Let's check this size length population.
It is equal to 20.
This same number we defined here.
So it means that there are 20 random individuals in our genetic algorithm object.
We can take a look at some of them population position zero.
This is the first objects are a solution in the genetic algorithm.
Let's take a look at the chromosome run this quote we can see a random solution.
We can also open a new cell of code.
Let's take a look at the sequence in the video.
Here we can see the solution and so on.
And now the implementation of the first step of the genetic algorithm is ready, and in the next lecture,
we are going to evaluate the population.
See you there.