2016-05-03 22:27:42 +03:00
|
|
|
#!/usr/bin/env python
|
2016-06-01 16:15:18 +02:00
|
|
|
from __future__ import print_function
|
|
|
|
|
Cleanup, removal of unmaintained code (#836)
* add dtype to Box
* remove board_game, debugging, safety, parameter_tuning environments
* massive set of breaking changes
- remove python logging module
- _step, _reset, _seed, _close => non underscored method
- remove benchmark and scoring folder
* Improve render("human"), now resizable, closable window.
* get rid of default step and reset in wrappers, so it doesn’t silently fail for people with underscore methods
* CubeCrash unit test environment
* followup fixes
* MemorizeDigits unit test envrionment
* refactored spaces a bit
fixed indentation
disabled test_env_semantics
* fix unit tests
* fixes
* CubeCrash, MemorizeDigits tested
* gym backwards compatibility patch
* gym backwards compatibility, followup fixes
* changelist, add spaces to main namespaces
* undo_logger_setup for backwards compat
* remove configuration.py
2018-01-25 18:20:14 -08:00
|
|
|
import sys, gym, time
|
2016-05-03 22:27:42 +03:00
|
|
|
|
|
|
|
#
|
Cleanup, removal of unmaintained code (#836)
* add dtype to Box
* remove board_game, debugging, safety, parameter_tuning environments
* massive set of breaking changes
- remove python logging module
- _step, _reset, _seed, _close => non underscored method
- remove benchmark and scoring folder
* Improve render("human"), now resizable, closable window.
* get rid of default step and reset in wrappers, so it doesn’t silently fail for people with underscore methods
* CubeCrash unit test environment
* followup fixes
* MemorizeDigits unit test envrionment
* refactored spaces a bit
fixed indentation
disabled test_env_semantics
* fix unit tests
* fixes
* CubeCrash, MemorizeDigits tested
* gym backwards compatibility patch
* gym backwards compatibility, followup fixes
* changelist, add spaces to main namespaces
* undo_logger_setup for backwards compat
* remove configuration.py
2018-01-25 18:20:14 -08:00
|
|
|
# Test yourself as a learning agent! Pass environment name as a command-line argument, for example:
|
|
|
|
#
|
|
|
|
# python keyboard_agent.py SpaceInvadersNoFrameskip-v4
|
2016-05-03 22:27:42 +03:00
|
|
|
#
|
|
|
|
|
2016-06-01 16:15:18 +02:00
|
|
|
env = gym.make('LunarLander-v2' if len(sys.argv)<2 else sys.argv[1])
|
2016-05-03 22:27:42 +03:00
|
|
|
|
2017-02-27 10:00:58 -08:00
|
|
|
if not hasattr(env.action_space, 'n'):
|
|
|
|
raise Exception('Keyboard agent only supports discrete action spaces')
|
2016-05-03 22:27:42 +03:00
|
|
|
ACTIONS = env.action_space.n
|
|
|
|
SKIP_CONTROL = 0 # Use previous control decision SKIP_CONTROL times, that's how you
|
|
|
|
# can test what skip is still usable.
|
|
|
|
|
|
|
|
human_agent_action = 0
|
|
|
|
human_wants_restart = False
|
|
|
|
human_sets_pause = False
|
|
|
|
|
|
|
|
def key_press(key, mod):
|
|
|
|
global human_agent_action, human_wants_restart, human_sets_pause
|
|
|
|
if key==0xff0d: human_wants_restart = True
|
|
|
|
if key==32: human_sets_pause = not human_sets_pause
|
2016-08-25 02:08:32 +03:00
|
|
|
a = int( key - ord('0') )
|
2016-05-03 22:27:42 +03:00
|
|
|
if a <= 0 or a >= ACTIONS: return
|
|
|
|
human_agent_action = a
|
|
|
|
|
|
|
|
def key_release(key, mod):
|
|
|
|
global human_agent_action
|
2016-08-25 02:08:32 +03:00
|
|
|
a = int( key - ord('0') )
|
2016-05-03 22:27:42 +03:00
|
|
|
if a <= 0 or a >= ACTIONS: return
|
|
|
|
if human_agent_action == a:
|
|
|
|
human_agent_action = 0
|
|
|
|
|
|
|
|
env.render()
|
2017-02-01 15:54:41 -08:00
|
|
|
env.unwrapped.viewer.window.on_key_press = key_press
|
|
|
|
env.unwrapped.viewer.window.on_key_release = key_release
|
2016-05-03 22:27:42 +03:00
|
|
|
|
|
|
|
def rollout(env):
|
|
|
|
global human_agent_action, human_wants_restart, human_sets_pause
|
|
|
|
human_wants_restart = False
|
|
|
|
obser = env.reset()
|
|
|
|
skip = 0
|
Cleanup, removal of unmaintained code (#836)
* add dtype to Box
* remove board_game, debugging, safety, parameter_tuning environments
* massive set of breaking changes
- remove python logging module
- _step, _reset, _seed, _close => non underscored method
- remove benchmark and scoring folder
* Improve render("human"), now resizable, closable window.
* get rid of default step and reset in wrappers, so it doesn’t silently fail for people with underscore methods
* CubeCrash unit test environment
* followup fixes
* MemorizeDigits unit test envrionment
* refactored spaces a bit
fixed indentation
disabled test_env_semantics
* fix unit tests
* fixes
* CubeCrash, MemorizeDigits tested
* gym backwards compatibility patch
* gym backwards compatibility, followup fixes
* changelist, add spaces to main namespaces
* undo_logger_setup for backwards compat
* remove configuration.py
2018-01-25 18:20:14 -08:00
|
|
|
total_reward = 0
|
|
|
|
total_timesteps = 0
|
|
|
|
while 1:
|
2016-05-03 22:27:42 +03:00
|
|
|
if not skip:
|
2016-06-01 16:15:18 +02:00
|
|
|
#print("taking action {}".format(human_agent_action))
|
2016-05-03 22:27:42 +03:00
|
|
|
a = human_agent_action
|
Cleanup, removal of unmaintained code (#836)
* add dtype to Box
* remove board_game, debugging, safety, parameter_tuning environments
* massive set of breaking changes
- remove python logging module
- _step, _reset, _seed, _close => non underscored method
- remove benchmark and scoring folder
* Improve render("human"), now resizable, closable window.
* get rid of default step and reset in wrappers, so it doesn’t silently fail for people with underscore methods
* CubeCrash unit test environment
* followup fixes
* MemorizeDigits unit test envrionment
* refactored spaces a bit
fixed indentation
disabled test_env_semantics
* fix unit tests
* fixes
* CubeCrash, MemorizeDigits tested
* gym backwards compatibility patch
* gym backwards compatibility, followup fixes
* changelist, add spaces to main namespaces
* undo_logger_setup for backwards compat
* remove configuration.py
2018-01-25 18:20:14 -08:00
|
|
|
total_timesteps += 1
|
2016-05-03 22:27:42 +03:00
|
|
|
skip = SKIP_CONTROL
|
|
|
|
else:
|
|
|
|
skip -= 1
|
|
|
|
|
|
|
|
obser, r, done, info = env.step(a)
|
Cleanup, removal of unmaintained code (#836)
* add dtype to Box
* remove board_game, debugging, safety, parameter_tuning environments
* massive set of breaking changes
- remove python logging module
- _step, _reset, _seed, _close => non underscored method
- remove benchmark and scoring folder
* Improve render("human"), now resizable, closable window.
* get rid of default step and reset in wrappers, so it doesn’t silently fail for people with underscore methods
* CubeCrash unit test environment
* followup fixes
* MemorizeDigits unit test envrionment
* refactored spaces a bit
fixed indentation
disabled test_env_semantics
* fix unit tests
* fixes
* CubeCrash, MemorizeDigits tested
* gym backwards compatibility patch
* gym backwards compatibility, followup fixes
* changelist, add spaces to main namespaces
* undo_logger_setup for backwards compat
* remove configuration.py
2018-01-25 18:20:14 -08:00
|
|
|
if r != 0:
|
|
|
|
print("reward %0.3f" % r)
|
|
|
|
total_reward += r
|
|
|
|
window_still_open = env.render()
|
|
|
|
if window_still_open==False: return False
|
2016-05-03 22:27:42 +03:00
|
|
|
if done: break
|
|
|
|
if human_wants_restart: break
|
|
|
|
while human_sets_pause:
|
|
|
|
env.render()
|
|
|
|
time.sleep(0.1)
|
Cleanup, removal of unmaintained code (#836)
* add dtype to Box
* remove board_game, debugging, safety, parameter_tuning environments
* massive set of breaking changes
- remove python logging module
- _step, _reset, _seed, _close => non underscored method
- remove benchmark and scoring folder
* Improve render("human"), now resizable, closable window.
* get rid of default step and reset in wrappers, so it doesn’t silently fail for people with underscore methods
* CubeCrash unit test environment
* followup fixes
* MemorizeDigits unit test envrionment
* refactored spaces a bit
fixed indentation
disabled test_env_semantics
* fix unit tests
* fixes
* CubeCrash, MemorizeDigits tested
* gym backwards compatibility patch
* gym backwards compatibility, followup fixes
* changelist, add spaces to main namespaces
* undo_logger_setup for backwards compat
* remove configuration.py
2018-01-25 18:20:14 -08:00
|
|
|
time.sleep(0.1)
|
|
|
|
print("timesteps %i reward %0.2f" % (total_timesteps, total_reward))
|
2016-05-03 22:27:42 +03:00
|
|
|
|
2016-06-01 16:15:18 +02:00
|
|
|
print("ACTIONS={}".format(ACTIONS))
|
|
|
|
print("Press keys 1 2 3 ... to take actions 1 2 3 ...")
|
|
|
|
print("No keys pressed is taking action 0")
|
2016-05-03 22:27:42 +03:00
|
|
|
|
|
|
|
while 1:
|
Cleanup, removal of unmaintained code (#836)
* add dtype to Box
* remove board_game, debugging, safety, parameter_tuning environments
* massive set of breaking changes
- remove python logging module
- _step, _reset, _seed, _close => non underscored method
- remove benchmark and scoring folder
* Improve render("human"), now resizable, closable window.
* get rid of default step and reset in wrappers, so it doesn’t silently fail for people with underscore methods
* CubeCrash unit test environment
* followup fixes
* MemorizeDigits unit test envrionment
* refactored spaces a bit
fixed indentation
disabled test_env_semantics
* fix unit tests
* fixes
* CubeCrash, MemorizeDigits tested
* gym backwards compatibility patch
* gym backwards compatibility, followup fixes
* changelist, add spaces to main namespaces
* undo_logger_setup for backwards compat
* remove configuration.py
2018-01-25 18:20:14 -08:00
|
|
|
window_still_open = rollout(env)
|
|
|
|
if window_still_open==False: break
|
|
|
|
|