-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.py
156 lines (129 loc) · 4.99 KB
/
main.py
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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
import copy
from random import random
from GreedyPlanner import GreedyPlanner
from RandomPlanner import RandomPlanner
from mcts import dec_mcts
from Map import Map
from Robot import Robot
from Simulator import Simulator
import numpy as np
from plot_tree import plotTree
def run_dec_mcts(budget, num_samples, c_budget, explore_exploit, input_robots, input_map, r_policy):
robots = copy.deepcopy(input_robots)
world_map = copy.deepcopy(input_map)
#Generate a path the robots (Dec-MCTS goes here)
dec_mcts_paths = dec_mcts(budget, num_samples, c_budget, explore_exploit, robots, world_map, r_policy)
for r in robots:
r.reset_robot()
#Use the Simulator to evaluate the final paths
simulator = Simulator(world_map, robots)
simulator.run()
score = simulator.get_score()
#See the results
# simulator.visualize()
# print("{} Dec-MCTS Score: {}".format(r_policy, simulator.get_score()))
for r in robots:
r.reset_robot()
simulator.reset_game()
#Determine longest path to guide the random and Greedy Planners
max_length = -1
for path in dec_mcts_paths:
if len(path) > max_length:
max_length = len(path)
return max_length, score
def run_random_planner(budget, input_robots, input_map):
robots = copy.deepcopy(input_robots)
world_map = copy.deepcopy(input_map)
for r in robots:
r.reset_robot()
for r in robots:
planner = RandomPlanner(budget)
random_path = planner.random_path(r)
r.final_path = random_path
#Use the Simulator to evaluate the final paths
simulator = Simulator(world_map, robots)
simulator.run()
score = simulator.get_score()
#See the results
# simulator.visualize()
# print("random Score: {}".format(simulator.get_score()))
simulator.reset_game()
return score
def run_greedy_planner(budget, input_robots, input_map):
robots = copy.deepcopy(input_robots)
world_map = copy.deepcopy(input_map)
for r in robots:
r.reset_robot()
for r in robots:
planner = GreedyPlanner(budget)
greedy_path = planner.greedy_path(r, world_map)
r.final_path = greedy_path
#Use the Simulator to evaluate the final paths
simulator = Simulator(world_map, robots)
simulator.run()
score = simulator.get_score()
#See the results
# simulator.visualize()
# print("Greedy Score: {}".format(simulator.get_score()))
simulator.reset_game()
return score
if __name__ == "__main__":
#Number of Trials
num_trials = 100
#Dec-MCTS Parameters
budget = 1000
computational_budget = 50
num_samples = 20
exploration_exploitation_parameter = 1.0 # =1.0 is recommended. <1.0 more exploitation. >1.0 more exploration.
#Map Parameters
bounds = (0, 20)
num_survivors = 50
num_hotspots = 10
num_damages = 25
winner_dict = dict()
scores_dict = dict()
scores_dict['dec-heuristic'] = list()
scores_dict['dec-uniform'] = list()
scores_dict['random'] = list()
scores_dict['greedy'] = list()
for i in range(num_trials):
#Set up problem
print("Experiment {}".format(i))
world_map = Map(bounds, num_survivors, num_hotspots, num_damages)
robots = list()
robot = Robot(0, 0, bounds, world_map)
robot2 = Robot(0, 0, bounds, world_map)
robots = [robot, robot2]
#Run Planners
length_of_path, h_score = run_dec_mcts(budget, num_samples, computational_budget, exploration_exploitation_parameter, robots, world_map, 'heuristic')
length_of_path, u_score = run_dec_mcts(budget, num_samples, computational_budget, exploration_exploitation_parameter, robots, world_map, 'uniform')
r_score = run_random_planner(length_of_path, robots, world_map)
g_score = run_greedy_planner(length_of_path, robots, world_map)
#Collect Data
scores = [h_score, u_score, r_score, g_score]
scores_dict['dec-heuristic'].append(h_score)
scores_dict['dec-uniform'].append(u_score)
scores_dict['random'].append(r_score)
scores_dict['greedy'].append(g_score)
max_index = scores.index(max(scores))
if max_index == 0:
if not 'dec-heuristic' in winner_dict:
winner_dict['dec-heuristic'] = 0
winner_dict['dec-heuristic'] += 1
elif max_index == 1:
if not 'dec-uniform' in winner_dict:
winner_dict['dec-uniform'] = 0
winner_dict['dec-uniform'] += 1
elif max_index == 2:
if not 'random' in winner_dict:
winner_dict['random'] = 0
winner_dict['random'] += 1
elif max_index == 3:
if not 'greedy' in winner_dict:
winner_dict['greedy'] = 0
winner_dict['greedy'] += 1
#Summary Stats
print(scores_dict)
for k, v in scores_dict.items():
print('{} Avg: {}, Std: {}'.format(k, np.mean(v), np.std(v)))
print("Winners dict: {}".format(winner_dict))