-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathaction_handler.py
More file actions
66 lines (46 loc) · 2.07 KB
/
action_handler.py
File metadata and controls
66 lines (46 loc) · 2.07 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
import math
import time
import numpy as np
class action_handler:
#Class to handle the action supported by the environment
def __init__(self, env, step_size, start_pose, goal_pose) -> None:
"""Initilaize action handler parameters
Args:
env (environment): referance ot environment
"""
self.env = env
self.start_pose = start_pose
self.goal_pose = goal_pose
self.step_size = step_size
#define al the actions possible in the environment
self.Actions = {-60, -30, 0, 30, 60}
def ActionValues(self, action, pose):
x, y, theta = pose
theta_new = (theta + action) % 360
theta_new_rad = (theta_new)*math.pi/180
x_new = x + self.step_size * math.cos( theta_new_rad )
y_new = y + self.step_size * math.sin( theta_new_rad )
return (round(x_new), round(y_new), theta_new)
def cost_norm(self, pose1, pose2):
x1, y1, theta1 = pose1
x2, y2, theta2 = pose2
return np.linalg.norm([x1 - x2, y1 - y2])
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_pose = parent_node.Node_State
#Simulate agents nect position
simulated_position = self.ActionValues(action, agents_pose)
#Compute the total cost of the action
action_cost_to_come = parent_node.Cost_to_Come + self.cost_norm(agents_pose, simulated_position)
action_cost_to_go = self.cost_norm(simulated_position, self.goal_pose)#+abs(agents_pose[2] - simulated_position[2])
#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_to_come, action_cost_to_go)