From d6dd0abd79922c233d3550d6d2bd104bc17113e8 Mon Sep 17 00:00:00 2001 From: kuhyx <147418882+kuhyx@users.noreply.github.com> Date: Fri, 4 Oct 2024 14:22:34 +0200 Subject: [PATCH 01/18] Initial commit --- .gitignore | 162 +++++++++++++++++++++++++++++++++++++++++++++++++++++ LICENSE | 21 +++++++ README.md | 1 + 3 files changed, 184 insertions(+) create mode 100644 .gitignore create mode 100644 LICENSE create mode 100644 README.md diff --git a/.gitignore b/.gitignore new file mode 100644 index 00000000..82f92755 --- /dev/null +++ b/.gitignore @@ -0,0 +1,162 @@ +# Byte-compiled / optimized / DLL files +__pycache__/ +*.py[cod] +*$py.class + +# C extensions +*.so + +# Distribution / packaging +.Python +build/ +develop-eggs/ +dist/ +downloads/ +eggs/ +.eggs/ +lib/ +lib64/ +parts/ +sdist/ +var/ +wheels/ +share/python-wheels/ +*.egg-info/ +.installed.cfg +*.egg +MANIFEST + +# PyInstaller +# Usually these files are written by a python script from a template +# before PyInstaller builds the exe, so as to inject date/other infos into it. +*.manifest +*.spec + +# Installer logs +pip-log.txt +pip-delete-this-directory.txt + +# Unit test / coverage reports +htmlcov/ +.tox/ +.nox/ +.coverage +.coverage.* +.cache +nosetests.xml +coverage.xml +*.cover +*.py,cover +.hypothesis/ +.pytest_cache/ +cover/ + +# Translations +*.mo +*.pot + +# Django stuff: +*.log +local_settings.py +db.sqlite3 +db.sqlite3-journal + +# Flask stuff: +instance/ +.webassets-cache + +# Scrapy stuff: +.scrapy + +# Sphinx documentation +docs/_build/ + +# PyBuilder +.pybuilder/ +target/ + +# Jupyter Notebook +.ipynb_checkpoints + +# IPython +profile_default/ +ipython_config.py + +# pyenv +# For a library or package, you might want to ignore these files since the code is +# intended to run in multiple environments; otherwise, check them in: +# .python-version + +# pipenv +# According to pypa/pipenv#598, it is recommended to include Pipfile.lock in version control. +# However, in case of collaboration, if having platform-specific dependencies or dependencies +# having no cross-platform support, pipenv may install dependencies that don't work, or not +# install all needed dependencies. +#Pipfile.lock + +# poetry +# Similar to Pipfile.lock, it is generally recommended to include poetry.lock in version control. +# This is especially recommended for binary packages to ensure reproducibility, and is more +# commonly ignored for libraries. +# https://python-poetry.org/docs/basic-usage/#commit-your-poetrylock-file-to-version-control +#poetry.lock + +# pdm +# Similar to Pipfile.lock, it is generally recommended to include pdm.lock in version control. +#pdm.lock +# pdm stores project-wide configurations in .pdm.toml, but it is recommended to not include it +# in version control. +# https://pdm.fming.dev/latest/usage/project/#working-with-version-control +.pdm.toml +.pdm-python +.pdm-build/ + +# PEP 582; used by e.g. github.com/David-OConnor/pyflow and github.com/pdm-project/pdm +__pypackages__/ + +# Celery stuff +celerybeat-schedule +celerybeat.pid + +# SageMath parsed files +*.sage.py + +# Environments +.env +.venv +env/ +venv/ +ENV/ +env.bak/ +venv.bak/ + +# Spyder project settings +.spyderproject +.spyproject + +# Rope project settings +.ropeproject + +# mkdocs documentation +/site + +# mypy +.mypy_cache/ +.dmypy.json +dmypy.json + +# Pyre type checker +.pyre/ + +# pytype static type analyzer +.pytype/ + +# Cython debug symbols +cython_debug/ + +# PyCharm +# JetBrains specific template is maintained in a separate JetBrains.gitignore that can +# be found at https://github.com/github/gitignore/blob/main/Global/JetBrains.gitignore +# and can be added to the global gitignore or merged into this file. For a more nuclear +# option (not recommended) you can uncomment the following to ignore the entire idea folder. +#.idea/ diff --git a/LICENSE b/LICENSE new file mode 100644 index 00000000..6dabd439 --- /dev/null +++ b/LICENSE @@ -0,0 +1,21 @@ +MIT License + +Copyright (c) 2024 kuhyx + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. diff --git a/README.md b/README.md new file mode 100644 index 00000000..980e0a93 --- /dev/null +++ b/README.md @@ -0,0 +1 @@ +# USD \ No newline at end of file From 07e68108ec6eb4df1b0e84b84048db8ccddd2b4b Mon Sep 17 00:00:00 2001 From: KRZYSZTOF RUDNICKI Date: Fri, 4 Oct 2024 14:31:22 +0200 Subject: [PATCH 02/18] feat: hello world open ai gymnasium --- .gitignore | 1 + README.md | 6 +++++- main.py | 11 +++++++++++ requirements.txt | 2 ++ 4 files changed, 19 insertions(+), 1 deletion(-) create mode 100644 main.py create mode 100644 requirements.txt diff --git a/.gitignore b/.gitignore index 82f92755..0417715d 100644 --- a/.gitignore +++ b/.gitignore @@ -160,3 +160,4 @@ cython_debug/ # and can be added to the global gitignore or merged into this file. For a more nuclear # option (not recommended) you can uncomment the following to ignore the entire idea folder. #.idea/ +lectures \ No newline at end of file diff --git a/README.md b/README.md index 980e0a93..631e4e82 100644 --- a/README.md +++ b/README.md @@ -1 +1,5 @@ -# USD \ No newline at end of file +# USD + +Install swig, python and pip +Install libraries required by program: +`pip install -r requirements.txt` \ No newline at end of file diff --git a/main.py b/main.py new file mode 100644 index 00000000..a724c66f --- /dev/null +++ b/main.py @@ -0,0 +1,11 @@ +import gymnasium as gym +env = gym.make("LunarLander-v2", render_mode="human") +observation, info = env.reset(seed=42) +for _ in range(1000): + action = env.action_space.sample() # this is where you would insert your policy + observation, reward, terminated, truncated, info = env.step(action) + + if terminated or truncated: + observation, info = env.reset() + +env.close() \ No newline at end of file diff --git a/requirements.txt b/requirements.txt new file mode 100644 index 00000000..426012ee --- /dev/null +++ b/requirements.txt @@ -0,0 +1,2 @@ +gymnasium +gymnasium[box2d] \ No newline at end of file From ca440c6be0bc51577c34fea02ff0c7cc3cfe19c9 Mon Sep 17 00:00:00 2001 From: Krzysztof Rudnicki Date: Sun, 27 Oct 2024 20:19:56 +0100 Subject: [PATCH 03/18] chore: changed required libraries to reflect assignment --- requirements.txt | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/requirements.txt b/requirements.txt index 426012ee..c60d1b47 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,2 +1 @@ -gymnasium -gymnasium[box2d] \ No newline at end of file +metadrive-simulator \ No newline at end of file From 0b954cb1eab3fd82026cc39b8c66887178936440 Mon Sep 17 00:00:00 2001 From: Krzysztof Rudnicki Date: Sun, 27 Oct 2024 20:21:37 +0100 Subject: [PATCH 04/18] chore: updated readme with assignemnt description --- README.md | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 631e4e82..1965c85e 100644 --- a/README.md +++ b/README.md @@ -1,5 +1,13 @@ # USD +## Task: +Zapoznaj się z [MetaDrive](https://github.com/metadriverse/metadrive/). Wytrenuj co najmniej dwóch różnych agentów +wykorzystując algorytmy **wieloagentowe** (MA), na co +najmniej trzech różnych mapach. Omów otrzymane wyniki oraz zwizualizuj działanie +wytrenowanych agentów. + +## How to run: Install swig, python and pip Install libraries required by program: -`pip install -r requirements.txt` \ No newline at end of file +`pip install -r requirements.txt` + From ee59f88e4c8b2f6d8be288a24065216b210f153c Mon Sep 17 00:00:00 2001 From: Krzysztof Rudnicki Date: Sun, 27 Oct 2024 20:27:56 +0100 Subject: [PATCH 05/18] chore: metadrivee --- main.py | 23 ++++++++++++++--------- requirements.txt | 4 ++++ 2 files changed, 18 insertions(+), 9 deletions(-) diff --git a/main.py b/main.py index a724c66f..e7c3feec 100644 --- a/main.py +++ b/main.py @@ -1,11 +1,16 @@ -import gymnasium as gym -env = gym.make("LunarLander-v2", render_mode="human") -observation, info = env.reset(seed=42) -for _ in range(1000): - action = env.action_space.sample() # this is where you would insert your policy - observation, reward, terminated, truncated, info = env.step(action) +import metadrive - if terminated or truncated: - observation, info = env.reset() +# Initialize the MetaDrive environment +env = metadrive.MetaDriveEnv() -env.close() \ No newline at end of file +# Reset the environment to start a new episode +env.reset() + +# Run the environment for a few steps +for _ in range(10): + env.step(env.action_space.sample()) # Take random actions + +print("Hello, MetaDrive World!") # Print a greeting + +# Close the environment after use +env.close() diff --git a/requirements.txt b/requirements.txt index c60d1b47..4ffb0465 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1 +1,5 @@ +metadrive +ray +rlib +ray[rllib] metadrive-simulator \ No newline at end of file From 69d3df40eab2a885a55d24c054ef1ad260118ba6 Mon Sep 17 00:00:00 2001 From: Krzysztof Rudnicki Date: Sun, 27 Oct 2024 20:38:11 +0100 Subject: [PATCH 06/18] chore: split repo into code and report parth --- .gitignore | 309 +++++++++++++++++++- main.py | 16 - report/report.tex | 5 + script/main.py | 50 ++++ requirements.txt => script/requirements.txt | 0 5 files changed, 363 insertions(+), 17 deletions(-) delete mode 100644 main.py create mode 100644 report/report.tex create mode 100644 script/main.py rename requirements.txt => script/requirements.txt (100%) diff --git a/.gitignore b/.gitignore index 0417715d..c771340a 100644 --- a/.gitignore +++ b/.gitignore @@ -160,4 +160,311 @@ cython_debug/ # and can be added to the global gitignore or merged into this file. For a more nuclear # option (not recommended) you can uncomment the following to ignore the entire idea folder. #.idea/ -lectures \ No newline at end of file +lectures + +## Core latex/pdflatex auxiliary files: +*.aux +*.lof +*.log +*.lot +*.fls +*.out +*.toc +*.fmt +*.fot +*.cb +*.cb2 +.*.lb + +## Intermediate documents: +*.dvi +*.xdv +*-converted-to.* +# these rules might exclude image files for figures etc. +# *.ps +# *.eps +# *.pdf + +## Generated if empty string is given at "Please type another file name for output:" +.pdf + +## Bibliography auxiliary files (bibtex/biblatex/biber): +*.bbl +*.bcf +*.blg +*-blx.aux +*-blx.bib +*.run.xml + +## Build tool auxiliary files: +*.fdb_latexmk +*.synctex +*.synctex(busy) +*.synctex.gz +*.synctex.gz(busy) +*.pdfsync +*.rubbercache +rubber.cache + +## Build tool directories for auxiliary files +# latexrun +latex.out/ + +## Auxiliary and intermediate files from other packages: +# algorithms +*.alg +*.loa + +# achemso +acs-*.bib + +# amsthm +*.thm + +# beamer +*.nav +*.pre +*.snm +*.vrb + +# changes +*.soc + +# comment +*.cut + +# cprotect +*.cpt + +# elsarticle (documentclass of Elsevier journals) +*.spl + +# endnotes +*.ent + +# fixme +*.lox + +# feynmf/feynmp +*.mf +*.mp +*.t[1-9] +*.t[1-9][0-9] +*.tfm + +#(r)(e)ledmac/(r)(e)ledpar +*.end +*.?end +*.[1-9] +*.[1-9][0-9] +*.[1-9][0-9][0-9] +*.[1-9]R +*.[1-9][0-9]R +*.[1-9][0-9][0-9]R +*.eledsec[1-9] +*.eledsec[1-9]R +*.eledsec[1-9][0-9] +*.eledsec[1-9][0-9]R +*.eledsec[1-9][0-9][0-9] +*.eledsec[1-9][0-9][0-9]R + +# glossaries +*.acn +*.acr +*.glg +*.glo +*.gls +*.glsdefs +*.lzo +*.lzs +*.slg +*.slo +*.sls + +# uncomment this for glossaries-extra (will ignore makeindex's style files!) +# *.ist + +# gnuplot +*.gnuplot +*.table + +# gnuplottex +*-gnuplottex-* + +# gregoriotex +*.gaux +*.glog +*.gtex + +# htlatex +*.4ct +*.4tc +*.idv +*.lg +*.trc +*.xref + +# hypdoc +*.hd + +# hyperref +*.brf + +# knitr +*-concordance.tex +# TODO Uncomment the next line if you use knitr and want to ignore its generated tikz files +# *.tikz +*-tikzDictionary + +# listings +*.lol + +# luatexja-ruby +*.ltjruby + +# makeidx +*.idx +*.ilg +*.ind + +# minitoc +*.maf +*.mlf +*.mlt +*.mtc[0-9]* +*.slf[0-9]* +*.slt[0-9]* +*.stc[0-9]* + +# minted +_minted* +*.pyg + +# morewrites +*.mw + +# newpax +*.newpax + +# nomencl +*.nlg +*.nlo +*.nls + +# pax +*.pax + +# pdfpcnotes +*.pdfpc + +# sagetex +*.sagetex.sage +*.sagetex.py +*.sagetex.scmd + +# scrwfile +*.wrt + +# svg +svg-inkscape/ + +# sympy +*.sout +*.sympy +sympy-plots-for-*.tex/ + +# pdfcomment +*.upa +*.upb + +# pythontex +*.pytxcode +pythontex-files-*/ + +# tcolorbox +*.listing + +# thmtools +*.loe + +# TikZ & PGF +*.dpth +*.md5 +*.auxlock + +# titletoc +*.ptc + +# todonotes +*.tdo + +# vhistory +*.hst +*.ver + +# easy-todo +*.lod + +# xcolor +*.xcp + +# xmpincl +*.xmpi + +# xindy +*.xdy + +# xypic precompiled matrices and outlines +*.xyc +*.xyd + +# endfloat +*.ttt +*.fff + +# Latexian +TSWLatexianTemp* + +## Editors: +# WinEdt +*.bak +*.sav + +# Texpad +.texpadtmp + +# LyX +*.lyx~ + +# Kile +*.backup + +# gummi +.*.swp + +# KBibTeX +*~[0-9]* + +# TeXnicCenter +*.tps + +# auto folder when using emacs and auctex +./auto/* +*.el + +# expex forward references with \gathertags +*-tags.tex + +# standalone packages +*.sta + +# Makeindex log files +*.lpz + +# xwatermark package +*.xwm + +# REVTeX puts footnotes in the bibliography by default, unless the nofootinbib +# option is specified. Footnotes are the stored in a file with suffix Notes.bib. +# Uncomment the next line to have this generated file ignored. +#*Notes.bib \ No newline at end of file diff --git a/main.py b/main.py deleted file mode 100644 index e7c3feec..00000000 --- a/main.py +++ /dev/null @@ -1,16 +0,0 @@ -import metadrive - -# Initialize the MetaDrive environment -env = metadrive.MetaDriveEnv() - -# Reset the environment to start a new episode -env.reset() - -# Run the environment for a few steps -for _ in range(10): - env.step(env.action_space.sample()) # Take random actions - -print("Hello, MetaDrive World!") # Print a greeting - -# Close the environment after use -env.close() diff --git a/report/report.tex b/report/report.tex new file mode 100644 index 00000000..fffed033 --- /dev/null +++ b/report/report.tex @@ -0,0 +1,5 @@ +\documentclass[12pt]{article} +\begin{document} +Hello world! +$Hello world!$ %math mode +\end{document} \ No newline at end of file diff --git a/script/main.py b/script/main.py new file mode 100644 index 00000000..195653d3 --- /dev/null +++ b/script/main.py @@ -0,0 +1,50 @@ +import ray +from ray import tune +from ray.rllib.agents.ppo import PPOTrainer +from metadrive import MultiAgentTIntersectionEnv +import random + +# Initialize Ray +ray.init(ignore_reinit_error=True) + +# Define a custom environment class that switches between three maps +class MultiMapEnv(MultiAgentTIntersectionEnv): + def __init__(self, config): + # Define available maps + self.maps = ["TIntersection", "Roundabout", "Straight"] + super().__init__(config) + + def reset(self): + # Randomly choose a map from the available ones at the start of each episode + self.config["map"] = random.choice(self.maps) + return super().reset() + +# Multi-agent configuration with two independent policies +config = { + "env": MultiMapEnv, + "env_config": { + "num_agents": 2, # Set to 2 agents for this multi-agent scenario + }, + "framework": "torch", # Use PyTorch as the backend + "num_workers": 1, # Set to 1 worker for simplicity + "multiagent": { + "policies": { + "policy_1": {}, # Configuration for the first agent's policy + "policy_2": {}, # Configuration for the second agent's policy + }, + "policy_mapping_fn": lambda agent_id: "policy_1" if agent_id == "agent_1" else "policy_2", + }, +} + +# Initialize the trainer with PPO algorithm +trainer = PPOTrainer(env=MultiMapEnv, config=config) + +# Training loop +print("Starting training for two agents across multiple maps...") +for i in range(10): # Number of training iterations + result = trainer.train() + print(f"Iteration {i + 1}: reward = {result['episode_reward_mean']}") + +# Clean up resources +trainer.cleanup() +ray.shutdown() diff --git a/requirements.txt b/script/requirements.txt similarity index 100% rename from requirements.txt rename to script/requirements.txt From fe31f46096a05b093fb9e73338d65215f9028a65 Mon Sep 17 00:00:00 2001 From: Krzysztof Rudnicki Date: Sun, 27 Oct 2024 20:38:36 +0100 Subject: [PATCH 07/18] chore: removed license file --- LICENSE | 21 --------------------- 1 file changed, 21 deletions(-) delete mode 100644 LICENSE diff --git a/LICENSE b/LICENSE deleted file mode 100644 index 6dabd439..00000000 --- a/LICENSE +++ /dev/null @@ -1,21 +0,0 @@ -MIT License - -Copyright (c) 2024 kuhyx - -Permission is hereby granted, free of charge, to any person obtaining a copy -of this software and associated documentation files (the "Software"), to deal -in the Software without restriction, including without limitation the rights -to use, copy, modify, merge, publish, distribute, sublicense, and/or sell -copies of the Software, and to permit persons to whom the Software is -furnished to do so, subject to the following conditions: - -The above copyright notice and this permission notice shall be included in all -copies or substantial portions of the Software. - -THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR -IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, -FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE -AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER -LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, -OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE -SOFTWARE. From b93c2bc693cab2cd5981bf4611931967e423d5da Mon Sep 17 00:00:00 2001 From: Krzysztof Rudnicki Date: Sun, 27 Oct 2024 21:14:26 +0100 Subject: [PATCH 08/18] feat: metadrive hello world --- README.md | 14 +++++++++- script/main.py | 58 +++++++---------------------------------- script/requirements.txt | 7 ++--- 3 files changed, 24 insertions(+), 55 deletions(-) diff --git a/README.md b/README.md index 1965c85e..96235e83 100644 --- a/README.md +++ b/README.md @@ -7,7 +7,19 @@ najmniej trzech różnych mapach. Omów otrzymane wyniki oraz zwizualizuj dział wytrenowanych agentów. ## How to run: + +### Install metadrive +``` +pip install metadrive-simulator +python -m metadrive.pull_asset +``` + +#### Verify +`python -m metadrive.examples.profile_metadrive` + Install swig, python and pip -Install libraries required by program: + +Install other libraries required by program: + `pip install -r requirements.txt` diff --git a/script/main.py b/script/main.py index 195653d3..7f9f9aa0 100644 --- a/script/main.py +++ b/script/main.py @@ -1,50 +1,10 @@ -import ray -from ray import tune -from ray.rllib.agents.ppo import PPOTrainer -from metadrive import MultiAgentTIntersectionEnv -import random +from metadrive.envs.metadrive_env import MetaDriveEnv +import gymnasium as gym -# Initialize Ray -ray.init(ignore_reinit_error=True) - -# Define a custom environment class that switches between three maps -class MultiMapEnv(MultiAgentTIntersectionEnv): - def __init__(self, config): - # Define available maps - self.maps = ["TIntersection", "Roundabout", "Straight"] - super().__init__(config) - - def reset(self): - # Randomly choose a map from the available ones at the start of each episode - self.config["map"] = random.choice(self.maps) - return super().reset() - -# Multi-agent configuration with two independent policies -config = { - "env": MultiMapEnv, - "env_config": { - "num_agents": 2, # Set to 2 agents for this multi-agent scenario - }, - "framework": "torch", # Use PyTorch as the backend - "num_workers": 1, # Set to 1 worker for simplicity - "multiagent": { - "policies": { - "policy_1": {}, # Configuration for the first agent's policy - "policy_2": {}, # Configuration for the second agent's policy - }, - "policy_mapping_fn": lambda agent_id: "policy_1" if agent_id == "agent_1" else "policy_2", - }, -} - -# Initialize the trainer with PPO algorithm -trainer = PPOTrainer(env=MultiMapEnv, config=config) - -# Training loop -print("Starting training for two agents across multiple maps...") -for i in range(10): # Number of training iterations - result = trainer.train() - print(f"Iteration {i + 1}: reward = {result['episode_reward_mean']}") - -# Clean up resources -trainer.cleanup() -ray.shutdown() +env = MetaDriveEnv(config={"use_render": True}) +obs, info = env.reset() +for i in range(1000): + obs, reward, terminated, truncated, info = env.step(env.action_space.sample()) + if terminated or truncated: + env.reset() +env.close() \ No newline at end of file diff --git a/script/requirements.txt b/script/requirements.txt index 4ffb0465..f84ab97d 100644 --- a/script/requirements.txt +++ b/script/requirements.txt @@ -1,5 +1,2 @@ -metadrive -ray -rlib -ray[rllib] -metadrive-simulator \ No newline at end of file +metadrive-simulator +gymnasium \ No newline at end of file From 954ef7dbf2d3df5fd1de5228899daf6b20cd481c Mon Sep 17 00:00:00 2001 From: Krzysztof Rudnicki Date: Sun, 27 Oct 2024 21:17:59 +0100 Subject: [PATCH 09/18] feat: made metadrive use gym insstead of gymnasium --- script/main.py | 9 +++++---- script/requirements.txt | 3 ++- 2 files changed, 7 insertions(+), 5 deletions(-) diff --git a/script/main.py b/script/main.py index 7f9f9aa0..4963f322 100644 --- a/script/main.py +++ b/script/main.py @@ -1,10 +1,11 @@ from metadrive.envs.metadrive_env import MetaDriveEnv import gymnasium as gym +from metadrive.envs.gym_wrapper import createGymWrapper # import the wrapper -env = MetaDriveEnv(config={"use_render": True}) -obs, info = env.reset() +env = createGymWrapper(MetaDriveEnv)(config={"use_render": True}) # wrap the environment +obs = env.reset() for i in range(1000): - obs, reward, terminated, truncated, info = env.step(env.action_space.sample()) - if terminated or truncated: + obs, reward, done, info = env.step(env.action_space.sample()) # the return value contains no truncate + if done: env.reset() env.close() \ No newline at end of file diff --git a/script/requirements.txt b/script/requirements.txt index f84ab97d..c6cd7a39 100644 --- a/script/requirements.txt +++ b/script/requirements.txt @@ -1,2 +1,3 @@ metadrive-simulator -gymnasium \ No newline at end of file +gymnasium +gym \ No newline at end of file From d78fa0102a726b8db414256da8ba870a8f448872 Mon Sep 17 00:00:00 2001 From: Krzysztof Rudnicki Date: Sun, 27 Oct 2024 21:28:12 +0100 Subject: [PATCH 10/18] feat: copied and pasteed multi agent env from metadrive examples --- script/main.py | 90 +++++++++++++++++++++++++++++++++++++++++++++----- 1 file changed, 81 insertions(+), 9 deletions(-) diff --git a/script/main.py b/script/main.py index 4963f322..c55214e6 100644 --- a/script/main.py +++ b/script/main.py @@ -1,11 +1,83 @@ -from metadrive.envs.metadrive_env import MetaDriveEnv -import gymnasium as gym -from metadrive.envs.gym_wrapper import createGymWrapper # import the wrapper +#!/usr/bin/env python +""" +This script demonstrates how to setup the Multi-agent RL environments. -env = createGymWrapper(MetaDriveEnv)(config={"use_render": True}) # wrap the environment -obs = env.reset() -for i in range(1000): - obs, reward, done, info = env.step(env.action_space.sample()) # the return value contains no truncate - if done: + +Usage: python -m metadrive.examples.drive_in_multi_agent_env --env pgma + +Options for --env argument: + (1) roundabout + (2) intersection + (3) tollgate + (4) bottleneck + (5) parkinglot + (6) pgma + +""" +import argparse +from metadrive.component.sensors.rgb_camera import RGBCamera +from metadrive import ( + MultiAgentMetaDrive, MultiAgentTollgateEnv, MultiAgentBottleneckEnv, MultiAgentIntersectionEnv, + MultiAgentRoundaboutEnv, MultiAgentParkingLotEnv +) +from metadrive.constants import HELP_MESSAGE +from metadrive.policy.idm_policy import ManualControllableIDMPolicy + +if __name__ == "__main__": + envs = dict( + roundabout=MultiAgentRoundaboutEnv, + intersection=MultiAgentIntersectionEnv, + tollgate=MultiAgentTollgateEnv, + bottleneck=MultiAgentBottleneckEnv, + parkinglot=MultiAgentParkingLotEnv, + pgma=MultiAgentMetaDrive + ) + + parser = argparse.ArgumentParser() + parser.add_argument("--env", type=str, default="roundabout", choices=list(envs.keys())) + parser.add_argument("--top_down", "--topdown", action="store_true") + args = parser.parse_args() + env_cls_name = args.env + extra_args = dict(film_size=(800, 800)) if args.top_down else {} + assert env_cls_name in envs.keys(), "No environment named {}, argument accepted: \n" \ + "(1) roundabout\n" \ + "(2) intersection\n" \ + "(3) tollgate\n" \ + "(4) bottleneck\n" \ + "(5) parkinglot\n" \ + "(6) pgma" \ + .format(env_cls_name) + env = envs[env_cls_name]( + { + "use_render": True if not args.top_down else False, + "crash_done": False, + "sensors": dict(rgb_camera=(RGBCamera, 400, 300)), + "interface_panel": ["rgb_camera", "dashboard"], + "agent_policy": ManualControllableIDMPolicy + } + ) + try: env.reset() -env.close() \ No newline at end of file + # if env.current_track_agent: + # env.current_track_agent.expert_takeover = True + print(HELP_MESSAGE) + env.switch_to_third_person_view() # Default is in Top-down view, we switch to Third-person view. + for i in range(1, 10000000000): + o, r, tm, tc, info = env.step({agent_id: [0, 0] for agent_id in env.agents.keys()}) + env.render( + **extra_args, + mode="top_down" if args.top_down else None, + text={ + "Quit": "ESC", + "Number of existing vehicles": len(env.agents), + "Tracked agent (Press Q)": env.engine.agent_manager.object_to_agent(env.current_track_agent.id), + "Keyboard Control": "W,A,S,D", + # "Auto-Drive (Switch mode: T)": "on" if env.current_track_agent.expert_takeover else "off", + } if not args.top_down else {} + ) + if tm["__all__"]: + env.reset() + # if env.current_track_agent: + # env.current_track_agent.expert_takeover = True + finally: + env.close() \ No newline at end of file From 02a8644f93ffdba0fb0b709c75f62489dae01099 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Piotr=20Kit=C5=82owski?= Date: Fri, 10 Jan 2025 18:04:28 +0100 Subject: [PATCH 11/18] Fixed req --- script/requirements.txt | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/script/requirements.txt b/script/requirements.txt index c6cd7a39..01787135 100644 --- a/script/requirements.txt +++ b/script/requirements.txt @@ -1,3 +1,4 @@ +wheel metadrive-simulator gymnasium -gym \ No newline at end of file +gym From f7c0756f7e3ec08d87ce295097651dba3b8d4d49 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Piotr=20Kit=C5=82owski?= Date: Sat, 25 Jan 2025 17:57:51 +0100 Subject: [PATCH 12/18] modified repo --- CoPO | 1 + README.md | 29 +++++++++----- script/main.py | 83 ----------------------------------------- script/requirements.txt | 4 -- 4 files changed, 21 insertions(+), 96 deletions(-) create mode 160000 CoPO delete mode 100644 script/main.py delete mode 100644 script/requirements.txt diff --git a/CoPO b/CoPO new file mode 160000 index 00000000..86975e92 --- /dev/null +++ b/CoPO @@ -0,0 +1 @@ +Subproject commit 86975e924df9f7d0bee701955228e6855ae3f9ff diff --git a/README.md b/README.md index 96235e83..2d9911e7 100644 --- a/README.md +++ b/README.md @@ -6,20 +6,31 @@ wykorzystując algorytmy **wieloagentowe** (MA), na co najmniej trzech różnych mapach. Omów otrzymane wyniki oraz zwizualizuj działanie wytrenowanych agentów. -## How to run: -### Install metadrive +## Setup ``` -pip install metadrive-simulator -python -m metadrive.pull_asset +conda create -n copo python=3.7 +conda activate copo + +# Install MetaDrive version 0.2.5 +pip install git+https://github.com/metadriverse/metadrive.git@releases/0.2.5 + +pip install torch + +git clone https://github.com/decisionforce/CoPO +cd CoPO/copo_code +pip install -e . + +pip install -U ray==1.2.0 "ray[rllib]==1.2.0" +pip install -U "numpy<1.24.0" +pip uninstall opencv-python +pip uninstall opencv-python-headless +pip install opencv-python==4.5.5.64 +pip install pydantic==1.9.0 ``` -#### Verify -`python -m metadrive.examples.profile_metadrive` +## How to train a RL agents -Install swig, python and pip -Install other libraries required by program: -`pip install -r requirements.txt` diff --git a/script/main.py b/script/main.py deleted file mode 100644 index c55214e6..00000000 --- a/script/main.py +++ /dev/null @@ -1,83 +0,0 @@ -#!/usr/bin/env python -""" -This script demonstrates how to setup the Multi-agent RL environments. - - -Usage: python -m metadrive.examples.drive_in_multi_agent_env --env pgma - -Options for --env argument: - (1) roundabout - (2) intersection - (3) tollgate - (4) bottleneck - (5) parkinglot - (6) pgma - -""" -import argparse -from metadrive.component.sensors.rgb_camera import RGBCamera -from metadrive import ( - MultiAgentMetaDrive, MultiAgentTollgateEnv, MultiAgentBottleneckEnv, MultiAgentIntersectionEnv, - MultiAgentRoundaboutEnv, MultiAgentParkingLotEnv -) -from metadrive.constants import HELP_MESSAGE -from metadrive.policy.idm_policy import ManualControllableIDMPolicy - -if __name__ == "__main__": - envs = dict( - roundabout=MultiAgentRoundaboutEnv, - intersection=MultiAgentIntersectionEnv, - tollgate=MultiAgentTollgateEnv, - bottleneck=MultiAgentBottleneckEnv, - parkinglot=MultiAgentParkingLotEnv, - pgma=MultiAgentMetaDrive - ) - - parser = argparse.ArgumentParser() - parser.add_argument("--env", type=str, default="roundabout", choices=list(envs.keys())) - parser.add_argument("--top_down", "--topdown", action="store_true") - args = parser.parse_args() - env_cls_name = args.env - extra_args = dict(film_size=(800, 800)) if args.top_down else {} - assert env_cls_name in envs.keys(), "No environment named {}, argument accepted: \n" \ - "(1) roundabout\n" \ - "(2) intersection\n" \ - "(3) tollgate\n" \ - "(4) bottleneck\n" \ - "(5) parkinglot\n" \ - "(6) pgma" \ - .format(env_cls_name) - env = envs[env_cls_name]( - { - "use_render": True if not args.top_down else False, - "crash_done": False, - "sensors": dict(rgb_camera=(RGBCamera, 400, 300)), - "interface_panel": ["rgb_camera", "dashboard"], - "agent_policy": ManualControllableIDMPolicy - } - ) - try: - env.reset() - # if env.current_track_agent: - # env.current_track_agent.expert_takeover = True - print(HELP_MESSAGE) - env.switch_to_third_person_view() # Default is in Top-down view, we switch to Third-person view. - for i in range(1, 10000000000): - o, r, tm, tc, info = env.step({agent_id: [0, 0] for agent_id in env.agents.keys()}) - env.render( - **extra_args, - mode="top_down" if args.top_down else None, - text={ - "Quit": "ESC", - "Number of existing vehicles": len(env.agents), - "Tracked agent (Press Q)": env.engine.agent_manager.object_to_agent(env.current_track_agent.id), - "Keyboard Control": "W,A,S,D", - # "Auto-Drive (Switch mode: T)": "on" if env.current_track_agent.expert_takeover else "off", - } if not args.top_down else {} - ) - if tm["__all__"]: - env.reset() - # if env.current_track_agent: - # env.current_track_agent.expert_takeover = True - finally: - env.close() \ No newline at end of file diff --git a/script/requirements.txt b/script/requirements.txt deleted file mode 100644 index 01787135..00000000 --- a/script/requirements.txt +++ /dev/null @@ -1,4 +0,0 @@ -wheel -metadrive-simulator -gymnasium -gym From e1c60396bafa1798292c8b01ce2b2689df815a39 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Piotr=20Kit=C5=82owski?= Date: Sat, 25 Jan 2025 18:07:35 +0100 Subject: [PATCH 13/18] temporary deleted copo --- CoPO | 1 - 1 file changed, 1 deletion(-) delete mode 160000 CoPO diff --git a/CoPO b/CoPO deleted file mode 160000 index 86975e92..00000000 --- a/CoPO +++ /dev/null @@ -1 +0,0 @@ -Subproject commit 86975e924df9f7d0bee701955228e6855ae3f9ff From 9fe46309753625e235ca45fc1efb11b39e928f4c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Piotr=20Kit=C5=82owski?= Date: Sat, 25 Jan 2025 18:08:24 +0100 Subject: [PATCH 14/18] Added repo --- .gitmodules | 3 +++ CoPO | 1 + 2 files changed, 4 insertions(+) create mode 100644 .gitmodules create mode 160000 CoPO diff --git a/.gitmodules b/.gitmodules new file mode 100644 index 00000000..b809be84 --- /dev/null +++ b/.gitmodules @@ -0,0 +1,3 @@ +[submodule "CoPO"] + path = CoPO + url = https://github.com/cncPomper/CoPO diff --git a/CoPO b/CoPO new file mode 160000 index 00000000..86975e92 --- /dev/null +++ b/CoPO @@ -0,0 +1 @@ +Subproject commit 86975e924df9f7d0bee701955228e6855ae3f9ff From 95d0f6af456e18c72c28233e229507ee3f24531a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Piotr=20Kit=C5=82owski?= Date: Sat, 25 Jan 2025 18:31:39 +0100 Subject: [PATCH 15/18] how to train agent --- README.md | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index 2d9911e7..9d278e19 100644 --- a/README.md +++ b/README.md @@ -31,6 +31,12 @@ pip install pydantic==1.9.0 ## How to train a RL agents - - - +``` +cd CoPo/copo_code/copo/ +python train_all_cl.py --exp-name my_cl +``` +Training process 4.7h +``` +python train_all_ippo.py --exp-name my_ippo +``` +Training process 7.3h From e2fb7abf48c63817eb14b0c1d3099eee0b9625f3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Piotr=20Kit=C5=82owski?= Date: Sat, 25 Jan 2025 18:34:45 +0100 Subject: [PATCH 16/18] evaluate --- CoPO | 2 +- README.md | 6 ++++++ 2 files changed, 7 insertions(+), 1 deletion(-) diff --git a/CoPO b/CoPO index 86975e92..2d526ead 160000 --- a/CoPO +++ b/CoPO @@ -1 +1 @@ -Subproject commit 86975e924df9f7d0bee701955228e6855ae3f9ff +Subproject commit 2d526eadc72f6f605873080cd40771e83be808bf diff --git a/README.md b/README.md index 9d278e19..dde9e0e8 100644 --- a/README.md +++ b/README.md @@ -40,3 +40,9 @@ Training process 4.7h python train_all_ippo.py --exp-name my_ippo ``` Training process 7.3h + +## how to evaluate +``` +python eval.py --root my_cl +python eval.py --root my_ippo +``` \ No newline at end of file From d87a858dae6a8c1ed469f2654d1dc0b9acbb8b52 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Piotr=20Kit=C5=82owski?= Date: Sat, 25 Jan 2025 18:43:24 +0100 Subject: [PATCH 17/18] type --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index dde9e0e8..2b1c402f 100644 --- a/README.md +++ b/README.md @@ -41,7 +41,7 @@ python train_all_ippo.py --exp-name my_ippo ``` Training process 7.3h -## how to evaluate +## How to evaluate ``` python eval.py --root my_cl python eval.py --root my_ippo From b10db883f21e4356d3b370201f3a87898cf1dc5a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Piotr=20Kit=C5=82owski?= Date: Sat, 25 Jan 2025 18:55:58 +0100 Subject: [PATCH 18/18] Fixed some depency --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 2b1c402f..97deb78f 100644 --- a/README.md +++ b/README.md @@ -22,7 +22,7 @@ cd CoPO/copo_code pip install -e . pip install -U ray==1.2.0 "ray[rllib]==1.2.0" -pip install -U "numpy<1.24.0" +pip install -U "numpy<1.19.0" pip uninstall opencv-python pip uninstall opencv-python-headless pip install opencv-python==4.5.5.64