-
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 snake environment
- Loading branch information
Showing
36 changed files
with
2,931 additions
and
82 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.
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 |
---|---|---|
@@ -0,0 +1,17 @@ | ||
|
||
This is the example for the snake game. | ||
|
||
## Usage | ||
|
||
```bash | ||
python train_selfplay.py | ||
``` | ||
|
||
|
||
## Submit to JiDi | ||
|
||
Submition site: http://www.jidiai.cn/env_detail?envid=1. | ||
|
||
Snake senarios: [here](https://github.com/jidiai/ai_lib/blob/7a6986f0cb543994277103dbf605e9575d59edd6/env/config.json#L94) | ||
Original Snake environment: [here](https://github.com/jidiai/ai_lib/blob/master/env/snakes.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,3 @@ | ||
seed: 0 | ||
callbacks: | ||
- id: "ProgressBarCallback" |
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,29 @@ | ||
# -*- coding:utf-8 -*- | ||
def sample_single_dim(action_space_list_each, is_act_continuous): | ||
if is_act_continuous: | ||
each = action_space_list_each.sample() | ||
else: | ||
if action_space_list_each.__class__.__name__ == "Discrete": | ||
each = [0] * action_space_list_each.n | ||
idx = action_space_list_each.sample() | ||
each[idx] = 1 | ||
elif action_space_list_each.__class__.__name__ == "MultiDiscreteParticle": | ||
each = [] | ||
nvec = action_space_list_each.high - action_space_list_each.low + 1 | ||
sample_indexes = action_space_list_each.sample() | ||
|
||
for i in range(len(nvec)): | ||
dim = nvec[i] | ||
new_action = [0] * dim | ||
index = sample_indexes[i] | ||
new_action[index] = 1 | ||
each.extend(new_action) | ||
return each | ||
|
||
|
||
def my_controller(observation, action_space, is_act_continuous): | ||
joint_action = [] | ||
for i in range(len(action_space)): | ||
player = sample_single_dim(action_space[i], is_act_continuous) | ||
joint_action.append(player) | ||
return joint_action |
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,107 @@ | ||
#!/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 time | ||
|
||
import numpy as np | ||
from wrappers import ConvertObs | ||
|
||
from openrl.envs.snake.snake import SnakeEatBeans | ||
from openrl.envs.snake.snake_pettingzoo import SnakeEatBeansAECEnv | ||
from openrl.selfplay.wrappers.random_opponent_wrapper import RandomOpponentWrapper | ||
|
||
|
||
def test_raw_env(): | ||
env = SnakeEatBeans() | ||
|
||
obs, info = env.reset() | ||
|
||
done = False | ||
while not np.any(done): | ||
a1 = np.zeros(4) | ||
a1[env.action_space.sample()] = 1 | ||
a2 = np.zeros(4) | ||
a2[env.action_space.sample()] = 1 | ||
obs, reward, done, info = env.step([a1, a2]) | ||
print("obs:", obs) | ||
print("reward:", reward) | ||
print("done:", done) | ||
print("info:", info) | ||
|
||
|
||
def test_aec_env(): | ||
from PIL import Image | ||
|
||
img_list = [] | ||
env = SnakeEatBeansAECEnv(render_mode="rgb_array") | ||
env.reset(seed=0) | ||
# time.sleep(1) | ||
img = env.render() | ||
img_list.append(img) | ||
step = 0 | ||
for player_name in env.agent_iter(): | ||
if step > 20: | ||
break | ||
observation, reward, termination, truncation, info = env.last() | ||
if termination or truncation: | ||
break | ||
action = env.action_space(player_name).sample() | ||
# if player_name == "player_0": | ||
# action = 2 | ||
# elif player_name == "player_1": | ||
# action = 3 | ||
# else: | ||
# raise ValueError("Unknown player name: {}".format(player_name)) | ||
env.step(action) | ||
img = env.render() | ||
if player_name == "player_0": | ||
img_list.append(img) | ||
# time.sleep(1) | ||
|
||
step += 1 | ||
print("Total steps: {}".format(step)) | ||
|
||
save_path = "test.gif" | ||
img_list = [Image.fromarray(img) for img in img_list] | ||
img_list[0].save(save_path, save_all=True, append_images=img_list[1:], duration=500) | ||
|
||
|
||
def test_vec_env(): | ||
from openrl.envs.common import make | ||
|
||
env = make( | ||
"snakes_1v1", | ||
opponent_wrappers=[ | ||
RandomOpponentWrapper, | ||
], | ||
env_wrappers=[ConvertObs], | ||
render_mode="group_human", | ||
env_num=2, | ||
) | ||
obs, info = env.reset() | ||
step = 0 | ||
done = False | ||
while not np.any(done): | ||
action = env.random_action() | ||
obs, reward, done, info = env.step(action) | ||
time.sleep(0.3) | ||
step += 1 | ||
print("Total steps: {}".format(step)) | ||
|
||
|
||
if __name__ == "__main__": | ||
test_vec_env() |
Oops, something went wrong.