1
00:00:05,040 --> 00:00:07,660
Hi, welcome to this new lesson.

2
00:00:07,660 --> 00:00:10,640
We're going to continue
and conclude, in a way,

3
00:00:10,640 --> 00:00:12,800
this second week of

4
00:00:12,800 --> 00:00:16,605
discourse by concluding the
Langton's Ant algorithm.

5
00:00:16,605 --> 00:00:19,220
We have already started so if
you haven't really started,

6
00:00:19,220 --> 00:00:21,720
this algorithm is something

7
00:00:21,720 --> 00:00:23,775
that we've been working
for three sessions now.

8
00:00:23,775 --> 00:00:25,815
This is going to be the
final fourth session,

9
00:00:25,815 --> 00:00:28,500
so please go back
and check some of

10
00:00:28,500 --> 00:00:30,020
those videos before in case you

11
00:00:30,020 --> 00:00:32,540
are joining us at this point.

12
00:00:32,540 --> 00:00:34,220
Let's dive into it.

13
00:00:34,220 --> 00:00:36,960
This is where we left
off from the last video.

14
00:00:36,960 --> 00:00:39,380
We have a walker.

15
00:00:39,380 --> 00:00:41,520
We have very simple behavior

16
00:00:41,520 --> 00:00:43,570
at the moment, which is driven.

17
00:00:43,570 --> 00:00:45,300
Let's find the place down

18
00:00:45,300 --> 00:00:49,560
here which is the
walker's movement.

19
00:00:49,560 --> 00:00:51,860
The walker movement is

20
00:00:51,860 --> 00:00:53,850
based on some functions
that we wrote,

21
00:00:53,850 --> 00:00:57,015
rotate clockwise, move forward,

22
00:00:57,015 --> 00:00:59,745
invoking that function twice.

23
00:00:59,745 --> 00:01:02,820
We are going to be
using these functions.

24
00:01:02,820 --> 00:01:05,635
We actually need to
create one more function

25
00:01:05,635 --> 00:01:08,230
that I'm going to
do again manually,

26
00:01:08,230 --> 00:01:10,750
is the counterclockwise
rotation.

27
00:01:10,750 --> 00:01:13,910
Perhaps you can explore ways

28
00:01:13,910 --> 00:01:16,850
of writing these functions
in a much more succinct way.

29
00:01:16,850 --> 00:01:19,750
I'm trying to make them very

30
00:01:19,750 --> 00:01:22,250
explicit so that we're
understanding what's going on.

31
00:01:22,250 --> 00:01:25,280
But if you think about the
counterclockwise function,

32
00:01:25,280 --> 00:01:29,520
it's very much the same as
the clockwise rotation.

33
00:01:29,520 --> 00:01:33,630
We're going to copy
this function here.

34
00:01:33,630 --> 00:01:36,580
Let's call it counterclockwise.

35
00:01:41,930 --> 00:01:45,855
Then we're go, counterclockwise
in 90 degrees.

36
00:01:45,855 --> 00:01:48,530
The counterclockwise
rotation will also

37
00:01:48,530 --> 00:01:51,705
affect the direction but
the other way around.

38
00:01:51,705 --> 00:01:55,485
If it's zero, it's going
to move towards the three.

39
00:01:55,485 --> 00:01:58,365
If it's one,

40
00:01:58,365 --> 00:02:02,115
it should move towards zero.

41
00:02:02,115 --> 00:02:06,405
If it's two, it should
be moved to one.

42
00:02:06,405 --> 00:02:10,140
If it's three, it
should go to two.

43
00:02:10,140 --> 00:02:13,530
We're just basically imagining

44
00:02:13,530 --> 00:02:20,055
the last rotation is three.

45
00:02:20,055 --> 00:02:22,915
If we're looking
towards the ride,

46
00:02:22,915 --> 00:02:25,870
facing up would be going
to the rotation 3.

47
00:02:25,870 --> 00:02:27,745
If we're facing down,

48
00:02:27,745 --> 00:02:29,515
we look to the right.

49
00:02:29,515 --> 00:02:36,210
If we're facing
left, we go down.

50
00:02:36,210 --> 00:02:38,890
If we're actually facing up,

51
00:02:38,890 --> 00:02:41,400
we end up facing left,

52
00:02:41,400 --> 00:02:43,910
so again, very straightforward.

53
00:02:43,910 --> 00:02:45,770
Maybe a long code here.

54
00:02:45,770 --> 00:02:48,070
I'm sure there are
much better ways

55
00:02:48,070 --> 00:02:49,710
that you can figure out
on how to write this,

56
00:02:49,710 --> 00:02:51,830
but we want to be
very explicit about

57
00:02:51,830 --> 00:02:53,270
some of these algorithms as we

58
00:02:53,270 --> 00:02:54,950
are learning how to write them.

59
00:02:54,950 --> 00:02:58,710
Now that we have the
rotate counterclockwise,

60
00:02:58,710 --> 00:03:01,790
let's just copy
paste this function.

61
00:03:03,660 --> 00:03:07,295
We can actually create a
sequence of behaviors.

62
00:03:07,295 --> 00:03:08,980
This is the area in
which we're actually

63
00:03:08,980 --> 00:03:11,045
drawing the walker movement.

64
00:03:11,045 --> 00:03:15,760
Let's do something like
rotate clockwise, 90 degrees,

65
00:03:15,760 --> 00:03:19,720
move forward, rotate
counterclockwise,

66
00:03:19,720 --> 00:03:21,700
and then move forward again.

67
00:03:21,700 --> 00:03:25,630
This should create
something like a diagonal.

68
00:03:26,040 --> 00:03:30,000
But I've realized that

69
00:03:30,000 --> 00:03:32,360
I think just checking some
of the code that we have,

70
00:03:32,360 --> 00:03:34,380
I feel like we made a
mistake somewhere here.

71
00:03:34,380 --> 00:03:38,835
Our boundary condition
has an error.

72
00:03:38,835 --> 00:03:42,540
We actually get to the
bottom of the screen.

73
00:03:42,540 --> 00:03:48,860
We will be requiring
the entity, the walker,

74
00:03:48,860 --> 00:03:52,600
to stay in a number
that doesn't exist

75
00:03:52,600 --> 00:03:54,280
within our data
structure and that's

76
00:03:54,280 --> 00:03:56,850
going to create a
null reference.

77
00:03:56,850 --> 00:03:59,380
We are going to say here

78
00:03:59,380 --> 00:04:02,480
resolution minus
one in both cases.

79
00:04:02,480 --> 00:04:03,920
This is something
that, if you look at

80
00:04:03,920 --> 00:04:08,110
the notebooks that we
prepared for the class,

81
00:04:08,110 --> 00:04:10,315
this is the way it's
supposed to be written.

82
00:04:10,315 --> 00:04:12,790
I think that we
forgot to add this.

83
00:04:12,790 --> 00:04:15,500
Basically, because
we start at zero,

84
00:04:15,500 --> 00:04:18,410
the actual final number for

85
00:04:18,410 --> 00:04:19,570
the resolution is not the

86
00:04:19,570 --> 00:04:21,220
50 or the 100 that
we actually have.

87
00:04:21,220 --> 00:04:23,620
It's going to be
the 49 or the 99.

88
00:04:23,620 --> 00:04:26,650
We want to add this
minus one as a way of

89
00:04:26,650 --> 00:04:30,170
accounting for starting at
zero and not starting at one.

90
00:04:30,170 --> 00:04:33,170
Let's see if this
actually works.

91
00:04:33,170 --> 00:04:35,270
We actually want the walker

92
00:04:35,270 --> 00:04:37,810
to rotate first in 90 degrees,

93
00:04:37,810 --> 00:04:39,650
move forward, rotate

94
00:04:39,650 --> 00:04:42,410
counterclockwise, and
then move forward again.

95
00:04:42,980 --> 00:04:46,000
We actually do get this behavior

96
00:04:46,000 --> 00:04:48,680
because the behavior
gets executed until

97
00:04:48,680 --> 00:04:50,820
here and then it starts picking

98
00:04:50,820 --> 00:04:53,600
up on the constrained
condition of the boundary,

99
00:04:53,600 --> 00:04:56,475
so we only get the
movement to the right.

100
00:04:56,475 --> 00:05:01,400
That's an interesting
also interaction

101
00:05:01,400 --> 00:05:02,900
between the two different pieces

102
00:05:02,900 --> 00:05:04,395
of behaviors that we have.

103
00:05:04,395 --> 00:05:08,295
On the one hand, we have
a sequence of movements.

104
00:05:08,295 --> 00:05:10,320
On the other hand,
we have a constraint

105
00:05:10,320 --> 00:05:13,510
for the boundary.

106
00:05:14,110 --> 00:05:16,400
This is a very quick example.

107
00:05:16,400 --> 00:05:18,060
This is not in fact
the Langton's Ant,

108
00:05:18,060 --> 00:05:24,310
but the Langton's Ant uses
these functions as movements.

109
00:05:24,470 --> 00:05:27,465
I'm going to comment
this out for us,

110
00:05:27,465 --> 00:05:29,840
it's just for reference.

111
00:05:29,840 --> 00:05:32,640
Feel free to just delete
them if you want.

112
00:05:32,640 --> 00:05:35,080
Let's write first some
comments of what do we

113
00:05:35,080 --> 00:05:38,860
want the Langton Ant to do.

114
00:05:39,350 --> 00:05:42,045
Sorry. Here we go.

115
00:05:42,045 --> 00:05:44,700
The Langton's Ant is
an algorithm that

116
00:05:44,700 --> 00:05:48,780
considers the cell information.

117
00:05:48,780 --> 00:05:51,750
If the cell
information is white,

118
00:05:51,750 --> 00:05:54,435
we are going to move clockwise.

119
00:05:54,435 --> 00:05:56,420
We're going to rotate clockwise.

120
00:05:56,420 --> 00:05:58,850
We're going to change the cell.

121
00:05:58,850 --> 00:06:00,905
We're going to flip the cell
which we are already doing.

122
00:06:00,905 --> 00:06:02,570
I think we can actually,

123
00:06:02,570 --> 00:06:03,890
this is the area of

124
00:06:03,890 --> 00:06:08,990
our code that currently
flips the cell.

125
00:06:08,990 --> 00:06:11,645
We're going to get rid
of that as well or,

126
00:06:11,645 --> 00:06:13,715
you know, change it
in some capacity.

127
00:06:13,715 --> 00:06:16,475
If it's white, we
rotate clockwise,

128
00:06:16,475 --> 00:06:19,730
we flip the cell and
then we move forward.

129
00:06:19,730 --> 00:06:21,455
If the cell is black,

130
00:06:21,455 --> 00:06:23,375
we're going to rotate
counterclockwise,

131
00:06:23,375 --> 00:06:26,465
we flip the cell as well,
and we move forward.

132
00:06:26,465 --> 00:06:29,825
The only thing that
really changes is

133
00:06:29,825 --> 00:06:34,355
the clockwise or
counter clockwise

134
00:06:34,355 --> 00:06:37,440
based on the cell condition.

135
00:06:39,790 --> 00:06:45,950
I'd really like to make this
algorithm quite explicit.

136
00:06:45,950 --> 00:06:50,645
Let's just write it
almost from scratch here.

137
00:06:50,645 --> 00:06:52,610
We're going to get rid of
the flipping of the cells,

138
00:06:52,610 --> 00:06:54,830
even though we're
going to rewrite this.

139
00:06:54,830 --> 00:06:59,880
Let's just make sure that
our index calculations.

140
00:07:00,970 --> 00:07:03,830
Let's just leave that
there. Let's write

141
00:07:03,830 --> 00:07:08,100
our agent movement down here.

142
00:07:15,010 --> 00:07:17,045
Let's write a comment here.

143
00:07:17,045 --> 00:07:22,950
If the cell is white, so
how do we check for that?

144
00:07:23,620 --> 00:07:26,180
That would be an if statement

145
00:07:26,180 --> 00:07:29,255
that considers that the cell,

146
00:07:29,255 --> 00:07:36,150
considering the index
is equals to 1.

147
00:07:37,180 --> 00:07:40,880
This is cell is white condition.

148
00:07:40,880 --> 00:07:52,385
We want to do the rotate
clockwise in 90 degrees.

149
00:07:52,385 --> 00:07:55,190
We also want to flip the cell.

150
00:07:55,190 --> 00:08:03,470
We're going to say cells
using the index value,

151
00:08:03,470 --> 00:08:06,860
which we learned how to use
a couple of sessions ago.

152
00:08:06,860 --> 00:08:08,690
That's going to become zero.

153
00:08:08,690 --> 00:08:13,290
This will convert the
cell into a black cell.

154
00:08:13,570 --> 00:08:16,280
Finally, we're going to say move

155
00:08:16,280 --> 00:08:25,565
forward considering
the variable direction

156
00:08:25,565 --> 00:08:28,715
that we have created.

157
00:08:28,715 --> 00:08:30,590
Those are the three
conditions that

158
00:08:30,590 --> 00:08:33,335
happens if I'm on
top of a white cell.

159
00:08:33,335 --> 00:08:37,030
The second condition, again,

160
00:08:37,030 --> 00:08:37,720
we're going to write it as

161
00:08:37,720 --> 00:08:39,130
an LF because we
don't want to be

162
00:08:39,130 --> 00:08:43,970
checking these two
conditions at the same time.

163
00:08:44,200 --> 00:08:48,755
Let's just maybe copy
the whole thing.

164
00:08:48,755 --> 00:08:53,130
It's quite similar, but
it's slightly different.

165
00:08:53,590 --> 00:09:00,870
If the cell in this
case is black,

166
00:09:05,680 --> 00:09:09,005
then we actually
change the cell.

167
00:09:09,005 --> 00:09:13,620
We are going to rotate
counter clockwise.

168
00:09:16,360 --> 00:09:19,940
We will switch the
cell from black

169
00:09:19,940 --> 00:09:23,670
to white and then we
will move forward.

170
00:09:24,970 --> 00:09:27,980
Let's add a comment here.

171
00:09:27,980 --> 00:09:31,410
If the cell is black.

172
00:09:33,070 --> 00:09:37,385
This is basically the
Langton's ant algorithm.

173
00:09:37,385 --> 00:09:38,990
It's taking into consideration

174
00:09:38,990 --> 00:09:40,430
a cell that has its direction.

175
00:09:40,430 --> 00:09:42,215
Let's see if it runs

176
00:09:42,215 --> 00:09:44,885
and then we can explain
it a bit further.

177
00:09:44,885 --> 00:09:48,000
I think we are.

178
00:09:50,500 --> 00:09:54,170
Here we go. As you can see

179
00:09:54,170 --> 00:09:57,410
the ant in this case is

180
00:09:57,410 --> 00:10:01,190
actually operating
within this grid.

181
00:10:01,190 --> 00:10:03,680
What is happening is

182
00:10:03,680 --> 00:10:05,870
that we're starting to
see a pattern emerge.

183
00:10:05,870 --> 00:10:07,790
There's a behavior if you leave

184
00:10:07,790 --> 00:10:09,800
this algorithm running for
quite some time and you can

185
00:10:09,800 --> 00:10:12,290
probably find in YouTube
different instances where

186
00:10:12,290 --> 00:10:15,320
people have made this algorithm
run for many, many hours.

187
00:10:15,320 --> 00:10:17,120
You'll see that there's

188
00:10:17,120 --> 00:10:19,715
an emergent pattern
that starts occurring.

189
00:10:19,715 --> 00:10:23,735
The sequence is quite
simple sequence,

190
00:10:23,735 --> 00:10:31,490
but it actually operates over
the outcome it's producing.

191
00:10:31,490 --> 00:10:34,760
There's a circularity
between what the algorithm

192
00:10:34,760 --> 00:10:38,540
is doing ultimately and how
is that used for information,

193
00:10:38,540 --> 00:10:43,205
for the behavior of the worker
for the next iteration.

194
00:10:43,205 --> 00:10:45,440
It's not absolutely random,

195
00:10:45,440 --> 00:10:48,020
it's a specific pattern that

196
00:10:48,020 --> 00:10:51,335
keeps coming back to
its own production.

197
00:10:51,335 --> 00:10:54,020
These are very powerful
techniques that we

198
00:10:54,020 --> 00:10:56,705
can actually start
using for design.

199
00:10:56,705 --> 00:10:58,550
You might like the
pattern graphically,

200
00:10:58,550 --> 00:10:59,930
but more than that,

201
00:10:59,930 --> 00:11:02,690
the idea of the emergence
of simple rules

202
00:11:02,690 --> 00:11:05,630
leading towards
larger behaviors is

203
00:11:05,630 --> 00:11:07,820
something that really is

204
00:11:07,820 --> 00:11:09,260
one of the great
attributes that you can

205
00:11:09,260 --> 00:11:13,850
explore using code as a
generative design approach.

206
00:11:13,850 --> 00:11:17,870
Yes, you can certainly use
the patterns graphically,

207
00:11:17,870 --> 00:11:19,835
but at the same time

208
00:11:19,835 --> 00:11:21,440
when you're starting
to learn how to code,

209
00:11:21,440 --> 00:11:23,960
you're starting to
understand how to

210
00:11:23,960 --> 00:11:27,695
basically design with code
perhaps in a different way.

211
00:11:27,695 --> 00:11:29,015
We're going to be continue

212
00:11:29,015 --> 00:11:32,540
exploring these ideas
throughout this course.

213
00:11:32,540 --> 00:11:34,430
We're going to be expanding on

214
00:11:34,430 --> 00:11:36,350
these notions in the next weeks,

215
00:11:36,350 --> 00:11:38,030
but with this algorithm,

216
00:11:38,030 --> 00:11:39,395
we are going to
conclude this week.

217
00:11:39,395 --> 00:11:42,000
I'll see you in
the next session.