summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorJonZhao <[email protected]>2019-06-04 10:51:20 +0800
committerJonZhao <[email protected]>2019-06-04 10:51:20 +0800
commit2af7b7905705d4ebc774e3f9a95850bd042fe0e3 (patch)
treed6dd1c2c20a7264e119c23ff527430f03e621ee9
parent6c95eeebca7c843a3b9a775a4215836af2f5ab42 (diff)
downloadVRPTW-ACO-python-2af7b7905705d4ebc774e3f9a95850bd042fe0e3.tar.gz
VRPTW-ACO-python-2af7b7905705d4ebc774e3f9a95850bd042fe0e3.tar.bz2
VRPTW-ACO-python-2af7b7905705d4ebc774e3f9a95850bd042fe0e3.zip
add function: print_and_write_in_file
-rw-r--r--example3.py20
-rw-r--r--multiple_ant_colony_system.py64
2 files changed, 62 insertions, 22 deletions
diff --git a/example3.py b/example3.py
new file mode 100644
index 0000000..a01434c
--- /dev/null
+++ b/example3.py
@@ -0,0 +1,20 @@
+from vrptw_base import VrptwGraph
+from multiple_ant_colony_system import MultipleAntColonySystem
+import os
+
+
+if __name__ == '__main__':
+ ants_num = 10
+ beta = 1
+ q0 = 0.1
+ show_figure = False
+ for file_name in os.listdir('./solomon-100'):
+ file_path = os.path.join('./solomon-100', file_name)
+ print('-' * 100)
+ print('file_path: %s' % file_path)
+ print('\n')
+ file_to_write_path = os.path.join('./result', file_name.split('.')[0] + '-result.txt')
+ graph = VrptwGraph(file_path)
+ macs = MultipleAntColonySystem(graph, ants_num=ants_num, beta=beta, q0=q0, whether_or_not_to_show_figure=show_figure)
+ macs.run_multiple_ant_colony_system(file_to_write_path)
+ print('\n' * 10)
diff --git a/multiple_ant_colony_system.py b/multiple_ant_colony_system.py
index 3a47ff1..c0fc3c8 100644
--- a/multiple_ant_colony_system.py
+++ b/multiple_ant_colony_system.py
@@ -302,13 +302,13 @@ class MultipleAntColonySystem:
del ant
ants.clear()
- def run_multiple_ant_colony_system(self):
+ def run_multiple_ant_colony_system(self, file_to_write_path=None):
"""
开启另外的线程来跑multiple_ant_colony_system, 使用主线程来绘图
:return:
"""
path_queue_for_figure = MPQueue()
- multiple_ant_colony_system_thread = Process(target=self._multiple_ant_colony_system, args=(path_queue_for_figure,))
+ multiple_ant_colony_system_thread = Process(target=self._multiple_ant_colony_system, args=(path_queue_for_figure, file_to_write_path, ))
multiple_ant_colony_system_thread.start()
# 是否要展示figure
@@ -317,16 +317,17 @@ class MultipleAntColonySystem:
figure.run()
multiple_ant_colony_system_thread.join()
- # 传入None作为结束标志
- if self.whether_or_not_to_show_figure:
- path_queue_for_figure.put(PathMessage(None, None))
-
- def _multiple_ant_colony_system(self, path_queue_for_figure: MPQueue):
+ def _multiple_ant_colony_system(self, path_queue_for_figure: MPQueue, file_to_write_path=None):
"""
调用acs_time 和 acs_vehicle进行路径的探索
:param path_queue_for_figure:
:return:
"""
+ if file_to_write_path is not None:
+ file_to_write = open(file_to_write_path, 'w')
+ else:
+ file_to_write = None
+
start_time_total = time.time()
# 在这里需要两个队列,time_what_to_do、vehicle_what_to_do, 用来告诉acs_time、acs_vehicle这两个线程,当前的best path是什么,或者让他们停止计算
@@ -375,13 +376,20 @@ class MultipleAntColonySystem:
given_time = 5
if time.time() - start_time_found_improved_solution > 60 * given_time:
stop_event.set()
- print('*' * 50)
- print('time is up: cannot find a better solution in given time(%d minutes)' % given_time)
- print('it takes %0.3f second from multiple_ant_colony_system running' % (time.time()-start_time_total))
- print('the best path have found is:')
- print(self.best_path)
- print('best path distance is %f, best vehicle_num is %d' % (self.best_path_distance, self.best_vehicle_num))
- print('*' * 50)
+ self.print_and_write_in_file(file_to_write, '*' * 50)
+ self.print_and_write_in_file(file_to_write, 'time is up: cannot find a better solution in given time(%d minutes)' % given_time)
+ self.print_and_write_in_file(file_to_write, 'it takes %0.3f second from multiple_ant_colony_system running' % (time.time()-start_time_total))
+ self.print_and_write_in_file(file_to_write, 'the best path have found is:')
+ self.print_and_write_in_file(file_to_write, self.best_path)
+ self.print_and_write_in_file(file_to_write, 'best path distance is %f, best vehicle_num is %d' % (self.best_path_distance, self.best_vehicle_num))
+ self.print_and_write_in_file(file_to_write, '*' * 50)
+
+ # 传入None作为结束标志
+ if self.whether_or_not_to_show_figure:
+ path_queue_for_figure.put(PathMessage(None, None))
+
+ file_to_write.flush()
+ file_to_write.close()
return
if path_found_queue.empty():
@@ -405,10 +413,12 @@ class MultipleAntColonySystem:
# 搜索到更好的结果,更新start_time
start_time_found_improved_solution = time.time()
- print('*' * 50)
- print('[macs]: distance of found path (%f) better than best path\'s (%f)' % (found_path_distance, self.best_path_distance))
- print('it takes %0.3f second from multiple_ant_colony_system running' % (time.time()-start_time_total))
- print('*' * 50)
+ self.print_and_write_in_file(file_to_write, '*' * 50)
+ self.print_and_write_in_file(file_to_write, '[macs]: distance of found path (%f) better than best path\'s (%f)' % (found_path_distance, self.best_path_distance))
+ self.print_and_write_in_file(file_to_write, 'it takes %0.3f second from multiple_ant_colony_system running' % (time.time()-start_time_total))
+ self.print_and_write_in_file(file_to_write, '*' * 50)
+ file_to_write.flush()
+
self.best_path = found_path
self.best_vehicle_num = found_path_used_vehicle_num
self.best_path_distance = found_path_distance
@@ -427,11 +437,13 @@ class MultipleAntColonySystem:
# 搜索到更好的结果,更新start_time
start_time_found_improved_solution = time.time()
- print('*' * 50)
- print('[macs]: vehicle num of found path (%d) better than best path\'s (%d), found path distance is %f'
+ self.print_and_write_in_file(file_to_write, '*' * 50)
+ self.print_and_write_in_file(file_to_write, '[macs]: vehicle num of found path (%d) better than best path\'s (%d), found path distance is %f'
% (found_path_used_vehicle_num, best_vehicle_num, found_path_distance))
- print('it takes %0.3f second multiple_ant_colony_system running' % (time.time() - start_time_total))
- print('*' * 50)
+ self.print_and_write_in_file(file_to_write, 'it takes %0.3f second multiple_ant_colony_system running' % (time.time() - start_time_total))
+ self.print_and_write_in_file(file_to_write, '*' * 50)
+ file_to_write.flush()
+
self.best_path = found_path
self.best_vehicle_num = found_path_used_vehicle_num
self.best_path_distance = found_path_distance
@@ -443,3 +455,11 @@ class MultipleAntColonySystem:
print('[macs]: send stop info to acs_time and acs_vehicle')
# 通知acs_vehicle和acs_time两个线程,当前找到的best_path和best_path_distance
stop_event.set()
+
+ @staticmethod
+ def print_and_write_in_file(file_to_write=None, message='default message'):
+ if file_to_write is None:
+ print(message)
+ else:
+ print(message)
+ file_to_write.write(str(message)+'\n')