forked from proroklab/VectorizedMultiAgentSimulator
-
Notifications
You must be signed in to change notification settings - Fork 0
Rezas expert #3
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
RezaTorbati
wants to merge
13
commits into
main
Choose a base branch
from
RezasExpert
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Rezas expert #3
Changes from all commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
abdda38
Adding everything needed for proof of concept expert
RezaTorbati a85b4b3
Modified the expert to use continuous actions. Its still very not opt…
RezaTorbati 5fb3c6d
Cleaned up the expert code, loaded in the map, and added a bunch more…
RezaTorbati fbefc96
Adds obstacles from the map now, added a readme, and added an example…
RezaTorbati a0f99e2
Converted the ccbs expert to use diff drive instead of holonomic
RezaTorbati 3c8585f
Merge branch 'main' of github.com:GT-STAR-Lab/VectorizedMultiAgentSim…
RezaTorbati 8005434
properly reset environment with obstacles in navigation2
shalin-j ccf927a
update entity filter to include obstacles
shalin-j 413c4d7
add collision penalty for obstacles
shalin-j 387ad3b
Merge branch 'RezasExpert' of github.com:GT-STAR-Lab/VectorizedMultiA…
RezaTorbati df130c1
Think I updated what is needed in vmas for the expert
RezaTorbati cbe2594
saving trajectories now
RezaTorbati dc8c8fa
Simplified the format of the expert trajectories
RezaTorbati File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,244 @@ | ||
| ''' | ||
| This is a proof of concept expert. | ||
| The goal of this is to act as a template on how to create the real expert once the policy architecture is created | ||
| This is not meant to be the final code that we are using | ||
| ''' | ||
|
|
||
| import argparse | ||
| import torch | ||
| from torch.nn import ModuleDict | ||
| import time | ||
| import os | ||
| import xml.etree.ElementTree as ET | ||
| from PIL import Image | ||
| from vmas import make_env | ||
| import numpy as np | ||
| import torch | ||
| from tensordict import TensorDict | ||
| import pickle | ||
|
|
||
| stepsPerTimeInterval = 20 #This still assumes constant velocity... Needs to be large enough so agents actually reach their goal before moving on | ||
|
|
||
| def load_map(map_file, device): | ||
| #Load in map | ||
| f = os.path.dirname(__file__) | ||
| map = ET.parse(f'{f}/{map_file}') | ||
| map_root = map.getroot() | ||
|
|
||
| # Extract grid dimensions | ||
| width = int(map_root.find('.//width').text) | ||
| height = int(map_root.find('.//height').text) | ||
|
|
||
| # Initialize a numpy array to store the grid | ||
| grid = torch.zeros((height, width), dtype=torch.int, device=device) | ||
|
|
||
| # Find all row elements and populate the numpy array | ||
| row_elements = map_root.findall('.//row') | ||
| for i, row_element in enumerate(row_elements): | ||
| row_data = row_element.text.split() | ||
| for j, value in enumerate(row_data): | ||
| grid[height - 1 - i, j] = int(value) | ||
| return grid, width, height | ||
|
|
||
| def load_episodes(log_dir, device, width, height): | ||
| f = os.path.dirname(__file__) | ||
| episodes = {} | ||
| for file in os.listdir(f'{f}/{log_dir}/'): | ||
| if file[-3:] != 'xml': | ||
| continue | ||
|
|
||
| #episode name must be a number! | ||
| #Assumes the files are named <what ever you want>_<id>_log.xml | ||
| episode_name = int(file[:-4].split('_')[-2]) | ||
|
|
||
| agent_paths = {} | ||
| log = ET.parse(f'{f}/{log_dir}/{file}') | ||
| log_root = log.getroot() | ||
| agents = log_root.findall(".//agent[@number]") | ||
| for agent in agents: | ||
| number = int(agent.get('number')) | ||
| waypoints = [] | ||
| path = agent.find('.//path') | ||
| sections = path.findall('.//section') | ||
| first = True | ||
| for waypoint in sections: | ||
| if first: | ||
| y = height - 1 - int(waypoint.get('start_i')) #i is row, j is column... | ||
| x = int(waypoint.get('start_j')) | ||
| duration = 0.0 | ||
| waypoints.append(torch.tensor([duration, x, y], device=device)) | ||
| first=False | ||
| y = height - 1 - int(waypoint.get("goal_i")) | ||
| x = int(waypoint.get('goal_j')) | ||
| duration = float(waypoint.get('duration')) + waypoints[-1][0] | ||
| waypoints.append(torch.tensor([duration, x, y], device=device)) | ||
| agent_paths[number] = waypoints | ||
| episodes[episode_name] = agent_paths.copy() | ||
| return episodes | ||
|
|
||
| def select_action(position, waypoint, steps): | ||
| # Constants | ||
| total_time = .1 * (stepsPerTimeInterval - steps%stepsPerTimeInterval) | ||
| max_linear_velocity = 2.0 # Maximum linear velocity | ||
| max_angular_velocity = torch.pi/3 # Maximum angular velocity | ||
|
|
||
| # Unpack robot position | ||
| x, y, theta = position | ||
|
|
||
| # Unpack desired position | ||
| x_d, y_d = waypoint | ||
|
|
||
| # Calculate the difference in position and angle | ||
| delta_x = x_d - x | ||
| delta_y = y_d - y | ||
| delta_theta = np.arctan2(delta_y, delta_x) - theta | ||
|
|
||
| # Ensure angle difference is within -pi to pi range | ||
| delta_theta = np.arctan2(np.sin(delta_theta), np.cos(delta_theta)) | ||
|
|
||
| # Calculate required linear and angular velocities | ||
| v = np.sqrt(delta_x**2 + delta_y**2) / total_time | ||
| w = delta_theta / total_time | ||
|
|
||
| # Cap linear and angular velocities to maximum values | ||
| v = min(v, max_linear_velocity) | ||
| w = min(w, max_angular_velocity) | ||
| w = max(w, -max_angular_velocity) | ||
| return [v, 0, w] #The second parameter is ignored | ||
|
|
||
| def get_agent_action(episodes, obs, steps): | ||
| scenario_time = steps / stepsPerTimeInterval | ||
| actions = [] | ||
| for i, ob in enumerate(obs): | ||
| env_actions = [] | ||
| for env_ob in ob: | ||
| episode = episodes[int(env_ob[0])] | ||
| x, y, theta = env_ob[1:4] | ||
| appended = False | ||
| for waypoint in episode[i]: | ||
| if waypoint[0] > scenario_time: | ||
| env_actions.append(select_action([x,y,theta],waypoint[1:], steps)) | ||
| appended = True | ||
| break | ||
| if not appended: | ||
| env_actions.append(select_action([x,y,theta],episode[i][-1][1:], steps)) | ||
| actions.append(env_actions) | ||
|
|
||
| return actions | ||
|
|
||
|
|
||
| def main(args): | ||
| device = args.device # or cuda or any other torch device | ||
|
|
||
| #Load in the config to get agent sizes and the time limit | ||
| f = os.path.dirname(__file__) | ||
| config = ET.parse(f'{f}/{args.config}') | ||
| config_root = config.getroot() | ||
| agent_size = float(config_root.find('.//agent_size').text) | ||
|
|
||
| #load map. Currently assumes every task takes place on the same map | ||
| grid_map, width, height = load_map(args.map, device) | ||
|
|
||
| #Load in all of the pre generated episodes | ||
| episodes = load_episodes(args.log_dir, device, width, height) | ||
|
|
||
| scenario_name = 'navigation2' #Scenario name | ||
|
|
||
| # Scenario specific variables | ||
| n_agents = len(episodes[next(iter(episodes))].keys()) #Assumes all episodes has same number of agents | ||
|
|
||
| num_envs = args.num_envs # Number of vectorized environments | ||
| n_steps = args.max_steps # Number of steps before returning done | ||
|
|
||
| start_action = ( | ||
| [0, 0, 0] | ||
| ) # Simple action to start the program | ||
|
|
||
| env = make_env( | ||
| scenario=scenario_name, | ||
| num_envs=num_envs, | ||
| device=device, | ||
| dict_spaces=False, | ||
| wrapper=None, | ||
| seed=None, | ||
| # Environment specific variables | ||
| n_agents=n_agents, | ||
| episodes = episodes, | ||
| map=grid_map, | ||
| agent_radius = agent_size/2 | ||
| ) | ||
|
|
||
| frame_list = [] # For creating a gif | ||
| actions = {} | ||
| obs = [] | ||
| episodes_list = [] | ||
| for e in range(len(episodes)): | ||
| print('Episode:',e) | ||
| step = 0 | ||
| step_list = [] | ||
| for s in range(n_steps): | ||
| step += 1 | ||
|
|
||
| if len(obs) == 0: | ||
| agent_actions = [[start_action] * num_envs]* len(env.agents) | ||
| else: | ||
| agent_actions = get_agent_action(episodes, obs, step) | ||
|
|
||
| # for i, agent in enumerate(env.agents): | ||
| # action = torch.tensor( | ||
| # agent_actions[i], | ||
| # device=device, | ||
| # ) | ||
| # actions.update({agent.name: action}) | ||
|
|
||
| obs, rews, dones, info = env.step(agent_actions) | ||
|
|
||
| if args.save_gif and e == 0: | ||
| frame_list.append( | ||
| Image.fromarray(env.render(mode="rgb_array", agent_index_focus=None)) | ||
| ) | ||
|
|
||
| step_data = TensorDict({ | ||
| 'actions': agent_actions.copy(), | ||
| 'observations': [o[:, 1:] for o in obs], | ||
| 'rewards': rews.copy(), | ||
| 'dones': dones.clone(), | ||
| }, batch_size=[]) | ||
| step_list.append(step_data) | ||
|
|
||
| if dones.all(): | ||
| break | ||
|
|
||
| if args.save_gif and e == 0: | ||
| gif_name = scenario_name + ".gif" | ||
| # Produce a gif | ||
| frame_list[0].save( | ||
| gif_name, | ||
| save_all=True, | ||
| append_images=frame_list[1:], | ||
| duration=3, | ||
| loop=0, | ||
| ) | ||
|
|
||
| episodes_list.append(step_list) | ||
| if e < len(episodes)-1: | ||
| env.reset() | ||
|
|
||
| file_path = f"{args.log_dir}/trajectories.pkl" | ||
| with open(file_path, 'wb') as file: | ||
| pickle.dump(episodes_list, file) | ||
|
|
||
| def create_parser(): | ||
| parser = argparse.ArgumentParser(description='Use the expert to solve the navigation2 scenario') | ||
| parser.add_argument('--map', '-m', default='grid_map.xml', type=str, help='The map to generate tasks for. Only supports grid type maps') | ||
| parser.add_argument('--config', '-c', default='config.xml', help='The config file for the task') | ||
| parser.add_argument('--log_dir', '-l', default='logs', help='local path to logs directory') | ||
| parser.add_argument('--device', '-d', default = 'cpu', help='device to run on') | ||
| parser.add_argument('--num_envs', type=int, default = 1, help='number of environments to run at once') | ||
| parser.add_argument('--save_gif', action="store_true", help='whether or not to save the first episode as a gif') | ||
| parser.add_argument('--max_steps', type=int, default = 500, help='Max number of steps to run for') | ||
| return parser | ||
|
|
||
| if __name__ == "__main__": | ||
| args = create_parser().parse_args() | ||
| main(args) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,13 @@ | ||
| <?xml version="1.0" ?> | ||
| <root> | ||
| <algorithm> | ||
| <use_cardinal>true</use_cardinal> | ||
| <use_disjoint_splitting>true</use_disjoint_splitting> | ||
| <hlh_type>2</hlh_type> | ||
| <connectedness>3</connectedness> | ||
| <focal_weight>1.0</focal_weight> | ||
| <agent_size>0.5</agent_size> | ||
| <timelimit>30</timelimit> | ||
| <precision>0.0000001</precision> | ||
| </algorithm> | ||
| </root> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,19 @@ | ||
| <?xml version="1.0" ?> | ||
| <root> | ||
| <map> | ||
| <width>10</width> | ||
| <height>10</height> | ||
| <grid> | ||
| <row>0 0 0 0 0 0 0 0 0 0</row> | ||
| <row>0 0 0 0 0 0 0 0 0 0</row> | ||
| <row>0 0 0 0 0 0 0 0 0 0</row> | ||
| <row>0 0 0 0 0 0 0 0 0 0</row> | ||
| <row>0 0 0 0 0 0 0 0 0 0</row> | ||
| <row>0 0 0 0 0 0 0 0 0 0</row> | ||
| <row>0 0 0 0 0 0 0 0 0 0</row> | ||
| <row>0 0 0 0 0 0 0 0 0 0</row> | ||
| <row>0 0 0 0 0 0 0 0 0 0</row> | ||
| <row>0 0 0 0 0 0 0 0 0 0</row> | ||
| </grid> | ||
| </map> | ||
| </root> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,19 @@ | ||
| <?xml version="1.0" ?> | ||
| <root> | ||
| <map> | ||
| <width>10</width> | ||
| <height>10</height> | ||
| <grid> | ||
| <row>0 1 0 0 0 0 0 0 0 0</row> | ||
| <row>0 1 0 0 0 0 0 0 0 0</row> | ||
| <row>0 1 0 0 0 0 0 0 0 0</row> | ||
| <row>0 1 0 0 0 0 0 0 0 0</row> | ||
| <row>0 1 0 0 0 0 0 0 0 0</row> | ||
| <row>0 1 0 0 0 0 0 0 0 0</row> | ||
| <row>0 1 0 0 0 0 0 0 0 0</row> | ||
| <row>0 1 0 0 0 0 0 0 0 0</row> | ||
| <row>0 1 0 0 0 0 0 0 1 1</row> | ||
| <row>0 0 0 0 0 0 0 0 0 0</row> | ||
| </grid> | ||
| </map> | ||
| </root> |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This file doesn't appear to actually run the expert algorithm, rather it constructions the agent action from the expert. Do you plan to merge the expert into this file? Maybe we call this file something like navigation_select_actions_from_expert.py
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
How do you anticipate we will integrate these expert demonstrations into the RLPD architecture. I imaging that the files in log_dir will act like our expert dataset, which we will then call to rollout an episode on the environment. Before pushing this to main, I think we should figure out this integration. For example, I think we could create Collector class that does "symmetric sampling", wherein it pulls have the batch from the expert and half from the policy. See the following link: https://pytorch.org/rl/reference/collectors.html
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I have verified this file runs! Good job