|
| 1 | +import numpy as np |
| 2 | +from gymnasium.wrappers import FlattenObservation |
| 3 | + |
| 4 | +from openrl.configs.config import create_config_parser |
| 5 | +from openrl.envs.common import make |
| 6 | +from openrl.envs.wrappers.base_wrapper import BaseWrapper |
| 7 | +from openrl.envs.wrappers.extra_wrappers import GIFWrapper |
| 8 | +from openrl.modules.common import PPONet as Net |
| 9 | +from openrl.runners.common import PPOAgent as Agent |
| 10 | + |
| 11 | + |
| 12 | +class FrameSkip(BaseWrapper): |
| 13 | + def __init__(self, env, num_frames: int = 8): |
| 14 | + super().__init__(env) |
| 15 | + self.num_frames = num_frames |
| 16 | + |
| 17 | + def step(self, action): |
| 18 | + num_skips = self.num_frames |
| 19 | + total_reward = 0.0 |
| 20 | + |
| 21 | + for x in range(num_skips): |
| 22 | + obs, rew, term, trunc, info = super().step(action) |
| 23 | + total_reward += rew |
| 24 | + if term or trunc: |
| 25 | + break |
| 26 | + |
| 27 | + return obs, total_reward, term, trunc, info |
| 28 | + |
| 29 | + |
| 30 | +env_name = "dm_control/cartpole-balance-v0" |
| 31 | +# env_name = "dm_control/walker-walk-v0" |
| 32 | + |
| 33 | + |
| 34 | +def train(): |
| 35 | + # create the neural network |
| 36 | + cfg_parser = create_config_parser() |
| 37 | + cfg = cfg_parser.parse_args(["--config", "ppo.yaml"]) |
| 38 | + |
| 39 | + # create environment, set environment parallelism to 9 |
| 40 | + env = make( |
| 41 | + env_name, |
| 42 | + env_num=64, |
| 43 | + cfg=cfg, |
| 44 | + asynchronous=True, |
| 45 | + env_wrappers=[FrameSkip, FlattenObservation], |
| 46 | + ) |
| 47 | + |
| 48 | + net = Net(env, cfg=cfg, device="cuda") |
| 49 | + # initialize the trainer |
| 50 | + agent = Agent( |
| 51 | + net, |
| 52 | + ) |
| 53 | + # start training, set total number of training steps to 20000 |
| 54 | + agent.train(total_time_steps=100000) |
| 55 | + agent.save("./ppo_agent") |
| 56 | + env.close() |
| 57 | + return agent |
| 58 | + |
| 59 | + |
| 60 | + |
| 61 | + |
| 62 | + |
| 63 | +def evaluation(): |
| 64 | + cfg_parser = create_config_parser() |
| 65 | + cfg = cfg_parser.parse_args(["--config", "ppo.yaml"]) |
| 66 | + # begin to test |
| 67 | + # Create an environment for testing and set the number of environments to interact with to 9. Set rendering mode to group_human. |
| 68 | + render_mode = "group_human" |
| 69 | + render_mode = "group_rgb_array" |
| 70 | + env = make( |
| 71 | + env_name, |
| 72 | + render_mode=render_mode, |
| 73 | + env_num=4, |
| 74 | + asynchronous=True, |
| 75 | + env_wrappers=[FrameSkip,FlattenObservation], |
| 76 | + cfg=cfg |
| 77 | + ) |
| 78 | + env = GIFWrapper(env, gif_path="./new.gif", fps=5) |
| 79 | + |
| 80 | + |
| 81 | + |
| 82 | + net = Net(env, cfg=cfg, device="cuda") |
| 83 | + # initialize the trainer |
| 84 | + agent = Agent( |
| 85 | + net, |
| 86 | + ) |
| 87 | + agent.load("./ppo_agent") |
| 88 | + |
| 89 | + # The trained agent sets up the interactive environment it needs. |
| 90 | + agent.set_env(env) |
| 91 | + # Initialize the environment and get initial observations and environmental information. |
| 92 | + obs, info = env.reset() |
| 93 | + done = False |
| 94 | + step = 0 |
| 95 | + total_reward = 0.0 |
| 96 | + while not np.any(done): |
| 97 | + if step > 500: |
| 98 | + break |
| 99 | + # Based on environmental observation input, predict next action. |
| 100 | + action, _ = agent.act(obs, deterministic=True) |
| 101 | + obs, r, done, info = env.step(action) |
| 102 | + step += 1 |
| 103 | + total_reward += np.mean(r) |
| 104 | + if step % 50 == 0: |
| 105 | + print(f"{step}: reward:{np.mean(r)}") |
| 106 | + print("total step:", step, total_reward) |
| 107 | + env.close() |
| 108 | + |
| 109 | +train() |
| 110 | +evaluation() |
0 commit comments