summaryrefslogtreecommitdiffstats
path: root/vprtw_aco_figure.py
blob: 99e71f4d1fc2fdd0bed329a30e4484680a2672f5 (plain)
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
import matplotlib.pyplot as plt
from vrptw_base import VrptwGraph
from queue import Queue


class VrptwAcoFigure:
    def __init__(self, graph: VrptwGraph, path_queue: Queue):
        self.graph = graph
        self.figure = plt.figure(figsize=(10, 10))
        self.figure_ax = self.figure.add_subplot(1, 1, 1)
        self.path_queue = path_queue
        self._dot_color = 'k'
        self._line_color_list = ['r', 'y', 'g', 'c', 'b', 'm']

    def _draw_point(self):
        # 先画出图中的点
        self.figure_ax.plot(list(node.x for node in self.graph.nodes),
                            list(node.y for node in self.graph.nodes), '%s.' % self._dot_color)
        self.figure.show()
        plt.pause(0.5)

    def run(self):
        self._draw_point()

        # 从队列中读取新的path,进行绘制
        while True:
            if not self.path_queue.empty():
                info = self.path_queue.get()
                while not self.path_queue.empty():
                    info = self.path_queue.get()
                path, distance = info.get_path_info()

                if path is None:
                    break

                self.figure_ax.clear()
                self._draw_point()
                self.figure.canvas.draw()
                plt.pause(1)

                self._draw_line(path)
            plt.pause(1)

    def _draw_line(self, path):
        # 根据path中index进行路劲的绘制
        for i in range(1, len(path)):
            x_list = [self.graph.nodes[path[i - 1]].x, self.graph.nodes[path[i]].x]
            y_list = [self.graph.nodes[path[i - 1]].y, self.graph.nodes[path[i]].y]
            self.figure_ax.plot(x_list, y_list, '%s-' % self._line_color_list[0])
            plt.pause(0.05)