1
00:00:05,816 --> 00:00:13,174
Hi, welcome to this new and final video of
this week's code regarding the ecosystem.

2
00:00:13,174 --> 00:00:16,352
So, in this video,
we are going to be calibrating, but

3
00:00:16,352 --> 00:00:20,606
also visualizing some of the behaviors
within the ecosystem, right?

4
00:00:20,606 --> 00:00:23,598
We have our herbivore
that has a decision tree.

5
00:00:23,598 --> 00:00:28,439
And sometimes debugging or visualizing
what is this behavior is actually

6
00:00:28,439 --> 00:00:33,670
doing might be incredibly useful to
kind of calibrating the system further.

7
00:00:33,670 --> 00:00:36,937
And the calibration really talks
about the relationships and

8
00:00:36,937 --> 00:00:41,017
what is the kind of the way in which
we're kind of establishing a relationship

9
00:00:41,017 --> 00:00:43,134
between the different classes.

10
00:00:43,134 --> 00:00:46,435
The rate of growth, the rate of decay, and

11
00:00:46,435 --> 00:00:50,462
the rate of how entries come in or
out of the system.

12
00:00:50,462 --> 00:00:54,974
Or perhaps calibrating the expression of
what are we trying to really achieve.

13
00:00:54,974 --> 00:00:58,665
So we're going to be working with this
kind of graphic interface that we're

14
00:00:58,665 --> 00:01:00,446
going to put on top of our herbivore.

15
00:01:00,446 --> 00:01:03,220
It's going to let us see the starvation
level, the resting level,

16
00:01:03,220 --> 00:01:04,126
the hunger level.

17
00:01:04,126 --> 00:01:08,121
So everything that you see in the screen
right now is something that we can

18
00:01:08,121 --> 00:01:13,086
actually build as a dynamic visualization
of the data inside the herbivore's brain.

19
00:01:13,086 --> 00:01:15,078
So let's jump into the code and do that.

20
00:01:15,078 --> 00:01:18,198
Okay, so we are here in processing.

21
00:01:18,198 --> 00:01:22,024
We are continuing working on the code
that we have been working for

22
00:01:22,024 --> 00:01:24,342
the past several lessons this week.

23
00:01:24,342 --> 00:01:26,902
So let's have a quick
recap of where we're at.

24
00:01:26,902 --> 00:01:32,228
We have the population of herbivores
having their own kind of decision tree

25
00:01:32,228 --> 00:01:38,614
sequence in seeking food, then resting,
then going back to wandering, right?

26
00:01:38,614 --> 00:01:43,150
Let's just start visualizing a little bit
the information that we have, especially

27
00:01:43,150 --> 00:01:47,434
this hunger level, resting level, and
starvation level or starvation value.

28
00:01:48,934 --> 00:01:52,870
What I would like to do,
we do have a function called self display.

29
00:01:52,870 --> 00:01:55,870
Let's just create another function
here which we haven't constructed yet.

30
00:01:55,870 --> 00:02:00,794
But I'm going to call it
first self.display_behavior.

31
00:02:05,934 --> 00:02:09,865
So our display behavior function,
I'm going to write it all the way down at

32
00:02:09,865 --> 00:02:14,334
the bottom where our display function
is just to have those two together.

33
00:02:14,334 --> 00:02:19,374
But I'm going to go down here to display.

34
00:02:19,374 --> 00:02:23,854
Let's just create an area here where
we can do our display behavior.

35
00:02:23,854 --> 00:02:27,875
And I like doing this as
a separate function to display,

36
00:02:27,875 --> 00:02:33,326
just because eventually I might want to
be able to turn this layer on and off.

37
00:02:33,326 --> 00:02:38,170
So think of it as like, I might always
want to be able to display the entities,

38
00:02:38,170 --> 00:02:39,406
the herbivores.

39
00:02:39,406 --> 00:02:44,143
But maybe the graphic overlay that shows
me the data might be something that I

40
00:02:44,143 --> 00:02:47,854
only turn on to kind of
visualize its behavior.

41
00:02:47,854 --> 00:02:48,830
So what do we want to do?

42
00:02:48,830 --> 00:02:51,054
Let's start with the hunger level, right?

43
00:02:51,054 --> 00:02:53,654
So let's type here hunger.

44
00:02:53,654 --> 00:02:59,174
And we're going to visualize
this using a push pop matrix.

45
00:02:59,174 --> 00:03:07,398
pushMatrix first,
popMatrix to kind of situate a rectangle.

46
00:03:07,398 --> 00:03:15,363
We're going to do a rectangle
that's going to be (0,

47
00:03:15,363 --> 00:03:19,621
0, 40, or maybe 30,

48
00:03:19,621 --> 00:03:23,894
6) something like that.

49
00:03:25,514 --> 00:03:30,762
And let's do fill white, right?

50
00:03:30,762 --> 00:03:35,906
So we have a white rectangle,
quite small, and we have to position it.

51
00:03:35,906 --> 00:03:41,167
So let's translate that rectangle to be

52
00:03:41,167 --> 00:03:46,581
in the position of the herbivore self.,

53
00:03:46,581 --> 00:03:51,083
in the position of the herbivore.

54
00:03:51,083 --> 00:03:52,509
Well, that's it, right?

55
00:03:52,509 --> 00:03:54,660
In the position of the herbivore.

56
00:03:54,660 --> 00:03:58,492
At this point, we should be
able to have a wide rectangle.

57
00:03:58,492 --> 00:03:59,664
Just place.

58
00:04:01,724 --> 00:04:03,372
We're having some error here.

59
00:04:03,372 --> 00:04:04,184
Let's see.

60
00:04:05,244 --> 00:04:06,784
That should be a coma.

61
00:04:08,524 --> 00:04:09,364
Is that it?

62
00:04:09,364 --> 00:04:13,584
Okay, so we have the herbivore and
we have a rectangle.

63
00:04:13,584 --> 00:04:17,056
But you see that this is not really
placed in the right location.

64
00:04:17,056 --> 00:04:18,685
So in the translate,

65
00:04:18,685 --> 00:04:23,432
let's just push it back in x half
of the size of the rectangle.

66
00:04:23,432 --> 00:04:27,144
So if it's 30 here, we just do a 15 here.

67
00:04:27,144 --> 00:04:30,400
And now let's push it
in y a little bit up.

68
00:04:30,400 --> 00:04:37,840
So an offset, I'm going to do minus,
I don't know, 15, maybe -20, right?

69
00:04:37,840 --> 00:04:39,044
Let's see what we get.

70
00:04:41,324 --> 00:04:45,584
So we have a wide rectangle that's
just on top of the herbivore, right?

71
00:04:46,804 --> 00:04:50,292
So now we could actually
do a second rectangle.

72
00:04:50,292 --> 00:04:51,860
Let's call it the background rectangle.

73
00:04:51,860 --> 00:04:57,450
The background, the white rectangle is
going to have the maximum amount, but

74
00:04:57,450 --> 00:05:03,127
we want to include a second rectangle
that's going to be a growing bar that's

75
00:05:03,127 --> 00:05:08,356
going to be dynamic, that is going to
represent the hunger level, right?

76
00:05:08,356 --> 00:05:12,574
So let's do that rectangle from (0, 0,

77
00:05:12,574 --> 00:05:17,590
with the size in the width
of the rectangle would be,

78
00:05:17,590 --> 00:05:24,052
let's create a variable called h for
hunger and 6 for the height.

79
00:05:24,052 --> 00:05:25,636
And that hunger value.

80
00:05:25,636 --> 00:05:26,948
It's something we have to construct.

81
00:05:26,948 --> 00:05:28,384
We don't have it yet, right?

82
00:05:30,884 --> 00:05:37,954
Let's also make it feel
the color would be blue, right?

83
00:05:39,174 --> 00:05:41,734
So let's do the hunger variable.

84
00:05:41,734 --> 00:05:44,934
The hunger variable will be a mapping,
right?

85
00:05:44,934 --> 00:05:49,762
We know that hunger,
it's a value that goes from zero to

86
00:05:49,762 --> 00:05:54,014
the maximum hunger threshold, right?

87
00:05:54,014 --> 00:05:59,232
Hunger variable will be
mapping the variable

88
00:05:59,232 --> 00:06:03,204
self.hunger_level, right?

89
00:06:03,204 --> 00:06:04,384
So that's the variable.

90
00:06:05,604 --> 00:06:09,284
Sorry about this,

91
00:06:09,284 --> 00:06:14,576
self.hunger_level that

92
00:06:14,576 --> 00:06:19,406
goes from 0 to the hunger

93
00:06:19,406 --> 00:06:23,700
threshold, right?

94
00:06:23,700 --> 00:06:27,044
That's the range which we know
in which that variable moves.

95
00:06:27,044 --> 00:06:32,634
And the mapping will allow us to convert
this variable to a new range from zero to.

96
00:06:33,934 --> 00:06:37,107
In this case,
we want it to be between zero and

97
00:06:37,107 --> 00:06:41,414
30 because that's how big
the rectangle will be.

98
00:06:41,414 --> 00:06:44,422
We want it to be between 0 and
30 somewhere within that domain.

99
00:06:44,422 --> 00:06:49,288
So we're kind of converting that variable
that has its own domain to a variable that

100
00:06:49,288 --> 00:06:51,874
has the domain that we
can use graphically.

101
00:06:53,094 --> 00:06:55,550
So let's see what we have.

102
00:06:55,550 --> 00:06:59,540
And we have now this kind of little
growing rectangle because that variable

103
00:06:59,540 --> 00:07:01,366
obviously changes over time.

104
00:07:01,366 --> 00:07:05,857
When it reaches its max,
it transitions to eating food, and

105
00:07:05,857 --> 00:07:09,710
we can visualize the behavior of hunger,
right?

106
00:07:09,710 --> 00:07:16,971
Let's just comment out or
create a no stroke call here so

107
00:07:16,971 --> 00:07:20,994
we can see it a little bit less.

108
00:07:22,594 --> 00:07:25,778
Maybe you do like the kind of
the wide rectangle around.

109
00:07:25,778 --> 00:07:29,698
I mean, it's a little bit
cleaner in my view, right?

110
00:07:29,698 --> 00:07:31,514
So there we go.

111
00:07:31,514 --> 00:07:37,914
And we can do a function where we repeat
these for the different levels, right?

112
00:07:37,914 --> 00:07:42,042
But I'm going to manually
kind of do it now for rest.

113
00:07:42,042 --> 00:07:45,374
So let's just do it for rest.

114
00:07:46,394 --> 00:07:52,894
Maybe resting here, we can copy-paste
this part of our function.

115
00:07:54,354 --> 00:07:55,986
So this exactly the same.

116
00:07:55,986 --> 00:07:59,234
Let's just maintain the position in x,
the same location.

117
00:07:59,234 --> 00:08:03,698
But further up,
we're going to put it 30 pixels up.

118
00:08:03,698 --> 00:08:07,734
So we have a second bar that
is on top of the first one.

119
00:08:08,874 --> 00:08:11,498
And this is the resting variable.

120
00:08:11,498 --> 00:08:13,426
So we're going to call it r.

121
00:08:13,426 --> 00:08:15,522
And you're going to use r here.

122
00:08:15,522 --> 00:08:21,304
And instead of saying hunger level,
let's just call it rest level.

123
00:08:24,484 --> 00:08:26,264
And then also rest.

124
00:08:28,404 --> 00:08:32,259
And if you call these variables
differently make sure that you use your

125
00:08:32,259 --> 00:08:33,420
own name, right?

126
00:08:33,420 --> 00:08:35,784
So rest level, rest threshold.

127
00:08:36,844 --> 00:08:42,944
And then the color, we might also want to
change that to maybe something like red.

128
00:08:46,803 --> 00:08:50,139
All right, so this is when
the herbivore is taking a nap.

129
00:08:50,139 --> 00:08:54,275
After eating,
we should be able to see this red bar,

130
00:08:54,275 --> 00:08:57,943
which is going to be
the second bar filling up.

131
00:08:59,443 --> 00:09:04,955
So let's see, for instance,
this guy here went to eat.

132
00:09:04,955 --> 00:09:07,451
You see the red bar activating,

133
00:09:07,451 --> 00:09:12,364
that bars completely transitions
back to wandering, right?

134
00:09:13,384 --> 00:09:17,344
Which is the time when
eventually they will get hungry.

135
00:09:17,344 --> 00:09:18,760
So we have resting.

136
00:09:18,760 --> 00:09:24,904
And let's do this once more here.

137
00:09:24,904 --> 00:09:27,484
We're going to do it for starvation.

138
00:09:29,424 --> 00:09:30,879
And this time around,

139
00:09:30,879 --> 00:09:35,688
we're going to push the offset of this
third bar a little bit further up.

140
00:09:35,688 --> 00:09:37,934
It will start getting a bit cluttered,
but still.

141
00:09:37,934 --> 00:09:41,617
Okay, so in this case,

142
00:09:41,617 --> 00:09:47,782
we're going to call a starvation value.

143
00:09:47,782 --> 00:09:49,678
I think we call this one differently,
right?

144
00:09:49,678 --> 00:09:50,838
Starvation value.

145
00:09:50,838 --> 00:09:55,451
And here we did call this one

146
00:09:55,451 --> 00:10:01,074
starvation threshold, right?

147
00:10:02,594 --> 00:10:05,114
And once more,
we're going to change the color of this.

148
00:10:05,114 --> 00:10:06,586
It's going to be our green.

149
00:10:06,586 --> 00:10:09,415
So starvation will be green,

150
00:10:09,415 --> 00:10:14,634
was going to be the topmost
bar of our creature, right?

151
00:10:14,634 --> 00:10:17,242
So let's just look at one of them.

152
00:10:17,242 --> 00:10:22,804
When they get hungry, you see,
if that green bar builds up and completes,

153
00:10:22,804 --> 00:10:27,754
they will start to death and die and
disappear out of the system.

154
00:10:27,754 --> 00:10:30,654
But one thing I can already
can tell that we have a bug,

155
00:10:30,654 --> 00:10:33,813
it's not a bug because it's
not breaking the system, but

156
00:10:33,813 --> 00:10:38,600
it's actually an intended behavior, which
is our starvation bar never goes down.

157
00:10:38,600 --> 00:10:42,655
So even if you actually eat the food,
the creature eats the food,

158
00:10:42,655 --> 00:10:44,128
the bar remains high.

159
00:10:44,128 --> 00:10:49,008
So there's an inevitable buildup of that
bar to the point in which that starvation

160
00:10:49,008 --> 00:10:51,040
level will kill the creature.

161
00:10:51,040 --> 00:10:55,528
So we're getting creatures not
recovering from that starvation state.

162
00:10:55,528 --> 00:10:56,600
So let's just fix that.

163
00:10:56,600 --> 00:10:59,148
And that's something that really
kind of it may be visible for

164
00:10:59,148 --> 00:11:02,154
us right now as we kind of
created this visualization.

165
00:11:02,154 --> 00:11:04,306
So where would we do that?

166
00:11:04,306 --> 00:11:06,426
We probably want the starvation value.

167
00:11:06,426 --> 00:11:09,234
We can copy-paste that variable.

168
00:11:09,234 --> 00:11:13,714
We have to think about
this when we eat the food.

169
00:11:13,714 --> 00:11:15,154
Let's find the eating food.

170
00:11:15,154 --> 00:11:18,314
So here, when we eat the food,
the hunger level goes down.

171
00:11:18,314 --> 00:11:21,905
But if you ate the food means that you
actually got your nutrients back, right?

172
00:11:21,905 --> 00:11:27,748
So we could say the starvation
value also goes back to o, right?

173
00:11:27,748 --> 00:11:30,114
So that you don't have
a cumulative starvation.

174
00:11:30,114 --> 00:11:33,301
If you keep eating your
food in the right time,

175
00:11:33,301 --> 00:11:38,012
within your threshold of time,
you might still survive, right?

176
00:11:38,012 --> 00:11:43,004
So let's see if this is actually
doing the change that we intended.

177
00:11:43,004 --> 00:11:47,389
So we have some creatures here
chasing food, and you see,

178
00:11:47,389 --> 00:11:51,556
now they ate the food,
starvation went down to zero.

179
00:11:51,556 --> 00:11:53,876
So creatures will be more resilient now.

180
00:11:53,876 --> 00:11:59,788
But it's only perhaps when there's
maybe a competition for resources,

181
00:11:59,788 --> 00:12:05,664
when creatures are not going to be
able to acquire that food on time.

182
00:12:05,664 --> 00:12:10,312
So now that we have all this visualization
in place, the final calibration,

183
00:12:10,312 --> 00:12:14,527
I try to keep the key parameters for
calibration all the way to the top so

184
00:12:14,527 --> 00:12:18,664
I don't really have to revisit
the functions themselves.

185
00:12:18,664 --> 00:12:22,304
The hunger level, the zeros, is not
something that we need to work with.

186
00:12:22,304 --> 00:12:25,968
But here, this is the threshold of hunger.

187
00:12:25,968 --> 00:12:32,056
Do we think that this is how resilient
orhow much time the creature

188
00:12:32,056 --> 00:12:39,138
would need to wander around before we
transitions to a seeking food state?

189
00:12:39,138 --> 00:12:41,626
The resting state is the state
at the time of the nap.

190
00:12:41,626 --> 00:12:43,714
Maybe this nap should be higher.

191
00:12:43,714 --> 00:12:47,234
And the starvation threshold,
it's like also how much time?

192
00:12:47,234 --> 00:12:52,307
I think currently it's pretty brutal,
meaning pretty tight on how

193
00:12:52,307 --> 00:12:57,914
quickly the animal can actually
stay around wandering for food.

194
00:12:57,914 --> 00:13:03,164
So again, some parameters that you
can actually play around with.

195
00:13:03,164 --> 00:13:06,620
We also have this kind of action duration.

196
00:13:06,620 --> 00:13:09,560
This is actually, you would have to
go into the function because I think

197
00:13:09,560 --> 00:13:11,364
we implemented some randomness.

198
00:13:11,364 --> 00:13:16,860
So the wonder state kind of transitions
between idle, moving, and rotating.

199
00:13:16,860 --> 00:13:22,845
And finally, the moving function
has different speed of movement for

200
00:13:22,845 --> 00:13:28,396
wonder, different speed of movement for
the sick food state.

201
00:13:28,396 --> 00:13:29,804
So those are the mapping.

202
00:13:29,804 --> 00:13:33,098
What are the key parameters
that controls the behavior or

203
00:13:33,098 --> 00:13:36,636
the expression of the behavior
is something to consider.

204
00:13:36,636 --> 00:13:39,699
So I'll invite you to kind of really tune

205
00:13:39,699 --> 00:13:44,684
those values to something that
actually makes sense to you.

206
00:13:44,684 --> 00:13:47,460
So we're going to leave
this simulation here.

207
00:13:47,460 --> 00:13:50,162
I think that we have a new calibration and

208
00:13:50,162 --> 00:13:55,044
visualization of these behaviors
in a very kind of clear fashion.

209
00:13:55,044 --> 00:13:59,455
And we're going to conclude this
week discussing where could this

210
00:13:59,455 --> 00:14:00,698
ecosystem grow?

211
00:14:00,698 --> 00:14:04,522
How could we do more with it because you
could imagine that there's a lot more

212
00:14:04,522 --> 00:14:07,290
variables and
more functions that we can add to it.

213
00:14:07,290 --> 00:14:09,214
So we'll have that
conversation in the next one.

214
00:14:09,214 --> 00:14:09,769
I'll see you then.