-
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.
add mujoco
- Loading branch information
Showing
16 changed files
with
188 additions
and
13 deletions.
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
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
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
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file not shown.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
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
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,2 @@ | ||
dual_clip_ppo: true | ||
dual_clip_coeff: 3.0 |
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
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,9 @@ | ||
## Installation | ||
|
||
`pip install mujoco` | ||
|
||
## Usage | ||
|
||
```shell | ||
python train_ppo.py | ||
``` |
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,65 @@ | ||
#!/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 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 | ||
|
||
|
||
def train(): | ||
# create environment, set environment parallelism to 9 | ||
env = make("InvertedPendulum-v4", env_num=9) | ||
# create the neural network | ||
net = Net(env) | ||
# initialize the trainer | ||
agent = Agent(net) | ||
# start training, set total number of training steps to 20000 | ||
agent.train(total_time_steps=30000) | ||
env.close() | ||
return agent | ||
|
||
|
||
def evaluation(agent): | ||
# begin to test | ||
# Create an environment for testing and set the number of environments to interact with to 9. Set rendering mode to group_human. | ||
env = make("InvertedPendulum-v4", render_mode=None, env_num=9, asynchronous=False) | ||
|
||
# The trained agent sets up the interactive environment it needs. | ||
agent.set_env(env) | ||
# Initialize the environment and get initial observations and environmental information. | ||
obs, info = env.reset() | ||
|
||
done = False | ||
step = 0 | ||
totoal_reward = 0 | ||
while not np.any(done): | ||
# Based on environmental observation input, predict next action. | ||
action, _ = agent.act(obs, deterministic=True) | ||
obs, r, done, info = env.step(action) | ||
step += 1 | ||
if step % 100 == 0: | ||
print(f"{step}: reward:{np.mean(r)}") | ||
totoal_reward += np.mean(r) | ||
env.close() | ||
print(f"total reward: {totoal_reward}") | ||
|
||
|
||
if __name__ == "__main__": | ||
agent = train() | ||
evaluation(agent) |
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
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
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
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 |
---|---|---|
|
@@ -38,6 +38,7 @@ def get_install_requires() -> list: | |
"imageio", | ||
"opencv-python", | ||
"pygame", | ||
"mujoco", | ||
] | ||
|
||
|
||
|
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 numpy as np | ||
import pytest | ||
|
||
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_mujoco(config): | ||
env = make("InvertedPendulum-v4", env_num=9) | ||
agent = Agent(Net(env, cfg=config)) | ||
agent.train(total_time_steps=30000) | ||
|
||
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 >= 900, "InvertedPendulum-v4 should be solved." | ||
env.close() | ||
|
||
|
||
if __name__ == "__main__": | ||
sys.exit(pytest.main(["-sv", os.path.basename(__file__)])) |