1
00:00:05,080 --> 00:00:08,020
Hi, welcome to this
fifth video on

2
00:00:08,020 --> 00:00:13,605
our wave function
collapse project series.

3
00:00:13,605 --> 00:00:15,340
We are at the point of starting

4
00:00:15,340 --> 00:00:16,900
to learn about propagation.

5
00:00:16,900 --> 00:00:18,970
What do we mean by propagation?

6
00:00:18,970 --> 00:00:20,880
We are at the point
where we have

7
00:00:20,880 --> 00:00:24,020
collapsed one cell at
random in our grid.

8
00:00:24,020 --> 00:00:27,060
This cell needs to look to
its adjacent neighbors.

9
00:00:27,060 --> 00:00:28,820
We have done
calculations such as

10
00:00:28,820 --> 00:00:32,270
this one with the Langton's Ant,

11
00:00:32,270 --> 00:00:34,555
I think that was in Course two.

12
00:00:34,555 --> 00:00:38,000
In many cases we've seen that
data structure of a grid

13
00:00:38,000 --> 00:00:39,880
lends itself to understand

14
00:00:39,880 --> 00:00:42,550
neighborhood calculations
in an easy fashion.

15
00:00:42,550 --> 00:00:44,620
Similarly, we're going to
be using some of that.

16
00:00:44,620 --> 00:00:45,320
We're going to write it

17
00:00:45,320 --> 00:00:46,660
slightly different
just because we're

18
00:00:46,660 --> 00:00:47,680
wrapping up and getting

19
00:00:47,680 --> 00:00:48,880
towards the very
end of the course,

20
00:00:48,880 --> 00:00:50,380
we're going to be
seeing different ways

21
00:00:50,380 --> 00:00:51,840
in which we can access

22
00:00:51,840 --> 00:00:53,820
neighborhood information
in this case using

23
00:00:53,820 --> 00:00:56,075
a dictionary and a tuple.

24
00:00:56,075 --> 00:00:57,860
Let's see the kind

25
00:00:57,860 --> 00:00:59,260
of structure that we're
going to be using.

26
00:00:59,260 --> 00:01:01,000
We're going to be
using a dictionary

27
00:01:01,000 --> 00:01:03,140
that will combine a direction.

28
00:01:03,140 --> 00:01:04,460
Direction is going to be, again,

29
00:01:04,460 --> 00:01:05,860
top, bottom, left, and right,

30
00:01:05,860 --> 00:01:08,600
which talks about the
adjacencies of the cell,

31
00:01:08,600 --> 00:01:11,170
and then we're going to be
coupling that with a tuple,

32
00:01:11,170 --> 00:01:13,990
which is going to be the

33
00:01:13,990 --> 00:01:17,565
adjacent coordinates
in relative space.

34
00:01:17,565 --> 00:01:21,690
That means that whatever
that cell is a -1.

35
00:01:21,690 --> 00:01:24,150
In y, it's going to
be our top neighbor.

36
00:01:24,150 --> 00:01:27,405
A +1 in y it's going to
be our bottom neighbor.

37
00:01:27,405 --> 00:01:30,960
A -1 in x, left, +1 in x, right.

38
00:01:30,960 --> 00:01:33,990
We're also going to have
to learn once we create

39
00:01:33,990 --> 00:01:37,860
this matrix of neighbors,

40
00:01:37,860 --> 00:01:40,865
how do we loop through
these possible neighbors?

41
00:01:40,865 --> 00:01:42,160
This is going to be
a very interesting

42
00:01:42,160 --> 00:01:43,560
structure that could be

43
00:01:43,560 --> 00:01:44,720
expanded to all sorts of

44
00:01:44,720 --> 00:01:47,260
different types of geometry
later down the line.

45
00:01:47,260 --> 00:01:48,820
But it's a very different way in

46
00:01:48,820 --> 00:01:50,140
which we're going to be looping

47
00:01:50,140 --> 00:01:53,970
through this neighbor
adjacencies.

48
00:01:53,970 --> 00:01:56,700
Let's write it and
see how it works.

49
00:01:56,700 --> 00:01:59,780
We are continuing
where we left off.

50
00:01:59,780 --> 00:02:03,590
I'm working within the second
tab called Environment.

51
00:02:03,590 --> 00:02:05,250
We just wrote in the last video

52
00:02:05,250 --> 00:02:08,890
the wave function collapse
beginnings of the algorithm.

53
00:02:08,890 --> 00:02:10,830
I invite you to
add documentation

54
00:02:10,830 --> 00:02:13,330
and comments to your own code.

55
00:02:13,330 --> 00:02:15,550
We also wrote the collapse cell,

56
00:02:15,550 --> 00:02:17,490
which is a rather
simple function

57
00:02:17,490 --> 00:02:21,525
that selects a random entity.

58
00:02:21,525 --> 00:02:25,350
From within here we are
going to propagate.

59
00:02:25,350 --> 00:02:28,290
Let's just call a function
we haven't written yet.

60
00:02:28,290 --> 00:02:34,310
We're going to call
propagate x and y.

61
00:02:34,360 --> 00:02:36,630
We want to make it within

62
00:02:36,630 --> 00:02:39,395
this function because we're
passing this information

63
00:02:39,395 --> 00:02:45,090
x and y to basically the
collapse cell will propagate.

64
00:02:45,090 --> 00:02:54,690
Let's define the propagate
function by self, x, y.

65
00:02:54,690 --> 00:02:58,590
What do we need here? I'm going

66
00:02:58,590 --> 00:03:01,010
to create some data
we'll definitely need,

67
00:03:01,010 --> 00:03:04,630
but we're going to stop
a bit short of using it.

68
00:03:04,630 --> 00:03:08,650
What would be the
collapsed tile?

69
00:03:08,650 --> 00:03:17,860
The collapse tile we know
is the self.cells [x] [y].

70
00:03:18,860 --> 00:03:22,220
Because we're working
within the collapse tile,

71
00:03:22,220 --> 00:03:24,940
we can select the first
entry of that list,

72
00:03:24,940 --> 00:03:27,900
which is going to be the
only entry available.

73
00:03:27,900 --> 00:03:30,260
That's the collapse tile.

74
00:03:30,260 --> 00:03:32,860
That's going to be useful
for us in a minute.

75
00:03:32,860 --> 00:03:36,490
Let's define the possible
neighbors of that tile.

76
00:03:36,650 --> 00:03:41,455
Neighbors. As we discussed
in the intro slides,

77
00:03:41,455 --> 00:03:44,020
we are going to be
doing a dictionary,

78
00:03:44,020 --> 00:03:49,440
where we're going to be using
a direction in the form of

79
00:03:49,440 --> 00:03:55,855
a string that would be
coupled with a tuple x, y.

80
00:03:55,855 --> 00:03:58,015
We could do this,

81
00:03:58,015 --> 00:04:02,480
x, y, like that first.

82
00:04:02,480 --> 00:04:05,550
Repeat this four times.

83
00:04:05,550 --> 00:04:08,880
Now we can say bottom,

84
00:04:08,880 --> 00:04:11,730
left, and right.

85
00:04:11,730 --> 00:04:14,970
Now we can actually identify
what are the coordinates.

86
00:04:14,970 --> 00:04:16,470
Top, as we discussed,

87
00:04:16,470 --> 00:04:18,390
is the y - 1.

88
00:04:18,390 --> 00:04:23,415
The bottom would be +1,
and left and right.

89
00:04:23,415 --> 00:04:26,895
Left is -1 in x,

90
00:04:26,895 --> 00:04:33,110
+1 in x for the right
neighbor. That's so good.

91
00:04:33,110 --> 00:04:34,870
What we haven't done yet in

92
00:04:34,870 --> 00:04:36,990
this course is how
do we loop through

93
00:04:36,990 --> 00:04:39,410
this dictionary and
how do we get access

94
00:04:39,410 --> 00:04:42,050
to both the direction
and the tuple.

95
00:04:42,050 --> 00:04:43,910
Let's do a four where

96
00:04:43,910 --> 00:04:45,630
we use something
we're going to call

97
00:04:45,630 --> 00:04:53,270
this temporary variable
direction, nx, ny.

98
00:04:53,270 --> 00:04:58,130
What we're doing here
is giving a placeholder

99
00:04:58,130 --> 00:05:03,530
name to the direction.
Basically the tuple nx is going

100
00:05:03,530 --> 00:05:08,140
to be neighbor x and
neighbor y in neighbors.

101
00:05:08,140 --> 00:05:11,810
Just want to make sure that
we spell it correctly,

102
00:05:11,810 --> 00:05:15,670
neighbors.items. This is a
function that will allow us to

103
00:05:15,670 --> 00:05:19,580
iterate over a dictionary.

104
00:05:20,250 --> 00:05:22,965
We know that there is

105
00:05:22,965 --> 00:05:25,930
a case on the edges

106
00:05:25,930 --> 00:05:28,915
in which we do not
want to be checking.

107
00:05:28,915 --> 00:05:32,425
We are going to be
writing one function.

108
00:05:32,425 --> 00:05:37,470
If 0 is smaller or equal than

109
00:05:37,470 --> 00:05:45,000
nx and smaller than
self.columns and,

110
00:05:45,000 --> 00:05:47,575
this is basically ruling out

111
00:05:47,575 --> 00:05:52,620
the cases in which nx or ny,

112
00:05:52,620 --> 00:05:54,900
the neighbors in relation to

113
00:05:54,900 --> 00:06:00,640
the cell wouldn't actually work.

114
00:06:05,420 --> 00:06:08,115
We're dealing with
the edge cases.

115
00:06:08,115 --> 00:06:10,740
Let's just double check
that that's correct.

116
00:06:10,740 --> 00:06:14,100
Smaller than columns
and smaller than rows,

117
00:06:14,100 --> 00:06:18,990
so it has to be within zero
and the size of our grid.

118
00:06:18,990 --> 00:06:23,430
Right now I'm just going to
type something like x = 1.

119
00:06:23,430 --> 00:06:25,960
This wouldn't run if
we don't really have,

120
00:06:25,960 --> 00:06:28,560
but the calculation that
happens at this point,

121
00:06:28,560 --> 00:06:31,045
we're in fact already
achieving the propagation.

122
00:06:31,045 --> 00:06:34,180
We're evaluating the cell,

123
00:06:34,180 --> 00:06:35,940
we're defining the neighbors,

124
00:06:35,940 --> 00:06:37,960
and we are going to be checking

125
00:06:37,960 --> 00:06:42,350
the adjacent neighbors
of the cell.

126
00:06:42,350 --> 00:06:44,920
Let's just double check
that we're not running

127
00:06:44,920 --> 00:06:47,630
into any errors. That's good.

128
00:06:47,630 --> 00:06:50,760
But the calculation
that would happen here,

129
00:06:50,760 --> 00:06:53,100
let's just write it as
a comment before now,

130
00:06:53,100 --> 00:06:54,830
because we're going to see
this in the next video.

131
00:06:54,830 --> 00:06:58,630
So update the neighbors.

132
00:06:59,510 --> 00:07:03,070
The way we update the
neighbors will be

133
00:07:03,070 --> 00:07:06,170
done through evaluating
the neighbors and saying,

134
00:07:06,170 --> 00:07:08,850
am I compatible, is
this like let's kill

135
00:07:08,850 --> 00:07:12,840
all the non-compatible
options of that neighbor.

136
00:07:12,840 --> 00:07:15,010
We have to start running

137
00:07:15,010 --> 00:07:16,630
the compatibility test between

138
00:07:16,630 --> 00:07:19,425
the collapse cell and
the neighbor cell.

139
00:07:19,425 --> 00:07:22,110
Within this loop we're
already propagating,

140
00:07:22,110 --> 00:07:23,530
we're already in a loop

141
00:07:23,530 --> 00:07:26,090
that's dealing with the
adjacent neighbors,

142
00:07:26,090 --> 00:07:27,410
and we're going to be writing

143
00:07:27,410 --> 00:07:28,990
more logic to understand how do

144
00:07:28,990 --> 00:07:32,530
we evaluate this compatibility
test between tiles.

145
00:07:32,530 --> 00:07:35,350
We're going to be seeing
that in the next video.

146
00:07:35,350 --> 00:07:38,330
We'll leave this one
here. I'll see you then.