1
00:00:00,420 --> 00:00:07,260
Hello and welcome to this lecture, where we are going to implement a different function to select the

2
00:00:07,260 --> 00:00:13,830
parents in this part here off the flow of the genetic algorithm.

3
00:00:14,970 --> 00:00:25,470
And we will simulate this rule methods where the idea is to is being, they will in order to select

4
00:00:25,470 --> 00:00:30,360
some of the individuals, as we saw in last lecture.

5
00:00:30,690 --> 00:00:36,840
We have a higher probability of selecting goods solutions.

6
00:00:37,080 --> 00:00:45,960
However, there is a small probability off select the not that good solutions, as we can see here.

7
00:00:45,960 --> 00:00:54,630
For example, Ace three moving back to our Google collab fail, where we implement a new function in

8
00:00:54,630 --> 00:00:57,690
our genetic algorithm class.

9
00:00:58,740 --> 00:01:10,440
Let's define the function that select variants and it will receive as a parameter self, which is the

10
00:01:10,440 --> 00:01:11,730
default parameter.

11
00:01:12,030 --> 00:01:16,050
And also this some evaluation.

12
00:01:16,860 --> 00:01:28,650
The results of these function we implement is before it will return this some of this class of all individuals

13
00:01:28,650 --> 00:01:29,880
in the population.

14
00:01:30,180 --> 00:01:36,870
First, we will implement that, this function and then we will perform some tests.

15
00:01:37,110 --> 00:01:44,250
So it will be easier to understand how we can simulate the roulette methods.

16
00:01:44,700 --> 00:01:53,220
First, we will create a variable called Barings, which will be equal to minus one.

17
00:01:53,700 --> 00:02:03,680
These variable that we are going through returns, as we can see here, we have a list of individuals

18
00:02:03,960 --> 00:02:06,900
that are parts of the population.

19
00:02:07,200 --> 00:02:18,990
So the idea is to select one of these individuals here to apply crossover mutation to generate the new

20
00:02:18,990 --> 00:02:20,010
generation.

21
00:02:20,370 --> 00:02:27,720
So we will select a specific index of this list in our implementation.

22
00:02:27,960 --> 00:02:39,660
We create is a population composed of 20 individuals, so we will return an index which will be associate

23
00:02:39,660 --> 00:02:41,340
this to the population.

24
00:02:41,880 --> 00:02:51,450
This variable parents will be initialized with this value, which means that we are not to return any

25
00:02:51,450 --> 00:02:57,900
solo show because we know that indexes in Python starts at zero.

26
00:02:58,140 --> 00:03:03,630
So we need to select a number from zero to 19.

27
00:03:03,990 --> 00:03:12,190
Considering our population composed of 20 individuals, we will update this variable.

28
00:03:12,420 --> 00:03:16,020
Then we will create a new variable.

29
00:03:16,440 --> 00:03:29,610
Random value equals random and we will multiply by some evaluation the parameter that we are going to

30
00:03:29,610 --> 00:03:33,180
send just to perform some tests.

31
00:03:33,540 --> 00:03:42,660
We can go down here see that this evaluation equals one hundred and ninety six.

32
00:03:43,050 --> 00:03:49,590
So we are selecting a random number in this range when we run this code.

33
00:03:49,860 --> 00:03:58,590
We will get and the number in the range from zero to one and we can multiply by this sum.

34
00:03:59,730 --> 00:04:06,560
We will get our value in this range every time we run this code.

35
00:04:06,570 --> 00:04:18,690
We Gad's value one hundred sixty six to seven six to one, one hundred and thirteen.

36
00:04:18,690 --> 00:04:25,080
And so one, we will use this value to simulate that roulette.

37
00:04:25,650 --> 00:04:33,330
It will be this simulation when we spill the will to get one of the solutions.

38
00:04:33,900 --> 00:04:45,300
Let's go back to our glass and we will create another variable sum equals zero and I equals zero.

39
00:04:45,630 --> 00:04:50,160
The variable y will be used to control the loop.

40
00:04:50,580 --> 00:05:00,480
We are going to use a while loop while I less than length self population.

41
00:05:01,020 --> 00:05:09,240
It will guarantee that we are going to run these codes based on this size of the population.

42
00:05:09,710 --> 00:05:13,500
Will we implement a model condition and.

43
00:05:14,010 --> 00:05:25,590
Some less than they own, don't value, it will be easier to understand this condition when we passed

44
00:05:25,590 --> 00:05:31,320
this gold in some minutes, then we will increment this variable.

45
00:05:31,800 --> 00:05:37,730
Some blurs the equal cell population position.

46
00:05:37,740 --> 00:05:41,400
I thought score evaluation.

47
00:05:41,700 --> 00:05:46,830
We are doing something very similar what we implemented here.

48
00:05:47,100 --> 00:05:49,500
We are just adding these values.

49
00:05:50,010 --> 00:05:55,260
Then we will increments the parents plus equal one.

50
00:05:55,590 --> 00:05:59,310
And also I B+ equal one.

51
00:05:59,790 --> 00:06:08,400
Just a reminder that in this variable, we will return the index that was just selects.

52
00:06:08,670 --> 00:06:16,560
Or in other words, we will extracts an individual off these list here.

53
00:06:17,250 --> 00:06:23,010
Then we can just return after the while loop parents.

54
00:06:23,550 --> 00:06:30,300
And to better understand these codes, we can put some prints brands.

55
00:06:30,990 --> 00:06:39,920
Let's put some marks here to be easier to see the value random value when we run these codes.

56
00:06:39,930 --> 00:06:48,870
The first step is to generate a random value between zero and this sum evaluation.

57
00:06:49,200 --> 00:06:56,910
So let's put here the random value and inside the way you look.

58
00:06:57,540 --> 00:07:04,800
Let's variants first variable I and then this sum.

59
00:07:05,490 --> 00:07:10,430
And let's put here this sum will run again.

60
00:07:10,440 --> 00:07:13,350
This code should create the glass.

61
00:07:13,830 --> 00:07:20,440
Let's move back to this part where we are creating the new population.

62
00:07:21,060 --> 00:07:24,060
We can see the results here.

63
00:07:24,540 --> 00:07:28,770
The best in the video is in position zero.

64
00:07:29,460 --> 00:07:35,520
Then we can just run these codes to get the best in the video.

65
00:07:36,180 --> 00:07:46,260
We can prints this sum of évaluations equals eighteen thousand and just see the results.

66
00:07:46,590 --> 00:07:49,030
Let's create parents.

67
00:07:49,680 --> 00:07:53,520
One equals genetic algorithm.

68
00:07:53,940 --> 00:08:03,540
Let's call our new function select parents and we'll send the here this some less visualize parents.

69
00:08:04,170 --> 00:08:04,710
One.

70
00:08:05,700 --> 00:08:07,050
Run this code!

71
00:08:07,560 --> 00:08:17,070
See, that's the random value that was generated was one hundred and sixty seven.

72
00:08:17,430 --> 00:08:25,200
So we are in if the random value is less than this sum.

73
00:08:25,440 --> 00:08:35,700
As we can see here, these numbers here are equivalents to this sum of each one of the solutions.

74
00:08:36,270 --> 00:08:48,120
Take a look here that we keep odd the the values and then when I equals four, then we can take a look

75
00:08:48,120 --> 00:08:48,570
here.

76
00:08:48,900 --> 00:08:49,560
That's this.

77
00:08:49,560 --> 00:08:58,050
Some of the evaluations is less than the random value that was selected.

78
00:08:58,350 --> 00:09:08,070
So we stop is like, you think the codes and the results of the function is 14, which is the value

79
00:09:08,070 --> 00:09:10,770
of parents one variable.

80
00:09:11,070 --> 00:09:22,520
We can take a look here that we are selecting these individual words score is six thousands.

81
00:09:22,920 --> 00:09:27,270
Take a look here that we have bad their solutions.

82
00:09:27,540 --> 00:09:30,240
Should the initial parts of the list.

83
00:09:30,600 --> 00:09:40,380
But as I say adds in the theoretical lecture, the idea is to apply a random function to simulate that

84
00:09:40,380 --> 00:09:40,780
rule.

85
00:09:40,810 --> 00:09:41,880
Its methods.

86
00:09:42,450 --> 00:09:48,600
So we are not going to select only the best individuals.

87
00:09:48,990 --> 00:09:59,070
There is a chance of select the disordered individuals here that are not so good as these other ones

88
00:09:59,070 --> 00:09:59,610
here.

89
00:09:59,940 --> 00:10:09,060
But it is important to create diversity in this genetic algorithm, and the probability of selecting

90
00:10:09,300 --> 00:10:13,700
these last individuals are less than.

91
00:10:13,850 --> 00:10:21,890
Selecting these or other individuals in the initial positions off the list, because as we already know

92
00:10:22,250 --> 00:10:31,580
in the initial positions, we can see the best individuals, we can try to run this code again.

93
00:10:32,600 --> 00:10:36,790
See, that's another number was selected.

94
00:10:37,160 --> 00:10:48,740
Now the random value is 77 and doing the while loop, we are adding the scores of the individuals in

95
00:10:48,740 --> 00:10:50,240
the first position.

96
00:10:50,570 --> 00:10:52,220
That's conditional.

97
00:10:52,220 --> 00:10:55,100
Employment is in the way Loop is through.

98
00:10:55,490 --> 00:11:02,210
So we select this individual number five and then we can take a look here.

99
00:11:02,600 --> 00:11:04,040
12 thousands.

100
00:11:04,310 --> 00:11:14,660
And every time you run this code, you will get a different individual and it is based on this random

101
00:11:14,660 --> 00:11:17,330
value that is generated.

102
00:11:17,780 --> 00:11:26,510
Let's create another variable variance two equals genetic algorithm.

103
00:11:26,810 --> 00:11:28,520
Select variants.

104
00:11:28,730 --> 00:11:30,740
I will send again this sum.

105
00:11:31,340 --> 00:11:35,510
Let's visualize the index that was selected.

106
00:11:36,080 --> 00:11:45,260
See, that's in the first is equation with selected individual number four, and the SEC one is a commercial

107
00:11:45,560 --> 00:11:49,400
with selected individual number 12.

108
00:11:49,760 --> 00:12:01,700
So we can use both individuals to apply crossover and mutation to generate the new generation.

109
00:12:02,240 --> 00:12:07,970
So now we have implemented this select variants function.

110
00:12:08,270 --> 00:12:18,380
We will also select two random variants and then we are going to apply the genetic operators.

111
00:12:18,680 --> 00:12:23,300
That's what we are going to implement in the next lectures.

112
00:12:23,700 --> 00:12:24,380
So there.
