示例#1
0
    def show_as_blocks(self):

        block_size = 100

        grid = BlockGrid(self._number, 1, block_size=block_size)

        for block, color in zip(grid, self._colors):
            block.rgb = color

        grid.show()
示例#2
0
    def show_as_blocks(self, block_size=100):
        """
        Show colors in the IPython Notebook using ipythonblocks.

        Parameters
        ----------
        block_size : int, optional
            Size of displayed blocks.

        """
        from ipythonblocks import BlockGrid

        grid = BlockGrid(self.number, 1, block_size=block_size)

        for block, color in zip(grid, self.colors):
            block.rgb = color

        grid.show()
示例#3
0
    def show_as_blocks(self, block_size=100):
        """
        Show colors in the IPython Notebook using ipythonblocks.

        Parameters
        ----------
        block_size : int, optional
            Size of displayed blocks.

        """
        from ipythonblocks import BlockGrid

        grid = BlockGrid(self.number, 1, block_size=block_size)

        for block, color in zip(grid, self.colors):
            block.rgb = color

        grid.show()
示例#4
0
    def dibujaTablero(self,  fichasGanadoras=None):
        """Dibuja  en formato grafico en ipython, el tablero que se le pasa,
        si se le pasa una lista con fichas ganadoras, las resalta en amarillo."""
        grid = BlockGrid(width=self.colmax, height=self.filmax, block_size=25, lines_on=True)

        for fil in range(grid.height):
            for col in range(grid.width):
                if fichasGanadoras and [fil, col] in fichasGanadoras:
                    grid[fil, col] = colors['Yellow']
                    continue
                if self.tablero[fil][col] == 1:
                    grid[fil, col] = colors['Red']
                elif self.tablero[fil][col] == -1:
                    grid[fil, col] = colors['Green']
                else:
                    grid[fil, col] = colors['Black']

        grid.show()
示例#5
0
class CleanupPuzzleEnvironment(Environment):
    def __init__(self, width=5, height=5):
        super(CleanupPuzzleEnvironment, self).__init__()
        self.width = width
        self.height = height

        self.bg_color = (207, 216, 220)
        self.ball_color = (0, 191, 165)
        self.click_color = (244, 67, 54)
        self.grid = BlockGrid(width, height, fill=self.bg_color)

    def __str__(self):
        world = self.get_world()
        self.draw_grid(world)
        self.grid.show()
        return ''

    def draw_grid(self, world):
        self.grid[:] = self.bg_color
        for x in range(0, len(world)):
            for y in range(0, len(world[x])):
                if len(world[x][y]) and isinstance(world[x][y][0], Ball):
                    self.grid[x, y] = self.ball_color
                elif len(world[x][y]) and isinstance(world[x][y][0], Click):
                    self.grid[x, y] = self.click_color

    def get_world(self):
        '''returns the items in the world'''
        result = []
        for x in range(self.height):
            row = []
            for y in range(self.width):
                row.append(self.list_things_at((x, y)))
            result.append(row)
        return result

    def is_inbounds(self, location):
        '''Checks to make sure that the location is inbounds'''
        x, y = location
        return not (x < 0 or x >= self.height or y < 0 or y >= self.width)
示例#6
0
class GraphicEnvironment(XYEnvironment):
    def __init__(self,
                 width=10,
                 height=10,
                 boundary=True,
                 color={},
                 display=False):
        """Define all the usual XYEnvironment characteristics,
        but initialise a BlockGrid for GUI too."""
        super().__init__(width, height)
        self.grid = BlockGrid(width, height, fill=(200, 200, 200))
        if display:
            self.grid.show()
            self.visible = True
        else:
            self.visible = False
        self.bounded = boundary
        self.colors = color

    def get_world(self):
        """Returns all the items in the world in a format
        understandable by the ipythonblocks BlockGrid."""
        result = []
        x_start, y_start = (0, 0)
        x_end, y_end = self.width, self.height
        for x in range(x_start, x_end):
            row = []
            for y in range(y_start, y_end):
                row.append(self.list_things_at([x, y]))
            result.append(row)
        return result

    """
    def run(self, steps=1000, delay=1):
        "" "Run the Environment for given number of time steps,
        but update the GUI too." ""
        for step in range(steps):
            sleep(delay)
            if self.visible:
                self.reveal()
            if self.is_done():
                if self.visible:
                    self.reveal()
                return
            self.step()
        if self.visible:
            self.reveal()
    """

    def run(self, steps=1000, delay=1):
        """Run the Environment for given number of time steps,
        but update the GUI too."""
        for step in range(steps):
            self.update(delay)
            if self.is_done():
                break
            self.step()
        self.update(delay)

    def update(self, delay=1):
        sleep(delay)
        if self.visible:
            self.conceal()
            self.reveal()
        else:
            self.reveal()

    def reveal(self):
        """Display the BlockGrid for this world - the last thing to be added
        at a location defines the location color."""
        self.draw_world()
        self.grid.show()
        self.visible = True

    def draw_world(self):
        self.grid[:] = (200, 200, 200)
        world = self.get_world()
        for x in range(0, len(world)):
            for y in range(0, len(world[x])):
                if len(world[x][y]):
                    self.grid[y, x] = self.colors[world[x][y]
                                                  [-1].__class__.__name__]

    def conceal(self):
        """Hide the BlockGrid for this world"""
        self.visible = False
        display(HTML(''))
示例#7
0
class GraphicEnvironment(XYEnvironment):
    def __init__(self, width=10, height=10, boundary=True, color={}, display=False):
        """Define all the usual XYEnvironment characteristics,
        but initialise a BlockGrid for GUI too."""
        super().__init__(width, height)
        self.grid = BlockGrid(width, height, fill=(200, 200, 200))
        if display:
            self.grid.show()
            self.visible = True
        else:
            self.visible = False
        self.bounded = boundary
        self.colors = color

    def get_world(self):
        """Returns all the items in the world in a format
        understandable by the ipythonblocks BlockGrid."""
        result = []
        x_start, y_start = (0, 0)
        x_end, y_end = self.width, self.height
        for x in range(x_start, x_end):
            row = []
            for y in range(y_start, y_end):
                row.append(self.list_things_at([x, y]))
            result.append(row)
        return result

    """
    def run(self, steps=1000, delay=1):
        "" "Run the Environment for given number of time steps,
        but update the GUI too." ""
        for step in range(steps):
            sleep(delay)
            if self.visible:
                self.reveal()
            if self.is_done():
                if self.visible:
                    self.reveal()
                return
            self.step()
        if self.visible:
            self.reveal()
    """

    def run(self, steps=1000, delay=1):
        """Run the Environment for given number of time steps,
        but update the GUI too."""
        for step in range(steps):
            self.update(delay)
            if self.is_done():
                break
            self.step()
        self.update(delay)

    def update(self, delay=1):
        sleep(delay)
        if self.visible:
            self.conceal()
            self.reveal()
        else:
            self.reveal()

    def reveal(self):
        """Display the BlockGrid for this world - the last thing to be added
        at a location defines the location color."""
        self.draw_world()
        self.grid.show()
        self.visible = True

    def draw_world(self):
        self.grid[:] = (200, 200, 200)
        world = self.get_world()
        for x in range(0, len(world)):
            for y in range(0, len(world[x])):
                if len(world[x][y]):
                    self.grid[y, x] = self.colors[world[x][y][-1].__class__.__name__]

    def conceal(self):
        """Hide the BlockGrid for this world"""
        self.visible = False
        display(HTML(''))
示例#8
0
tups.sort(key=lambda x: x[1], reverse=True)
ranks = [(rank, elt[0], elt[1])
         for rank, elt in zip(range(0, board_squares), tups)]
palette = [0] * board_squares
for i in range(0, board_squares):
    idx = ranks[i][1]
    palette[idx] = ranks[i][0] * 6.25

# Paint edges
# Top row
for i in range(0, width):
    colour = palette[20 + i]
    grid[0, i] = (colour, colour, colour)

# Bottom row
for i in range(0, width):
    colour = palette[10 - i]
    grid[height - 1, i] = (colour, colour, colour)

# Left side
for i in range(0, height):
    colour = palette[20 - i]
    grid[i, 0] = (colour, colour, colour)

# Right side
for i in range(0, height - 1):
    colour = palette[30 + i]
    grid[i, width - 1] = (colour, colour, colour)

grid.show()
class GraphicEnvironment(XYEnvironment):
    def __init__(self,
                 width=10,
                 height=10,
                 boundary=True,
                 color={},
                 display=False):
        """Defina todas las características habituales del entorno XY,
        pero inicialice un BlockGrid para GUI también."""
        super().__init__(width, height)
        self.grid = BlockGrid(width, height, fill=(200, 200, 200))
        if display:
            self.grid.show()
            self.visible = True
        else:
            self.visible = False
        self.bounded = boundary
        self.colors = color

    def get_world(self):
        """Devuelve todos los artículos del mundo en un formato
        comprensible por el bloque de cuadrícula ipythonblocks."""
        result = []
        x_start, y_start = (0, 0)
        x_end, y_end = self.width, self.height
        for x in range(x_start, x_end):
            row = []
            for y in range(y_start, y_end):
                row.append(self.list_things_at([x, y]))
            result.append(row)
        return result

    def run(self, steps=1000, delay=1):
        """Ejecute el entorno durante un número determinado de pasos de tiempo,
        pero actualiza la GUI también."""
        for step in range(steps):
            sleep(delay)
            if self.visible:
                self.reveal()
            if self.is_done():
                if self.visible:
                    self.reveal()
                return
            self.step()
        if self.visible:
            self.reveal()

    """def run(self, steps=1000, delay=1):
        "Ejecute el entorno durante un número determinado de pasos de tiempo,
        pero actualiza la GUI también."
        for step in range(steps):
            self.update(delay)
            if self.is_done():
                break
            self.step()
        self.update(delay)"""

    def update(self, delay=1):
        sleep(delay)
        if self.visible:
            self.conceal()
            self.reveal()
        else:
            self.reveal()

    def reveal(self):
        """Muestre BlockGrid para este mundo: lo último que se agregará
        en una ubicación define el color de la ubicación."""
        self.draw_world()
        self.grid.show()
        self.visible = True

    def draw_world(self):
        self.grid[:] = (200, 200, 200)
        world = self.get_world()
        for x in range(0, len(world)):
            for y in range(0, len(world[x])):
                if len(world[x][y]):
                    self.grid[y, x] = self.colors[world[x][y]
                                                  [-1].__class__.__name__]

    def conceal(self):
        """Ocultar el BlockGrid para este mundo"""
        self.visible = False
        display(HTML(''))
示例#10
0
class GraphicEnvironment(XYEnvironment):
    def __init__(self,
                 width=10,
                 height=10,
                 boundary=True,
                 color={},
                 display=False):
        """define all the usual XYEnvironment characteristics,
        but initialise a BlockGrid for GUI too"""
        super().__init__(width, height)
        self.grid = BlockGrid(width, height, fill=(200, 200, 200))
        if display:
            self.grid.show()
            self.visible = True
        else:
            self.visible = False
        self.bounded = boundary
        self.colors = color

    #def list_things_at(self, location, tclass=Thing): # need to override because locations
    #    """Return all things exactly at a given location."""
    #    return [thing for thing in self.things
    #            if thing.location == location and isinstance(thing, tclass)]

    def get_world(self):
        """Returns all the items in the world in a format
        understandable by the ipythonblocks BlockGrid"""
        result = []
        x_start, y_start = (0, 0)
        x_end, y_end = self.width, self.height
        for x in range(x_start, x_end):
            row = []
            for y in range(y_start, y_end):
                row.append(self.list_things_at([x, y]))
            result.append(row)
        return result

    """def run(self, steps=1000, delay=1):
        "" "Run the Environment for given number of time steps,
        but update the GUI too." ""
        for step in range(steps):
            sleep(delay)
            if self.visible:
                self.reveal()
            if self.is_done():
                if self.visible:
                    self.reveal()
                return
            self.step()
        if self.visible:
            self.reveal()
    """

    def run(self, steps=1000, delay=1):
        """Run the Environment for given number of time steps,
        but update the GUI too."""
        for step in range(steps):
            self.update(delay)
            if self.is_done():
                break
            self.step()
        self.update(delay)

    def update(self, delay=1):
        sleep(delay)
        if self.visible:
            self.conceal()
            self.reveal()
        else:
            self.reveal()

    def reveal(self):
        """display the BlockGrid for this world - the last thing to be added
        at a location defines the location color"""
        #print("Grid={}".format(self.grid))
        self.draw_world()
        #if not self.visible == True:
        #    self.grid.show()
        self.grid.show()
        self.visible == True

    def draw_world(self):
        self.grid[:] = (200, 200, 200)
        world = self.get_world()
        #print("world {}".format(world))
        for x in range(0, len(world)):
            for y in range(0, len(world[x])):
                if len(world[x][y]):
                    self.grid[y, x] = self.colors[world[x][y]
                                                  [-1].__class__.__name__]
                    #print('location: ({}, {}) got color: {}'
                    #.format(y, x, self.colors[world[x][y][-1].__class__.__name__]))

    def conceal(self):
        """hide the BlockGrid for this world"""
        self.visible = False
        display(HTML(''))
示例#11
0
class my_gridworld():
    def __init__(self):
        ### initialize grid, agent, obstacles, etc.,
        self.width = 6
        self.height = 6
        self.grid = BlockGrid(self.height, self.width, fill=(234, 123, 234))

        # decide on obstacle and goal locations
        self.obstacles = [[1, 2], [3, 4], [2, 3],
                          [2, 1]]  # impenetrable obstacle locations
        self.goal = [4, 4]  # goal block
        self.player = [1, 1]  # initial location player

        # enumerate states based on obstacle locations
        self.states = []
        for i in range(self.grid.height):
            for j in range(self.grid.width):
                block = [i, j]
                if block not in self.obstacles and block not in self.goal:
                    self.states.append(str(i) + str(j))

        # initialize Q^* matrix
        self.Q_star = np.zeros((self.grid.width**2 - len(self.obstacles), 5))

        # initialize action choices
        self.action_choices = [[-1, 0], [0, -1], [1, 0], [0, 1], [0, 0]]

    def color_grid(self):
        # remake + recolor grid
        self.grid = BlockGrid(self.width, self.height, fill=(234, 123, 234))

        # color obstacles
        for i in range(len(self.obstacles)):
            self.grid[self.obstacles[i][0], self.obstacles[i][1]].red = 0

        # make and color goal
        self.grid[self.goal[0], self.goal[1]].green = 255
        self.grid[self.goal[0], self.goal[1]].red = 0
        self.grid[self.goal[0], self.goal[1]].blue = 0

        # color player location
        self.grid[self.player[0], self.player[1]].green = 0
        self.grid[self.player[0], self.player[1]].red = 0
        self.grid[self.player[0], self.player[1]].blue = 0

        self.grid.show()

    # make rewards array -
    def make_rewards(self):
        # create reward matrix
        R = -1 * np.ones((5, 5))
        R[goal[0], goal[1]] = 1000
        for i in range(len(obstacles)):
            R[obstacles[i][0], obstacles[i][1]] = 0

    ## Q-learning function
    def qlearn(self, gamma):
        num_episodes = 1000
        num_complete = 0

        # loop over episodes, for each run simulation and update Q
        for n in range(num_episodes):
            # pick random initialization - make sure its not an obstacle or goal
            obstical_free = 0
            loc = 0
            while obstical_free == 0:
                loc = [
                    np.random.randint(self.grid.width),
                    np.random.randint(self.grid.height)
                ]
                if loc not in self.obstacles:
                    obstical_free = 1

            # update Q matrix while loc != goal
            steps = 0
            max_steps = 200
            while steps < max_steps and loc != self.goal:

                # choose action - left = 0, right = 1, up = 2, down = 3, 4 = stay still
                k = np.random.randint(5)
                loc2 = [sum(x) for x in zip(loc, self.action_choices[k])]

                # check that new location within grid boundaries, if trying to go outside boundary -- either way can't move there!  So just place -1 for this location in Q
                if loc2[0] > self.grid.width - 1 or loc2[0] < 0 or loc2[
                        1] > self.grid.height - 1 or loc2[
                            1] < 0 or loc2 in self.obstacles:
                    # don't move location
                    ind_old = self.states.index(str(loc[0]) + str(loc[1]))
                    self.Q_star[ind_old, k] = -1
                else:
                    # we have a valid movement, if new state is goal set reward to 1000, otherwise set it to 0
                    r_k = 0
                    if loc2 == self.goal:
                        r_k = int(1000)

                    # update Q* matrix
                    ind_old = self.states.index(str(loc[0]) + str(loc[1]))
                    ind_new = self.states.index(str(loc2[0]) + str(loc2[1]))
                    self.Q_star[ind_old,
                                k] = r_k + gamma * max(self.Q_star[ind_new, :])

                    # update current location - one we just moved too
                    loc = loc2

                # update counter
                steps += 1
        print 'q-learning process complete'

    # print out
    def show_qmat(self):
        df = pd.DataFrame(self.Q_star,
                          columns=['up', 'down', 'left', 'right', 'still'],
                          index=self.states)
        print df.round(3)

    # animate the player based on completed Q-learning cycle
    def animate_movement(self, loc):
        # show movement based on an initial
        self.player = loc  # initial agent location
        self.color_grid()
        time.sleep(0.3)
        display.clear_output(wait=True)

        # if you chose an invalid starting position, break out and try again
        if loc in self.obstacles or loc == self.goal or loc[
                0] > self.grid.width - 1 or loc[0] < 0 or loc[
                    1] > self.grid.height - 1 or loc[1] < 0:
            print 'initialization is an obstacle or goal, initialize again'
        else:
            # now use the learned Q* matrix to run from any (valid) initial point to the goal
            count = 0
            max_count = self.grid.width * self.grid.height
            while count < max_count:
                # find next state using max Q* value
                ind_old = self.states.index(
                    str(self.player[0]) + str(self.player[1]))

                # find biggest value in Q* and determine block location
                action_ind = np.argmax(self.Q_star[ind_old, :])
                action = self.action_choices[action_ind]

                # move player to new location and recolor
                self.player = [sum(x) for x in zip(self.player, action)]

                # clear current screen for next step
                self.color_grid()
                time.sleep(0.3)
                if self.player == self.goal:
                    break
                display.clear_output(wait=True)
                count += 1