diff --git a/contributer_demo/demo3/ACO/aco.py b/contributer_demo/demo3/ACO/aco.py index 0dc95a0..3b36e6b 100644 --- a/contributer_demo/demo3/ACO/aco.py +++ b/contributer_demo/demo3/ACO/aco.py @@ -160,5 +160,5 @@ class ACO(): print("ACO log: ", log) end_time = time.time() print("ACO time:", end_time - start_time) - return path_new, end_time - start_time + return log, path_new, end_time - start_time diff --git a/contributer_demo/demo3/ACO/evaluate.py b/contributer_demo/demo3/ACO/evaluate.py index d029ce5..0021a2c 100644 --- a/contributer_demo/demo3/ACO/evaluate.py +++ b/contributer_demo/demo3/ACO/evaluate.py @@ -7,6 +7,8 @@ from aco import ACO from geographiclib.geodesic import Geodesic import math geod = Geodesic.WGS84 +import json +import os class Env(): def __init__(self, vehicle_num, target_num, map_size, visualized=True, time_cost=None, repeat_cost=None): @@ -97,7 +99,8 @@ class Env(): position = self.targets[self.assignment[i][j],:3] trajectory = np.insert(trajectory,j+1,values=position,axis=0) plt.scatter(x=trajectory[1:,0],y=trajectory[1:,1],s=trajectory[1:,2]*10,c='b') - plt.plot(trajectory[:,0], trajectory[:,1]) + plt.plot(trajectory[:,0], trajectory[:,1]) + os.makedirs('task_pic/' + self.size, exist_ok=True) plt.savefig('task_pic/'+self.size+'/'+self.algorithm+ "-%d-%d.png" % (self.play,self.rond)) plt.cla() @@ -110,19 +113,41 @@ def evaluate(vehicle_num, target_num, map_size): env = Env(vehicle_num,target_num,map_size,visualized=True) for j in range(1): aco = ACO(vehicle_num,target_num,env.vehicles_speed,env.targets,env.time_lim) - path_new, time = aco.run() + log, path_new, time = aco.run() env.run(path_new,'ACO',i+1,j+1) re_aco[i].append((env.total_reward,time)) env.reset() - + return log + +def build_dir(json2dic, path): + for i in range(len(path)): + json2dic['mission']['items'].append(copy.deepcopy(json2dic['mission']['items'][0])) + json2dic['mission']['items'][i+1]['command'] = 16 + json2dic['mission']['items'][i+1]['doJumpId'] = i+2 + json2dic['mission']['items'][i+1]['params'][0] = 0 + json2dic['mission']['items'][i+1]['params'][4] = path[i][0] + json2dic['mission']['items'][i+1]['params'][5] = path[i][1] + json2dic['mission']['items'].append(copy.deepcopy(json2dic['mission']['items'][0])) + json2dic['mission']['items'][-1]['Altitude'] = 0 + json2dic['mission']['items'][-1]['command'] = 21 + json2dic['mission']['items'][-1]['doJumpId'] = len(path) + 2 + json2dic['mission']['items'][-1]['params'][0] = 0 + json2dic['mission']['items'][-1]['params'][-1] = 0 + return json2dic if __name__=='__main__': - ''' - vehicle number: scalar - speeds of vehicles: array - target number: scalar - targets: array, the first line is depot, the first column is x position, the second column is y position, the third column is reward and the forth column is time consumption to finish the mission - time limit: scalar - ''' - evaluate(6,30,1e3) + log = evaluate(6,30,1e3) + + # 修改json + for i in range(len(log)): + json_name = '%d.json' % (i+1) + plan_name = '%d.plan' % (i+1) + if(os.path.isfile(plan_name)): + os.rename(plan_name, json_name) + with open(json_name, "r", encoding='utf-8') as f: + json2dic = json.load(f) + json2dic = build_dir(json2dic, log[i]) + with open(json_name, "w", encoding='utf-8') as f: + json.dump(json2dic, f, indent=2, sort_keys=True) + os.rename(json_name, plan_name) diff --git a/contributer_demo/demo3/Astar/Astar.py b/contributer_demo/demo3/Astar/Astar.py index 31a6ba3..9fb4243 100644 --- a/contributer_demo/demo3/Astar/Astar.py +++ b/contributer_demo/demo3/Astar/Astar.py @@ -237,7 +237,7 @@ class Weighted_A_star(object): if __name__ == '__main__': - workbook = open_workbook(r'附件.xlsx') + workbook = open_workbook(r'路径点.xlsx') sheet = workbook.sheet_by_index(0) content = np.array([sheet.row_values(i) for i in range(2, 589)]) @@ -245,4 +245,4 @@ if __name__ == '__main__': sta = time.time() path = Astar.run() print(time.time() - sta) - draw_path(sheet, path) + #draw_path(sheet, path) diff --git a/contributer_demo/demo3/Astar/judge.py b/contributer_demo/demo3/Astar/judge.py index 5f0bd93..9175527 100644 --- a/contributer_demo/demo3/Astar/judge.py +++ b/contributer_demo/demo3/Astar/judge.py @@ -54,7 +54,7 @@ def compute(all_paths): if __name__ == '__main__': all_paths = [] - workbook = open_workbook(r'附件.xlsx') + workbook = open_workbook(r'路径点.xlsx') sheet = workbook.sheet_by_index(0) content = np.array([sheet.row_values(i) for i in range(1, 589)]) diff --git a/contributer_demo/demo3/Astar/路径点.xlsx b/contributer_demo/demo3/Astar/路径点.xlsx new file mode 100644 index 0000000..6c557a0 Binary files /dev/null and b/contributer_demo/demo3/Astar/路径点.xlsx differ