|
| 1 | +import numpy as np |
| 2 | +import torch |
| 3 | + |
| 4 | +from openrl.configs.config import create_config_parser |
| 5 | +from openrl.envs.common import make |
| 6 | +from openrl.modules.common import PPONet as Net |
| 7 | +from openrl.runners.common import PPOAgent as Agent |
| 8 | + |
| 9 | +env_name = "pybullet_drones/hover-aviary-v0" |
| 10 | + |
| 11 | + |
| 12 | +def train(): |
| 13 | + # create the neural network |
| 14 | + cfg_parser = create_config_parser() |
| 15 | + cfg = cfg_parser.parse_args(["--config", "ppo.yaml"]) |
| 16 | + |
| 17 | + # create environment, set environment parallelism to 64 |
| 18 | + env_num = 20 |
| 19 | + # env_num = 1 |
| 20 | + |
| 21 | + env = make( |
| 22 | + env_name, |
| 23 | + env_num=env_num, |
| 24 | + cfg=cfg, |
| 25 | + asynchronous=True, |
| 26 | + env_wrappers=[], |
| 27 | + gui=False, |
| 28 | + ) |
| 29 | + |
| 30 | + net = Net(env, cfg=cfg, device="cuda" if torch.cuda.is_available() else "cpu") |
| 31 | + # initialize the trainer |
| 32 | + agent = Agent( |
| 33 | + net, |
| 34 | + ) |
| 35 | + # start training, set total number of training steps to 100000 |
| 36 | + agent.train(total_time_steps=1000000) |
| 37 | + |
| 38 | + agent.save("./ppo_agent") |
| 39 | + env.close() |
| 40 | + return agent |
| 41 | + |
| 42 | + |
| 43 | +def evaluation(): |
| 44 | + cfg_parser = create_config_parser() |
| 45 | + cfg = cfg_parser.parse_args(["--config", "ppo.yaml"]) |
| 46 | + # begin to test |
| 47 | + # Create an environment for testing and set the number of environments to interact with to 4. Set rendering mode to group_rgb_array. |
| 48 | + |
| 49 | + env = make( |
| 50 | + env_name, |
| 51 | + env_num=1, |
| 52 | + asynchronous=False, |
| 53 | + env_wrappers=[], |
| 54 | + cfg=cfg, |
| 55 | + gui=False, |
| 56 | + record=False, |
| 57 | + ) |
| 58 | + |
| 59 | + net = Net(env, cfg=cfg, device="cuda" if torch.cuda.is_available() else "cpu") |
| 60 | + # initialize the trainer |
| 61 | + agent = Agent( |
| 62 | + net, |
| 63 | + ) |
| 64 | + agent.load("./ppo_agent") |
| 65 | + |
| 66 | + # The trained agent sets up the interactive environment it needs. |
| 67 | + agent.set_env(env) |
| 68 | + # Initialize the environment and get initial observations and environmental information. |
| 69 | + obs, info = env.reset() |
| 70 | + done = False |
| 71 | + step = 0 |
| 72 | + total_reward = 0.0 |
| 73 | + while not np.any(done): |
| 74 | + # Based on environmental observation input, predict next action. |
| 75 | + action, _ = agent.act(obs, deterministic=True) |
| 76 | + print("action:", action) |
| 77 | + obs, r, done, info = env.step(action) |
| 78 | + step += 1 |
| 79 | + total_reward += np.mean(r) |
| 80 | + # if step % 50 == 0: |
| 81 | + # print(f"{step}: reward:{np.mean(r)}") |
| 82 | + print("total step:", step) |
| 83 | + print("total reward:", total_reward) |
| 84 | + env.close() |
| 85 | + |
| 86 | + |
| 87 | +if __name__ == "__main__": |
| 88 | + # train() |
| 89 | + evaluation() |
0 commit comments