Skip to content

Instantly share code, notes, and snippets.

View WhatIThinkAbout's full-sized avatar

Steve Roberts WhatIThinkAbout

View GitHub Profile
# register the local custom environment class
from gymnasium.envs.registration import register
register( id='BabyRobotEnv-v4', entry_point=BabyRobotEnv_v4 )
# make the environment
env = gym.make("BabyRobotEnv-v4")
# check the environment conforms to the API standard
check_env(env)
@WhatIThinkAbout
WhatIThinkAbout / environment_checker.py
Created January 3, 2023 17:25
Check an environment using the Gymnasium environment checker
# create an instance of our custom environment
env = BabyRobotEnv_v1()
# use the Gymnasium 'check_env' function to check the environment
# - returns nothing if the environment is verified as ok
from gymnasium.utils.env_checker import check_env
check_env(env)
setup = { 'width': 8,
'height': 5,
'add_maze': True,
'maze_seed': 42,
'end': [5,4]
}
walls = [((2, 0),'E'), # remove the east wall at (2,0)
((2, 2),'E'), # remove the east wall at (2,2)
((3, 2),'E'), # remove the east wall at (3,2)
((5, 2),'E')] # add an east wall at (5,2)
for step in range(2):
action = Actions.East
new_state, reward, terminated, truncated, info = env.step(action)
info_str = f"{Actions(action): <5}: {new_state} reward = {reward}"
target_str = f"Target Reached = {info['target_reached']}"
env.render(info = {'side_info': [((10,100),info_str),((10,130),target_str)]})
setup = { 'start':[0,1], 'end': [2,1] , 'add_compass':True }
setup['side_panel'] = {'width':200,'color':'#ddd'}
setup['walls'] = [((0, 1),'N'),((0, 1),'S')]
setup['puddles'] = [((1,1),2)]
env = BabyRobotEnv_v6(**setup)
env.render()
class BabyRobotEnv_v6( BabyRobotEnv_v5 ):
def __init__(self, **kwargs):
super().__init__(**kwargs)
def take_action(self, action):
''' apply the supplied action
returns: - the reward obtained for taking the action
- flag indicating if the target state was reached
'''
setup = {'add_compass':True}
walls = [((0, 0),'E'),
((2, 2),'W')]
setup['walls'] = walls
env = BabyRobotEnv_v5(**setup)
env.render()
env = BabyRobotEnv_v5(**{'side_panel':{'width':200,'color':'#ddd'}})
env.render()
# run the environment, taking random actions
env.reset()
info = {}
terminated = False
while not terminated:
# choose a random action
action = env.action_space.sample()
class BabyRobotEnv_v5( BabyRobotEnv_v4 ):
def __init__(self, **kwargs):
super().__init__(**kwargs)
def render(self, info=None):
''' render as an HTML5 canvas '''
# move baby robot to the current position
self.robot.move(self.x,self.y)
# write the info to the grid side-panel