From 1dcc89d6f9481368be538ae7339c1793fb546398 Mon Sep 17 00:00:00 2001 From: JonZhao <1044264932@qq.com> Date: Wed, 29 May 2019 14:30:38 +0800 Subject: =?UTF-8?q?=E6=9B=B4=E6=96=B0figure=20class=EF=BC=9A=E5=9C=A8?= =?UTF-8?q?=E9=87=8D=E6=96=B0=E7=BB=98=E5=88=B6line=E7=9A=84=E8=BF=87?= =?UTF-8?q?=E7=A8=8B=E4=B8=AD=EF=BC=8C=E5=8F=AA=E7=A7=BB=E9=99=A4=E4=B9=8B?= =?UTF-8?q?=E5=89=8D=E7=BB=98=E5=88=B6=E4=BA=86=E7=9A=84line=EF=BC=9B?= =?UTF-8?q?=E6=9B=B4=E6=96=B0=E4=BA=86=E9=85=8D=E8=89=B2=E6=96=B9=E6=A1=88?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- basic_aco.py | 16 +++++++++------- 1 file changed, 9 insertions(+), 7 deletions(-) (limited to 'basic_aco.py') diff --git a/basic_aco.py b/basic_aco.py index bbadb52..b6e2d50 100644 --- a/basic_aco.py +++ b/basic_aco.py @@ -8,7 +8,8 @@ from queue import Queue class BasicACO: - def __init__(self, graph: VrptwGraph, ants_num=10, max_iter=200, beta=2): + def __init__(self, graph: VrptwGraph, ants_num=10, max_iter=200, beta=2, q0=0.1, + whether_or_not_to_show_figure=True): super() # graph 结点的位置、服务时间信息 self.graph = graph @@ -18,18 +19,16 @@ class BasicACO: self.max_iter = max_iter # vehicle_capacity 表示每辆车的最大载重 self.max_load = graph.vehicle_capacity - # 信息素强度 - self.Q = 1 # beta 启发性信息重要性 self.beta = beta # q0 表示直接选择概率最大的下一点的概率 - self.q0 = 0.1 + self.q0 = q0 # best path self.best_path_distance = None self.best_path = None self.best_vehicle_num = None - self.whether_or_not_to_show_figure = True + self.whether_or_not_to_show_figure = whether_or_not_to_show_figure def run_basic_aco(self): # 开启一个线程来跑_basic_aco,使用主线程来绘图 @@ -85,6 +84,7 @@ class BasicACO: if self.best_path is None: self.best_path = ants[int(best_index)].travel_path self.best_path_distance = paths_distance[best_index] + start_iteration = iter # 图形化展示 if self.whether_or_not_to_show_figure: @@ -93,7 +93,7 @@ class BasicACO: elif paths_distance[best_index] < self.best_path_distance: self.best_path = ants[int(best_index)].travel_path self.best_path_distance = paths_distance[best_index] - + start_iteration = iter # 图形化展示 if self.whether_or_not_to_show_figure: path_queue_for_figure.put(PathMessage(self.best_path, self.best_path_distance)) @@ -102,9 +102,11 @@ class BasicACO: # 更新信息素表 self.graph.global_update_pheromone(self.best_path, self.best_path_distance) - given_iteration = 50 + given_iteration = 100 if iter - start_iteration > given_iteration: print('iteration is up: can not find better solution in %d iteration' % given_iteration) + print('exit.') + break def select_next_index(self, ant): """ -- cgit v1.2.3