Example #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()
Example #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()
Example #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
Example #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