-
Notifications
You must be signed in to change notification settings - Fork 29
/
animated_visualizer.py
53 lines (40 loc) · 1.56 KB
/
animated_visualizer.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
import matplotlib.pyplot as plt
from matplotlib.animation import FuncAnimation
import numpy as np
def animateTSP(history, points):
''' animate the solution over time
Parameters
----------
hisotry : list
history of the solutions chosen by the algorith
points: array_like
points with the coordinates
'''
''' approx 1500 frames for animation '''
key_frames_mult = len(history) // 1500
fig, ax = plt.subplots()
''' path is a line coming through all the nodes '''
line, = plt.plot([], [], lw=2)
def init():
''' initialize node dots on graph '''
x = [points[i][0] for i in history[0]]
y = [points[i][1] for i in history[0]]
plt.plot(x, y, 'co')
''' draw axes slighty bigger '''
extra_x = (max(x) - min(x)) * 0.05
extra_y = (max(y) - min(y)) * 0.05
ax.set_xlim(min(x) - extra_x, max(x) + extra_x)
ax.set_ylim(min(y) - extra_y, max(y) + extra_y)
'''initialize solution to be empty '''
line.set_data([], [])
return line,
def update(frame):
''' for every frame update the solution on the graph '''
x = [points[i, 0] for i in history[frame] + [history[frame][0]]]
y = [points[i, 1] for i in history[frame] + [history[frame][0]]]
line.set_data(x, y)
return line
''' animate precalulated solutions '''
ani = FuncAnimation(fig, update, frames=range(0, len(history), key_frames_mult),
init_func=init, interval=3, repeat=False)
plt.show()