-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvisualizer.py
More file actions
319 lines (278 loc) · 11.4 KB
/
visualizer.py
File metadata and controls
319 lines (278 loc) · 11.4 KB
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
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
import numpy as np
import random
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d.art3d import Poly3DCollection
import matplotlib.animation as animation
import pickle
# Constants
visualization_diameter = 3
g = np.array([0.0, 0.0, -9.81]) # Gravity
dt = 0.0001
k = 1000.0 # Spring constant
L0 = 1.0 # Rest length of the spring
damping = 0.75 # Damping constant
mu_s = 1.0 # Static friction coefficient
mu_k = 0.8 # Kinetic friction coefficient
half_L0 = L0/2
drop_height = 3.0
omega = 2*np.pi*2 # frequency of breathing
times_of_simulation = 10000
mutation_range_k = [1000, 1200]
mutation_range_b = [0.2, 0.3]
mutation_range_c = [0, 2*np.pi*0.1]
mass_mutation_probability = 0.5
spring_mutation_probability = 0.2
cross_over_removal_rate = 0.3
population_size = 2
generations = 2
new_mass_spring_num = 6
mass_to_mutate = 6
class Mass:
def __init__(self, p, v, m=0.1):
self.m = m
self.p = np.array(p)
self.v = np.array(v)
self.a = np.zeros(3,dtype=float)
self.f = np.zeros(3,dtype=float)
class Spring:
def __init__(self, L0, k, m1, m2):
self.L0 = L0
self.k = k
self.m1 = m1
self.m2 = m2
class Individual:
def __init__(self):
masses = [
Mass([-half_L0, -half_L0, -half_L0 + drop_height], [0.0, 0.0, 0.0]), # 0
Mass([half_L0, -half_L0, -half_L0 + drop_height], [0.0, 0.0, 0.0]), # 1
Mass([-half_L0, -half_L0, half_L0 + drop_height], [0.0, 0.0, 0.0]), # 2
Mass([half_L0, -half_L0, half_L0 + drop_height], [0.0, 0.0, 0.0]), # 3
Mass([-half_L0, half_L0, -half_L0 + drop_height], [0.0, 0.0, 0.0]), # 4
Mass([half_L0, half_L0, -half_L0 + drop_height], [0.0, 0.0, 0.0]), # 5
Mass([-half_L0, half_L0, half_L0 + drop_height], [0.0, 0.0, 0.0]), # 6
Mass([half_L0, half_L0, half_L0 + drop_height], [0.0, 0.0, 0.0]), # 7
Mass([-half_L0, half_L0 + L0, -half_L0 + drop_height], [0.0, 0.0, 0.0]), # 8
Mass([half_L0, half_L0 + L0, -half_L0 + drop_height], [0.0, 0.0, 0.0]), # 9
Mass([-half_L0, half_L0 + L0, half_L0 + drop_height], [0.0, 0.0, 0.0]), # 10
Mass([half_L0, half_L0 + L0, half_L0 + drop_height], [0.0, 0.0, 0.0]), # 11
]
short_diag_length = np.sqrt(2 * L0**2)
long_diag_length = np.sqrt(3 * L0**2)
springs = [
Spring(L0, k, masses[0], masses[1]), # Base square
Spring(L0, k, masses[1], masses[3]),
Spring(L0, k, masses[3], masses[2]),
Spring(L0, k, masses[2], masses[0]),
Spring(L0, k, masses[4], masses[5]), # Top square
Spring(L0, k, masses[5], masses[7]),
Spring(L0, k, masses[7], masses[6]),
Spring(L0, k, masses[6], masses[4]),
Spring(L0, k, masses[0], masses[4]), # Vertical edges
Spring(L0, k, masses[1], masses[5]),
Spring(L0, k, masses[2], masses[6]),
Spring(L0, k, masses[3], masses[7]),
Spring(short_diag_length, k, masses[0], masses[3]),
Spring(short_diag_length, k, masses[1], masses[2]),
Spring(short_diag_length, k, masses[4], masses[7]),
Spring(short_diag_length, k, masses[5], masses[6]),
Spring(short_diag_length, k, masses[0], masses[5]),
Spring(short_diag_length, k, masses[1], masses[4]),
Spring(short_diag_length, k, masses[2], masses[7]),
Spring(short_diag_length, k, masses[3], masses[6]),
Spring(short_diag_length, k, masses[1], masses[7]),
Spring(short_diag_length, k, masses[0], masses[6]),
Spring(short_diag_length, k, masses[3], masses[5]),
Spring(short_diag_length, k, masses[2], masses[4]),
Spring(long_diag_length, k, masses[0], masses[7]),
Spring(long_diag_length, k, masses[1], masses[6]),
Spring(long_diag_length, k, masses[2], masses[5]),
Spring(long_diag_length, k, masses[3], masses[4]),
Spring(L0, k, masses[8], masses[9]),
Spring(L0, k, masses[9], masses[11]),
Spring(L0, k, masses[11], masses[10]),
Spring(L0, k, masses[10], masses[8]),
Spring(L0, k, masses[6], masses[10]),
Spring(L0, k, masses[7], masses[11]),
Spring(L0, k, masses[4], masses[8]),
Spring(L0, k, masses[5], masses[9]),
Spring(short_diag_length, k, masses[6], masses[11]),
Spring(short_diag_length, k, masses[7], masses[10]),
Spring(short_diag_length, k, masses[4], masses[9]),
Spring(short_diag_length, k, masses[5], masses[8]),
Spring(short_diag_length, k, masses[4], masses[10]),
Spring(short_diag_length, k, masses[5], masses[11]),
Spring(short_diag_length, k, masses[6], masses[8]),
Spring(short_diag_length, k, masses[7], masses[9]),
Spring(short_diag_length, k, masses[9], masses[10]),
Spring(short_diag_length, k, masses[8], masses[11]),
Spring(long_diag_length, k, masses[6], masses[9]),
Spring(long_diag_length, k, masses[7], masses[8]),
Spring(long_diag_length, k, masses[4], masses[11]),
Spring(long_diag_length, k, masses[5], masses[10])
]
self.masses = masses
self.springs = springs
self.a_dict = {}
for spring in springs:
self.a_dict[spring] = spring.L0
self.b_dict = {spring:0.0 for spring in springs}
self.c_dict = {spring:0.0 for spring in springs}
self.k_dict = {spring:k for spring in springs}
def set_a_dict(self, a_dict):
self.a_dict = a_dict
def set_b_dict(self, b_dict):
self.b_dict = b_dict
def set_c_dict(self, c_dict):
self.c_dict = c_dict
def set_k_dict(self, k_dict):
self.k_dict = k_dict
def add_mass(self, mass):
self.masses.append(mass)
def add_spring(self, spring):
self.springs.append(spring)
self.a_dict[spring] = spring.L0
self.b_dict[spring] = 0.0
self.c_dict[spring] = 0.0
self.k_dict[spring] = k
def remove_spring(self, spring):
self.springs.remove(spring)
del self.a_dict[spring]
del self.b_dict[spring]
del self.c_dict[spring]
del self.k_dict[spring]
def remove_mass(self, mass):
self.masses.remove(mass)
springs_to_remove = []
for spring in self.springs:
if spring.m1 == mass or spring.m2 == mass:
springs_to_remove.append(spring)
for spring in springs_to_remove:
self.remove_spring(spring)
def add_random_mass(individual):
# Add a random mass to the individual
new_mass = Mass(np.random.rand(3), np.random.rand(3))
individual.add_mass(new_mass)
random_masses = random.sample(individual.masses, new_mass_spring_num)
for mass in random_masses:
distance = np.linalg.norm(new_mass.p - mass.p)
new_spring = Spring(distance, k, new_mass, mass)
individual.add_spring(new_spring)
def remove_random_mass(individual):
# Remove a random spring from the individual
mass = random.choice(individual.masses)
individual.remove_mass(mass)
def get_floor_tile():
floor_size = visualization_diameter + 0.5
return [[-floor_size, -floor_size, 0],
[floor_size, -floor_size, 0],
[floor_size, floor_size, 0],
[-floor_size, floor_size, 0]]
t = 0
def simulation_step(masses, springs, dt, a_dict, b_dict, c_dict, k_dict):
global t
t += dt
# Reset forces on each mass
for mass in masses:
mass.f = np.zeros(3, dtype=float)
mass.f += mass.m * g # Gravity
# Calculate spring forces
for spring in springs:
a = a_dict[spring]
b = b_dict[spring]
c = c_dict[spring]
spring.k = k_dict[spring]
spring.L0 = a + b*np.sin(omega*t+c)
delta_p = spring.m1.p - spring.m2.p
delta_length = np.linalg.norm(delta_p)
if delta_length == 0:
direction = np.zeros(3, dtype=float)
else:
direction = delta_p / delta_length
force_magnitude = spring.k * (delta_length - spring.L0)
force = force_magnitude * direction
# Apply spring force to masses
spring.m1.f -= force
spring.m2.f += force
# tally friction
for mass in masses:
if mass.p[2] > 0:
continue
F_n = mass.m * g[2]
F_H = np.linalg.norm(mass.f[:2])
direction = mass.f[:2] / F_H
if F_n < 0:
if F_H<=-mu_s*F_n:
mass.f[:2] = np.zeros(2)
print("static friction, ", mass.f)
else:
mass.f[:2] += -abs(mu_k*F_n)*direction
print("kinetic friction, ", mass.f)
# Update positions and velocities for each mass
for mass in masses:
mass.a = mass.f / mass.m
mass.v += mass.a * dt
mass.p += mass.v * dt
# Simple collision with the ground
if mass.p[2] < 0:
mass.p[2] = 0
mass.v[2] = -damping * mass.v[2] # Some damping on collision
with open("best_individual.pkl", "rb") as f:
I = pickle.load(f)
masses = I.masses
springs = I.springs
a_dict = I.a_dict
b_dict = I.b_dict
c_dict = I.c_dict
k_dict = I.k_dict
# Visualization setup
fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')
# Initialize 8 points for the cube's vertices
points = [ax.plot([], [], [], 'ro')[0] for _ in range(len(masses))]
# Initialize 12 lines for the springs
lines = [ax.plot([], [], [], 'b-')[0] for _ in range(len(springs))]
shadows = [ax.plot([], [], [], 'k-')[0] for _ in range(len(springs))]
floor_tile_collection = Poly3DCollection([get_floor_tile()], color='gray', alpha=0.5)
ax.add_collection3d(floor_tile_collection)
ax.set_xlim([-visualization_diameter, visualization_diameter])
ax.set_ylim([-visualization_diameter, visualization_diameter])
ax.set_zlim([0, 2*visualization_diameter])
ax.set_xlabel('X')
ax.set_ylabel('Y')
ax.set_zlabel('Z')
ax.set_title('Fastest Robot Moving')
def init():
for point in points:
point.set_data([], [])
point.set_3d_properties([])
for line in lines:
line.set_data([], [])
line.set_3d_properties([])
for shadow in shadows:
shadow.set_data([], [])
shadow.set_3d_properties([])
return points + lines + shadows
def animate(i):
for _ in range(300):
simulation_step(masses, springs, dt, a_dict, b_dict, c_dict, k_dict)
for mass, point in zip(masses, points):
x, y, z = mass.p
point.set_data([x], [y])
point.set_3d_properties([z]) # Setting the Z value for 3D
# Update the spring lines
for spring, line in zip(springs, lines):
x_data = [spring.m1.p[0], spring.m2.p[0]]
y_data = [spring.m1.p[1], spring.m2.p[1]]
z_data = [spring.m1.p[2], spring.m2.p[2]]
line.set_data(x_data, y_data)
line.set_3d_properties(z_data)
# Update the shadow lines
for spring, shadow in zip(springs, shadows):
x_data = [spring.m1.p[0], spring.m2.p[0]]
y_data = [spring.m1.p[1], spring.m2.p[1]]
z_data = [0, 0]
shadow.set_data(x_data, y_data)
shadow.set_3d_properties(z_data)
return points + lines + shadows
ani = animation.FuncAnimation(fig, animate, frames=1000, init_func=init, blit=False, interval=5)
plt.show()