Exemple #1
0
    def setWeights(self, weights):
        """
        Plot the logistic regression line for given weights
        
        This will draw on the existing pacman window with the existing points
        
        weights: array or list of 2 values (or if just one value, the bias weight is assumed to be zero). If None,
            no line is drawn. Default: None
        """
        weights = np.array(weights)

        if weights.size >= 2:
            w = weights[0]
            b = weights[1]
        else:
            w = float(weights)
            b = 0

        xmin = 1 - self.xShift
        xmax = self.width - 2 - self.xShift

        x = np.linspace(xmin, xmax, 30)
        y = 1.0 / (1 + np.exp(-(w * x + b)))
        x += self.xShift
        y += self.yShift

        if self.line is not None:
            for obj in self.line:
                graphicsUtils.remove_from_screen(obj)

        self.line = []

        prevPoint = self.to_screen((x[0], y[0]))
        for i in xrange(1, len(x)):
            point = self.to_screen((x[i], y[i]))
            self.line.append(graphicsUtils.line(prevPoint, point, LINE_COLOR))
            prevPoint = point


#         prevPoint = self.to_screen((x[0],y[0]))
#         for i in xrange(1,len(x)):
#             point = self.to_screen((x[i],y[i]))
#             line.append(graphicsUtils.line(prevPoint, point, LINE_COLOR, filled=0, behind=0)
#
#             prevPoint = point

        if self.addPacmanToLineStart is True and len(self.agentImages) > 0:
            # Bring pacman to front of display
            graphicsUtils._canvas.tag_raise(self.agentImages[0][1][0])

            # Put pacman at beginning of line
            if w >= 0:
                self.movePacman((x[0] - 0.5, y[0]), Directions.EAST,
                                self.agentImages[0][1])
            else:
                self.movePacman((x[-1] + 0.5, y[-1]), Directions.WEST,
                                self.agentImages[0][1])

        graphicsUtils.refresh()
        graphicsUtils.sleep(1)
Exemple #2
0
    def setWeights(self, weights):
        """
        Plot the logistic regression line for given weights
        
        This will draw on the existing pacman window with the existing points
        
        weights: array or list of 2 values (or if just one value, the bias weight is assumed to be zero). If None,
            no line is drawn. Default: None
        """
        weights = np.array(weights)

        if weights.size >= 2:
            w = weights[0]
            b = weights[1]
        else:
            w = float(weights)
            b = 0
            
        xmin = 1 - self.xShift
        xmax = self.width-2 - self.xShift
    
        x = np.linspace(xmin, xmax,30)
        y = 1.0/(1+np.exp(-(w*x+b)))
        x += self.xShift
        y += self.yShift

        if self.line is not None:
            for obj in self.line:
                graphicsUtils.remove_from_screen(obj)
                 
        self.line = []
        
        prevPoint = self.to_screen((x[0],y[0]))
        for i in xrange(1,len(x)):
            point = self.to_screen((x[i],y[i]))
            self.line.append(graphicsUtils.line(prevPoint, point, LINE_COLOR))
            prevPoint = point
            
#         prevPoint = self.to_screen((x[0],y[0]))
#         for i in xrange(1,len(x)):
#             point = self.to_screen((x[i],y[i]))
#             line.append(graphicsUtils.line(prevPoint, point, LINE_COLOR, filled=0, behind=0)
#                         
#             prevPoint = point

            
        if self.addPacmanToLineStart is True and len(self.agentImages) > 0:
            # Bring pacman to front of display
            graphicsUtils._canvas.tag_raise(self.agentImages[0][1][0])
            
            # Put pacman at beginning of line
            if w >= 0:
                self.movePacman((x[0]-0.5,y[0]), Directions.EAST, self.agentImages[0][1])
            else:
                self.movePacman((x[-1]+0.5,y[-1]), Directions.WEST, self.agentImages[0][1])

        graphicsUtils.refresh()
    def drawWalls(self, wallMatrix):
        wallColor = WALL_COLOR
        for xNum, x in enumerate(wallMatrix):
            if self.capture and (xNum * 2) < wallMatrix.width:
                wallColor = TEAM_COLORS[0]
            if self.capture and (xNum * 2) >= wallMatrix.width:
                wallColor = TEAM_COLORS[1]

            for yNum, cell in enumerate(x):
                if cell:  # There's a wall here
                    pos = (xNum, yNum)
                    screen = self.to_screen(pos)
                    screen2 = self.to_screen2(pos)

                    # Draw each quadrant of the square based on adjacent walls
                    wIsWall = self.isWall(xNum - 1, yNum, wallMatrix)
                    eIsWall = self.isWall(xNum + 1, yNum, wallMatrix)
                    nIsWall = self.isWall(xNum, yNum + 1, wallMatrix)
                    sIsWall = self.isWall(xNum, yNum - 1, wallMatrix)
                    nwIsWall = self.isWall(xNum - 1, yNum + 1, wallMatrix)
                    swIsWall = self.isWall(xNum - 1, yNum - 1, wallMatrix)
                    neIsWall = self.isWall(xNum + 1, yNum + 1, wallMatrix)
                    seIsWall = self.isWall(xNum + 1, yNum - 1, wallMatrix)

                    # NE quadrant
                    if (not nIsWall) and (not eIsWall):
                        # inner circle
                        gU.circle(screen2, WALL_RADIUS * self.gridSize,
                                  wallColor, wallColor, (0, 91), 'arc')
                    if (nIsWall) and (not eIsWall):
                        # vertical line
                        gU.line(add(screen, (self.gridSize * WALL_RADIUS, 0)),
                                add(screen, (self.gridSize * WALL_RADIUS,
                                             self.gridSize * (-0.5) - 1)), wallColor)
                    if (not nIsWall) and (eIsWall):
                        # horizontal line
                        gU.line(add(screen,
                                    (0, self.gridSize * (-1) * WALL_RADIUS)),
                                add(screen, (self.gridSize * 0.5 + 1,
                                             self.gridSize * (-1) *
                                             WALL_RADIUS)),
                                wallColor)
                    if (nIsWall) and (eIsWall) and (not neIsWall):
                        # outer circle
                        gU.circle(add(screen2, (self.gridSize * 2 * WALL_RADIUS,
                                                self.gridSize * (-2) * WALL_RADIUS)),
                                  WALL_RADIUS * self.gridSize - 1, wallColor,
                                  wallColor, (180, 271), 'arc')
                        gU.line(add(screen, (self.gridSize * 2 * WALL_RADIUS - 1,
                                             self.gridSize * (-1) * WALL_RADIUS)),
                                add(screen, (self.gridSize * 0.5 + 1,
                                             self.gridSize * (-1) * WALL_RADIUS)),
                                wallColor)
                        gU.line(add(screen, (self.gridSize * WALL_RADIUS,
                                             self.gridSize * (-2) * WALL_RADIUS + 1)),
                                add(screen, (self.gridSize * WALL_RADIUS,
                                             self.gridSize * (-0.5))), wallColor)

                    # NW quadrant
                    if (not nIsWall) and (not wIsWall):
                        # inner circle
                        gU.circle(screen2, WALL_RADIUS * self.gridSize,
                                  wallColor, wallColor, (90, 181), 'arc')
                    if (nIsWall) and (not wIsWall):
                        # vertical line
                        gU.line(add(screen,
                                    (self.gridSize * (-1) * WALL_RADIUS, 0)),
                                add(screen, (self.gridSize * (-1) * WALL_RADIUS,
                                             self.gridSize * (-0.5) - 1)), wallColor)
                    if (not nIsWall) and (wIsWall):
                        # horizontal line
                        gU.line(add(screen,
                                    (0, self.gridSize * (-1) * WALL_RADIUS)),
                                add(screen, (self.gridSize * (-0.5) - 1,
                                             self.gridSize * (-1) * WALL_RADIUS)),
                                wallColor)
                    if (nIsWall) and (wIsWall) and (not nwIsWall):
                        # outer circle
                        gU.circle(add(screen2,
                                      (self.gridSize * (-2) * WALL_RADIUS,
                                       self.gridSize * (-2) * WALL_RADIUS)),
                                  WALL_RADIUS * self.gridSize - 1, wallColor,
                                  wallColor, (270, 361), 'arc')
                        gU.line(add(screen,
                                    (self.gridSize * (-2) * WALL_RADIUS + 1,
                                     self.gridSize * (-1) * WALL_RADIUS)),
                                add(screen, (self.gridSize * (-0.5),
                                             self.gridSize * (-1) * WALL_RADIUS)),
                                wallColor)
                        gU.line(add(screen, (self.gridSize * (-1) * WALL_RADIUS,
                                             self.gridSize * (-2) * WALL_RADIUS + 1)),
                                add(screen, (self.gridSize * (-1) * WALL_RADIUS,
                                             self.gridSize * (-0.5))), wallColor)

                    # SE quadrant
                    if (not sIsWall) and (not eIsWall):
                        # inner circle
                        gU.circle(screen2, WALL_RADIUS * self.gridSize,
                                  wallColor, wallColor, (270, 361), 'arc')
                    if (sIsWall) and (not eIsWall):
                        # vertical line
                        gU.line(add(screen, (self.gridSize * WALL_RADIUS, 0)),
                                add(screen, (self.gridSize * WALL_RADIUS,
                                             self.gridSize * (0.5) + 1)), wallColor)
                    if (not sIsWall) and (eIsWall):
                        # horizontal line
                        gU.line(add(screen,
                                    (0, self.gridSize * (1) * WALL_RADIUS)),
                                add(screen, (self.gridSize * 0.5 + 1,
                                             self.gridSize * (1) * WALL_RADIUS)),
                                wallColor)
                    if (sIsWall) and (eIsWall) and (not seIsWall):
                        # outer circle
                        gU.circle(add(screen2, (self.gridSize * 2 * WALL_RADIUS,
                                                self.gridSize * (2) * WALL_RADIUS)),
                                  WALL_RADIUS * self.gridSize - 1, wallColor,
                                  wallColor, (90, 181), 'arc')
                        gU.line(add(screen, (self.gridSize * 2 * WALL_RADIUS - 1,
                                             self.gridSize * (1) * WALL_RADIUS)),
                                add(screen, (self.gridSize * 0.5,
                                             self.gridSize * (1) * WALL_RADIUS)),
                                wallColor)
                        gU.line(add(screen, (self.gridSize * WALL_RADIUS,
                                             self.gridSize * (2) * WALL_RADIUS - 1)),
                                add(screen, (self.gridSize * WALL_RADIUS,
                                             self.gridSize * (0.5))), wallColor)

                    # SW quadrant
                    if (not sIsWall) and (not wIsWall):
                        # inner circle
                        gU.circle(screen2, WALL_RADIUS * self.gridSize,
                                  wallColor, wallColor, (180, 271), 'arc')
                    if (sIsWall) and (not wIsWall):
                        # vertical line
                        gU.line(add(screen,
                                    (self.gridSize * (-1) * WALL_RADIUS, 0)),
                                add(screen, (self.gridSize * (-1) * WALL_RADIUS,
                                             self.gridSize * (0.5) + 1)), wallColor)
                    if (not sIsWall) and (wIsWall):
                        # horizontal line
                        gU.line(add(screen,
                                    (0, self.gridSize * (1) * WALL_RADIUS)),
                                add(screen, (self.gridSize * (-0.5) - 1,
                                             self.gridSize * (1) * WALL_RADIUS)),
                                wallColor)
                    if (sIsWall) and (wIsWall) and (not swIsWall):
                        # outer circle
                        gU.circle(add(screen2,
                                      (self.gridSize * (-2) * WALL_RADIUS,
                                       self.gridSize * (2) * WALL_RADIUS)),
                                  WALL_RADIUS * self.gridSize - 1,
                                  wallColor, wallColor, (0, 91), 'arc')
                        gU.line(add(screen,
                                    (self.gridSize * (-2) * WALL_RADIUS + 1,
                                     self.gridSize * (1) * WALL_RADIUS)),
                                add(screen, (self.gridSize * (-0.5),
                                             self.gridSize * (1) * WALL_RADIUS)),
                                wallColor)
                        gU.line(add(screen, (self.gridSize * (-1) * WALL_RADIUS,
                                             self.gridSize * (2) * WALL_RADIUS - 1)),
                                add(screen, (self.gridSize * (-1) * WALL_RADIUS,
                                             self.gridSize * (0.5))), wallColor)