OpenAI GYM을 이용한 간단한 게임 실습
자동으로 행동하는 게임 실습
import gym
env = gym.make("FrozenLake-v0")
observation = env.reset()
gym 패키지를 import하고 OpenAI GYM에서 제공하고 있는 환경 중 FrozenLake-v0 환경을 생성합니다.
observation = env.reset()
환경을 초기화 하고 초기화된 첫번째 observation(상태)을 가지고 옵니다.
for _ in range(1000):
env.render() # 출력
action = env.action_space.sample() # agent의 action
observation, reward, done, info = env.step(action)
현재 환경을 화면으로 출력합니다.
환경에 따라 적절한 action을 정해야합니다. 이 부분에 알고리즘이 적용되게 되며, 여기서는 랜덤하게 action을 정하고 있습니다.
위에서 정한 action을 취합니다. 이 action을 취한 결과로 ovservation, reward, done, info를 얻습니다.
(Jupyter Notebook 실습코드 - 하단이 보이지 않을 경우 새로고침)
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
사용자 입력을 받아 행동하는 게임 실습
import sys,tty,termios
class _Getch:
def __call__(self):
fd = sys.stdin.fileno()
old_settings = termios.tcgetattr(fd)
try:
tty.setraw(sys.stdin.fileno())
ch = sys.stdin.read(3)
finally:
termios.tcsetattr(fd, termios.TCSADRAIN, old_settings)
return ch
inkey = _Getch()
# MACROS
LEFT = 0
DOWN = 1
RIGHT = 2
UP = 3
# Key mapping
arrow_keys = {
'\x1b[A' : UP,
'\x1b[B': DOWN,
'\x1b[C': RIGHT,
'\x1b[D': LEFT
}
Python에서 입력된 key를 받는 코드입니다.
UP 키 입력시 \x1b[A이 입력, DOWN 키 입력시 \x1b[B 입력...
import gym
from gym.envs.registration import register
register(
id='FrozenLake-v3',
entry_point='gym.envs.toy_text:FrozenLakeEnv',
kwargs={'map_name' : '4x4', 'is_slippery': False}
)
env = gym.make('FrozenLake-v3')
env.render() # Show the initial board
gym 패키지를 import하고 OpenAI GYM에서 제공하고 있는 환경 중 FrozenLake-v3 환경을 생성합니다.
while True:
# choese an action from keyboard
key = inkey()
if key not in arrow_keys.keys():
print("Game aborted!")
break
action = arrow_keys[key]
state, reward, done, info = env.step(action)
env.render() # show the board after action
print("State :", state, "Action: ", action, "Reward: ", reward, "info: ", info)
if done:
print("Finished with reward", reward)
break
키 값을 입력 받고 사용자가 입력한 키 값에 따라 action을 취합니다.
이 action을 취한 결과로 state, reward, done, info를 받아 출력 합니다.
- 키를 입력 받는 코드는 notebook 또는 pycharm등의 애플리케이션 환경에서는 정상적으로 실행되지 않습니다. 터미널을 이용하여 직접 실행 해야 합니다.
(Jupyter Notebook 실습코드 - 하단이 보이지 않을 경우 새로고침)
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.