-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstate.py
63 lines (48 loc) · 2.05 KB
/
state.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
from copy import deepcopy
from actions import all_actions
class State:
def __init__(self):
self.past_actions = []
self.current_subgoal = ""
self.all_subgoals = tuple()
self.visited_subgoals = []
self.num_subgoals = 1
self.done = False
self.possible_actions_given_past_actions= {}
def __str__(self):
return self.current_subgoal
def add_attempted_action(self, action):
past_actions = tuple(self.past_actions)
if past_actions not in self.possible_actions_given_past_actions:
self.possible_actions_given_past_actions[past_actions] = all_actions.copy()
self.possible_actions_given_past_actions[past_actions].remove(action)
def get_available_actions(self):
past_actions = tuple(self.past_actions)
if len(past_actions) > 25: # max out at more than 25 steps
return []
if self.num_subgoals > 4: # max out at more than 4 subgoals
return []
if past_actions not in self.possible_actions_given_past_actions:
self.possible_actions_given_past_actions[past_actions] = all_actions.copy()
return self.possible_actions_given_past_actions[past_actions]
def append_action(self, action):
""" Adds last action """
self.past_actions.append(action)
def remove_last_action(self):
""" Removes last action """
self.past_actions.pop()
def get_copy(self):
"""
Need a copy for indexing in python dictionary
Otherwise the state changes, and the key in the dictionary changes with it, and so it is in the wrong hashbucket
"""
return deepcopy(self)
def __eq__(self, other):
return hash(self) == hash(other)
def __hash__(self):
"""
Hash uses only current_subgoal information (disregards variables, assumptions, and past actions) for qtable
"""
# only use last 5 steps...because that's probably the most relevant
# for what to do next
return hash(self.current_subgoal)