-
Notifications
You must be signed in to change notification settings - Fork 8
/
VRP_Animations.py
195 lines (150 loc) · 5.82 KB
/
VRP_Animations.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
from manim import *
import numpy as np
def generate_vrp_instance(n, frame_width, frame_height, seed=None):
# Set seed
if seed is not None:
np.random.seed(seed)
# Generate VRP instance
xc = (np.random.rand(n + 1) - 0.5) * frame_width
yc = (np.random.rand(n + 1) - 0.5) * frame_height
instance = np.zeros((n + 1, n + 1))
for ii in range(n + 1):
for jj in range(ii + 1, n + 1):
instance[ii, jj] = np.sqrt((xc[ii] - xc[jj]) ** 2 + (yc[ii] - yc[jj]) ** 2)
instance[jj, ii] = instance[ii, jj]
# Return output
return instance, xc, yc
class FQS(Scene):
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self.n = 5
self.k = 3
self.XC = [-2.3, -4, 1, 4.8, 1]
self.YC = [-0.4, 0.8, -1.5, 1.5, 2.3]
def construct(self):
#instance, xc, yc = generate_vrp_instance(self.n, self.camera.frame_width-1, self.camera.frame_height-1)
dots = [Dot([x, y, 0]) for x, y in zip(self.XC, self.YC)]
squares = [Square(0.3, color=BLUE).move_to(dots[0].get_center()) for i in range(self.k)]
lines = [Line(dots[i].get_center(), dots[i+1].get_center(), stroke_color=GREEN) for i in range(4)]
for dot in dots:
self.add(dot)
self.wait()
self.play(
Write(squares[0])
)
for i in range(3):
self.play(squares[0].animate.move_to(dots[i]))
self.cur_time_step(dots, 0, i, i)
self.show_distance(dots[2], dots[3])
self.wait()
def cur_time_step(self, nodes, vehicle_num, cur_node_num, time_step):
label_strings = []
labels = []
sigma = "\\sum_{j=1}^{N}{x_{" + str(vehicle_num) + ",j," + str(time_step) + "}="
summation_str = sigma
for j in range(self.n):
# Check whether x_ijk = 0 or 1
if cur_node_num == j:
activated = 1
label_color = BLUE
else:
activated = 0
label_color = GREEN
# Add label
label_str = "x_{"+str(vehicle_num)+","+str(j)+","+str(time_step)+"}"
label_strings.append(label_str)
summation_str += label_str
if j != self.n-1:
summation_str += "+"
label = MathTex(label_str, "=" + str(activated), substrings_to_isolate=[label_str])
label.scale(0.75)
label.next_to(nodes[j], direction = DOWN)
label.set_color(label_color)
labels.append(label)
summation_tex = MathTex(summation_str, substrings_to_isolate = label_strings)
summation_tex.to_edge(DOWN)
# PLAY COMMANDS
self.play(
Write(labels[cur_node_num][0])
)
self.play(
Write(labels[cur_node_num][1])
)
self.wait()
self.play(
AnimationGroup(
*[Write(labels[i][0]) for i in range(self.n) if i != cur_node_num],
lag_ratio = 0.25
)
)
self.play(
*[Write(labels[i][1]) for i in range(self.n) if i != cur_node_num]
)
self.wait(2)
self.play(
AnimationGroup(
*[TransformMatchingTex(labels[i], summation_tex) for i in range(self.n)],
lag_ratio = 0.25
)
)
self.wait()
self.play(
FadeOut(summation_tex)
)
def show_distance(self, initial_node, final_node):
brace = BraceBetweenPoints(initial_node.get_center(), final_node.get_center())
brace_tex = brace.get_tex("C_{i,j}=" + str(self.get_distance(initial_node, final_node)))
self.play(Write(brace))
self.play(Write(brace_tex))
def get_distance(self, initial_node, final_node):
x_diff = final_node.get_center()[0] - initial_node.get_center()[1]
y_diff = final_node.get_center()[1] - initial_node.get_center()[1]
return np.round(np.sqrt(x_diff**2 + y_diff**2), decimals=3)
class SPS(Scene):
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self.n = 5
self.k = 3
self.XC = [-2.3, -4, 1, 4.8, 1]
self.YC = [-0.4, 0.8, -1.5, 1.5, 2.3]
self.TSP = [0, 2, 4, 3, 1, 0]
def construct(self):
#instance, xc, yc = generate_vrp_instance(self.n, self.camera.frame_width-1, self.camera.frame_height-1)
nodes = [
LabeledDot(Tex(str(i), color=BLACK)).move_to([self.XC[i], self.YC[i], 0])
for i in range(len(self.XC))
]
arrows = [
Arrow(start = nodes[self.TSP[i]].get_center(), end = nodes[self.TSP[i+1]].get_center(), color=GOLD)
for i in range(len(self.TSP)-1)
]
"""
# Add updaters to arrows
for i in range(len(arrows)):
arrows[i].add_updater(
lambda m: m.put_start_and_end_on(
nodes[self.TSP[i]].get_center(), nodes[self.TSP[i+1]].get_center()
)
)
"""
# PLAY COMMANDS
for node in nodes:
self.add(node)
self.wait()
for arrow in arrows:
self.play(Write(arrow))
self.wait()
# Rearrange dots
client_group = VGroup(*[nodes[self.TSP[i]] for i in range(1, len(self.TSP)-1)])
self.play(
nodes[0].animate.to_edge(UP, buff=2).set_x(0),
client_group.animate.arrange(direction=RIGHT, buff = 2),
*[FadeOut(arrow) for arrow in arrows]
)
self.wait()
arrows = [
Arrow(start = nodes[self.TSP[i]].get_center(), end = nodes[self.TSP[i+1]].get_center(), color=GOLD)
for i in range(len(self.TSP)-1)
]
self.play(*[Write(arrow) for arrow in arrows])
self.wait()