This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| from .baby_robot_env_v0 import BabyRobotEnv_v0 | |
| from .baby_robot_env_v1 import BabyRobotEnv_v1 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # 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) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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]) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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', |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| from setuptools import setup | |
| setup( | |
| name="babyrobot", | |
| version="1.0.0", | |
| install_requires=['gymnasium'] | |
| ) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| class BabyRobotEnv_v0(gym.Env): | |
| def __init__(self): | |
| super().__init__() | |
| pass | |
| def step(self, action): | |
| state = 1 | |
| reward = -1 | |
| terminated = True |