<commit_before>import gym
from gym import error, spaces, utils
from gym.utils import seeding
import math
import numpy as np
from gym_aigame.envs.rendering import *

# Size in pixels of a cell in the human view
CELL_PIXELS = 32

# Size of the image given as an observation to the agent
IMG_ARRAY_SIZE = (160, 160, 3)

COLORS = {
    'red'   : (255, 0, 0),
    'green' : (0, 255, 0),
    'blue'  : (0, 0, 255),
    'purple': (112, 39, 195),
    'grey'  : (100, 100, 100)
}

class WorldObj:
    """
    Base class for grid world objects
    """

    def __init__(self, type, color):
        assert color in COLORS, color
        self.type = type
        self.color = color
        self.contains = None

    def canOverlap(self):
        """Can the agent overlap with this?"""
        return False

    def canPickup(self):
        """Can the agent pick this up?"""
        return False

    def canContain(self):
        """Can this contain another object?"""
        return False

    def render(self, r):
        assert False

    def _setColor(self, r):
        c = COLORS[self.color]
        r.setLineColor(c[0], c[1], c[2])
        r.setColor(c[0], c[1], c[2])

class Goal(WorldObj):
    def __init__(self):
        super(Goal, self).__init__('goal', 'green')

    def render(self, r):
        self._setColor(r)
        r.drawPolygon([
            (0          , CELL_PIXELS),
            (CELL_PIXELS, CELL_PIXELS),
            (CELL_PIXELS,           0),
            (0          ,           0)
        ])

class Wall(WorldObj):
    def __init__(self):
        super(Wall, self).__init__('wall', 'grey')

    def render(self, r):
        self._setColor(r)
        r.drawPolygon([
            (0          , CELL_PIXELS),
            (CELL_PIXELS, CELL_PIXELS),
            (CELL_PIXELS,           0),
            (0          ,           0)
        ])

class Ball(WorldObj):
    def __init__(self, color='blue'):
        super(Ball, self).__init__('ball', color)

    def render(self, r):
        self._setColor(r)
        r.drawCircle(CELL_PIXELS * 0.5, CELL_PIXELS * 0.5, 10)

    def canPickup(self):
        return True





class AIGameEnv(gym.Env):
    """
    2D grid world game environment
    """

    metadata = {
        'render.modes': ['human', 'rgb_array'],
        'video.frames_per_second' : 10
    }

    def __init__(self, gridSize=20):
        assert (gridSize >= 4)

        # For visual rendering
        self.renderer = None

        self.action_space = spaces.Discrete(4)

        # The observations are RGB images
        self.observation_space = spaces.Box(
            low=0,
            high=255,
            shape = IMG_ARRAY_SIZE
        )

        self.reward_range = (-1, 1000)

        # Environment configuration
        self.gridSize = gridSize
        self.maxSteps = 100
        self.startPos = (1, 1)

        # Initialize the state
        self.seed()
        self.reset()

    def _reset(self):
        # Place the agent in the starting position
        self.agentPos = self.startPos

        # Agent direction, initially pointing right (+x axis)
        self.agentDir = 0

        # Step count since episode start
        self.stepCount = 0

        # Last step the environment was rendered
        self.lastRender = None

        # Return first observation
        self.render()
        obs = self.renderer.getArray(IMG_ARRAY_SIZE)
        return obs

    def _seed(self, seed=None):
        """
        The seed function sets the random elements of the environment,
        and initializes the world.
        """

        gridSz = self.gridSize

        # Initialize the grid
        self.grid = [None] * gridSz * gridSz

        # Place walls around the edges
        for i in range(0, gridSz):
            self.setGrid(i, 0, Wall())
            self.setGrid(i, gridSz - 1, Wall())
            self.setGrid(0, i, Wall())
            self.setGrid(gridSz - 1, i, Wall())

        self.setGrid(4, 8, Ball('blue'))



        # Place a goal in the bottom-left corner
        self.setGrid(gridSz - 2, gridSz - 2, Goal())

        self.np_random, _ = seeding.np_random(seed)

        return [seed]

    def setGrid(self, i, j, v):
        assert i >= 0 and i < self.gridSize
        assert j >= 0 and j < self.gridSize<commit_msg>Added key item<commit_after>import gym
from gym import error, spaces, utils
from gym.utils import seeding
import math
import numpy as np
from gym_aigame.envs.rendering import *

# Size in pixels of a cell in the human view
CELL_PIXELS = 32

# Size of the image given as an observation to the agent
IMG_ARRAY_SIZE = (160, 160, 3)

COLORS = {
    'red'   : (255, 0, 0),
    'green' : (0, 255, 0),
    'blue'  : (0, 0, 255),
    'purple': (112, 39, 195),
    'yellow': (255, 255, 0),
    'grey'  : (100, 100, 100)
}

class WorldObj:
    """
    Base class for grid world objects
    """

    def __init__(self, type, color):
        assert color in COLORS, color
        self.type = type
        self.color = color
        self.contains = None

    def canOverlap(self):
        """Can the agent overlap with this?"""
        return False

    def canPickup(self):
        """Can the agent pick this up?"""
        return False

    def canContain(self):
        """Can this contain another object?"""
        return False

    def render(self, r):
        assert False

    def _setColor(self, r):
        c = COLORS[self.color]
        r.setLineColor(c[0], c[1], c[2])
        r.setColor(c[0], c[1], c[2])

class Goal(WorldObj):
    def __init__(self):
        super(Goal, self).__init__('goal', 'green')

    def render(self, r):
        self._setColor(r)
        r.drawPolygon([
            (0          , CELL_PIXELS),
            (CELL_PIXELS, CELL_PIXELS),
            (CELL_PIXELS,           0),
            (0          ,           0)
        ])

class Wall(WorldObj):
    def __init__(self):
        super(Wall, self).__init__('wall', 'grey')

    def render(self, r):
        self._setColor(r)
        r.drawPolygon([
            (0          , CELL_PIXELS),
            (CELL_PIXELS, CELL_PIXELS),
            (CELL_PIXELS,           0),
            (0          ,           0)
        ])

class Ball(WorldObj):
    def __init__(self, color='blue'):
        super(Ball, self).__init__('ball', color)

    def canPickup(self):
        return True

    def render(self, r):
        self._setColor(r)
        r.drawCircle(CELL_PIXELS * 0.5, CELL_PIXELS * 0.5, 10)

class Key(WorldObj):
    def __init__(self, color='blue'):
        super(Key, self).__init__('ball', color)

    def canPickup(self):
        return True

    def render(self, r):
        self._setColor(r)

        # Vertical quad
        r.drawPolygon([
            (16, 10),
            (20, 10),
            (20, 28),
            (16, 28)
        ])

        # Teeth
        r.drawPolygon([
            (12, 19),
            (16, 19),
            (16, 21),
            (12, 21)
        ])
        r.drawPolygon([
            (12, 26),
            (16, 26),
            (16, 28),
            (12, 28)
        ])

        r.drawCircle(18, 9, 6)
        r.setLineColor(0, 0, 0)
        r.setColor(0, 0, 0)
        r.drawCircle(18, 9, 2)

class AIGameEnv(gym.Env):
    """
    2D grid world game environment
    """

    metadata = {
        'render.modes': ['human', 'rgb_array'],
        'video.frames_per_second' : 10
    }

    def __init__(self, gridSize=20):
        assert (gridSize >= 4)

        # For visual rendering
        self.renderer = None

        self.action_space = spaces.Discrete(4)

        # The observations are RGB images
        self.observation_space = spaces.Box(
            low=0,
            high=255,
            shape = IMG_ARRAY_SIZE
        )

        self.reward_range = (-1, 1000)

        # Environment configuration
        self.gridSize = gridSize
        self.maxSteps = 100
        self.startPos = (1, 1)

        # Initialize the state
        self.seed()
        self.reset()

    def _reset(self):
        # Place the agent in the starting position
        self.agentPos = self.startPos

        # Agent direction, initially pointing right (+x axis)
        self.agentDir = 0

        # Step count since episode start
        self.stepCount = 0

        # Last step the environment was rendered
        self.lastRender = None

        # Return first observation
        self.render()
        obs = self.renderer.getArray(IMG_ARRAY_SIZE)
        return obs

    def _seed(self, seed=None):
        """
        The seed function sets the random elements of the environment,
        and initializes the world.
        """

        gridSz = self.gridSize

        # Initialize the grid
        self.grid = [None] * gridSz * gridSz

        # Place walls around the edges
        for i in range(0, gridSz):
            self.setGrid(i, 0, Wall())
            self.setGrid(i, gridSz - 1, Wall())
            self.setGrid(0, i, Wall())
            self.setGrid(gridSz - 1, i, Wall())

        self.setGrid(4, 8, Ball('blue'))
        self.setGrid(12, 3, Key('yellow'))



        # Place a goal in the bottom-left corner
        self.setGrid(gridSz - 2, gridSz - 2, Goal())

        self.np_random, _ = seeding.np_random(seed)

        return [seed]

    def setGrid(self, i, j, v):
        assert i >= 0 and i < self.gridSize
        assert j >= 0 and j < self.gridSize
{'fraction_non_alphanumeric': 0.1156193127074189, 'fraction_numerical': 0.028048388823466438, 'mean_word_length': 1.753315649867374, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1530495', 'n_tokens_mistral': 3224, 'n_tokens_neox': 2938, 'n_words': 828}