Skip to content

Instantly share code, notes, and snippets.

View WhatIThinkAbout's full-sized avatar

Steve Roberts WhatIThinkAbout

View GitHub Profile
class Dynamic(gym.Space):
def __init__(self, action_list = []):
' set the list of initially available actions '
self.set_actions(action_list)
def sample(self):
' select a random action from the set of available actions '
return np.random.choice(self.available_actions)
from babyrobot.envs.lib import Direction
class BabyRobotEnv_v4( BabyRobotEnv_v3 ):
def __init__(self, **kwargs):
super().__init__(**kwargs)
# initially no actions are available
self.dynamic_action_space = Dynamic()
# initialize the environment
env.reset()
terminated = False
while not terminated:
# choose a random action
action = env.action_space.sample()
# take the action and get the information from the environment
''' the first graphical environment '''
class BabyRobotEnv_v3( BabyRobotEnv_v2 ):
def __init__(self, **kwargs):
super().__init__(**kwargs)
# graphical creation of the level
self.level = GridLevel( **kwargs )
# add baby robot
from babyrobot.envs.lib import GridLevel
from babyrobot.envs.lib import RobotDraw
# draw the default grid level
level = GridLevel()
# add Baby Robot
robot = RobotDraw(level)
robot.set_cell_position([1,1])
robot.draw()
from ipycanvas import MultiCanvas
layers = 2
multi_canvas = MultiCanvas(layers, width=width_pixels, height=height_pixels)
draw_base(multi_canvas[0])
draw_grid(multi_canvas[0])
draw_border(multi_canvas[0])
def draw_border(canvas):
canvas.stroke_style = 'black'
canvas.line_width = 5
canvas.set_line_dash([0,0])
canvas.stroke_rect(0,0,width_pixels,height_pixels)
@WhatIThinkAbout
WhatIThinkAbout / draw_grid_lines.py
Created June 18, 2022 16:40
draw_grid_lines.py
def draw_grid( canvas ):
canvas.stroke_style = '#777' # grid line color - medium gray
canvas.line_width = 1
canvas.set_line_dash([4,8])
# draw the grid onto the canvas
for y in range(grid_height):
for x in range(grid_width):
canvas.stroke_rect(cell_pixels * x, cell_pixels * y, cell_pixels, cell_pixels)
@WhatIThinkAbout
WhatIThinkAbout / draw_base.py
Last active June 18, 2022 16:41
draw_base.py
def draw_base(canvas):
canvas.fill_style = 'orange'
canvas.fill_rect(0, 0, canvas.width, canvas.height)
draw_base(canvas)
canvas
@WhatIThinkAbout
WhatIThinkAbout / BabyRobotEnv_v2_Runner.py
Last active January 3, 2023 18:08
Test framework for BabyRobotEnv_v2
# create the environment
env = gym.make('BabyRobotEnv-v2')
# initialize the environment
env.reset()
env.render()
terminated = False
while not terminated: