-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathaction_handler.py
More file actions
75 lines (54 loc) · 2.53 KB
/
action_handler.py
File metadata and controls
75 lines (54 loc) · 2.53 KB
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
class action_handler:
#Class to handle the action supported by the environment
def __init__(self, env) -> None:
"""Initilaize action handler parameters
Args:
env (environment): referance ot environment
"""
self.env = env
#Define action handler dictionary for each type of action supported
self.ActionHandlers = {
"LEFT" : self.ActionMoveLeft,
"RIGHT" : self.ActionMoveRight,
"UP" : self.ActionMoveUp,
"DOWN" : self.ActionMoveDown,
"UP_LEFT" : self.ActionMoveUpLeft,
"UP_RIGHT" : self.ActionMoveUpRight,
"DOWN_RIGHT" : self.ActionMoveDownLeft,
"DOWN_LEFT" : self.ActionMoveDownRight,
}
def PerformAction(self, parent_node, action):
"""Compute the next state and estimate the total cost for the next state
Args:
parent_node (node): node of the
action (string): action label
Returns:
tuple: validity of the action, action cost
"""
agents_postion = parent_node.Node_State
#Simulate agents nect position
simulated_position = (agents_postion[0] + self.env.ActionValues[action][0],
agents_postion[1] + self.env.ActionValues[action][1])
#Compute the total cost of the action
action_cost = parent_node.Cost_to_Come + self.env.ActionCost[action]
#Check if the computed position os valid
if not self.env.is_valid_position(simulated_position):
return False, None
#return the estimated position and cost
return True, (simulated_position, action_cost)
def ActionMoveLeft(self, parent_node):
return self.PerformAction(parent_node, "LEFT")
def ActionMoveRight(self, parent_node):
return self.PerformAction(parent_node, "RIGHT")
def ActionMoveUp(self, parent_node):
return self.PerformAction(parent_node, "UP")
def ActionMoveDown(self, parent_node):
return self.PerformAction(parent_node, "DOWN")
def ActionMoveUpLeft(self, parent_node):
return self.PerformAction(parent_node, "UP_LEFT")
def ActionMoveUpRight(self, parent_node):
return self.PerformAction(parent_node, "UP_RIGHT")
def ActionMoveDownLeft(self, parent_node):
return self.PerformAction(parent_node, "DOWN_LEFT")
def ActionMoveDownRight(self, parent_node):
return self.PerformAction(parent_node, "DOWN_RIGHT")