Skip to content

Instantly share code, notes, and snippets.

View WhatIThinkAbout's full-sized avatar

Steve Roberts WhatIThinkAbout

View GitHub Profile
from .baby_robot_env_v0 import BabyRobotEnv_v0
from .baby_robot_env_v1 import BabyRobotEnv_v1
# animate an image on the canvas
baby_robot = Image.from_file('images/baby_robot.png')
robot_size = 64
canvas[1].clear()
y = 70
for x in range(0,200,4):
with hold_canvas(canvas[1]):
canvas[1].clear_rect(x, y, robot_size)
canvas[1].draw_image(baby_robot, x, y )
sleep(0.1)
from ipycanvas import Canvas
cell_pixels = 64 # pixel dimensions of a grid square
grid_width = 3 # number of horizontal cells
grid_height = 3 # number of vertical cells
width_pixels = grid_width * cell_pixels # total horizontal pixels
height_pixels = grid_height * cell_pixels # total vertical pixels
canvas = Canvas(width=width_pixels, height=height_pixels)
class BabyRobotEnv_v2( BabyRobotEnv_v1 ):
metadata = {'render_modes': ['human']}
def __init__(self, **kwargs):
super().__init__(**kwargs)
# the start and end positions in the grid
# - by default these are the top-left and bottom-right respectively
self.start = kwargs.get('start',[0,0])
from enum import IntEnum
''' simple helper class to enumerate actions in the grid levels '''
class Actions(IntEnum):
Stay = 0
North = 1
East = 2
South = 3
West = 4
@WhatIThinkAbout
WhatIThinkAbout / baby_robot_gym_init.py
Last active January 3, 2023 17:33
Baby Robot Gym init
from gymnasium.envs.registration import register
register(
id='BabyRobotEnv-v0',
entry_point='babyrobot.envs:BabyRobotEnv_v0',
)
register(
id='BabyRobotEnv-v1',
entry_point='babyrobot.envs:BabyRobotEnv_v1',
@WhatIThinkAbout
WhatIThinkAbout / baby_robot_gym_setup.py
Last active January 3, 2023 17:31
Baby Robot Gym setup
from setuptools import setup
setup(
name="babyrobot",
version="1.0.0",
install_requires=['gymnasium']
)
from stable_baselines3.common.env_checker import check_env
# create an instance of our custom environment
env = BabyRobotEnv_v1()
# returns nothing if the environment is verified as ok
check_env(env)
@WhatIThinkAbout
WhatIThinkAbout / BabyRobotEnv_V1.py
Last active January 3, 2023 17:20
Baby Robot Environment V1
import numpy as np
from gymnasium.spaces import Discrete,MultiDiscrete
class BabyRobotEnv_v1(gym.Env):
def __init__(self, **kwargs):
super().__init__()
# dimensions of the grid
@WhatIThinkAbout
WhatIThinkAbout / BabyRobotEnv_V0.py
Last active January 3, 2023 17:11
Baby Robot Environment V0
class BabyRobotEnv_v0(gym.Env):
def __init__(self):
super().__init__()
pass
def step(self, action):
state = 1
reward = -1
terminated = True