Hello and welcome to this lecture, where we are going to create a new project in bicarb to connect
our genetic algorithm to the database we created using my scale so we can click New Project in Dubai
Sha idea.
We can define it the name of the project, for example, genetic.
And here you can specify the folder you were going to save it.
We are going to use the goal in that environment.
So when we created the project, a new environment will be created in Anaconda that we installed before.
Let's click Create.
We can delete this file that was automatically generated.
Let's create a new by file how will call genetic algorithm.
And now we can import some libraries from random in part, random in part.
Mads Lots lib dots by plot s VLT.
This is a library to visualize graphs by them and also lets you boards by my L to contact our Python
file through the database.
When we try to run this code, see, that's when it was returns because there is no module named Matt
blot the Lib, so it means we need to install this package.
We can click through, you know, here and let's type B install Matt Blot Lib.
This is a Python Colman's to install libraries.
Run this code.
We can see that the downloads process has already started.
After that, we can see that the package was installed.
Now we need to install by my --.
Well, let's type people install by my ask.
You'll run this code.
We can see the message successfully installed.
Now we can try to run this code again.
See, that's no air or was returned.
So it means that the libraries were installed correctly.
We will.
We use the same code we implemented as before.
So we'll just based the code.
Here it is this same.
We implemented step by step in Google CoLab.
Glass products, glass in the video glass genetic algorithm.
And now we need to generate the list of products because we are not going to use up by phone list as
we did before.
The idea is to connect to the database in order to do that.
We can create a main function here in by Shaam.
If Maine equals Maine, we can see here these arul.
It means that every time we run these codes, the flow of the program will start executing all the codes,
which is inside this block.
We can create our products list and an empty list, and now we can connect to our database.
So I will create this new variable connection.
Let's call the new library dots connect and we need to specify so parameters.
The first one host local hosts.
It means that we are connecting to our local machine.
If you are using another database which starts in the internet, you can just put the IP number, the
next parameter user ID.
The phone user in this database is roads and the order parameter.
The passwords.
Now you'll need to specify here that passwords you choose when you install the library.
In this case, here I use this best word here.
One two three four, five, six, seven and eight and the last parameter.
The database, which defines the name as products.
As we can see here in work bank, let's create another variable cursor, equal connection dots cursor.
It means that we are connecting to the database.
Now we can execute some queries.
Cursor.
But is it cute and we can type ASCII L code?
For example, this code here just selects all the columns so we can type here the name of the columns.
For example, we want to get product name space price quantity from products.
We can just copy these codes and go back to the workbench.
When we run this code, we will select all Carlos products, the names, base price and quantity.
We can type that code here, select and run.
See that we are specifying only the columns we want to get product name, space, price and quantity.
Now we can go through these results for products.
This is a new variable and great email in cursor, and let's pourrions the product.
Now we can run this code, see that we are going through each one of the rows of our data sets.
We are going through these stable here.
We can see that we have positioned zero position one and position two.
If we type here, position zero, we will get only the names.
We can.
Also type products was the position number one.
We can see this basis and in position number two, we will see the prices of the products now that we
have the information we need.
We can access products list, but up bands.
We will create a new object product and let's sans the parameters.
The first one, as we can see, is the name of the product.
This second one, this base was issue one and the third one, the price was issue number.
Sure, we can put these quotes in comments, and after executing the aquaria in the database, we can
close the cursor.
And also we can close the connection to release the memory.
After that, we can print products lists.
Let's run these codes and we can see that we have one object in each one of the positions.
We can also implement a for loop just to see the results for product in product list for its product.
Let's run this code.
We can see the objects and just see the names we can just type product that name.
Run this code and we have the names of each one of the products.
But there's something additional in this code, which is the quantity column.
For example, there are five cell phones on the start and that are two TV 55.
So we need to create five cell phone objects and to be these 55 objects.
And regarding order products, we can see that that are then ventilators.
So we need to create than objects regarding this product should do that is quite simple.
We can just implement another far loop here.
For I in range product imposition number three, we can see that position zero is the name World Space
Shoe is the price and position three is the quantity and odds each one of the products in the list.
We can just run this code again, and now it's easy to see the results.
There is only one refrigerator a.
And there are five cell phones.
We can see that there are three 550 for the VFR de Q and so one we can also bring two here product list.
Let's check the length.
Run this code.
See that before there were 14 products and now that are 47 products.
Basically, this is the only change we need to perform to run the genetic algorithm connected to my
scale.
We just need to access the product stable.
Now we can put all these screens in commands and then we'll just base this same code we had implemented
before.
We can see here that we are creating this basis prices and names variables.
We are going through each one of the products for these seven products, then limits population size,
mutation, number of four generations.
We are creating here the genetic algorithm.
And after that, we are calling this solve methods.
We can run this set of codes.
And now we can see the results, for example, in generation number zero.
The total prize was won.
It means that this space occupied by all the products exceeded the maximum capacity of the truck as
we saw earlier, and the chromosome is different that are more products are here because before there
were only 14 products and now that are four, these seven products because we are considering the quantity
we can see here that we have bad results.
Total price is equal to one for all generations, and it may have been because now there are more products
and we have the limits only three.
So we need to increase the size of the limits to work with more products.
Let's change should then run this code again.
And now we can take a look at this solution.
For example, generation zero four thousands.
And this base is equal to zero point zero nine in generation one.
The total prize equals one because as we can see here than point six, the six is greater than the capacity
of the truck.
We can see all the results considering all the generations.
And finally, we can see that the best solution was found one generation for this seven, and the total
price is fifty five thousand.
And this space equals 9.10.
And here we can see the products that we are going to load on the truck.
Something very interesting about this new implementation, considering the quantity of the products,
is that there are five cell phones and all of them are going to be loaded on the truck.
On the order of hands that are banned, ventilators and only five of them were loaded.
And it happens because this space occupied by this cell phone is less then this space occupied by the
ventilator.
And the price of the ventilator is only one hundred and ninety nine, and the price of the cell phone
is two thousand one hundred and ninety nine.
So the genetic algorithm will adapt to this kind of situation.
It's very interesting because the genetic algorithm knows that when loading more cell phones, the profits
will be bad.
We can try to run this code again.
As I said before, a good way to test the algorithm is to run it for a couple of times and see the results.
See, that's now.
The best solution was found on generation number 59.
And the total price is a little lower than the last is a crucial.
And we can see that's only for cell phones were loaded on the truck.
And regarding the ventilator.
Only three of band were is on that trip.
And finally, we can see the graph with the results here.
So we'll put this quote here.
BLT Dot plots the healthy dots by, though we are using the matte blot label library to see the results.
Let's run again.
And then we can follow their results, considering each one of the generations something very similar.
We implemented using Google CoLab and we can see the results 50 Q, and then you can take a look at
the products that will be loaded on the truck for cell phones.
Now we are loading Refrigerator A..
And in this solution, we are going to load one two three four five six ventilators.
And just a reminder, if you are also fans of a company, you need to sand's.
This list of products should be manager of the company, and in the next section of the course, you
are going to learn how to work with genetic algorithm libraries.
See you there!