diff options
Diffstat (limited to 'vprtw_aco_figure.py')
| -rw-r--r-- | vprtw_aco_figure.py | 40 |
1 files changed, 28 insertions, 12 deletions
diff --git a/vprtw_aco_figure.py b/vprtw_aco_figure.py index 01dbc80..6fa8765 100644 --- a/vprtw_aco_figure.py +++ b/vprtw_aco_figure.py @@ -18,42 +18,58 @@ class VrptwAcoFigure: 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'] + self._depot_color = 'k' + self._customer_color = 'steelblue' + self._line_color = 'darksalmon' def _draw_point(self): - # 先画出图中的点 - self.figure_ax.plot(list(node.x for node in self.nodes), - list(node.y for node in self.nodes), '%s.' % self._dot_color) - self.figure.show() + # 画出depot + self.figure_ax.scatter([self.nodes[0].x], [self.nodes[0].y], c=self._depot_color, label='depot', s=40) + + # 画出customer + self.figure_ax.scatter(list(node.x for node in self.nodes[1:]), + list(node.y for node in self.nodes[1:]), c=self._customer_color, label='customer', s=20) plt.pause(0.5) def run(self): + # 先绘制出各个结点 self._draw_point() + self.figure.show() # 从队列中读取新的path,进行绘制 while True: if not self.path_queue.empty(): + # 取队列中最新的一个path,其他的path丢弃 info = self.path_queue.get() while not self.path_queue.empty(): info = self.path_queue.get() - path, distance, used_vehicle_num = info.get_path_info() + path, distance, used_vehicle_num = info.get_path_info() if path is None: + print('[draw figure]: exit') break - self.figure_ax.clear() - self._draw_point() - self.figure.canvas.draw() + # 需要先记录要移除的line,不能直接在第一个循环中进行remove, + # 不然self.figure_ax.lines会在循环的过程中改变,导致部分line无法成功remove + remove_obj = [] + for line in self.figure_ax.lines: + if line._label == 'line': + remove_obj.append(line) + + for line in remove_obj: + self.figure_ax.lines.remove(line) + remove_obj.clear() plt.pause(1) + # 重新绘制line + self.figure_ax.set_title('current path travel distance: %f' % distance) self._draw_line(path) plt.pause(1) def _draw_line(self, path): - # 根据path中index进行路劲的绘制 + # 根据path中index进行路径的绘制 for i in range(1, len(path)): x_list = [self.nodes[path[i - 1]].x, self.nodes[path[i]].x] y_list = [self.nodes[path[i - 1]].y, self.nodes[path[i]].y] - self.figure_ax.plot(x_list, y_list, '%s-' % self._line_color_list[0]) + self.figure_ax.plot(x_list, y_list, color=self._line_color, linewidth=1.5, label='line') plt.pause(0.05) |
