-
Notifications
You must be signed in to change notification settings - Fork 15
/
actions.py
132 lines (103 loc) · 3.88 KB
/
actions.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
import itertools
import random
class Actions:
"""Common base class for action methods
Parameters
----------
action_space : tuple
Set of tuples defining combinations of actions
for all dimensions
"""
def __init__(self, action_space=None, verbose=False, **kwargs):
if action_space is None:
raise ValueError("Action space must be defined by action(s) (set)")
self.action_space = action_space
self.action_list = self.setup_action_list()
self.verbose = verbose
if verbose:
self.print_action_info()
def avail_actions(self):
"""Return set of available actions"""
return self.action_space
def get_action_list(self):
"""Return ordered list of actions"""
return self.action_list
# returns index of action given an action
def action_to_index(self, action):
return self.action_space.index(action)
# returns action given an index
def index_to_action(self, a_idx):
return self.action_space[a_idx]
def setup_action_list(self):
"""Define ordered list of actions"""
return list(map(self.action_to_index, self.action_space))
def get_random_action(self):
"""Return random action and associated index"""
random_action_index = random.choice(self.get_action_list())
return self.index_to_action(random_action_index), random_action_index
def print_action_info(self):
print("Available Actions:")
print(" ID, Values")
for action_info in zip(self.get_action_list(), self.avail_actions()):
print(f" {action_info[0]} {action_info[1]}")
class WalkingActions(Actions):
"""
WalkingActions for a human walking
"""
def __init__(self):
# change in heading
self.del_theta = [-45, 0, 45]
# speed
self.del_r = [0, 1.5]
simple_action_space = tuple(itertools.product(self.del_theta, self.del_r))
super().__init__(action_space=simple_action_space, verbose=False)
class SimpleActions(Actions):
"""SimpleActions for testing purposes"""
def __init__(self):
self.del_theta = [-30, 0, 30]
self.del_r = [0, 4]
simple_action_space = tuple(itertools.product(self.del_theta, self.del_r))
super().__init__(action_space=simple_action_space, verbose=False)
# returns index of action given an action
def action_to_index(self, action):
return self.action_space.index(action)
# returns action given an index
def index_to_action(self, a_idx):
return self.action_space[a_idx]
class BaselineActions(Actions):
"""SimpleActions for testing purposes"""
def __init__(self, sensor_speed=1):
self.del_theta = [-90, -45, 0, 45, 90]
self.del_r = [sensor_speed]
baseline_action_space = tuple(itertools.product(self.del_theta, self.del_r))
super().__init__(action_space=baseline_action_space, verbose=False)
# returns index of action given an action
def action_to_index(self, action):
return self.action_space.index(action)
# returns action given an index
def index_to_action(self, a_idx):
return self.action_space[a_idx]
AVAIL_ACTIONS = {
"simpleactions": SimpleActions,
"baselineactions": BaselineActions,
"walkingactions": WalkingActions,
}
def get_action(action_name=""):
"""Convenience function for retrieving BirdsEye action methods
Parameters
----------
action_name : {'simpleactions'}
Name of action method.
Returns
-------
action_obj : Action class object
BirdsEye action method.
"""
action_name = action_name.lower()
if action_name in AVAIL_ACTIONS:
action_obj = AVAIL_ACTIONS[action_name]
return action_obj
raise ValueError(
f"Invalid action method name, {action_name}, entered. Must be "
"in {AVAIL_ACTIONS.keys()}"
)