예제 #1
0
    def setWeights(self, weights):
        """
        Plot the linear regression line for given weights; assuming h_w(x) = weights[0]*x + weights[1].

        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 = min(x)
#         xmax = max(x)
#
#         ymin = w*xmin + b
#         ymax = w*xmax + b
#
#         point1 = (xmin+self.xShift, ymin+self.yShift)
#         point2 = (xmax+self.xShift, ymax+self.yShift)

        (point1, point2) = plotUtil.lineBoxIntersection(
            w, -1, b, 1 - self.xShift, 1 - self.yShift,
            self.width - 2 - self.xShift, self.height - 2 - self.yShift)

        if point1 is not None and point2 is not None:
            point1 = (point1[0] + self.xShift, point1[1] + self.yShift)
            point2 = (point2[0] + self.xShift, point2[1] + self.yShift)

            dx = point2[0] - point1[0]
            dy = point2[1] - point1[1]
            if dx == 0:
                angle = 90 + 180 * dy * 1.0 / abs(dy)
            else:
                angle = math.atan(dy * 1.0 / dx) * 180.0 / math.pi

            if self.line is not None:
                graphicsUtils.remove_from_screen(self.line)
            self.line = graphicsUtils.polygon(
                [self.to_screen(point1),
                 self.to_screen(point2)],
                LINE_COLOR,
                filled=0,
                behind=0)

            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
                self.movePacman(point1, angle, self.agentImages[0][1])

            graphicsUtils.refresh()
예제 #2
0
    def setWeights(self, weights):
        """
        Plot the linear regression line for given weights; assuming h_w(x) = weights[0]*x + weights[1].
        
        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 = min(x)
#         xmax = max(x)
#         
#         ymin = w*xmin + b
#         ymax = w*xmax + b        
#         
#         point1 = (xmin+self.xShift, ymin+self.yShift)
#         point2 = (xmax+self.xShift, ymax+self.yShift)

        (point1, point2) = plotUtil.lineBoxIntersection(w, -1, b,
                                                        1-self.xShift, 1-self.yShift,
                                                        self.width-2-self.xShift, self.height-2-self.yShift)
        
        if point1 is not None and point2 is not None:
            point1 = (point1[0]+self.xShift, point1[1]+self.yShift)
            point2 = (point2[0]+self.xShift, point2[1]+self.yShift)
            
            dx = point2[0]-point1[0]
            dy = point2[1]-point1[1]
            if dx == 0:
                angle = 90 + 180*dy*1.0/abs(dy)
            else:
                angle = math.atan(dy*1.0/dx)*180.0/math.pi
                   
            if self.line is not None:
                graphicsUtils.remove_from_screen(self.line) 
            self.line = graphicsUtils.polygon([self.to_screen(point1), self.to_screen(point2)], LINE_COLOR, filled=0, behind=0)
                
            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
                self.movePacman(point1, angle, self.agentImages[0][1])

            graphicsUtils.refresh()
예제 #3
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 self.prev_weights is not None:
            if np.allclose(self.prev_weights, weights):
                return False  # no line update
        self.prev_weights = weights.copy()

        w1 = weights[0]
        w2 = weights[1]
        if weights.size >= 3:
            b = weights[2]
        else:
            b = 0

        # Line functions
        # Line where w1*x1 + w2*x2 + b = 0
        # x2 = -(w1*x1 + b)/w2  or
        # x1 = -(w2*x2 + b)/w1

        # Figure out where line intersections bounding box around points
        if w1 == 0 and w2 == 0:
            return

        (point1, point2) = plotUtil.lineBoxIntersection(
            w1, w2, b, 1 - self.xShift, 1 - self.yShift,
            self.width - 2 - self.xShift, self.height - 2 - self.yShift)

        if point1 is not None and point2 is not None:
            point1 = (point1[0] + self.xShift, point1[1] + self.yShift)
            point2 = (point2[0] + self.xShift, point2[1] + self.yShift)

            if self.line is not None:
                graphicsUtils.remove_from_screen(self.line)
            self.line = graphicsUtils.polygon(
                [self.to_screen(point1),
                 self.to_screen(point2)],
                LINE_COLOR,
                filled=0,
                behind=0)

        graphicsUtils.refresh()
        return True  # updated line
예제 #4
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 self.prev_weights is not None:
            if np.allclose(self.prev_weights, weights):
                return False # no line update
        self.prev_weights = weights.copy()

        w1 = weights[0]
        w2 = weights[1]
        if weights.size >= 3:
            b = weights[2]
        else:
            b = 0

        # Line functions
        # Line where w1*x1 + w2*x2 + b = 0
        # x2 = -(w1*x1 + b)/w2  or
        # x1 = -(w2*x2 + b)/w1

        # Figure out where line intersections bounding box around points
        if w1 == 0 and w2 == 0:
            return

        (point1, point2) = plotUtil.lineBoxIntersection(w1, w2, b,
                                                        1-self.xShift, 1-self.yShift,
                                                        self.width-2-self.xShift, self.height-2-self.yShift)

        if point1 is not None and point2 is not None:
            point1 = (point1[0]+self.xShift, point1[1]+self.yShift)
            point2 = (point2[0]+self.xShift, point2[1]+self.yShift)

            if self.line is not None:
                graphicsUtils.remove_from_screen(self.line)
            self.line = graphicsUtils.polygon([self.to_screen(point1), self.to_screen(point2)], LINE_COLOR, filled=0, behind=0)

        graphicsUtils.refresh()
        return True # updated line
예제 #5
0
    def drawGhost(self, ghost, agentIndex):
        pos = self.getPosition(ghost)
        dir = self.getDirection(ghost)
        (screen_x, screen_y) = (self.to_screen(pos))
        coords = []
        for (x, y) in GHOST_SHAPE:
            coords.append((x * self.gridSize * GHOST_SIZE + screen_x,
                           y * self.gridSize * GHOST_SIZE + screen_y))

        colour = self.getGhostColor(ghost, agentIndex)
        body = gU.polygon(coords, colour, filled=1)
        WHITE = gU.formatColor(1.0, 1.0, 1.0)
        BLACK = gU.formatColor(0.0, 0.0, 0.0)

        dx = 0
        dy = 0
        if dir == 'North':
            dy = -0.2
        if dir == 'South':
            dy = 0.2
        if dir == 'East':
            dx = 0.2
        if dir == 'West':
            dx = -0.2
        leftEye = gU.circle((screen_x + self.gridSize * GHOST_SIZE * (-0.3 + dx / 1.5),
                             screen_y - self.gridSize * GHOST_SIZE * (0.3 - dy / 1.5)),
                            self.gridSize * GHOST_SIZE * 0.2, WHITE, WHITE)
        rightEye = gU.circle((screen_x + self.gridSize * GHOST_SIZE * (0.3 + dx / 1.5),
                              screen_y - self.gridSize * GHOST_SIZE * (0.3 - dy / 1.5)),
                             self.gridSize * GHOST_SIZE * 0.2, WHITE, WHITE)
        leftPupil = gU.circle((screen_x + self.gridSize * GHOST_SIZE * (-0.3 + dx),
                               screen_y - self.gridSize * GHOST_SIZE * (0.3 - dy)),
                              self.gridSize * GHOST_SIZE * 0.08, BLACK, BLACK)
        rightPupil = gU.circle((screen_x + self.gridSize * GHOST_SIZE * (0.3 + dx),
                                screen_y - self.gridSize * GHOST_SIZE * (0.3 - dy)),
                               self.gridSize * GHOST_SIZE * 0.08, BLACK, BLACK)
        ghostImageParts = []
        ghostImageParts.append(body)
        ghostImageParts.append(leftEye)
        ghostImageParts.append(rightEye)
        ghostImageParts.append(leftPupil)
        ghostImageParts.append(rightPupil)

        return ghostImageParts