Skip to content

Commit

Permalink
Merge pull request #40 from huangshiyu13/dev
Browse files Browse the repository at this point in the history
- add test for train examples
  • Loading branch information
huangshiyu13 authored May 5, 2023
2 parents 233b301 + 0c7fb57 commit c7f56d6
Show file tree
Hide file tree
Showing 4 changed files with 111 additions and 1 deletion.
2 changes: 1 addition & 1 deletion .github/workflows/unit_test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ jobs:
if: "! contains(toJSON(github.event.commits.*.message), '[ci skip]')"
strategy:
matrix:
python-version: [ 3.8, 3.9 ]
python-version: [ 3.8 ]

steps:
- uses: actions/checkout@v3
Expand Down
File renamed without changes.
58 changes: 58 additions & 0 deletions tests/test_examples/test_train_cartpole.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# Copyright 2023 The OpenRL Authors.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# https://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

""""""

import os
import sys

import pytest
import numpy as np

from openrl.envs.common import make
from openrl.modules.common import PPONet as Net
from openrl.runners.common import PPOAgent as Agent


@pytest.fixture(scope="module", params=[""])
def config(request):
from openrl.configs.config import create_config_parser

cfg_parser = create_config_parser()
cfg = cfg_parser.parse_args(request.param.split())
return cfg


@pytest.mark.unittest
def test_train_cartpole(config):
env = make("CartPole-v1", env_num=9)
agent = Agent(Net(env, cfg=config))
agent.train(total_time_steps=20000)

agent.set_env(env)
obs, info = env.reset()
done = False
total_reward = 0
while not np.any(done):
action, _ = agent.act(obs, deterministic=True)
obs, r, done, info = env.step(action)
total_reward += np.mean(r)
assert total_reward >= 450, "CartPole-v1 should be solved."
env.close()


if __name__ == "__main__":
sys.exit(pytest.main(["-sv", os.path.basename(__file__)]))
52 changes: 52 additions & 0 deletions tests/test_examples/test_train_mpe.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
""""""
import os
import sys

import pytest
import numpy as np

from openrl.configs.config import create_config_parser
from openrl.envs.common import make
from openrl.modules.common import PPONet as Net
from openrl.runners.common import PPOAgent as Agent


@pytest.fixture(
scope="module",
params=[
"--episode_length 5 --use_recurrent_policy true --use_joint_action_loss true --use_valuenorm true --use_adv_normalize true"
],
)
def config(request):
cfg_parser = create_config_parser()
cfg = cfg_parser.parse_args(request.param.split())
return cfg


@pytest.mark.unittest
def test_train_mpe(config):
env_num = 2
env = make(
"simple_spread",
env_num=env_num,
asynchronous=True,
)
net = Net(env, cfg=config)
agent = Agent(net)
agent.train(total_time_steps=30)
agent.save("./ppo_agent/")
agent.load("./ppo_agent/")
agent.set_env(env)
obs, info = env.reset(seed=0)
step = 0
while step < 5:
action, _ = agent.act(obs, deterministic=True)
obs, r, done, info = env.step(action)
if np.any(done):
break
step += 1
env.close()


if __name__ == "__main__":
sys.exit(pytest.main(["-sv", os.path.basename(__file__)]))

0 comments on commit c7f56d6

Please sign in to comment.