Skip to content

Commit

Permalink
Merge branch 'main' into deepsource-autofix-5c035f8a
Browse files Browse the repository at this point in the history
  • Loading branch information
manuandru authored Jan 27, 2024
2 parents 41f7661 + d06de6a commit bbfe601
Show file tree
Hide file tree
Showing 17 changed files with 3,848 additions and 1,893 deletions.
5,275 changes: 3,470 additions & 1,805 deletions log.csv

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion model-gym-ros-env/car_node/car_node/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
class PPOModelEvaluator(Node):

def __init__(self):
super().__init__('wall_follow_node')
super().__init__('car_node')

#Topics & Subs, Pubs
lidarscan_topic = '/scan'
Expand Down
11 changes: 0 additions & 11 deletions model-gym-ros-env/car_node/car_node/wrapper.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import gym
import numpy as np
#from gym import spaces
from gym import spaces
from pathlib import Path
from argparse import Namespace
Expand Down Expand Up @@ -157,11 +156,6 @@ def step(self, action):
np.power((observation["poses_y"][0] - self.last_position['y']), 2)
)

# if distance_from_last_position > 0.0005:
# reward += 0.8
# else:
# reward = 0
# # Update the last position
reward += distance_from_last_position

self.last_position['x'] = observation['poses_x'][0]
Expand Down Expand Up @@ -228,8 +222,6 @@ def reset(self):
self.race_line_y = race_line_y
self.race_line_theta = race_line_theta

# else:
# x, y, t = self.start_position()

self.episode_returns = []

Expand All @@ -243,9 +235,6 @@ def update_map(self, map_name, map_extension, update_render=False):
self.env.map_name = map_name
self.env.map_ext = map_extension
self.env.update_map(f"{map_name}.yaml", map_extension)
# if update_render and self.env.renderer:
# self.env.renderer.close()
# self.env.renderer = None

def seed(self, seed):
self.current_seed = seed
Expand Down
Binary file added models/YasMarina_model.zip
Binary file not shown.
Binary file added models/base_model.zip
Binary file not shown.
11 changes: 10 additions & 1 deletion report/bibliography.bib
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ @misc{OpenAIGym
eprint = {arXiv:1606.01540}
}

@article{ROS2,
@article{Ros2,
author = {Steven Macenski and Tully Foote and Brian Gerkey and Chris Lalancette and William Woodall},
title = {Robot Operating System 2: Design, architecture, and uses in the wild},
journal = {Science Robotics},
Expand All @@ -69,3 +69,12 @@ @article{ROS2
doi = {10.1126/scirobotics.abm6074},
url = {https://www.science.org/doi/abs/10.1126/scirobotics.abm6074}
}

@inproceedings{F1tenthGym,
title = {F1TENTH: An Open-source Evaluation Environment for Continuous Control and Reinforcement Learning},
author = {O’Kelly, Matthew and Zheng, Hongrui and Karthik, Dhruv and Mangharam, Rahul},
booktitle = {NeurIPS 2019 Competition and Demonstration Track},
pages = {77--89},
year = {2020},
organization = {PMLR}
}
Binary file added report/img/ppo.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
266 changes: 243 additions & 23 deletions report/index.tex

Large diffs are not rendered by default.

Binary file modified src/base_model.zip
Binary file not shown.
22 changes: 20 additions & 2 deletions src/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import random
import sys

from train import train, run
from train import train, run, evaluate


base_path = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
Expand All @@ -13,7 +13,8 @@ def get_available_maps():
dir_content = os.listdir(map_path)
maps = []
for item in dir_content:
if os.path.isdir(os.path.join(map_path, item)):
if os.path.isdir(os.path.join(map_path, item)) and \
os.path.exists(os.path.join(map_path, item, item + "_raceline.csv")):
maps.append(item)
return maps

Expand All @@ -39,12 +40,18 @@ def check_map(map_name):
train_parser.add_argument('--min-learning-rate', type=float, help='Learning rate for the model. Default: 0.0005', default=0.0005)
train_parser.add_argument('--max-learning-rate', type=float, help='Learning rate for the model. Default: 0.0001', default=0.0001)
train_parser.add_argument('--num-of-steps', type=int, help='Number of steps to train the model. Default: 10', default=10)
train_parser.add_argument('--num-of-sub-steps', type=int, help='Number of steps for each traing cicle. Default: 10', default=10)
train_parser.add_argument('--optimize-speed', action='store_true', help='Save the model to the specified path. Default: None')

run_parser = subparser.add_parser('run', help='Run the model in the simulator')
run_parser.add_argument('-m', '--map', type=str, help='Map to train the model on. Default: random maps', default='random')
run_parser.add_argument('--timesteps', type=float, help='Number of timesteps to train the model. Default: 0.01', default=0.01)

evaluate_parser = subparser.add_parser('evaluate', help='Evaluate the model')
evaluate_parser.add_argument('-m', '--map', type=str, help='Map to train the model on. Default: random maps', default='random')
evaluate_parser.add_argument('--timesteps', type=float, help='Number of timesteps to train the model. Default: 0.01', default=0.01)
evaluate_parser.add_argument('--n-evaluate', type=int, help='Number of timesteps to train the model. Default: 20', default=20)

args = parser.parse_args()

available_maps = get_available_maps()
Expand All @@ -65,6 +72,7 @@ def check_map(map_name):
args.min_learning_rate,
args.max_learning_rate,
args.num_of_steps,
args.num_of_sub_steps,
args.optimize_speed)
sys.exit(0)
elif args.command == 'run':
Expand All @@ -74,6 +82,16 @@ def check_map(map_name):
check_map(args.map)
run(args.map, args.timesteps)
sys.exit(0)
elif args.command == 'evaluate':
print("Evaluating the model")
if 'random' not in args.map:
check_map(args.map)
evaluate(
args.map == 'random',
random.choice(available_maps) if args.map == 'random' else args.map,
args.timesteps,
args.n_evaluate)
sys.exit(0)
else:
print("Invalid command")
sys.exit(1)
23 changes: 21 additions & 2 deletions src/train.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ def train(
min_learning_rate: float,
max_learning_rate: float,
num_of_steps: int,
substeps: int,
optimize_speed: bool) -> None:

tensorboard_path = './train_test/'
Expand All @@ -28,7 +29,7 @@ def train(
path = get_map(map)
map_path = get_formatted_map(path)
eval_env = gym.make('f110_gym:f110-v0', map=map_path, map_ext=map_ext, num_agents=1, timestep=timestep, integrator=Integrator.RK4)
eval_env = F110_Wrapped(eval_env, random_map=True)
eval_env = F110_Wrapped(eval_env, random_map=random_map)
eval_env.set_map_path(path)
eval_env.seed(1773449316)
eval_env.set_optimize_speed(optimize_speed)
Expand Down Expand Up @@ -67,7 +68,7 @@ def train(
model.save("./src/base_model")

eval_callback = EvalCallback(eval_env, best_model_save_path='./train_test/',
log_path='./train_test/', eval_freq= int(timesteps/20),
log_path='./train_test/', eval_freq= int(timesteps/substeps),
deterministic=True, render=False)

model.learn(total_timesteps=timesteps, progress_bar=True, callback=eval_callback)
Expand All @@ -94,6 +95,24 @@ def train(

del model

def evaluate(random_map: bool, map: str, timestep: int, n_evaluate: int) -> None:
device = 'cpu'

if random_map:
map = "BrandsHatch"
path = get_map(map)
map_path = get_formatted_map(path)

eval_env = gym.make('f110_gym:f110-v0', map=map_path, map_ext=map_ext, num_agents=1, timestep=timestep, integrator=Integrator.RK4)
eval_env = F110_Wrapped(eval_env, random_map=random_map)
eval_env.set_map_path(path)

model = PPO.load("./train_test/best_global_model", eval_env, device=device)

mean_reward, std_reward = evaluate_policy(model, model.get_env(), n_eval_episodes=n_evaluate)

print(mean_reward)
print(std_reward)

def run(map: str, timestep: float) -> None:

Expand Down
Loading

0 comments on commit bbfe601

Please sign in to comment.