/
rollout.py
98 lines (81 loc) · 2.75 KB
/
rollout.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
from cost import cost
import random
import copy
import sys
def uniform_rollout(path, robot, budget):
#Possible Actions
action_set = list()
action_set.append('left')
action_set.append('right')
action_set.append('forward')
action_set.append('backward')
# Random rollout policy
# Pick random actions until budget is exhausted
new_path = copy.deepcopy(path)
robot.set_path(path)
#Move Robot along the path
action = "start"
while action:
action = robot.follow_path()
# Move the robot
robot.move(action)
while cost(new_path) < budget:
r = random.randint(0, len(action_set)-1)
action = action_set[r]
robot.move(action)
new_path.append(robot.get_loc())
return path
def heuristic_rollout(path, robot, budget, map):
#Possible Actions
action_set = list()
action_set.append('left')
action_set.append('right')
action_set.append('forward')
action_set.append('backward')
# Random rollout policy
# Pick random actions until budget is exhausted
new_path = copy.deepcopy(path)
robot.set_path(path)
#Move Robot along the path
action = "start"
while action:
action = robot.follow_path()
# Move the robot
robot.move(action)
visited_hotspots = set()
while cost(new_path) < budget:
curr_loc = robot.get_loc()
#Get closest hotspot
closest_hotspot = None
min_dist = sys.maxsize
hotspots = [h for h in map.hotspots if not h in visited_hotspots]
for h in hotspots:
dist = map.euclidean_distance(curr_loc, h)
if dist < min_dist:
closest_hotspot = h
min_dist = dist
#Determine action that moves us towards a hotspot
if closest_hotspot:
#If we haven't visited all the hotspots, keep visiting them
best_action = None
min_dist = sys.maxsize
for a in action_set:
robot.move(a)
dist = map.euclidean_distance(robot.get_loc(), closest_hotspot)
if dist < min_dist:
best_action = a
min_dist = dist
robot.set_loc(curr_loc[0], curr_loc[1])
#If the robot visits a hotspot, don't visit it again
if min_dist <= robot.sensing_range:
visited_hotspots.add(closest_hotspot)
#Choose best action
robot.move(best_action)
new_path.append(robot.get_loc())
else:
#Else move Randomly
r = random.randint(0, len(action_set)-1)
action = action_set[r]
robot.move(action)
new_path.append(robot.get_loc())
return path