-
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 #67 from ChildTang/main
Add Retro Environment
- Loading branch information
Showing
13 changed files
with
1,228 additions
and
9 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
"""""" | ||
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(): | ||
# 创建环境,若需要并行多个环境,需要设置参数asynchronous为True;若需要设定关卡,可以设定state参数,该参数与具体游戏有关 | ||
env = make("Airstriker-Genesis", state="Level1", env_num=2, asynchronous=True) | ||
# 创建网络 | ||
net = Net(env, device="cuda") | ||
# 初始化训练器 | ||
agent = Agent(net) | ||
# 开始训练 | ||
agent.train(total_time_steps=20000) | ||
# 关闭环境 | ||
env.close() | ||
return agent | ||
|
||
|
||
def game_test(agent): | ||
# 开始测试环境 | ||
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: | ||
# 智能体根据 observation 预测下一个动作 | ||
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): | ||
env.reset() | ||
|
||
env.close() | ||
|
||
|
||
if __name__ == "__main__": | ||
agent = train() | ||
game_test(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
Oops, something went wrong.