-
Notifications
You must be signed in to change notification settings - Fork 2
/
plot_link_generations.py
89 lines (72 loc) · 2.96 KB
/
plot_link_generations.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
import matplotlib.pyplot as plt
from matplotlib.widgets import Slider, TextBox
from mpl_toolkits.mplot3d import Axes3D
import matplotlib.patheffects as patheffects
def main(filename):
with open(filename) as f:
points = f.readlines()
target = list(map(float, points[0].split(',')))
generations = []
for line in points[1:]:
links = line.split('\t')
links_as_points = []
for link in links:
links_as_points.append(list(map(float, link.split(','))))
generations.append(links_as_points)
fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')
ax.grid(False)
ax.set_xlim((-.3, .3))
ax.set_ylim((-.3, .3))
ax.set_zlim((-.3, .3))
gen_slider_ax = fig.add_axes([0.2, .95, .13, .03], label='Generation: ')
gen_slider = Slider(gen_slider_ax,
'Generation',
1, len(generations),
valfmt='%d', valinit=1, valstep=1)
fitness_ax = fig.add_axes([0.2, .88, .13, .03], label='Fitness')
fitness_text = TextBox(fitness_ax, 'Fitness: ', str('0.00000'))
x, y, z = target
target_text = f" Target: {x:+.5f} {y:+.5f} {z:+.5f}"
target_annotation = ax.annotate(target_text, (-.01, .09), color='white', size=10)
target_annotation.set_path_effects([patheffects.withStroke(linewidth=3,
foreground='black')])
zero5f = f"{0:+.5f}"
xyz_annotation = ax.annotate(f"Manipulator: {zero5f} {zero5f} {zero5f}", (-.019, .08), color='white', size=10)
xyz_annotation.set_path_effects([patheffects.withStroke(linewidth=3,
foreground='black')])
ax.plot(*zip(target), 'rx', markersize=10)
colors = 'black', 'red', 'yellow', 'blue', 'purple', 'pink'
link_plots = [
ax.plot((), (), (), color=colors[i % len(colors)], linewidth=4, solid_capstyle='round')[0]
for i in range(len(generations[0]))
]
gen = 0
def update_plot(_):
generation = generations[int(gen_slider.val)-1]
link0, *links1_n = generation
last = link0
for i, link in enumerate(links1_n):
x_data, y_data, z_data = zip(last, link)
link_plots[i].set_xdata(x_data)
link_plots[i].set_ydata(y_data)
link_plots[i].set_3d_properties(z_data)
last = link
def fitness(point, target):
x, y, z = point
tx, ty, tz = target
return ((x-tx)**2+(y-ty)**2+(z-tz)**2)**.5
fitness_text.set_val(f'{fitness(last, target):.5f}')
x, y, z = last
xyz = f"Manipulator: {x:+.5f} {y:+.5f} {z:+.5f}"
xyz_annotation.set_text(xyz)
fig.canvas.draw_idle()
gen_slider.on_changed(update_plot)
plt.show()
if __name__ == '__main__':
import sys
if len(sys.argv) == 1:
file_name = "example_output.txt"
else:
file_name = sys.argv[1]
main(file_name)