1
00:00:05,180 --> 00:00:08,800
Hi. Welcome to this new lesson.

2
00:00:08,800 --> 00:00:11,420
We're going to continue
working with our project,

3
00:00:11,420 --> 00:00:13,960
the Wave Function
Collapse project.

4
00:00:13,960 --> 00:00:16,300
We're in the middle of

5
00:00:16,300 --> 00:00:18,340
working on this
propagate function.

6
00:00:18,340 --> 00:00:21,560
At this point, what
we are meant to do is

7
00:00:21,560 --> 00:00:24,690
to evaluate if the cells are
compatible with each other.

8
00:00:24,690 --> 00:00:27,150
We're going to be talking
about neighbor compatibility.

9
00:00:27,150 --> 00:00:29,450
What do we mean by
neighbor compatibility?

10
00:00:29,450 --> 00:00:31,610
As you know, we have
been describing

11
00:00:31,610 --> 00:00:33,910
the compatibility of a cell with

12
00:00:33,910 --> 00:00:36,150
a dictionary that has the top,

13
00:00:36,150 --> 00:00:40,300
bottom, left, and
right keys basically,

14
00:00:40,300 --> 00:00:43,355
and we're using A and B to
define what is compatible.

15
00:00:43,355 --> 00:00:45,290
Let's look at it in
the case of when

16
00:00:45,290 --> 00:00:47,150
two cells are in
fact compatible,

17
00:00:47,150 --> 00:00:50,460
we're going to have a cell
on top of another one.

18
00:00:50,460 --> 00:00:52,575
If the bottom of a cell has

19
00:00:52,575 --> 00:00:54,030
the A level and

20
00:00:54,030 --> 00:00:56,285
the top of another cell
has also the A level,

21
00:00:56,285 --> 00:00:57,860
we'll consider that
to be compatible.

22
00:00:57,860 --> 00:00:59,990
We're going to do an operation
where we're going to

23
00:00:59,990 --> 00:01:02,550
do a check of true or
false, are those equal?

24
00:01:02,550 --> 00:01:05,760
If so, we have compatibility.

25
00:01:05,760 --> 00:01:07,810
If that is not the case.

26
00:01:07,810 --> 00:01:09,330
If you can see here, if

27
00:01:09,330 --> 00:01:12,150
the bottom is A and the
top of another cell is B,

28
00:01:12,150 --> 00:01:13,830
then it's in fact
not compatible.

29
00:01:13,830 --> 00:01:15,545
We're going to
just give a false.

30
00:01:15,545 --> 00:01:19,050
We are going to be writing
this function as follows.

31
00:01:19,050 --> 00:01:21,010
This is going to be
part of the tile.

32
00:01:21,010 --> 00:01:25,950
The tile will be able to
evaluate if basically a cell,

33
00:01:25,950 --> 00:01:28,260
it's in relation
to another cell.

34
00:01:28,260 --> 00:01:32,050
If the direction that is
being given is the top,

35
00:01:32,050 --> 00:01:33,650
bottom, left or right,

36
00:01:33,650 --> 00:01:35,510
and depending on
those conditions,

37
00:01:35,510 --> 00:01:38,950
we will be able to define

38
00:01:38,950 --> 00:01:42,585
true or false for a
compatibility test.

39
00:01:42,585 --> 00:01:44,710
Let's write this
function together and

40
00:01:44,710 --> 00:01:47,110
apply it to the
algorithm so that we can

41
00:01:47,110 --> 00:01:51,910
actually start reducing
the possibility space

42
00:01:51,910 --> 00:01:54,410
of the cell from
many to what it will

43
00:01:54,410 --> 00:01:56,920
become ultimately
a collapse cell.

44
00:01:56,920 --> 00:01:59,485
We're going to continue
working where we left off.

45
00:01:59,485 --> 00:02:01,380
We are here.

46
00:02:01,380 --> 00:02:03,160
We have our main tab,

47
00:02:03,160 --> 00:02:04,860
then we have our grid tab.

48
00:02:04,860 --> 00:02:07,160
Let's just expand
this a little bit,

49
00:02:07,160 --> 00:02:10,100
a bit more space.

50
00:02:10,100 --> 00:02:13,000
But first of all, I would
like to go to the tile class.

51
00:02:13,000 --> 00:02:15,980
Right now, we only have
a couple of functions.

52
00:02:15,980 --> 00:02:19,560
We have a way of displaying
the cell when it's collapsed,

53
00:02:19,560 --> 00:02:20,980
when it's not collapsed,

54
00:02:20,980 --> 00:02:24,315
and a way of defining
color depending

55
00:02:24,315 --> 00:02:27,785
on the type of edge,
so color by edge.

56
00:02:27,785 --> 00:02:29,900
Let's just define
another function here,

57
00:02:29,900 --> 00:02:35,530
which we're going to
call is_compatible.

58
00:02:37,550 --> 00:02:40,470
We're going to use self,

59
00:02:40,470 --> 00:02:44,110
other, and direction.

60
00:02:44,700 --> 00:02:47,720
Direction is going
to be a string

61
00:02:47,720 --> 00:02:51,640
and other is going to
refer to another cell.

62
00:02:51,640 --> 00:02:56,455
We're going to say if other is

63
00:02:56,455 --> 00:03:02,700
none, return true.

64
00:03:02,700 --> 00:03:04,230
This is like a safety measure.

65
00:03:04,230 --> 00:03:05,810
Let's just quickly return if

66
00:03:05,810 --> 00:03:08,210
that other for some
reason is empty,

67
00:03:08,210 --> 00:03:11,560
we're just going to say
true at this point.

68
00:03:11,560 --> 00:03:13,350
That's just to make sure

69
00:03:13,350 --> 00:03:15,890
that we're not
running into a bug.

70
00:03:15,890 --> 00:03:19,585
But let's just actually
write the directions first.

71
00:03:19,585 --> 00:03:24,700
If direction equals top,

72
00:03:24,950 --> 00:03:28,535
we're going to return
true or false here.

73
00:03:28,535 --> 00:03:33,340
Let's return an evaluation
of adjacencies.

74
00:03:33,340 --> 00:03:41,780
We will return the current
cell, so self.edges.

75
00:03:41,780 --> 00:03:44,260
Here, because this
is a dictionary,

76
00:03:44,260 --> 00:03:46,760
we will select bottom.

77
00:03:48,500 --> 00:03:58,940
If our bottom edge is the same
then the other edges top,

78
00:04:00,260 --> 00:04:04,150
so if those are the same,
that's going to be true.

79
00:04:04,150 --> 00:04:05,455
If not, false.

80
00:04:05,455 --> 00:04:06,910
That's one of the conditions,

81
00:04:06,910 --> 00:04:11,425
that's if we are asking
for the top of a cell.

82
00:04:11,425 --> 00:04:14,900
We're going to be
doing this four times.

83
00:04:17,080 --> 00:04:20,825
We have direction top,

84
00:04:20,825 --> 00:04:23,375
bottom, left, and right.

85
00:04:23,375 --> 00:04:25,680
Let's write those one at a time.

86
00:04:25,680 --> 00:04:34,440
Bottom, left, and right,

87
00:04:35,390 --> 00:04:38,320
and then in the case of bottom,

88
00:04:38,320 --> 00:04:40,475
we need to make sure the top,

89
00:04:40,475 --> 00:04:45,245
and let's just write
that correctly,

90
00:04:45,245 --> 00:04:49,290
bottom like that. Bottom.

91
00:04:50,610 --> 00:04:54,235
We just basically
invert those two.

92
00:04:54,235 --> 00:05:01,640
In the left we have right
here and left here.

93
00:05:01,830 --> 00:05:04,690
Basically, what you're
doing is checking

94
00:05:04,690 --> 00:05:07,585
the opposite orientation
across cells.

95
00:05:07,585 --> 00:05:09,610
In case of the right,

96
00:05:09,610 --> 00:05:15,205
we will put the right
here and the left here.

97
00:05:15,205 --> 00:05:17,800
So this is a function and

98
00:05:17,800 --> 00:05:21,430
if none of those
conditions are true,

99
00:05:21,430 --> 00:05:29,710
we can say return false just
for good measure as well.

100
00:05:29,710 --> 00:05:31,825
So we have the
compatibility function

101
00:05:31,825 --> 00:05:34,480
written within our cell class.

102
00:05:34,480 --> 00:05:36,925
So this is what we were pending.

103
00:05:36,925 --> 00:05:38,845
If you remember from last video,

104
00:05:38,845 --> 00:05:41,020
we started writing the
propagate function

105
00:05:41,020 --> 00:05:44,230
here and we reached the point

106
00:05:44,230 --> 00:05:47,260
where we would expand

107
00:05:47,260 --> 00:05:51,550
a search from one cell to
its adjacent neighbors.

108
00:05:51,550 --> 00:05:53,980
What is the step
that happens after?

109
00:05:53,980 --> 00:05:55,510
The adjacent neighbors,

110
00:05:55,510 --> 00:05:57,220
we have to check compatibility.

111
00:05:57,220 --> 00:05:59,410
So we actually do
have a function now

112
00:05:59,410 --> 00:06:03,475
that would actually
allow us to do that.

113
00:06:03,475 --> 00:06:09,850
Let's just remove this
placeholder code here, x+1.

114
00:06:09,850 --> 00:06:16,580
Let's write
self.update_neighbors.

115
00:06:16,650 --> 00:06:19,930
This is a function that
we haven't written yet.

116
00:06:19,930 --> 00:06:22,750
By the way, we will pass
some information here

117
00:06:22,750 --> 00:06:25,465
which is going to
be the neighbor nx,

118
00:06:25,465 --> 00:06:27,865
the neighbor ny,

119
00:06:27,865 --> 00:06:36,170
and the collapsed_tile,
and the direction.

120
00:06:36,270 --> 00:06:39,055
All this information we do have.

121
00:06:39,055 --> 00:06:40,660
If you look up here, we have

122
00:06:40,660 --> 00:06:43,990
the collapsed_tile which is
the tile we're evaluating;

123
00:06:43,990 --> 00:06:45,910
basically the tile we'll be

124
00:06:45,910 --> 00:06:49,195
checking at the center
of the neighbor.

125
00:06:49,195 --> 00:06:50,830
Then we're looping
through the neighbor

126
00:06:50,830 --> 00:06:52,045
so we're going to be evaluating

127
00:06:52,045 --> 00:06:54,100
all the adjacent neighbors in

128
00:06:54,100 --> 00:06:56,470
this loop and with each
one of those neighbors,

129
00:06:56,470 --> 00:06:59,060
we're passing a direction.

130
00:06:59,790 --> 00:07:02,935
We're passing top
on the neighbor,

131
00:07:02,935 --> 00:07:04,210
bottom on the
neighbor, and so on.

132
00:07:04,210 --> 00:07:06,520
We're passing all the
information that is required for

133
00:07:06,520 --> 00:07:09,130
that to be handled by this
update neighbor function.

134
00:07:09,130 --> 00:07:11,425
So let's write that function

135
00:07:11,425 --> 00:07:15,400
together as well because we
don't have that function.

136
00:07:15,400 --> 00:07:20,210
So update_neighbors.

137
00:07:21,180 --> 00:07:25,375
We're going to take
self, the neighbor x,

138
00:07:25,375 --> 00:07:26,755
the neighbor y;

139
00:07:26,755 --> 00:07:28,840
these are going to be basically

140
00:07:28,840 --> 00:07:31,490
the same requirements
that we provided.

141
00:07:32,370 --> 00:07:36,215
Actually I think we
actually wrote it.

142
00:07:36,215 --> 00:07:39,520
Let's just spell that correctly.

143
00:07:39,520 --> 00:07:44,860
I think I was spelling it
with a single l by mistake.

144
00:07:44,860 --> 00:07:47,050
We will need the collapse style

145
00:07:47,050 --> 00:07:48,655
as an argument and
the direction.

146
00:07:48,655 --> 00:07:51,130
So all of these
are the arguments

147
00:07:51,130 --> 00:07:53,140
required for this
function to work.

148
00:07:53,140 --> 00:07:55,990
So let's first of
all ask ourselves,

149
00:07:55,990 --> 00:07:57,715
what are we doing
in this function?

150
00:07:57,715 --> 00:07:59,980
What we're doing is

151
00:07:59,980 --> 00:08:02,575
checking one of the
neighbors and we're going to

152
00:08:02,575 --> 00:08:05,380
check if the tiles within

153
00:08:05,380 --> 00:08:06,850
that neighbor are in

154
00:08:06,850 --> 00:08:08,770
fact compatible with
the collapse cell.

155
00:08:08,770 --> 00:08:12,475
If they're not, we're going
to remove them from the list.

156
00:08:12,475 --> 00:08:13,690
So we're going to only end

157
00:08:13,690 --> 00:08:16,015
up with tiles that
are compatible.

158
00:08:16,015 --> 00:08:18,640
Let's say we have two
tiles that are not

159
00:08:18,640 --> 00:08:21,955
compatible and two tiles
that are in fact compatible,

160
00:08:21,955 --> 00:08:24,745
we're going to kill the ones
that are not compatible.

161
00:08:24,745 --> 00:08:26,920
We're reducing the
possibility space of

162
00:08:26,920 --> 00:08:28,900
that tile to in this case,

163
00:08:28,900 --> 00:08:31,285
four to two or four
to one, and so on.

164
00:08:31,285 --> 00:08:33,685
What are the possible tiles?
Let's start with that.

165
00:08:33,685 --> 00:08:40,370
On neighbors,
neighbor_possibilities.

166
00:08:41,160 --> 00:08:44,155
So this is the possibilities
of a neighbor.

167
00:08:44,155 --> 00:08:47,050
Will be calculated
with the self.cell

168
00:08:47,050 --> 00:08:51,160
of the neighbor
we're evaluating.

169
00:08:51,160 --> 00:08:53,845
Remember that the nx and ny

170
00:08:53,845 --> 00:08:57,770
stands for the index
of our neighbor.

171
00:08:58,170 --> 00:09:00,910
Before we would
usually use x and y.

172
00:09:00,910 --> 00:09:02,830
It's the cell that
we're evaluating,

173
00:09:02,830 --> 00:09:04,000
but with the nx,

174
00:09:04,000 --> 00:09:05,320
we're denoting that it's

175
00:09:05,320 --> 00:09:08,690
the neighbor of the
cell we're evaluating.

176
00:09:10,270 --> 00:09:14,045
Let's say at that point we
have access to that neighbor.

177
00:09:14,045 --> 00:09:15,890
That neighbor requires or has

178
00:09:15,890 --> 00:09:18,125
four possibilities. That's
great. We have that.

179
00:09:18,125 --> 00:09:21,515
If the length of
that possibility,

180
00:09:21,515 --> 00:09:25,475
if there's more than one,

181
00:09:25,475 --> 00:09:26,750
because if it's only one,

182
00:09:26,750 --> 00:09:28,490
it means that the
cells collapse and we

183
00:09:28,490 --> 00:09:30,560
already have the solution
in a way for that.

184
00:09:30,560 --> 00:09:33,305
If it's more than one,
let's do something.

185
00:09:33,305 --> 00:09:35,600
What do we want to do here?

186
00:09:35,600 --> 00:09:38,630
We want to loop through each
one of the possibilities.

187
00:09:38,630 --> 00:09:45,185
Let's just do four tile in
neighbor possibilities,

188
00:09:45,185 --> 00:09:48,650
and here I'm going to do the
same notation that we use

189
00:09:48,650 --> 00:09:52,550
before to make a copy

190
00:09:52,550 --> 00:09:56,330
of this list as opposed
to a reference to it.

191
00:09:56,330 --> 00:09:58,940
I just want to make sure that
because what we're going to

192
00:09:58,940 --> 00:10:01,565
be doing is removing
entities from this list,

193
00:10:01,565 --> 00:10:03,470
we want to make sure that
we're not looking through

194
00:10:03,470 --> 00:10:06,210
the list itself
but a copy of it.

195
00:10:07,360 --> 00:10:12,950
If, note, a tile.

196
00:10:12,950 --> 00:10:14,540
Here's where we're going
to use the function

197
00:10:14,540 --> 00:10:16,700
that we wrote in the tile.

198
00:10:16,700 --> 00:10:20,700
Let's copy the name
of That's compatible.

199
00:10:22,810 --> 00:10:25,955
Which are the arguments
that we need to provide?

200
00:10:25,955 --> 00:10:28,760
We need to provide
the collapsed tile,

201
00:10:28,760 --> 00:10:34,980
so that's the cell we're
evaluating and the direction.

202
00:10:35,800 --> 00:10:40,325
If you remember, this
function takes the direction.

203
00:10:40,325 --> 00:10:45,185
If it stop, we're basically
asking this tile,

204
00:10:45,185 --> 00:10:48,335
I'm going to pass you
an adjacent tile.

205
00:10:48,335 --> 00:10:52,265
Are you compatible in the top,

206
00:10:52,265 --> 00:10:54,035
on the bottom, on the
left, on the right?

207
00:10:54,035 --> 00:10:56,930
We're looping through the
different directions and

208
00:10:56,930 --> 00:10:58,070
finding if in fact

209
00:10:58,070 --> 00:11:00,290
those tiles are
compatible to this one.

210
00:11:00,290 --> 00:11:01,580
These are the two
tiles that are being

211
00:11:01,580 --> 00:11:03,935
compared through this function,

212
00:11:03,935 --> 00:11:06,455
tile and the collapse tile

213
00:11:06,455 --> 00:11:07,895
through the function
is compatible.

214
00:11:07,895 --> 00:11:10,190
This will return true or false.

215
00:11:10,190 --> 00:11:12,500
We're doing it as an if.

216
00:11:12,500 --> 00:11:18,815
Saying if it's not
compatible, what do we do?

217
00:11:18,815 --> 00:11:27,560
Well, from the possibilities
we remove the tile.

218
00:11:27,560 --> 00:11:30,620
We're saying check this
style one at a time.

219
00:11:30,620 --> 00:11:32,750
We're looping through all
of them. If this style

220
00:11:32,750 --> 00:11:35,420
is not compatible, remove it.

221
00:11:35,420 --> 00:11:41,930
We're reducing the possibility
space of that cell.

222
00:11:41,930 --> 00:11:44,435
That's it. I think
that's everything.

223
00:11:44,435 --> 00:11:46,340
We're basically making sure

224
00:11:46,340 --> 00:11:50,015
that every time we do
one loop of propagation,

225
00:11:50,015 --> 00:11:52,070
we check all the
neighbors and we kill

226
00:11:52,070 --> 00:11:55,550
the non compatible tiles.

227
00:11:55,550 --> 00:11:58,130
That would allow the system

228
00:11:58,130 --> 00:11:59,630
to be at a perfect state where

229
00:11:59,630 --> 00:12:01,505
we can move to a new cell

230
00:12:01,505 --> 00:12:03,890
and start repeating the process.

231
00:12:03,890 --> 00:12:07,490
But let's just see if
we wrote any errors.

232
00:12:07,490 --> 00:12:09,500
Yes, it seems like it.

233
00:12:09,500 --> 00:12:13,715
Let's just revise a
little bit of our code.

234
00:12:13,715 --> 00:12:16,760
Let's just make sure I
actually realized that

235
00:12:16,760 --> 00:12:19,775
there was a
misspelling of cells.

236
00:12:19,775 --> 00:12:22,490
I spell cell instead of cells,

237
00:12:22,490 --> 00:12:25,400
so make sure that
you correct that.

238
00:12:25,400 --> 00:12:29,300
I think that now we should
be able to have it running.

239
00:12:29,300 --> 00:12:33,935
This is what we have. We
have one tile collapsed,

240
00:12:33,935 --> 00:12:36,860
and now we have some neighbors

241
00:12:36,860 --> 00:12:39,095
adjacent to it, we
changing the number.

242
00:12:39,095 --> 00:12:42,305
This tile on the left has 2,

243
00:12:42,305 --> 00:12:45,480
a 2, a 2 and a 3.

244
00:12:46,240 --> 00:12:48,425
That's basically what we wanted.

245
00:12:48,425 --> 00:12:50,570
We wanted to be able to change

246
00:12:50,570 --> 00:12:54,620
the entropy of our
neighbors adjacent to it.

247
00:12:54,620 --> 00:12:58,580
The compatibility test
is in fact working,

248
00:12:58,580 --> 00:13:02,105
and we have done one
loop of the algorithm.

249
00:13:02,105 --> 00:13:03,920
At this point, we will move to

250
00:13:03,920 --> 00:13:07,940
select the lowest entropy and
repeat the process again.

251
00:13:07,940 --> 00:13:10,580
We're close from concluding just

252
00:13:10,580 --> 00:13:12,875
because once the
algorithm starts looping,

253
00:13:12,875 --> 00:13:15,725
it's going to resolve
the entire grid.

254
00:13:15,725 --> 00:13:19,085
We just need to do one
loop and we are good.

255
00:13:19,085 --> 00:13:20,420
We're going to
continue next video,

256
00:13:20,420 --> 00:13:21,470
we're going to see how to

257
00:13:21,470 --> 00:13:23,600
move to the lowest entropy point

258
00:13:23,600 --> 00:13:28,530
available within the cells.
See you in the next video.