-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
334 additions
and
21 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
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
83 changes: 83 additions & 0 deletions
83
gym_pybullet_drones/examples/hover/hover_learn_multienv.py
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,83 @@ | ||
from stable_baselines3 import PPO, SAC, TD3 | ||
from gym_pybullet_drones.envs.single_agent_rl import HoverIMU, HoverGPS, HoverFullState | ||
import time | ||
import torch | ||
import os | ||
from stable_baselines3.common.callbacks import EvalCallback | ||
from stable_baselines3.common.vec_env import SubprocVecEnv, subproc_vec_env | ||
|
||
def make_env(env_class, rank): | ||
|
||
def init_(): | ||
env = env_class(seed=rank) | ||
env.rank = rank | ||
env.id = rank | ||
return env | ||
|
||
return init_ | ||
|
||
|
||
def main(test=True): | ||
|
||
proc_num = 4 | ||
|
||
savedir = '/home/led/robotics/engines/Bullet_sym/gym-pybullet-drones/gym_pybullet_drones/results/hover/multienv' | ||
savepath= os.path.join( | ||
savedir, | ||
'best_model' | ||
# "best_model_ppo_longlearn" | ||
# 'best_model_ppo_nonorm_imu_BEST' | ||
# 'best_model_ppo_nonorm' | ||
# 'best_model_random_noize' | ||
) | ||
|
||
trainer = PPO | ||
|
||
env_class = HoverFullState | ||
vec_env = SubprocVecEnv([make_env(env_class, i) for i in range(proc_num)]) | ||
eval_env = HoverFullState() | ||
|
||
# env.randomize = False | ||
agent = trainer( | ||
'MlpPolicy', | ||
env=vec_env, | ||
verbose=0, | ||
tensorboard_log=savedir, | ||
# policy_kwargs=policy_kwargs | ||
# n_steps=10000 | ||
) | ||
|
||
eval_callback = EvalCallback(eval_env, best_model_save_path=savedir, | ||
log_path=savedir, eval_freq=10000, | ||
deterministic=True, render=False) | ||
|
||
|
||
agent.learn(2000000, callback=eval_callback) | ||
agent.save(savepath) | ||
|
||
env = env_class(visualize=True) | ||
# env.randomize = False | ||
agent = trainer.load(savepath, env=env) | ||
|
||
state, _=env.reset() | ||
rew = 0 | ||
while test: | ||
action, _ = agent.predict( | ||
state.reshape(1,-1), | ||
deterministic=True | ||
) | ||
state, reward, terminated, truncated, info = env.step(action) | ||
# print(state, reward) | ||
msg = f"POS {state[0, :3]} VEL{state[0, 6:9]}, ACC {state[0, 12:15]}" | ||
print(msg) | ||
rew+=reward | ||
|
||
time.sleep(env.timestep) | ||
if terminated or truncated: | ||
print(rew) | ||
rew=0 | ||
state, _=env.reset() | ||
|
||
|
||
if __name__ == '__main__': | ||
main() |
71 changes: 71 additions & 0 deletions
71
gym_pybullet_drones/examples/hover/hover_learn_multiple.py
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,71 @@ | ||
from stable_baselines3 import PPO, SAC, TD3 | ||
from gym_pybullet_drones.envs.single_agent_rl import HoverIMU, HoverGPS, HoverFullState | ||
import time | ||
import torch | ||
import os | ||
from stable_baselines3.common.callbacks import EvalCallback | ||
|
||
|
||
def main(test=True): | ||
|
||
ep_size = 2000 | ||
for buffer_size in [2000, 10000]: | ||
for batch_size in [64, 512, 1024]: | ||
# for ep_size in [2000]: | ||
|
||
savedir = '/home/led/robotics/engines/Bullet_sym/gym-pybullet-drones/gym_pybullet_drones/results/hover/multiple' | ||
savepath= os.path.join( | ||
savedir, | ||
'model_' + str(buffer_size) + '_' + str(ep_size) | ||
) | ||
|
||
trainer = PPO | ||
env_class = HoverFullState | ||
policy_kwargs = dict(net_arch=dict(pi=[64, 64], qf=[64, 64])) | ||
|
||
env = env_class(max_step=ep_size) | ||
# env.randomize = False | ||
agent = trainer( | ||
'MlpPolicy', | ||
env=env, | ||
verbose=1, | ||
tensorboard_log=savedir, | ||
# policy_kwargs=policy_kwargs | ||
n_steps=buffer_size, | ||
batch_size=batch_size | ||
) | ||
|
||
eval_callback = EvalCallback(env, best_model_save_path=savedir, | ||
log_path=savedir, eval_freq=10000, | ||
deterministic=True, render=False) | ||
|
||
test_only=False | ||
# test_only=True | ||
agent.learn(1000000, callback=eval_callback) | ||
agent.save(savepath) | ||
# env = env_class(visualize=True) | ||
# # env.randomize = False | ||
# agent = trainer.load(savepath, env=env) | ||
|
||
# state, _=env.reset() | ||
# rew = 0 | ||
# while test: | ||
# action, _ = agent.predict( | ||
# state.reshape(1,-1), | ||
# deterministic=True | ||
# ) | ||
# state, reward, terminated, truncated, info = env.step(action) | ||
# # print(state, reward) | ||
# msg = f"POS {state[0, :3]} VEL{state[0, 6:9]}, ACC {state[0, 12:15]}" | ||
# print(msg) | ||
# rew+=reward | ||
|
||
# time.sleep(env.timestep) | ||
# if terminated or truncated: | ||
# print(rew) | ||
# rew=0 | ||
# state, _=env.reset() | ||
|
||
|
||
if __name__=='__main__': | ||
main() |
59 changes: 59 additions & 0 deletions
59
gym_pybullet_drones/examples/hover/hover_learn_multiple_multiproc.py
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,59 @@ | ||
from stable_baselines3 import PPO, SAC, TD3 | ||
from gym_pybullet_drones.envs.single_agent_rl import HoverIMU, HoverGPS, HoverFullState | ||
import time | ||
import torch | ||
import os | ||
import multiprocessing as mp | ||
from stable_baselines3.common.callbacks import EvalCallback | ||
|
||
|
||
def run_exp(buffer_size): | ||
|
||
batch_size = 128 | ||
# for model_scale in [1, 2]: | ||
|
||
savedir = os.path.join( | ||
'/home/led/robotics/engines/Bullet_sym/gym-pybullet-drones/gym_pybullet_drones/results/hover/multiple', | ||
str(buffer_size)#, str(batch_size), | ||
) | ||
for i in range(10): | ||
savepath= os.path.join( | ||
savedir, | ||
'model_'+str(i) | ||
) | ||
|
||
trainer = PPO | ||
env_class = HoverFullState | ||
policy_kwargs = dict(net_arch=dict(pi=[64, 64], qf=[64, 64])) | ||
|
||
env = env_class() | ||
# env.randomize = False | ||
agent = trainer( | ||
'MlpPolicy', | ||
env=env, | ||
verbose=1, | ||
tensorboard_log=savedir, | ||
# policy_kwargs=policy_kwargs | ||
n_steps=buffer_size, | ||
# batch_size=batch_size | ||
) | ||
|
||
eval_callback = EvalCallback(env, best_model_save_path=savedir, | ||
log_path=savedir, eval_freq=20000, | ||
deterministic=True, render=False) | ||
|
||
test_only=False | ||
# test_only=True | ||
agent.learn(3000000, callback=eval_callback) | ||
agent.save(savepath) | ||
|
||
|
||
def main(): | ||
args = [4000, 8000, 16000]# buffer size | ||
|
||
with mp.Pool(3) as p: | ||
p.map(run_exp, args) | ||
|
||
|
||
if __name__=='__main__': | ||
main() |
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
Oops, something went wrong.