|
| 1 | +#!/usr/bin/env python |
| 2 | +# -*- coding: utf-8 -*- |
| 3 | +# Copyright 2023 The OpenRL Authors. |
| 4 | +# |
| 5 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | +# you may not use this file except in compliance with the License. |
| 7 | +# You may obtain a copy of the License at |
| 8 | +# |
| 9 | +# https://www.apache.org/licenses/LICENSE-2.0 |
| 10 | +# |
| 11 | +# Unless required by applicable law or agreed to in writing, software |
| 12 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | +# See the License for the specific language governing permissions and |
| 15 | +# limitations under the License. |
| 16 | + |
| 17 | +"""""" |
| 18 | + |
| 19 | +# Use OpenRL to load stable-baselines's model for testing |
| 20 | + |
| 21 | +import numpy as np |
| 22 | +import torch |
| 23 | + |
| 24 | +from openrl.configs.config import create_config_parser |
| 25 | +from openrl.envs.common import make |
| 26 | +from openrl.modules.common.ppo_net import PPONet as Net |
| 27 | +from openrl.modules.networks.policy_value_network_sb3 import ( |
| 28 | + PolicyValueNetworkSB3 as PolicyValueNetwork, |
| 29 | +) |
| 30 | +from openrl.runners.common import PPOAgent as Agent |
| 31 | + |
| 32 | + |
| 33 | +def evaluation(local_trained_file_path=None): |
| 34 | + # begin to test |
| 35 | + |
| 36 | + cfg_parser = create_config_parser() |
| 37 | + cfg = cfg_parser.parse_args(["--config", "ppo.yaml"]) |
| 38 | + |
| 39 | + # Create an environment for testing and set the number of environments to interact with to 9. Set rendering mode to group_human. |
| 40 | + render_mode = "group_human" |
| 41 | + render_mode = None |
| 42 | + env = make("CartPole-v1", render_mode=render_mode, env_num=9, asynchronous=True) |
| 43 | + model_dict = {"model": PolicyValueNetwork} |
| 44 | + net = Net( |
| 45 | + env, |
| 46 | + cfg=cfg, |
| 47 | + model_dict=model_dict, |
| 48 | + device="cuda" if torch.cuda.is_available() else "cpu", |
| 49 | + ) |
| 50 | + # initialize the trainer |
| 51 | + agent = Agent( |
| 52 | + net, |
| 53 | + ) |
| 54 | + if local_trained_file_path is not None: |
| 55 | + agent.load(local_trained_file_path) |
| 56 | + # The trained agent sets up the interactive environment it needs. |
| 57 | + agent.set_env(env) |
| 58 | + # Initialize the environment and get initial observations and environmental information. |
| 59 | + obs, info = env.reset() |
| 60 | + done = False |
| 61 | + |
| 62 | + total_step = 0 |
| 63 | + total_reward = 0.0 |
| 64 | + while not np.any(done): |
| 65 | + # Based on environmental observation input, predict next action. |
| 66 | + action, _ = agent.act(obs, deterministic=True) |
| 67 | + obs, r, done, info = env.step(action) |
| 68 | + total_step += 1 |
| 69 | + total_reward += np.mean(r) |
| 70 | + if total_step % 50 == 0: |
| 71 | + print(f"{total_step}: reward:{np.mean(r)}") |
| 72 | + env.close() |
| 73 | + print("total step:", total_step) |
| 74 | + print("total reward:", total_reward) |
| 75 | + |
| 76 | + |
| 77 | +if __name__ == "__main__": |
| 78 | + evaluation() |
0 commit comments