Hi, I've implemented the following Evader Code. I want you to go deeper in my code,…
- User
Hi, I've implemented the following Evader Code. I want you to go deeper in my code, understand fully and after Come with coding standards to update my code to the industry standards . Later I'll provide the code for DQN do the same and also fix the matmul error it raises. Here is my Evader code: import numpy as np import random import math
import rospy import tf import time from rospy.rostime import Time from sensor_msgs.msg import LaserScan from ackermann_msgs.msg import AckermannDrive from nav_msgs.msg import Odometry from DQN import DQNAgentPytorch from reset import reset_car_position
FORWARD = 0 LEFT = 1 RIGHT = 2 REVERSE = 3 ACTION_SPACE = [FORWARD, LEFT, RIGHT, REVERSE]
class EvaderNode: def init(self, agent): rospy.init_node('evader_node', anonymous=True)
rospy.Subscriber('car_1/scan', LaserScan, self.scan_callback) rospy.Subscriber('car_1/base/odom', Odometry, self.odom_callback) self.drive_pub = rospy.Publisher('car_1/command', AckermannDrive, queue_size=20) self.init_location = None self.current_location = None self.goal = (10, 10) self.obstacle_penalty = -250 self.destination_reward = 500 self.distance_threshold = 0.5 self.collision_threshold = 0.2 self.agent = agent self.is_training = True self.init_odom_received = False self.latest_odom_msg = None self.reached_destination = False self.reward = 0 self.angle_range = [(-3 * np.pi / 4, -np.pi / 4), (np.pi / 4, 3 * np.pi / 4)] self.max_steps_per_episode = 100 self.current_episode = 0 self.max_episodes = 10000 def process_scan(self, scan): state = np.array(scan.ranges)[::10] return state def check_collision(self, scan): front_ranges = [] rear_ranges = [] for i, angle in enumerate(np.arange(len(scan.ranges)) * scan.angle_increment + scan.angle_min): if self.angle_range[0][0] <= angle <= self.angle_range[0][1]: front_ranges.append(scan.ranges[i]) elif self.angle_range[1][0] <= angle <= self.angle_range[1][1]: rear_ranges.append(scan.ranges[i]) # Check for obstacles in the front section min_front_range = min(front_ranges) if min_front_range < self.collision_threshold: # Obstacle detected in the front return True else: # Check for obstacles in the rear section min_rear_range = min(rear_ranges) if min_rear_range < self.collision_threshold: # Obstacle detected in the rear return True return False def action_to_drive_msg(self, action): drive_msg = AckermannDrive() if action == FORWARD: drive_msg.speed = 2.0 drive_msg.steering_angle = 0.0 elif action == LEFT: drive_msg.speed = 2.0 drive_msg.steering_angle = float(random.uniform(math.pi / 4, math.pi / 2)) elif action == RIGHT: drive_msg.speed = 2.0 drive_msg.steering_angle = float(random.uniform(-math.pi / 2, -math.pi / 4)) elif action == REVERSE: drive_msg.speed = -2.0 drive_msg.steering_angle = 0.0 return drive_msg def calculate_reward(self, collision_detected, done): reward = 0 if done: reward += self.destination_reward return reward if collision_detected: reward += self.obstacle_penalty return reward current_dist_diff = np.linalg.norm(np.array(self.goal) - np.array(self.current_location)[:2]) init_dist_diff = np.linalg.norm(np.array(self.goal) - np.array(self.init_location)[:2]) if current_dist_diff < init_dist_diff: reward += 100 else: reward -= 100 return reward def update_reward(self, collision_detected, done): self.reward += self.calculate_reward(collision_detected, done) def scan_callback(self, scan): state = self.process_scan(scan) scan_timestamp = scan.header.stamp odom_timestamp = self.latest_odom_msg.header.stamp print(f'Scan Timestamp: {scan_timestamp.to_sec()} Odometry Message Timestamp: {odom_timestamp.to_sec()}') time_diff = scan_timestamp - odom_timestamp time_diff_sec = time_diff.to_sec() # Adjust the time threshold as per your requirement time_threshold = 10 if abs(time_diff_sec) > time_threshold: # Adjust the threshold as per your requirement print("LaserScan and Odometry messages are not in sync!") return else: print('LaserScan and Odometry messages are in sync!') while self.current_episode <= self.max_episodes: distance_to_goal = np.linalg.norm(np.array(self.goal) - np.array(self.current_location)[:2]) print('Distance to reach goal:', distance_to_goal) # if distance_to_goal >= 20: # self.reward = 0 # reset_car_position() state = np.concatenate((state, [self.current_location[2]], [distance_to_goal])) action = self.agent.choose_action(state, test=not self.is_training) collision_detected = self.check_collision(scan) if collision_detected and not self.reached_destination: print('Collision Detected') drive_msg = self.action_to_drive_msg(action) print(f'Action chosen: {action}, Yaw: {drive_msg.steering_angle}') self.drive_pub.publish(drive_msg) done = False # if self.current_episode >self.max_episodes: if self.is_training : print("Current Episode: ", self.current_episode, 'Episode Left:',self.max_episodes -self.current_episode) self.agent.add_experience(state, action, self.reward, state, done) self.agent.train(32) # Set the batch size here self.agent.update_epsilon(self.agent.epsilon * 0.995) if distance_to_goal < self.distance_threshold: done = True drive_msg.speed = 0.0 self.drive_pub.publish(drive_msg) print("----Goal Reached----") # reset_car_position() self.reward = 0 rospy.sleep(1) self.current_episode +=1 # else: # reset_car_position() # self.agent.load_weights('model_weights.pth') # action = self.agent.choose_action(state, test=True) # print(action) print(f"Reward before taking the action {action}: {self.reward}") self.update_reward(collision_detected, done) print(f"Reward after taking the action {action}: {self.reward}") # self.current_episode = 0 # self.agent.save_weights('model_weights.pth') # self.is_training = False # rospy.signal_shutdown('Exit condition met') # return def update_current_location(self, odom_msg): position = odom_msg.pose.pose.position orientation = odom_msg.pose.pose.orientation euler_angles = tf.transformations.euler_from_quaternion( (orientation.x, orientation.y, orientation.z, orientation.w)) self.current_location = (position.x, position.y, euler_angles[2]) # (x, y, yaw) print("Current Location of the Car:", self.current_location[:2]) def odom_callback(self, odom_msg): if not self.init_odom_received: self.init_location = (odom_msg.pose.pose.position.x, odom_msg.pose.pose.position.y) print(f"Initial Location of the Car: {self.init_location}") self.init_odom_received = True self.update_current_location(odom_msg) self.latest_odom_msg = odom_msg - Assistant