-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #71 from Armandpl/sanity-check-robot
improve gym env / tune hps
- Loading branch information
Showing
23 changed files
with
942 additions
and
704 deletions.
There are no files selected for viewing
This file contains 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 |
---|---|---|
@@ -1,16 +1,27 @@ | ||
import sb3_contrib | ||
import sbx | ||
import stable_baselines3 | ||
|
||
|
||
# wrapper class for stable-baselines3.SAC | ||
# TODO can we make one class for all algos? | ||
# check if they all have the train freq param | ||
# check if they have other tuple args | ||
# check if it would be cleaner for sb3 to accept list instead of tuple? | ||
class SAC(stable_baselines3.SAC): | ||
# TODO is there a cleaner way to do this? | ||
class BaseAlgoWrapper: | ||
def __init__(self, **kwargs): | ||
# sb3 expects tuple, omegaconf returns list | ||
# so we need to convert kwarg train_freq from tuple to list | ||
if "train_freq" in kwargs and isinstance(kwargs["train_freq"], list): | ||
kwargs.update({"train_freq": tuple(kwargs["train_freq"])}) | ||
|
||
super().__init__(**kwargs) | ||
|
||
|
||
class SAC(BaseAlgoWrapper, stable_baselines3.SAC): | ||
pass | ||
|
||
|
||
class TQC(BaseAlgoWrapper, sb3_contrib.TQC): | ||
pass | ||
|
||
|
||
class SBXTQC(BaseAlgoWrapper, sbx.TQC): | ||
pass |
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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,95 @@ | ||
{ | ||
"cells": [ | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"from sbx import SAC as SAC_SBX\n", | ||
"from stable_baselines3 import SAC as SAC_SB3\n", | ||
"import matplotlib.pyplot as plt\n", | ||
"import gymnasium as gym\n", | ||
"from furuta.rl.envs.furuta_sim import FurutaSim\n", | ||
"from gymnasium.wrappers import TimeLimit\n", | ||
"\n", | ||
"class ActionLogger(gym.Wrapper):\n", | ||
" def __init__(self, env):\n", | ||
" super().__init__(env)\n", | ||
" self.actions = []\n", | ||
" def step(self, action):\n", | ||
" self.actions.append(action)\n", | ||
" return self.env.step(action)\n", | ||
" def plot_act(self):\n", | ||
" plt.plot(self.actions[-100:])\n", | ||
" plt.show()\n", | ||
"\n", | ||
"env = TimeLimit(ActionLogger(FurutaSim(speed_limits=[400, 400])), max_episode_steps=100)\n", | ||
"\n", | ||
"model = SAC_SB3(\"MlpPolicy\", env, verbose=1, use_sde=True, use_sde_at_warmup=True, learning_starts=500)\n", | ||
"model.learn(total_timesteps=1000, log_interval=4)\n", | ||
"\n", | ||
"env.plot_act()" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"model = SAC_SB3(\"MlpPolicy\", env, verbose=1, use_sde=True, use_sde_at_warmup=True, learning_starts=500, train_freq=(1, \"episode\"))\n", | ||
"model.learn(total_timesteps=1000, log_interval=4)\n", | ||
"\n", | ||
"env.plot_act()" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"model = SAC_SBX(\"MlpPolicy\", env, verbose=1, use_sde=True, use_sde_at_warmup=True, learning_starts=500)\n", | ||
"model.learn(total_timesteps=1000, log_interval=4)\n", | ||
"\n", | ||
"env.plot_act()" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"from sbx import TQC\n", | ||
"\n", | ||
"model = TQC(\"MlpPolicy\", env, verbose=1, use_sde=True, use_sde_at_warmup=True, learning_starts=500, train_freq=(1, \"episode\"))\n", | ||
"model.learn(total_timesteps=1000, log_interval=4)\n", | ||
"\n", | ||
"env.plot_act()" | ||
] | ||
} | ||
], | ||
"metadata": { | ||
"kernelspec": { | ||
"display_name": ".venv", | ||
"language": "python", | ||
"name": "python3" | ||
}, | ||
"language_info": { | ||
"codemirror_mode": { | ||
"name": "ipython", | ||
"version": 3 | ||
}, | ||
"file_extension": ".py", | ||
"mimetype": "text/x-python", | ||
"name": "python", | ||
"nbconvert_exporter": "python", | ||
"pygments_lexer": "ipython3", | ||
"version": "3.11.5" | ||
} | ||
}, | ||
"nbformat": 4, | ||
"nbformat_minor": 2 | ||
} |
Oops, something went wrong.