Hi, I've implemented the following for autonomous robot navigation in maze like env. In…
- User
Hi, I've implemented the following for autonomous robot navigation in maze like env. In our case it's car with few obstacles in place. I want you to implement the following. Sometimes if Jersey barrier is located a bit vertical or slant , our car is hitting the obstacle. So, If it somehow hits an obstacle, our car should reverse certain distance to avoid that obstacle to be set on path again. What I want you to do is to mark our initial location and the point to reach in the grid( choose any random point). After travelling either by hitting objects or avoiding it should reach the final destination. After reaching destination, I want you to reset the car's location to the initial position And Now using the learnt policy I want our car to go as quickly as possible to the same final destination avoiding each and every single obstacle. So, to achieve this I want you to first modify try block to train our agent and to save the weights and after to load the weights and selecting actions from the learnt policy to reach the same final path . I'm here to help you. Feel free to come up with new ideas and modifications. You can also suggest some great rewards I can use for this task. Feel free to make any kind of modification to our code to make it work better, efficient and optimize : #! /usr/bin/env python3
import rospy import random import numpy as np import math from collections import deque from sensor_msgs.msg import LaserScan from ackermann_msgs.msg import AckermannDrive import torch import torch.nn as nn import torch.optim as optim
FORWARD = 0 LEFT = 1 RIGHT = 2 REVERSE = 3 ACTION_SPACE = [FORWARD, LEFT, RIGHT, REVERSE]
DISTANCE_THRESHOLD = 2 # meters
class DQNAgentPytorch: def init(self, action_space, observation_space, memory_size=10000, epsilon=1.0, gamma=0.99, learning_rate=0.001): self.observation_space = observation_space self.action_space = action_space self.memory = deque(maxlen=memory_size) self.epsilon = epsilon self.gamma = gamma self.lr = learning_rate self.model = self.build_model() self.target_model = self.build_model() self.optimizer = optim.AdamW(self.model.parameters(), lr=self.lr, amsgrad= True) self.criterion = nn.SmoothL1Loss() self.update_target_net()
def build_model(self): model = nn.Sequential( nn.Linear(self.observation_space.shape[0], 128), nn.ReLU(), nn.Linear(128, 128), nn.ReLU(), nn.Linear(128, len(self.action_space)) ) return model def add_experience(self, state, action, reward, next_state, done): self.memory.append((state, action, reward, next_state, done)) def choose_action(self, state, test=False): if test or random.random() > self.epsilon: with torch.no_grad(): state_t = torch.tensor(state, dtype=torch.float32).unsqueeze(0) q_values = self.model(state_t) return torch.argmax(q_values).item() else: return np.random.choice(self.action_space) def train(self, batch_size): if len(self.memory) < batch_size: return minibatch = random.sample(self.memory, batch_size) for state, action, reward, next_state, done in minibatch: target = self.target_model(torch.tensor(state, dtype=torch.float32).unsqueeze(0)).squeeze(0) if done: target[action] = reward else: next_q_values = self.target_model(torch.tensor(next_state, dtype=torch.float32).unsqueeze(0)).squeeze(0) target[action] = reward + self.gamma * torch.max(next_q_values) inputs = torch.tensor(state, dtype=torch.float32).unsqueeze(0) targets = target.unsqueeze(0) self.optimizer.zero_grad() outputs = self.model(inputs) loss = self.criterion(outputs, targets) loss.backward() # In-place gradient clipping torch.nn.utils.clip_grad_value_(self.model.parameters(), 100) self.optimizer.step() def update_target_net(self): self.target_model.load_state_dict(self.model.state_dict()) def update_epsilon(self, new_epsilon): self.epsilon = new_epsilon def save_weights(self, file_name): torch.save(self.model.state_dict(), file_name) def load_weights(self, file_name): self.epsilon = 0 self.model.load_state_dict(torch.load(file_name)) def calculate_reward(self, state, action, min_distance): reward = 0 if action == REVERSE: reward -= 1 # decrease the reward for reversing else: reward += max(0, (min_distance - DISTANCE_THRESHOLD) / DISTANCE_THRESHOLD) # increase the reward for avoiding obstacles return reward - Assistant
- User
I want to mention few key points. First, The car publishes it’s odometry on the topic
car_1/base/odomusing an Odometry message(nav_msgs/odometry). You can get the global position and orientation of the car from this message. Orientations are represented using Quaternions and not Euler angles. ROS provides functions to convert between them. Based on this complete the update_current_location method. You can also set the final and intial points based on the given info. Second in try block , You haven't included steps to reset the env and start the train and after finishing training to set self.is_training to False. The rostopic echo at odom gives the following data : --- header: seq: 808 stamp: secs: 1449 nsecs: 461000000 frame_id: "odom" child_frame_id: "car_1_base_link" pose: pose: position: x: -4.37179780762853e-06 y: 1.6086410895432314e-07 z: -6.572433092974506e-07 orientation: x: -5.144695084007669e-09 y: 1.0286761457205243e-06 z: -7.232093441762001e-07 w: 0.9999999999992095 covariance: [0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0] twist: twist: linear: x: 4.14702798888639e-08 y: -3.0176029053610697e-07 z: 6.488728869591122e-06 angular: x: 7.503732170334387e-07 y: -6.303856343325667e-06 z: 1.4363966406954678e-07 covariance: [0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0] - Assistant