예제 #1
0
class twodWorld:
    def __init__(self): 
        self.bot = twodBot()
        self.intprt = Interpreter(cfg.INITIALSTATE)
        self.sensorVal = self.intprt.getInitialSensor()

    def update(self):
        bot = self.bot
        motor = bot.getMotorCommand(self.sensorVal)
            # two things happen here: robot receives sensory input; then
            # it issues a motor command
        self.sensorVal = self.intprt.getSensor(motor)
            # similarly, the interpreter executes the motor command; then
            # computes the new sensory input for the robot
        
        # self.redraw(self.bot)  #GRAPHICS
        

    def redraw(self, bot):
        pos = self.intprt.getBotPosition()
        bot.x = pos[0]
        bot.y = pos[1]
        bot.shape.center = (bot.x, bot.y)
        # bot.heading = pos[2] #HEADLINE
        # x1 = bot.x + bot.radius * np.cos(bot.heading)  #HEADLINE
        # y1 = bot.y + bot.radius * np.sin(bot.heading) #HEADLINE
        # plt.setp(bot.hline,'xdata', (bot.x, x1)) #HEADLINE
        # plt.setp(bot.hline,'ydata', (bot.y, y1)) #HEADLINE

    def printState(self):
        self.intprt.printState()
        # return
        
        tree = self.bot.memTree
        # error history from memTree
        errors = tree.errors[cfg.TRAINING:]
        errors1 = self.bot.getErrorHistory1()[cfg.TRAINING:]

        fig = plt.figure(2,figsize=(20,9))
        # ax1 = fig.add_subplot(211)
        avgerrors = [sum(errors[i*10:i*10+9])/10 for i in xrange((cfg.NTICKS-cfg.TRAINING)/10)]
        avgerrors1 = [sum(errors1[i*10:i*10+9])/10 for i in xrange((cfg.NTICKS-cfg.TRAINING)/10)]
        # plt.plot(errors,'b')
        # plt.plot(errors1,'r')
        # plt.ylabel("prediction error")
        # plt.ylabel("prediction error")
        # ax2 = fig.add_subplot(212)
        # ax2.axis([0,cfg.NTICKS,-1,3])
        # sns0 = tree.sensors[:,0]
        # plt.plot(sns0,'r')
        # pred0 = tree.predictions[:,0]        
        # plt.plot(pred0,'b')
        plt.plot(avgerrors,'b')
        plt.plot(avgerrors1,'r')
        plt.ylabel("average error")

        testErr = np.mean(errors)
        testErrFlat = np.mean(errors1)
        print "testing error: {}".format(testErr)
        print "flat: {}; ".format(testErrFlat)

    def getErrorsPerRun(self):
        tree = self.bot.memTree
        # error history from memTree
        errors = tree.errors[cfg.TRAINING:]
        errors1 = self.bot.getErrorHistory1()[cfg.TRAINING:]
        return [errors,errors1]
예제 #2
0
class twodWorld:
    def __init__(self): 
        self.bot = twodBot()
        self.intprt = Interpreter(cfg.INITIALSTATE)
        self.sensorVal = self.intprt.getInitialSensor()

    def update(self):
        bot = self.bot
        motor = bot.getMotorCommand(self.sensorVal)
            # two things happen here: robot receives sensory input; then
            # it issues a motor command
        self.sensorVal = self.intprt.getSensor(motor)
            # similarly, the interpreter executes the motor command; then
            # computes the new sensory input for the robot
        if cfg.GRAPHICS:
            self.redraw(self.bot)  #GRAPHICS
        

    def redraw(self, bot):
        pos = self.intprt.getBotPosition()
        bot.x = pos[0]
        bot.y = pos[1]
        bot.shape.center = (bot.x, bot.y)
        # bot.heading = pos[2] #HEADLINE
        # x1 = bot.x + bot.radius * np.cos(bot.heading)  #HEADLINE
        # y1 = bot.y + bot.radius * np.sin(bot.heading) #HEADLINE
        # plt.setp(bot.hline,'xdata', (bot.x, x1)) #HEADLINE
        # plt.setp(bot.hline,'ydata', (bot.y, y1)) #HEADLINE

    def printState(self):
        self.intprt.printState()
        # return
        
        tree = self.bot.memTree
        # error history from memTree
        errors = tree.errors
        errors1 = self.bot.getErrorHistory1()
        # minErrors = self.intprt.getErrors()

        fig = plt.figure(2,figsize=(20,9))
        avgerrors = [sum(errors[i*50:i*50+49])/50 for i in xrange((cfg.NTICKS)/50)]
        avgerrors1 = [sum(errors1[i*50:i*50+49])/50 for i in xrange((cfg.NTICKS)/50)]
        # avgerrorsMin = [sum(minErrors[i*50:i*50+49])/50 for i in xrange((cfg.NTICKS)/50)]

        plt.plot(avgerrors,'b')
        plt.plot(avgerrors1,'r')
        # plt.plot(avgerrorsMin,'g')
        plt.ylabel("average error")

        Err = np.mean(errors[cfg.TRAINING:])
        ErrFlat = np.mean(errors1[cfg.TRAINING:])
        # ErrMin = np.mean(minErrors[cfg.TRAINING:])
        print "testing error: {}".format(Err)
        print "flat: {}; ".format(ErrFlat)
        # print "minimum: {}; ".format(ErrMin)

    def getErrorsPerRun(self):
        tree = self.bot.memTree
        # error history from memTree
        errors = tree.errors
        errors1 = self.bot.getErrorHistory1()
        # errorsM = self.intprt.getErrors()
        return [errors,errors1]

    def getFootPrint(self):
        return self.intprt.getFootPrint()