forked from codebox/sarsa-lambda
-
Notifications
You must be signed in to change notification settings - Fork 0
/
states_actions.py
46 lines (32 loc) · 1.27 KB
/
states_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
import copy
DEFAULT_VALUE = 0
class StatesAndActions:
def __init__(self):
self.values = {}
def get(self, state, action):
if state in self.values:
return self.values[state].get(action, DEFAULT_VALUE)
return DEFAULT_VALUE
def get_all_for_state(self, state, all_states):
return {action: self.get(state, action) for action in all_states}
def get_all(self):
return copy.deepcopy(self.values)
def set_all(self, values):
self.values = copy.deepcopy(values)
def set(self, state, action, value=DEFAULT_VALUE):
if state not in self.values:
self.values[state] = {}
self.values[state][action] = value
def has(self, state, action):
return state in self.values and action in self.values[state]
def update(self, state, action, update_fn, value_to_set=None):
if self.has(state, action):
old_value = self.get(state, action)
new_value = update_fn(old_value)
self.set(state, action, new_value)
elif value_to_set is not None:
self.set(state, action, value_to_set)
def for_each(self, fn):
for state, actions in self.values.items():
for action in actions:
fn(state, action)