Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add sac #169

Merged
merged 1 commit into from
Jul 26, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 51 additions & 0 deletions examples/sac/train_sac_beta.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
""""""
import numpy as np

from openrl.configs.config import create_config_parser
from openrl.envs.common import make
from openrl.modules.common import SACNet as Net
from openrl.runners.common import SACAgent as Agent


def train():
cfg_parser = create_config_parser()
cfg = cfg_parser.parse_args()

# create environment, set environment parallelism
env = make("Pendulum-v1", env_num=9)

# create the neural network
net = Net(env, cfg=cfg)
# initialize the trainer
agent = Agent(net)
# start training, set total number of training steps
agent.train(total_time_steps=20000)

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.
render_mode = None
env = make("Pendulum-v1", render_mode=render_mode, env_num=9, asynchronous=True)
# 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
while not np.any(done):
# Based on environmental observation input, predict next action.
action = agent.act(obs, sample=False) # sample=False in evaluation
obs, r, done, info = env.step(action)
step += 1
if step % 50 == 0:
print(f"{step}: reward:{np.mean(r)}")
env.close()


if __name__ == "__main__":
agent = train()
evaluation(agent)
Loading