1
00:00:00,590 --> 00:00:08,270
Hello and welcome to this lecture, where we are going to finally run the genetic algorithm and then

2
00:00:08,270 --> 00:00:13,210
we will put all the cores together in this part of the code, the.

3
00:00:13,850 --> 00:00:21,320
We just did all these step-by-step implementations to see the partial results.

4
00:00:21,530 --> 00:00:30,860
But now that you have understood all the functions and all the classes we implemented, we can implement

5
00:00:31,100 --> 00:00:39,560
a new function to boards, all the codes together and to run the final genetic algorithm.

6
00:00:39,980 --> 00:00:46,850
We will create another function again in the genetic algorithm class.

7
00:00:47,360 --> 00:00:52,730
Then they will be so and it will receive some parameters.

8
00:00:53,120 --> 00:01:05,030
The first one, the probability of applying mutation, the number of generations, which is related

9
00:01:05,030 --> 00:01:16,550
to this stopping Darian and like in space, is occupied by the products, the prices and also the limits

10
00:01:16,550 --> 00:01:17,510
of the drug.

11
00:01:17,810 --> 00:01:22,040
Now we need to implement all this flow here.

12
00:01:22,490 --> 00:01:33,110
The first part, as you already know, is to create the initial population so we can just grow our function,

13
00:01:33,500 --> 00:01:35,630
initialize population.

14
00:01:35,960 --> 00:01:45,260
Let's send as a parameter spaces, prices and limits these parameters you can see here.

15
00:01:45,740 --> 00:01:55,700
The next step of the algorithm is to evaluate the initial population, and in order to do that, we

16
00:01:55,700 --> 00:02:04,130
can implement a for loop for individual in cell thought population.

17
00:02:04,730 --> 00:02:07,960
And let's just go in there, though.

18
00:02:08,700 --> 00:02:17,930
But fitness, when we run these codes, each one of the individuals will have ice.

19
00:02:18,650 --> 00:02:31,520
Now we need to order the population self, but our population put the best solutions at the low positions

20
00:02:31,520 --> 00:02:39,440
of the list and then we can call our new function visualize generation.

21
00:02:39,800 --> 00:02:49,940
So we will get the best individual and threatens the values now that we are going to implement this

22
00:02:49,940 --> 00:02:51,500
stopping criterion.

23
00:02:51,950 --> 00:02:59,840
That, as I said before, is based on the number of generations you want to create.

24
00:03:00,200 --> 00:03:02,330
We have this parameter here.

25
00:03:02,570 --> 00:03:16,700
Number of generations so we can implement a for loop for generation and range number of generations.

26
00:03:17,000 --> 00:03:26,900
If you define 102 generations, we will run the genetic operators 100 times.

27
00:03:27,500 --> 00:03:31,470
This is the implementation of this thought being carried, Daryll.

28
00:03:31,940 --> 00:03:43,460
Now we need to select the parents to combine dams to generate that new generation applying crossover

29
00:03:43,460 --> 00:03:52,190
and the mutation to select the individuals we needs to stem the violence of the generation.

30
00:03:52,550 --> 00:03:57,890
So we are going to call the function some evaluations.

31
00:03:58,370 --> 00:04:08,780
Let's create the new population and at the least, and now we need to apply the genetic operators.

32
00:04:09,170 --> 00:04:22,790
We will implement a major for look for new individuals and range from zero to this size of the population.

33
00:04:23,180 --> 00:04:29,600
Population size and we will increments by Chu as we implement as before.

34
00:04:29,840 --> 00:04:40,400
For example, if we have 20 individuals, we need to any new individuals and not for the new individuals.

35
00:04:40,940 --> 00:04:43,570
Now we can apply this function.

36
00:04:43,610 --> 00:04:45,350
Select parents.

37
00:04:45,830 --> 00:04:48,210
Let's select parents.

38
00:04:48,320 --> 00:04:52,250
Number one equals self.

39
00:04:52,970 --> 00:04:55,190
Select parent.

40
00:04:55,850 --> 00:04:59,040
I will send this some parents.

41
00:04:59,060 --> 00:04:59,510
Sure.

42
00:05:00,090 --> 00:05:03,710
Equals self select parent.

43
00:05:04,200 --> 00:05:15,030
Again, we send this sum after selecting the parents, we will have a list of children so copulation

44
00:05:15,240 --> 00:05:25,080
parents one and let's apply the cross over functional self calculation.

45
00:05:25,650 --> 00:05:30,780
Parents to this code is quite similar to what we implement as before.

46
00:05:31,500 --> 00:05:42,390
Now we will define this new population lots up bands, children in position zero.

47
00:05:42,720 --> 00:05:45,420
Let's apply the mutation.

48
00:05:46,140 --> 00:05:56,010
We'll send here the probability new population, but that bands children boys issue one.

49
00:05:56,490 --> 00:06:07,380
Let's once again the mutation probability and after the far loop we need to discard the old population

50
00:06:08,010 --> 00:06:10,350
should do that is quite simple.

51
00:06:10,680 --> 00:06:17,880
We just axis copulation, which is these are terrible at the EU.

52
00:06:18,240 --> 00:06:28,740
The list of the first population, it will receive the new individuals that were generated, so it will

53
00:06:28,740 --> 00:06:33,360
be equal short list new population.

54
00:06:33,660 --> 00:06:39,690
This list is just to make sure to have the date that in the correct format.

55
00:06:40,200 --> 00:06:50,360
So now we have all these parts already implemented with select parents applied crossover mutation.

56
00:06:50,640 --> 00:07:00,750
We discard discarded the older population and now we need to evaluate the population in quite similar

57
00:07:00,750 --> 00:07:03,450
to the process we implement as before.

58
00:07:03,870 --> 00:07:12,870
So we are going to repeat the codes we implemented here, and it is important to emphasize that this

59
00:07:12,870 --> 00:07:15,870
is only the initial population.

60
00:07:16,140 --> 00:07:27,450
And inside this far loop, we are evaluating the new population that is being generated so we can implement

61
00:07:27,450 --> 00:07:33,720
the for loop for our individual in self thoughts population.

62
00:07:34,140 --> 00:07:35,190
Let's go.

63
00:07:35,430 --> 00:07:41,920
The fit in this function to get this score for each one of them.

64
00:07:42,660 --> 00:07:47,610
We can also visualise the new generation.

65
00:07:47,970 --> 00:08:01,200
We can get that passed in the low self population position zero and then let's go our function based

66
00:08:01,650 --> 00:08:05,100
individual standing as a barometer.

67
00:08:05,100 --> 00:08:06,240
The best one.

68
00:08:06,630 --> 00:08:17,190
Just to make sure that we are going to star in this attribute here, the best individual, all three,

69
00:08:17,400 --> 00:08:23,880
all the population and all the generations after the far loop.

70
00:08:24,060 --> 00:08:32,790
After we go through each one of the generations we can bring into the result, I will copy this code

71
00:08:32,790 --> 00:08:35,220
from the visualizes generation.

72
00:08:35,490 --> 00:08:45,840
And let's face it here, we just needs to make sure that the Bryans is outside the far loop.

73
00:08:46,140 --> 00:08:48,900
We can both hear the final message.

74
00:08:49,230 --> 00:08:53,610
I will put these symbols just to be easier to visualize.

75
00:08:53,940 --> 00:09:04,260
Best solution and the results of the best solution of all generations will be in this attribute here,

76
00:09:04,800 --> 00:09:07,110
so we will just change here.

77
00:09:07,410 --> 00:09:14,330
Self Thoughts Best solution score evaluation.

78
00:09:14,880 --> 00:09:15,960
This space.

79
00:09:16,380 --> 00:09:22,590
Self Thoughts Best solution using space.

80
00:09:23,040 --> 00:09:32,160
And finally, let us visualize the chromosome self based Social Thoughts chromosome.

81
00:09:32,310 --> 00:09:43,280
And finally, we can return the chromosome self docked best solution, but chromosome.

82
00:09:43,380 --> 00:09:52,110
After running the algorithm for a specified number of generations, we will return to the chromosome

83
00:09:52,110 --> 00:09:54,060
of the best solution.

84
00:09:54,510 --> 00:09:59,370
Let's recreate this function again and here at.

85
00:10:00,150 --> 00:10:09,390
After the initial tests, we will open a new Baxter cell holding all the coal together and just to be

86
00:10:09,390 --> 00:10:11,450
easier to understand.

87
00:10:11,680 --> 00:10:14,370
I will based our call here.

88
00:10:14,580 --> 00:10:23,480
We have the products list, the refrigerator, the cell phone and so one with the fine, the spaces.

89
00:10:23,490 --> 00:10:31,080
The price is the names, one list for spaces, one list for price is and one list for our names.

90
00:10:31,500 --> 00:10:42,300
We have the limits the population size, mutation probability and also number of generations equals

91
00:10:42,420 --> 00:10:43,650
100.

92
00:10:43,920 --> 00:10:51,900
You can make some tests using 50, 100 200 and see the results.

93
00:10:52,320 --> 00:10:53,760
Now that's great.

94
00:10:54,180 --> 00:11:03,930
The genetic algorithm and let's sans the only parameter, which is the population size, we will build

95
00:11:03,930 --> 00:11:15,960
a genetic algorithm composed of 20 solutions per generation or 20 individuals, population software

96
00:11:15,960 --> 00:11:17,070
and individuals.

97
00:11:17,250 --> 00:11:27,180
And the way we were only called 100 times, which means the number of generations we can get the results

98
00:11:27,690 --> 00:11:30,570
equals genetic algorithm.

99
00:11:30,930 --> 00:11:37,020
Let's call our new function solve and sense the parameters.

100
00:11:37,230 --> 00:11:43,130
The first one mutational probability number of generations.

101
00:11:43,380 --> 00:11:47,760
This space is the price and the limits.

102
00:11:48,210 --> 00:11:50,750
We can't print the results.

103
00:11:51,330 --> 00:11:53,460
Let's run these codes.

104
00:11:53,940 --> 00:11:57,330
There is one a horror in this part two here.

105
00:11:57,480 --> 00:12:01,410
Best equals self population size.

106
00:12:01,740 --> 00:12:06,900
Here we can specify only the population and not population size.

107
00:12:07,410 --> 00:12:17,130
See that we are getting the messages regarding mutation before and after so we can use that messages

108
00:12:17,130 --> 00:12:18,150
in comments.

109
00:12:18,510 --> 00:12:27,430
So let's go up to the individual glass and put both lines here in command.

110
00:12:28,050 --> 00:12:31,290
Creates the individual class again.

111
00:12:31,650 --> 00:12:36,510
Now we just need to correct the genetic algorithm class.

112
00:12:36,900 --> 00:12:42,840
This part here population creates the class again.

113
00:12:43,170 --> 00:12:51,780
Now let's move back to this part of code and run again, and we can see the results, for example,

114
00:12:52,050 --> 00:12:54,150
in generation zero.

115
00:12:54,480 --> 00:13:03,750
The total price was eighteen thousand and the user space is two point fifty eight.

116
00:13:04,080 --> 00:13:08,950
Here we can see information about the chromosome zero.

117
00:13:09,000 --> 00:13:17,850
If we are not going to load the product and when if we are going to load the product, this prints here

118
00:13:18,120 --> 00:13:19,420
are related.

119
00:13:19,470 --> 00:13:30,330
Should these function visualize the generation, we are visualizing the best of the 20 individuals in

120
00:13:30,330 --> 00:13:32,910
each one of the populations.

121
00:13:33,180 --> 00:13:34,050
See here.

122
00:13:34,050 --> 00:13:37,200
That's after running generation one.

123
00:13:37,590 --> 00:13:42,690
The total price is less than the first generation.

124
00:13:42,960 --> 00:13:52,770
Just a reminder that's generation zero is randomly initialized, and the all other generations use cross

125
00:13:52,770 --> 00:13:57,060
over and mutation to create the new population.

126
00:13:57,390 --> 00:14:00,180
See that in generation number one.

127
00:14:00,510 --> 00:14:05,910
This base used was only zero point eighty two.

128
00:14:06,240 --> 00:14:12,120
This is a very bad results so we can keep moving to all other generations.

129
00:14:12,480 --> 00:14:19,720
For example, Generational Florida one, the total price is twenty two thousand.

130
00:14:20,040 --> 00:14:26,370
And after some other generations, the values starts to be bad.

131
00:14:26,940 --> 00:14:36,900
For example, from seventy choo choo 79 80, the total price has a very good result.

132
00:14:37,260 --> 00:14:44,040
So you bands two finds bad results in the last two generations.

133
00:14:44,280 --> 00:14:54,570
But this is not always true because as we are working with random initialization, we can get the badder

134
00:14:54,570 --> 00:14:57,810
solution here as the first generation.

135
00:14:57,990 --> 00:14:58,890
But maybe.

136
00:14:59,190 --> 00:15:07,620
It will be hard to happen at the hands of the algorithm, we can see the last sprint here.

137
00:15:07,740 --> 00:15:14,430
The best solution was found on generation 100s.

138
00:15:14,730 --> 00:15:24,000
See, that's the total price twenty two thousand eight hundred and forty for this base, two point sixty

139
00:15:24,000 --> 00:15:24,510
eight.

140
00:15:24,750 --> 00:15:33,180
And the way it can take a look at it chromosome and every time you run these codes, you may get different

141
00:15:33,180 --> 00:15:34,020
results.

142
00:15:34,140 --> 00:15:35,440
Let's see here.

143
00:15:35,460 --> 00:15:47,260
The first generation 16000 and the final result here was also found on the last generation generation

144
00:15:47,280 --> 00:15:52,200
one hundred and a total value twenty four thousand.

145
00:15:52,500 --> 00:16:01,650
And there is a small typo in the schools because when you run this code, the best solution will also

146
00:16:01,650 --> 00:16:05,700
be sad to hear Generation 100.

147
00:16:06,030 --> 00:16:12,480
So we need to perform a small change in this last spring statement.

148
00:16:12,750 --> 00:16:22,440
See here that we are accessing population zero, which is the last population that was generated, so

149
00:16:22,440 --> 00:16:25,980
we will always have this same result here.

150
00:16:26,070 --> 00:16:32,670
So we need just change here based solution thought generation.

151
00:16:33,030 --> 00:16:36,810
Now we can recreate this function.

152
00:16:37,050 --> 00:16:42,780
We are accessing the attributes a year to get to the result.

153
00:16:42,900 --> 00:16:45,210
And now let's run it again.

154
00:16:45,540 --> 00:16:53,670
Now we can see that's the best solution was found on Generation 27.

155
00:16:54,060 --> 00:17:03,750
So it doesn't means that you will get always the best results in the last two generations, so you'll

156
00:17:03,750 --> 00:17:12,120
need to pass this code and see in what generation you are going to find the best results.

157
00:17:12,210 --> 00:17:14,460
Let's run it again now.

158
00:17:14,460 --> 00:17:23,850
The best solution was found on generation number 20 and in total value twenty four thousand.

159
00:17:23,910 --> 00:17:33,360
When working with four genetic algorithms, you need to run the algorithm a couple of times and see

160
00:17:33,360 --> 00:17:36,900
if you can get bad and bad results.

161
00:17:37,260 --> 00:17:40,620
Then you can choose this solution here.

162
00:17:40,950 --> 00:17:49,140
When you are satisfied with the results, let's suppose you are also turns off at Albany and you will

163
00:17:49,140 --> 00:17:53,970
give to the company just this final solution here.

164
00:17:54,330 --> 00:18:00,570
You will be responsible for running the algorithm and giving to the manager.

165
00:18:00,810 --> 00:18:08,160
The list of products that they show, the loads on the truck and you see bad results.

166
00:18:08,430 --> 00:18:17,400
We can implement an additional quota here for I in range length product list.

167
00:18:17,940 --> 00:18:21,300
We are going through each one of the products.

168
00:18:21,840 --> 00:18:26,490
If results position, I equals one.

169
00:18:26,670 --> 00:18:29,550
It means we are going to load the product.

170
00:18:29,640 --> 00:18:34,170
So let's sprint to hear the name of the product.

171
00:18:34,380 --> 00:18:37,570
Let's access products list.

172
00:18:37,890 --> 00:18:38,640
Was you sure?

173
00:18:38,640 --> 00:18:40,140
I thought.

174
00:18:40,470 --> 00:18:46,140
Name and we can also brand the price of the product.

175
00:18:46,380 --> 00:18:49,470
Let's access product list.

176
00:18:49,830 --> 00:18:51,900
I thought Price.

177
00:18:52,110 --> 00:18:55,710
Let's run this genetic algorithm again.

178
00:18:56,100 --> 00:19:04,110
We can see the values regarding each one of the generations, and the final results is here.

179
00:19:04,200 --> 00:19:12,600
For example, the total price is twenty four thousand and you are going to Sand's to the manager.

180
00:19:12,630 --> 00:19:23,850
This list of products the cell phones, TV, 55 TV, 50 notebook, microwave, refrigerator and so on.

181
00:19:24,000 --> 00:19:33,210
And we can see that's the algorithm is performing well since the ventilator has a very low price.

182
00:19:33,420 --> 00:19:42,990
If we compare to the other products and it occupies a large space on the truck, so the algorithm pans

183
00:19:42,990 --> 00:19:54,060
to not select the ventilator because our goal is to maximize this sales price and then the next lecture

184
00:19:54,300 --> 00:19:58,890
we are going to generate a graph to bed through visualizes is or.

185
00:19:59,010 --> 00:20:00,690
Suits, see you there.
