-
Notifications
You must be signed in to change notification settings - Fork 66
/
Copy pathtrain_retro.py
53 lines (43 loc) · 1.36 KB
/
train_retro.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
""""""
import numpy as np
from custom_registration import make
from openrl.modules.common import PPONet as Net
from openrl.runners.common import PPOAgent as Agent
def train():
# Create an environment. If multiple environments need to be run in parallel, set the asynchronous parameter to True.
# If you need to specify a level, you can set the state parameter which is specific to each game.
env = make("Airstriker-Genesis", state="Level1", env_num=2, asynchronous=True)
# create the neural network
net = Net(env, device="cuda")
# initialize the trainer
agent = Agent(net)
# start training
agent.train(total_time_steps=2000)
# close the environment
env.close()
return agent
def game_test(agent):
# begin to test
env = make(
"Airstriker-Genesis",
state="Level1",
render_mode="group_human",
env_num=4,
asynchronous=True,
)
agent.set_env(env)
obs, info = env.reset()
done = False
step = 0
while True:
# Based on environmental observation input, predict next action.
action, _ = agent.act(obs, deterministic=True)
obs, r, done, info = env.step(action)
step += 1
print(f"{step}: reward:{np.mean(r)}")
if any(done):
break
env.close()
if __name__ == "__main__":
agent = train()
game_test(agent)