Пример #1
0
    def __init__(self,
                 inputs,
                 labels,
                 featureName,
                 featureNames,
                 featureTypes,
                 subset=[],
                 boundary=None,
                 operator=''):
        #Find the actual feature array based on the feature name
        colIndex = np.where(featureName == featureNames)[0][0]
        features = inputs[:, colIndex]
        #The key feature of noonterminal nodes is that they have a decision
        if featureTypes[colIndex] == 'string':
            #then the feature is of type categorical and the decision must be categorical
            self.decision = Decision.Categorical(featureName, subset)
        else:
            #else the feature is of type numerical and decision must be numerical
            self.decision = Decision.Numerical(featureName, operator, boundary)

        #create the true and false nodes, both unexplored, by filtering the
        #data to each node based on the decision
        trueIndices = np.vectorize(self.decision.function)(features)
        falseIndices = np.logical_not(trueIndices)
        self.trueNode = Node(inputs[trueIndices], labels[trueIndices], '')
        self.falseNode = Node(inputs[falseIndices], labels[falseIndices], '')

        #the node will never use its input field again, but the labels field
        #is needed for tree pruning
        Node.__init__(self, [], labels, self.decision.__str__(), 'decision')
Пример #2
0
def new_generate_agents(num_new_agent, agents):
    """
    new agents generate in Move section
    """
    #num_new_agent = rnd.randrange(num_new_generate)
    new_agents = [Agent_man() for new_agent_id in range(num_new_agent)]
    Move.state_task_for_new_agents(new_agents)
    Decision.initial_strategy(new_agents)
    for id, agent in enumerate(new_agents):
        print(
            f'new_agents No.{id}, state:{agent.state}, next_state:{agent.next_state}, strategy:{agent.strategy}, next_strategy:{agent.next_strategy}, task:{agent.task}'
        )
    agents.extend(new_agents)
Пример #3
0
 def __init__(self):
     self.loading = LoadingScreen(self)
     self.camera = Camera(self)
     self.lighting = Lighting(self)
     self.balltable = BallTable(self)
     self.menu = Menu(self)
     self.decision = Decision(self)
     self.mousekey = MouseKey(self)
     self.shotStrength = ShotStrength(self)
     self.state = GameState.START
     self.mode = GameMode.TUTORIAL
     self.balls = [Ball(self)] * 16
     self.ballinHole = [None] * 16
     self.LastFrameTimePoint = 0
Пример #4
0
	def get_fitness(population):
		def mean_square_error(x,y): return np.sqrt(((x - y) ** 2).mean(axis=0))
		fitness = []
		for ind in population:
			ind = get_phenotype(ind,units_by_layer)
			aux_mean_error = 0
			for (x,y) in S: aux_mean_error += mean_square_error(y,Decision.get_output_vector(units_by_layer,x,ind,factivation))
			fitness.append(aux_mean_error)
		return fitness
Пример #5
0
 def mainAI(self):
     """Run AI mode"""
     start = time.time()
     mainGrid = Grid()
     mainGrid.computerAddTile()
     aiPlayer = Decision()
     while not self.checkGameOver(mainGrid):
         print("\nCOMPUTER TURN:")
         mainGrid.computerAddTile()
         mainGrid.displayGrid()
         if not self.checkGameOver(mainGrid):
             move = aiPlayer.getMove(mainGrid)
             mainGrid.move(move)
             print("\nPLAYER TURN:")
             mainGrid.displayGrid()
     print("\nFINAL GRID:")
     mainGrid.displayGrid()
     end = time.time()
     scores = mainGrid.scores()
     print("Max score: ", mainGrid.getMaxTile())
     print("Total time: ", end - start)
     return scores, end - start
Пример #6
0
def weaponMenuSelectWeapon(num, menu, char, wmenu):
    decision = Decision()
    panel = menu.panels[0]
    panel.clearOptions()
    panel.addOptions([Option("Yes", panel.osize, promptMenuYes, [decision]),\
                      Option("No", panel.osize, promptMenuNo, [decision])])
    menu.addText("Equip?", 0, 0)
    try:
        menu.run()
    except SystemExit:
        pass
    if decision.bool:
        char.equipWeapon(num)
    wpanel = wmenu.panels[0]
    sel = wpanel.selected
    weapon = char.weapons[sel]
    weaponMenuPrintWeapon(weapon, wmenu, char)
Пример #7
0
def armorMenuSelectArmor(num, menu, char, amenu):
    decision = Decision()
    panel = menu.panels[0]
    panel.clearOptions()
    panel.addOptions([Option("Yes", panel.osize, promptMenuYes, [decision]),\
                      Option("No", panel.osize, promptMenuNo, [decision])])
    menu.addText("Equip?", 0, 0)
    try:
        menu.run()
    except SystemExit:
        pass
    if decision.bool:
        char.equipArmor(num)
    apanel = amenu.panels[0]
    sel = apanel.selected
    armor = char.armors[sel]
    armorMenuPrintArmor(armor, amenu, char)
Пример #8
0
	########
	# Aprender vector theta      #
	rho = 0.5
	nu  = 0.5
	l   = 1
	theta1 = MLPLearning.back_propagation_batch(S,rho,units_by_layer,factivation,850,50)
	theta2 = MLPLearning.back_propagation_online(S,rho,units_by_layer,factivation,850,50)
	theta3 = MLPLearning.back_propagation_batch_momentum(S,rho,nu,units_by_layer,factivation,850,50)
	theta4 = MLPLearning.back_propagation_batch_buffer(S,rho,l,units_by_layer,factivation,850,50)
	theta5 = MLPLearning.back_propagation_online_buffer(S,rho,l,units_by_layer,factivation,850,50)
	theta6 = MLPLearning.back_propagation_online_momentum(S,rho,nu,units_by_layer,factivation,850,50)
	theta7,fitness = MLPLearning.evolutional(S,units_by_layer,factivation,200,500,-2,2,1.1,0.9)
	##############################
	
	# Clasificacion #
	logging.info("Clase con theta1: (Backprop batch): "+str(Decision.classify(units_by_layer,[1.0,-6.3,1.0],theta1,factivation)))
	logging.info("Clase con theta2: (Backprop online): "+str(Decision.classify(units_by_layer,[1.0,-6.3,1.0],theta2,factivation)))
	logging.info("Clase con theta3: (Backprop batch con momentum): "+str(Decision.classify(units_by_layer,[1.0,-6.3,1.0],theta3,factivation)))
	logging.info("Clase con theta4: (Backprop batch con amortiguamiento): "+str(Decision.classify(units_by_layer,[1.0,-6.3,1.0],theta4,factivation)))
	logging.info("Clase con theta5: (Backprop online con amortiguamiento): "+str(Decision.classify(units_by_layer,[1.0,-6.3,1.0],theta5,factivation)))
	logging.info("Clase con theta6: (Backprop online con momentum): "+str(Decision.classify(units_by_layer,[1.0,-6.3,1.0],theta6,factivation)))
	logging.info("Clase con theta7: (Algoritmo genetico): "+str(Decision.classify(units_by_layer,[1.0,-5.0,1.0],theta7,factivation))+" fitness: "+str(fitness))

	#################
	# Regresion #
	logging.info("Regresion con theta1: (Backprop batch): "+str(Decision.regression(units_by_layer,[1.0,-6.3,1.0],theta1,factivation)))
	logging.info("Regresion con theta2: (Backprop online): "+str(Decision.regression(units_by_layer,[1.0,-6.3,1.0],theta2,factivation)))
	logging.info("Regresion con theta3: (Backprop batch con momentum): "+str(Decision.regression(units_by_layer,[1.0,-6.3,1.0],theta3,factivation)))
	logging.info("Regresion con theta4: (Backprop batch con amortiguamiento): "+str(Decision.regression(units_by_layer,[1.0,-6.3,1.0],theta4,factivation)))
	logging.info("Regresion con theta5: (Backprop online con amortiguamiento): "+str(Decision.regression(units_by_layer,[1.0,-6.3,1.0],theta5,factivation)))
	logging.info("Regresion con theta6: (Backprop online con momentum): "+str(Decision.regression(units_by_layer,[1.0,-6.3,1.0],theta6,factivation)))
Пример #9
0
class Game:
    '''
	'''
    def __init__(self):
        self.loading = LoadingScreen(self)
        self.camera = Camera(self)
        self.lighting = Lighting(self)
        self.balltable = BallTable(self)
        self.menu = Menu(self)
        self.decision = Decision(self)
        self.mousekey = MouseKey(self)
        self.shotStrength = ShotStrength(self)
        self.state = GameState.START
        self.mode = GameMode.TUTORIAL
        self.balls = [Ball(self)] * 16
        self.ballinHole = [None] * 16
        self.LastFrameTimePoint = 0

    def Load(self):
        glutInit(sys.argv)
        glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH)
        glutInitWindowSize(ParamDef.ScreenResolution[0],
                           ParamDef.ScreenResolution[1])
        glutInitWindowPosition(0, 0)
        glutCreateWindow(APP_NAME)
        glClearColor(0, 0, 0, 0)
        glutIdleFunc(self.loading.Idle)
        glutDisplayFunc(self.loading.UpdateGL)
        self.currentWindow = glutGetWindow()
        glutMainLoop()

    def Run(self):
        ParamDef.DelayCompensation = 1
        glutIgnoreKeyRepeat(1)
        glEnable(GL_DEPTH_TEST)
        glEnable(GL_CULL_FACE)
        glEnable(GL_NORMALIZE)
        glEnable(GL_BLEND)
        glShadeModel(GL_SMOOTH)
        glHint(GL_POLYGON_SMOOTH_HINT, GL_NICEST)
        glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST)

        glutMouseFunc(self.mousekey.MouseClick)
        glutMotionFunc(self.mousekey.MouseMove)
        glutKeyboardFunc(self.mousekey.KeyPress)
        glutKeyboardUpFunc(self.mousekey.KeyRelease)
        glutSpecialFunc(self.mousekey.SpecialKeyPress)
        glutSpecialUpFunc(self.mousekey.SpecialKeyRelease)

        glutVisibilityFunc(self.Visible)
        glutIdleFunc(self.timerEvent)
        glutDisplayFunc(self.updateGL)

    def timerEvent(self):
        #print "timerEvent"
        ParamDef.FrameTimePoint = glutGet(GLUT_ELAPSED_TIME) / 10
        ParamDef.Factor = ParamDef.FrameTimePoint - self.LastFrameTimePoint

        if (ParamDef.DelayCompensation != 0):
            ParamDef.Factor = 1
            ParamDef.DelayCompensation = 0

        if (ParamDef.Factor != 0):
            if (ParamDef.ShowFPS):
                if ((ParamDef.FrameTimePoint % 200) <
                    (self.LastFrameTimePoint % 200)):
                    self.menu.SetFPS(Frames / 2)
                    ParamDef.Frames = 0
                else:
                    ParamDef.Frames += 1

            self.menu.Update(ParamDef.Factor)

            if self.state == GameState.START:
                self.StartHandling()
            elif self.state == GameState.VIEWING:
                self.ViewingHandling()
            elif self.state == GameState.AIMING:
                self.AimHandling()
            elif self.state == GameState.SWING:
                self.BackswingHandling()
            elif self.state == GameState.SHOT:
                self.ShotHandling()
            elif self.state == GameState.NEW_WHITE:
                self.NewWhiteHandling()
            elif self.state == GameState.JUDGEING:
                self.JudgeHandling()

            self.camera.Ride(ParamDef.Factor)
            self.LastFrameTimePoint = ParamDef.FrameTimePoint
            glutPostWindowRedisplay(self.currentWindow)

    def updateGL(self):
        '''
		'''
        #print "updateGL"
        if ParamDef.ZBufferDelete != 0:
            glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT)

        self.camera.draw()
        self.lighting.draw()
        self.balltable.drawSurface()

        glDisable(GL_DEPTH_TEST)
        self.balltable.drawLine()
        for ball in self.balls:
            ball.drawShadow()
        glEnable(GL_DEPTH_TEST)

        self.balltable.drawBorder()

        distance = 0
        #distance
        resolution = 1
        #Display resolution
        for ball in self.balls:
            x = ball.Pos_xCM() - self.camera.Pos_x
            y = ball.Pos_yCM() - self.camera.Pos_y
            z = self.camera.Pos_z
            distance = sqrt(x * x + y * y + z * z)
            resolution = (400 / distance)
            if (resolution < 3):
                resolution = 3
            resolution = (resolution / 2) * 2 + 1
            if (resolution > ParamDef.BallResolution):
                resolution = ParamDef.BallResolution
            ball.draw(resolution)

        glDisable(GL_DEPTH_TEST)
        glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA)
        glDisable(GL_LIGHTING)

        self.menu.draw()

        if (self.state != GameState.START):
            self.shotStrength.draw()

        glEnable(GL_DEPTH_TEST)

        glutSwapBuffers()

    def Visible(self, visible):
        print "Visible"
        if (visible == GLUT_VISIBLE):
            glutIdleFunc(self.timerEvent)

    def StartHandling(self):
        self.camera.ScenicFlight(ParamDef.Factor)
        self.menu.Update(ParamDef.Factor)

    def ViewingHandling(self):
        self.menu.Update(ParamDef.Factor)

        if (KEY_UP):
            self.camera.Move_Front(ParamDef.Factor)
        if (KEY_DOWN):
            self.camera.Move_Back(ParamDef.Factor)
        if (KEY_RIGHT):
            self.camera.Move_Right(ParamDef.Factor)
        if (KEY_LEFT):
            self.camera.Move_Left(ParamDef.Factor)
        if (KEY_SHIFT):
            self.camera.Move_Up(ParamDef.Factor)
        if (KEY_CTRL):
            self.camera.Move_Down(ParamDef.Factor)
        if (KEY_PAGE_UP):
            self.camera.Move_In(ParamDef.Factor)
        if (KEY_PAGE_DOWN):
            self.camera.Move_Out(ParamDef.Factor)
        if (KEY_HOME):
            self.camera.Zoom_In(ParamDef.Factor)
        if (KEY_END):
            self.camera.Zoom_Out(ParamDef.Factor)

    '''
	* Aim Handling
	'''

    def AimHandling(self):
        if (KEY_UP):
            self.camera.Move_In(ParamDef.Factor)
        if (KEY_DOWN):
            self.camera.Move_Out(ParamDef.Factor)
        if (KEY_RIGHT):
            self.camera.SwingRight(2 * ParamDef.Factor,
                                   self.balls[0].Pos_xCM(),
                                   self.balls[0].Pos_yCM())
        if (KEY_LEFT):
            self.camera.SwingLeft(2 * ParamDef.Factor, self.balls[0].Pos_xCM(),
                                  self.balls[0].Pos_yCM())
        if (KEY_SHIFT):
            self.camera.SwingDown(ParamDef.Factor, self.balls[0].Pos_xCM(),
                                  self.balls[0].Pos_yCM())
        if (KEY_CTRL):
            self.camera.SwingUp(ParamDef.Factor, self.balls[0].Pos_xCM(),
                                self.balls[0].Pos_yCM())
        if (KEY_PAGE_UP):
            self.camera.Move_In(ParamDef.Factor)
        if (KEY_PAGE_DOWN):
            self.camera.Move_Out(ParamDef.Factor)
        if (KEY_HOME):
            self.camera.Vertigo_In(ParamDef.Factor)
        if (KEY_END):
            self.camera.Vertigo_Out(ParamDef.Factor)

    def BackswingHandling(self):
        '''
		'''
        if (KEY_UP):
            self.camera.Move_In(ParamDef.Factor)
        if (KEY_DOWN):
            self.camera.Move_Out(ParamDef.Factor)
        if (KEY_RIGHT):
            self.camera.SwingRight(2 * ParamDef.Factor,
                                   self.balls[0].Pos_xCM(),
                                   self.balls[0].Pos_yCM())
        if (KEY_LEFT):
            self.camera.SwingLeft(2 * ParamDef.Factor, self.balls[0].Pos_xCM(),
                                  self.balls[0].Pos_yCM())
        if (KEY_SHIFT):
            self.camera.SwingDown(ParamDef.Factor, self.balls[0].Pos_xCM(),
                                  self.balls[0].Pos_yCM())
        if (KEY_CTRL):
            self.camera.SwingUp(ParamDef.Factor, self.balls[0].Pos_xCM(),
                                self.balls[0].Pos_yCM())
        if (KEY_PAGE_UP):
            self.camera.Move_In(ParamDef.Factor)
        if (KEY_PAGE_DOWN):
            self.camera.Move_Out(ParamDef.Factor)
        if (KEY_HOME):
            self.camera.Zoom_In(ParamDef.Factor)
        if (KEY_END):
            self.camera.Zoom_Out(ParamDef.Factor)

        shotStrength = MaxAusholStaerke * (1 - exp(
            (-ParamDef.FrameTimePoint + ParamDef.ShotStartTime) / 400.0))
        self.ShotStrength.setShockStrength(shotStrength / 3.333)

    '''
	 * 击球处理
	 *
	 '''

    def ShotHandling(self):
        FirstShot = 0
        #WeisseVersetzbar=0;

        if (KEY_UP):
            self.camera.Move_Front(ParamDef.Factor)
        if (KEY_DOWN):
            self.camera.Move_Back(ParamDef.Factor)
        if (KEY_RIGHT):
            self.camera.Move_Right(ParamDef.Factor)
        if (KEY_LEFT):
            self.camera.Move_Left(ParamDef.Factor)
        if (KEY_SHIFT):
            self.camera.Move_Up(ParamDef.Factor)
        if (KEY_CTRL):
            self.camera.Move_Down(ParamDef.Factor)
        if (KEY_PAGE_UP):
            self.camera.Move_In(ParamDef.Factor)
        if (KEY_PAGE_DOWN):
            self.camera.Move_Out(ParamDef.Factor)
        if (KEY_HOME):
            self.camera.Zoom_In(ParamDef.Factor)
        if (KEY_END):
            self.camera.Zoom_Out(ParamDef.Factor)

        #Frames++;                       # F"ur die Frames/sec-Anzeige

        # Zeit seit Stossbeginn
        time = ParamDef.FrameTimePoint - ParamDef.StartTime
        #time

        # Letzten Zustand noch zeichnen, wenn Stoss
        if (time > ParamDef.ShotTime):
            time = ParamDef.ShotTime  # eigentlich schon vorbei

        #printf("%i-%i=%i: ",FrameZeitpunkt,Startzeit,Zeit);

        for i in range(16):  # Alle Kugeln neu positionieren
            if (LightingList[time][i][2] <= 0):
                self.balls[i].newPositionD(LightingList[Zeit][i])

        self.ShotStrength.setShockStrength(shotStrength / 3.333 - Zeit / 3.0)

        if (not (time & 31)):
            neu = 0
            for i in range(16):
                if (self.ballsInGame[i] and self.ballsInHole[i] != 0
                        and (self.balls[i].Pos_x() == 3000)):
                    self.ballsInHole[i] = 1
                    neu = 1
            if (neu):
                self.menu.NewMenuState()

        if (time == ParamDef.ShotTime
                and not (self.mode == GameMode.TUTORIAL and
                         ParamDef.FrameTimePoint - ParamDef.StartTime < 1900)):
            # Animation schon fertig?

            ##ifndef _WIN32
            #printf(" %f Frames/sec\n",(Frames*100.0)/(Stossdauer+1.0));
            ##endif

            for i in range(16):
                if (self.ballssInGame[i] and not self.ballssInHole[i]
                        and (self.balls[i].Pos_x() == 3000)):
                    self.ballssInHole[i] = 1
            if (self.mode == GameMode.TRAINING
                    or self.mode == GameMode.TUTORIAL):
                if (self.balls[0].Pos_x() == 3000):
                    self.state = GameState.NEW_WHITE
                    ShotStrength.setShockStrength(0.0)
                    ParamDef.LageVerbesserung = 1
                    ParamDef.LageVerbesserungKopffeld = 1
                    WhiteChosen()
                    self.menu.NewMenuState()
                else:
                    self.state = GameState.VIEWING
                    ShotStrength.setShockStrength(0.0)
                    self.menu.NewMenuState()
            elif self.decision.Decisioning() == 0:
                self.state = GameState.JUDGEING
                ShotStrength.setShockStrength(0.0)
            else:
                self.state = GameState.VIEWING
                self.shotStrength.setShockStrength(0.0)
                self.menu.NewMenuState()

    '''
	* 放置白球
	'''

    def NewWhiteHandling(self):
        if (self.mode == GameMode.TRAINING):
            #ParamDef.
            LageVerbesserungKopffeld = 0
            #Location improving header
            LageVerbesserung = 1

        x = self.balls[0].Pos_xCM()
        y = self.balls[0].Pos_yCM()

        self.Factor2 = sqrt((self.balls[0].Pos_xCM() - self.camera.Pos_xCM()) *
                            (self.balls[0].Pos_xCM() - self.camera.Pos_xCM()) +
                            (self.balls[0].Pos_yCM() - self.camera.Pos_yCM()) *
                            (self.balls[0].Pos_yCM() - self.camera.Pos_yCM()) +
                            self.camera.Pos_zCM() * self.camera.Pos_zCM())

        self.Factor2 *= .005

        if (KEY_UP):
            x += .3 * Factor * Faktor2 * sin(self.camera.Beta * M_PI / 180.0)
            y += .3 * Factor * Faktor2 * cos(self.camera.Beta * M_PI / 180.0)
        if (KEY_DOWN):
            x -= .3 * Factor * Faktor2 * sin(self.camera.Beta * M_PI / 180.0)
            y -= .3 * Factor * Faktor2 * cos(self.camera.Beta * M_PI / 180.0)
        if (KEY_LEFT):
            x -= .3 * Factor * Faktor2 * cos(self.camera.Beta * M_PI / 180.0)
            y += .3 * Factor * Faktor2 * sin(self.camera.Beta * M_PI / 180.0)
        if (KEY_RIGHT):
            x += .3 * Factor * Faktor2 * cos(self.camera.Beta * M_PI / 180.0)
            y -= .3 * Factor * Faktor2 * sin(self.camera.Beta * M_PI / 180.0)

        invalid = 0
        if (x < -124 or x > 124 or (x > -63.5 and LageVerbesserungKopffeld)):
            x = self.balls[0].Pos_xCM()

        if (y < -60.5 or y > 60.5):
            y = self.balls[0].Pos_yCM()

        for i in range(16):
            if ((self.balls[i].Pos_xCM() - x) * (self.balls[i].Pos_xCM() - x) +
                (self.balls[i].Pos_yCM() - y) *
                (self.balls[i].Pos_yCM() - y) < 32.7):
                invalid = 1
                #与其他球太近,无效

        #invalid
        if (not invalid):
            self.self.balls[0].newPositionCM(x, y)
        if (KEY_SHIFT):
            self.camera.Move_Up(Factor)
        if (KEY_CTRL):
            self.camera.Move_Down(Factor)
        if (KEY_PAGE_UP):
            self.camera.Move_In(Factor)
        if (KEY_PAGE_DOWN):
            self.camera.Move_Out(Factor)
        if (KEY_HOME):
            self.camera.Zoom_In(Factor)
        if (KEY_END):
            self.camera.Zoom_Out(Factor)

    def JudgeHandling(self):
        if (ParamDef.JudgeDecision == -1):
            ParamDef.JudgeDecision = Judge.Decisioning()
            ParamDef.RecodingChanges = ParamDef.JudgeDecision & 1
            ParamDef.Foul = ParamDef.JudgeDecision & 2
            ParamDef.LageVerbesserungKopffeld = ParamDef.JudgeDecision & 4
            ParamDef.LageVerbesserung = ParamDef.JudgeDecision & 8
            ParamDef.NeuAufbauenOderWeiterspielen = ParamDef.JudgeDecision & 16
            ParamDef.NeuAufbauenOderAchtEinsetzen = ParamDef.JudgeDecision & 32
            ParamDef.Player1Win = ParamDef.JudgeDecision & 64
            ParamDef.Player2Win = ParamDef.JudgeDecision & 128

            self.menu.NewMenuState()
Пример #10
0
def back_propagation_online_momentum(S,rho,nu,units_by_layer,factivation,max_it=250,report_it=50):
	k = 0      # it
	delta = [] # Errores  
	
	# Inicializar pesos a 0 #
	theta = []
	for l in xrange(1,len(units_by_layer)):
		theta.append([])
		if l-1==0: sm = units_by_layer[l-1]
		else:	   sm = units_by_layer[l-1]+1
		for i in xrange(units_by_layer[l]): theta[-1].append(np.zeros(sm))
				
	# Inicializar incr_theta_ant #
	incr_theta_ant = []
	for l in xrange(1,len(units_by_layer)):
		incr_theta_ant.append([])
		if l-1==0: sm = units_by_layer[l-1]
		else:	   sm = units_by_layer[l-1]+1
		for i in xrange(units_by_layer[l]): incr_theta_ant [-1].append(np.zeros(sm))
		
	# Plot #
	if Config.VERBOSE: color_classes = [Config.COLORS[c%len(Config.COLORS)]+Config.STYLE[c%len(Config.STYLE)] for c in xrange(len(S[0][1]))]
	
	# Mientras no converja #
	while k<max_it:
		# Inicializar delta #
		delta = []
		for l in xrange(1,len(units_by_layer)):
			delta.append([])
			for i in xrange(units_by_layer[l]): delta[-1].append(0)
		# Para cada muestra #
		m = 0	   # N muestra
		for (xk,tk) in S: 
			# Inicializar incr_theta a cada muestra #
			incr_theta = []
			for l in xrange(1,len(units_by_layer)):
				incr_theta.append([])
				if l-1==0: sm = units_by_layer[l-1]
				else:	   sm = units_by_layer[l-1]+1
				for i in xrange(units_by_layer[l]): incr_theta[-1].append(np.zeros(sm))
			##########################
			phi,s = forward_propagation(units_by_layer,xk,theta,factivation)
			# Desde la salida a la entrada #
			for l in xrange(len(units_by_layer)-1,0,-1):
				# Para cada nodo #
				for i in xrange(units_by_layer[l]):
					########## Calcular delta ###########	
					if l==len(units_by_layer)-1: delta[l-1][i] = (factivation[l-1][i][1](phi[l-1][i])*(tk[i]-s[l][i]))
					else: delta[l-1][i] += factivation[l-1][i][1](phi[l-1][i])*sum([delta[l][r]*theta[l][r][i+1] for r in xrange(units_by_layer[l+1])])					
					#####################################
					if l==len(units_by_layer)-1:
						# Calcular incr_theta (capa salida) #
						incr_theta[l-1][i][0] += delta[l-1][i]
						for j in xrange(units_by_layer[l-1]): incr_theta[l-1][i][j+1] += delta[l-1][i]*s[l-1][j]
						#####################################	
					else:
						# Calcular incr_theta (capas ocultas) #
						for j in xrange(units_by_layer[l-1]):
							if j==0: incr_theta[l-1][i][j] += delta[l-1][i]
							else:    incr_theta[l-1][i][j] += delta[l-1][i]*s[l-1][j]
						#####################################
			# Actualizaciones #					
			for l in xrange(len(theta)):
				for i in xrange(len(theta[l])): 	
					# Actualizar los pesos #	
					theta[l][i] += (rho*incr_theta[l][i]) + (nu*incr_theta_ant[l][i])
					# Actualizar incr_theta_ant #
					incr_theta_ant[l][i] = (rho*incr_theta[l][i]) + (nu*incr_theta_ant[l][i])
			m += 1	
		
		# Plot de las muestras #
		if Config.VERBOSE:
			if k%report_it==0:
				plt.clf()
				plt.ylabel("Y")
				plt.xlabel("X")
				maxX = float("-inf")
				maxY = float("-inf")
				plt.title("Iteracion backprop online con momentum: "+str(k))
				for (xk,tk) in S:
					predicted_tk = Decision.classify(units_by_layer,xk,theta,factivation)
					plt.plot(xk[1],xk[2],color_classes[predicted_tk])
					if xk[1]>maxX: maxX = xk[1]
					if xk[2]>maxY: maxY = xk[2]
				plt.axis([-maxX,2*maxX,-maxY,2*maxY])
				plt.show(block=False)
				print "Siguientes ",report_it," iteraciones [Enter]"
				raw_input()
				plt.close()
		k += 1
	return theta
Пример #11
0
    theta3 = MLPLearning.back_propagation_batch_momentum(
        S, rho, nu, units_by_layer, factivation, 850, 50)
    theta4 = MLPLearning.back_propagation_batch_buffer(S, rho, l,
                                                       units_by_layer,
                                                       factivation, 850, 50)
    theta5 = MLPLearning.back_propagation_online_buffer(
        S, rho, l, units_by_layer, factivation, 850, 50)
    theta6 = MLPLearning.back_propagation_online_momentum(
        S, rho, nu, units_by_layer, factivation, 850, 50)
    theta7, fitness = MLPLearning.evolutional(S, units_by_layer, factivation,
                                              200, 500, -2, 2, 1.1, 0.9)
    ##############################

    # Clasificacion #
    logging.info("Clase con theta1: (Backprop batch): " + str(
        Decision.classify(units_by_layer, [1.0, -6.3, 1.0], theta1,
                          factivation)))
    logging.info("Clase con theta2: (Backprop online): " + str(
        Decision.classify(units_by_layer, [1.0, -6.3, 1.0], theta2,
                          factivation)))
    logging.info("Clase con theta3: (Backprop batch con momentum): " + str(
        Decision.classify(units_by_layer, [1.0, -6.3, 1.0], theta3,
                          factivation)))
    logging.info("Clase con theta4: (Backprop batch con amortiguamiento): " +
                 str(
                     Decision.classify(units_by_layer, [1.0, -6.3, 1.0],
                                       theta4, factivation)))
    logging.info("Clase con theta5: (Backprop online con amortiguamiento): " +
                 str(
                     Decision.classify(units_by_layer, [1.0, -6.3, 1.0],
                                       theta5, factivation)))
    logging.info("Clase con theta6: (Backprop online con momentum): " + str(
Пример #12
0
import numpy as np
import Perception
import Decision
import Memory
import random
import gym
import Learning
import math
import naoenvSimulation
import matplotlib.pyplot as plt

camera = Perception.Camera()
memory = Memory.Memory(camera)
Perc = Perception.Ball(memory)
learning = Learning.Learning(memory)
Dec = Decision.Decision(memory, learning)


def createEnvironment(numberOfPossibleBalls, environment, center):
    #create some balls
    ball = [0, 0]
    gaze = [0, 0]
    while gaze[0] == 0 or gaze[1] == 0:
        ball[0] = random.randrange(environment[0], environment[1], 1)
        ball[1] = random.randrange(environment[2], environment[3], 1)
        #create the gaze
        gaze = [ball[0] - center[0], ball[1] - center[1]]

    #find the direction of the gaze
    directionx = 1
    directiony = 1
Пример #13
0
def selectBestFeature(inputs,
                      labels,
                      impurityCat,
                      impurityNum,
                      featuresToConsider,
                      featureNames,
                      featureTypes,
                      numIntervals,
                      seed=0):
    random.seed(seed)
    if featuresToConsider > inputs.shape[1] or featuresToConsider < 0:
        Exception('featuresToConsider must be between 0 and {maxx}'.format(
            maxx=featuresToConsider))
    possibleFeatures = np.random.choice(featureNames, featuresToConsider,
                                        False)
    bestImpurity = np.inf
    bestSplit = ('', '', ''
                 )  #in the form (featureName, isCategorical, arguements)

    for name in possibleFeatures:
        colIndex = np.where(name == featureNames)[0][0]
        vals = inputs[:, colIndex]
        isCategorical = False
        if featureTypes[colIndex] == 'string':
            isCategorical = True
        if isCategorical:
            #if the feature is categorical, compute a split for each possible
            #value and pick the maximum one
            subset = []
            splitImpurity = bestImpurity
            for cat in np.unique(vals):
                dec = Decision.Categorical(name, [cat])
                trueIndices = np.vectorize(dec.function)(vals)
                falseIndices = np.logical_not(trueIndices)
                trueNodeImpurity = impurityResubLabel(
                    labels[trueIndices], impurityCat, impurityNum,
                    sum(trueIndices) / len(labels))
                falseNodeImpurity = impurityResubLabel(
                    labels[falseIndices], impurityCat, impurityNum,
                    sum(falseIndices) / len(labels))
                if splitImpurity > trueNodeImpurity + falseNodeImpurity:
                    subset = [cat]
                    splitImpurity = trueNodeImpurity + falseNodeImpurity
            split = (name, True, subset)

        else:
            #else the feature is numerical. In that case I discretize the
            #continuous features into "numIntervals" bins  or
            #however many intervals are possible.

            def findReImpurityGivenBoundary(bound):
                lessBound = labels[vals <= bound]
                moreBound = labels[vals > bound]
                trueNodeReImpurity = impurityResubLabel(
                    lessBound, impurityCat, impurityNum,
                    len(lessBound) / len(labels))
                falseNodeReImpurity = impurityResubLabel(
                    moreBound, impurityCat, impurityNum,
                    len(moreBound) / len(labels))
                return trueNodeReImpurity + falseNodeReImpurity

            numIntervalsForContinuousFeat = min(numIntervals, inputs.shape[0])
            possibleBounds = [
                max(i) for i in np.array_split(np.sort(vals),
                                               numIntervalsForContinuousFeat)
            ]
            possibleBounds = np.array(possibleBounds)
            splitImpurity = np.inf
            bestBoundary = 0
            for bound in possibleBounds:
                imp = findReImpurityGivenBoundary(bound)
                if splitImpurity > imp:
                    splitImpurity = imp
                    bestBoundary = bound
            split = (name, False, ('less', bestBoundary))
        if bestImpurity > splitImpurity:
            bestImpurity = splitImpurity
            bestSplit = split
    return bestSplit
    print "error was ", e
    sys.exit(1)

# disable ALAutonomousMoves bug
am = ALProxy("ALAutonomousMoves", ip, port)
am.setExpressiveListeningEnabled(False)
am.setBackgroundStrategy("none")

#######Global classes#######
camera = Perc.Camera()
memory = memo.Memory(camera)
facePerception = Perc.Face(memory)
gazeFollowing = Perc.Gaze(memory)
ballPerception = Perc.Ball(memory)
learning = Learn.Learning(memory)
decision = Deci.Decision(memory, learning)
motivation = Moti.Motivation(memory)
motorControl = MC.MotorControl(motionProxy, memory, facePerception, tts)

################################################################
# Main functions
################################################################


def setup():
    say("Setting up")
    postureProxy.goToPosture("Crouch", 0.6667)


def say(text):
    tts.say(text)
Пример #15
0
def main():
    ###Calcualtion setting ###

    #low:100, 30
    #medium:130, 60
    #high: 160, 100
    num_agent = 160
    """
    average_range
    beta
    gamma
    param_ramge
    """
    max_season = 37
    list_state = []
    list_task = []
    result_list_state = []
    result_list_task = []
    state_to_nextstate = []
    list_Gr = []
    list_Cl = []
    list_Rt = []
    list_Cf = []
    list_Mg = []
    info = pd.DataFrame({
        'Gr': [],
        'Cl': [],
        'Rt': [],
        'Cf': [],
        'Mg': [],
        'New': [],
        's0': [],
        'Sa': [],
        'sA': [],
        'sB': [],
        'sC': []
    })
    #initialization of ramdom int
    num_new_agent = 0

    #Prepare agents & initialize state & task &strategy
    agents = Agent_man.generate_agents(num_agent)
    agents_store = Agent_store.generate_agent_store(5)
    Agent_store.init_agent_store(agents_store)
    Agent_store.show_store_info(agents_store)
    Move.initialize_state_task(agents)
    Decision.initial_strategy(agents)
    #Agent_man.show_agent_info(agents)

    for season in range(1, max_season):

        print('###########################')
        print(f'simulate season:{season}')
        if season != 1 and season != max_season - 1:

            num_new_agent = rnd.randint(1, 100)
            #generate new agents
            print('==== generate new agents ====')
            Agent_man.new_generate_agents(num_new_agent, agents)

        #move next_state
        print('===== agent move =====')
        Move.agents_moves(agents, season)
        #divide visitor per stores
        list_Gr, list_Cl, list_Rt, list_Cf, list_Mg = Manage_store.divide_visitor(
            agents)

        if season == 1:
            #manage instore & waiting for 1st season
            Manage_store.manage_instore_waiting_first(agents, agents_store,
                                                      list_Gr, 'Gr')
            Manage_store.manage_instore_waiting_first(agents, agents_store,
                                                      list_Cl, 'Cl')
            Manage_store.manage_instore_waiting_first(agents, agents_store,
                                                      list_Rt, 'Rt')
            Manage_store.manage_instore_waiting_first(agents, agents_store,
                                                      list_Cf, 'Cf')
            Manage_store.manage_instore_waiting_first(agents, agents_store,
                                                      list_Mg, 'Mg')
        if season != 1:
            Manage_store.manage_instore_waiting_second(agents, agents_store,
                                                       list_Gr, 'Gr')
            Manage_store.manage_instore_waiting_second(agents, agents_store,
                                                       list_Cl, 'Cl')
            Manage_store.manage_instore_waiting_second(agents, agents_store,
                                                       list_Rt, 'Rt')
            Manage_store.manage_instore_waiting_second(agents, agents_store,
                                                       list_Cf, 'Cf')
            Manage_store.manage_instore_waiting_second(agents, agents_store,
                                                       list_Mg, 'Mg')

        #Agent_man.show_agent_info(agents)

        #strategy determine next_state
        print('==== decide next state ====')
        Decision.strategy_determine_state(agents, agents_store)
        #agents determine next_strategy
        print('==== decide next strategy ====')
        Decision.decide_next_strategy(agents)

        print('==== insert next strategy into strategy ====')
        Decision.insert_strategy(agents)

        Gr, Cl, Rt, Cf, Mg, s0 = Move.count_state_num(agents)
        Sa = Move.count_store_all(agents)
        sA, sB, sC = Decision.count_strategy(agents)
        new_info = pd.DataFrame(
            [[Gr, Cl, Rt, Cf, Mg, num_new_agent, s0, Sa, sA, sB, sC]],
            columns=[
                'Gr', 'Cl', 'Rt', 'Cf', 'Mg', 'New', 's0', 'Sa', 'sA', 'sB',
                'sC'
            ])
        #new_info = pd.DataFrame([[format(Gr, '.4'),format(Cl,'.4'),format(Rt,'.4'),format(Cf,'.4'),format(Mg,'.4'),num_new_agent,format(s0,'.4'),Sa,sA,sB,sC]],columns=['Gr','Cl','Rt','Cf','Mg','New','s0','Sa','sA','sB','sC'])
        info = info.append(new_info)
        #print(f'info:{info}')

        #infomation of state store result_list_state
        for id, agent in enumerate(agents):
            list_state.append(agent.state + '_' + agent.status)
            list_task.append(agent.task)
        #print(f'list_state:{list_state}')
        #print(f'result_list_state:{result_list_state}')
        result_list_state.append(list_state)
        result_list_task.append(list_task)
        #print(f'result_list_state:{result_list_state}')

        list_state = []
        list_task = []
        list_Gr = []
        list_Cl = []
        list_Rt = []
        list_Cf = []
        list_Mg = []

        #print(f'list_state:{list_state}')

    state = pd.DataFrame(result_list_state)
    task = pd.DataFrame(result_list_task)

    print(f'result:{state}')

    #ディレクトリの変更必須!!!
    state.to_csv(f'result/high/result_list_state_high.csv')
    task.to_csv(f'result/high/result_list_task_high.csv')
    info.to_csv(f'result/high/result_info_high.csv')