-
Notifications
You must be signed in to change notification settings - Fork 62
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #40 from huangshiyu13/dev
- add test for train examples
- Loading branch information
Showing
4 changed files
with
111 additions
and
1 deletion.
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
File renamed without changes.
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,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__)])) |
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,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__)])) |