-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathagent.py
executable file
·52 lines (43 loc) · 1.69 KB
/
agent.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
import math
class Agent:
def __init__(self, x, y, theta, color, noise_linear, noise_angular, noise_measurement):
self.pose_x = x
self.pose_y = y
self.pose_theta = theta
self.color = color
self.weight = 0.0
self.noise_linear = noise_linear
self.noise_angular = noise_angular
self.noise_measurement = noise_measurement
#this function moves the agent
def move(self, dist, rot, width, height):
angle = self.pose_theta + self.noise_angular
angle = self.normalize_angle_radians(angle)
#compute new poses
new_pose_x = self.pose_x + (dist * math.cos(angle) + self.noise_linear)
new_pose_y = self.pose_y + (dist * math.sin(angle) + self.noise_linear)
#check boundaries
if new_pose_x > width:
new_pose_x = 0
elif new_pose_x < 0:
new_pose_x = width
if new_pose_y > height:
new_pose_y = 0
elif new_pose_y < 0:
new_pose_y = height
#update agent's pose
self.pose_x = new_pose_x
self.pose_y = new_pose_y
self.pose_theta += rot
def observe(self, landmarks):
distances = []
for landmark in landmarks:
land_x, land_y = landmark
distances.append((math.sqrt((land_x - self.pose_x)**2 + (land_y - self.pose_y)**2) + self.noise_measurement))
return distances
def normalize_angle_radians(self, angle):
# Ensure the angle is within the range [-180, 180]
normalized_angle = angle % (2 * math.pi)
if normalized_angle > math.pi:
normalized_angle -= 2 * math.pi
return normalized_angle