Esempio n. 1
0
def eval_genome(genome, config, stage):
    global sim, action
    global numtimes
    global lastcleared
    global clearnum
    global firstclear
    global firstclearnum
    global nowstageindex
    global nowBest
    cleared = set()
    net = neat.nn.FeedForwardNetwork.create(genome, config)
    #net = neat.nn.RecurrentNetwork.create(genome,config)
    trystage = "data/multi" + str(stage) + ".map"
    fitness = 0.0
    for i in range(1):
        sim = snaketrain2.PyAction(0, trystage)
        firstxdif = (math.sqrt(abs(sim.map.goalx - sim.map.python.fpx) / 2.0))
        checkact = 0
        # Run the given simulation for up to num_steps time steps.
        bonus = 0.0
        hp = 0.0
        score = 0.0
        clear = False
        checkact = 0
        prev_action = 0
        prev_inputs = []
        while sim.game_state != 2 and sim.game_state != 3 and sim.t < simulation_seconds:
            if (checkact % frameskip == 0):
                inputs = sim.get_displayinfo()
                #print(inputs)
                prev_inputs = inputs
                action = eval_network(net, inputs)
                prev_action = action
            else:
                inputs = prev_inputs
                action = prev_action
            checkact += 1
            # Apply action to the simulated cart-pole
            #controll = snaketrain2.discrete_actuator_force(action)
            #print(sim.t)
            #print(sim.map.python.fpx,sim.map.python.fpy)
            #sleep(1)
            sim.running(action)
            #sleep(1)
            #print(sim.game_state)
            #if sim.game_state==2 or sim.game_state==3 or sim.t > simulation_seconds:
            #break
            # Stop if the network fails to keep the cart within the position or angle limits.
            # The per-run fitness is the number of time steps the network can balance the pole
            # without exceeding these limits.
            #print(sim.t)
            hp = sim.map.python.HP
            score = sim.map.python.score
            x = sim.map.python.fpx
            y = sim.map.python.fpy
            if sim.game_state == 2 or sim.t >= simulation_seconds:
                bonus = -10.0
            elif sim.game_state == 3:
                clear = True
        if (clear == True):
            global trainend
            fitness += 150 + hp + score // 50 + sim.gametime // 60
            if (trainend == False):
                trainend = True
                clearinfo.append((nowtry, 100.0))
                with open('Results/multinormalneat' + str(stage) + '.txt',
                          'a') as f:
                    f.write("(" + str(nowtry) + "," + str(100.0) + ")")
                    f.write('\n')
        #fitness = sim.map.python.HP/4.0+sim.map.python.score/100.0+bonus-math.sqrt((sim.map.python.rect.x-sim.map.goalx)**2+(sim.map.python.rect.y-sim.map.goaly)**2)/20.0
        else:
            fitness = fitness + bonus + hp // 20 + score // 50 + firstxdif - (
                math.sqrt(abs(sim.map.goalx - x) / 2.0))
        #math.sqrt((sim.map.python.rect.x-sim.map.goalx)**2+(sim.map.python.rect.y-sim.map.goaly)**2)/20.0
        if (nowBest < (x / sim.map.goalx) * 100):
            nowBest = (x / sim.map.goalx) * 100
        del sim
    #pygame.quit()
    #del sim
    #print(numtimes,fitness)
    #fitnesses.append(fitness)
    # The genome's fitness is its worst performance across all runs.
    if (len(cleared) > len(lastcleared)):
        lastcleared = cleared
    return fitness
Esempio n. 2
0
def eval_genome(genome, config, stage):
    global sim, action
    global numtimes
    global lastcleared
    global clearnum
    global firstclear
    global firstclearnum
    global nowstageindex
    global nowBest
    cleared = set()
    net = neat.nn.FeedForwardNetwork.create(genome, config)
    #net = neat.nn.RecurrentNetwork.create(genome,config)
    trystage = "data/multi" + str(stage) + ".map"
    fitness = 0.0
    for i in range(1):
        sim = snaketrain2.PyAction(0, trystage)
        firstxdif = (math.sqrt(abs(sim.map.goalx - sim.map.python.fpx) / 2.0))
        checkact = 0
        # Run the given simulation for up to num_steps time steps.
        bonus = 0.0
        hp = 0.0
        score = 0.0
        clear = False
        checkact = 0
        prev_action = 0
        prev_inputs = []
        while sim.game_state != 2 and sim.game_state != 3 and sim.t < simulation_seconds:
            if (checkact % 20 == 0):
                metainputs = sim.get_metainfo()
                choice = eval_network(net, metainputs)
                if (choice == 0):
                    actor = neat.nn.FeedForwardNetwork.create(
                        NoNets[choice], actorconfig)
                elif (choice >= 1 and choice <= 15):
                    actor = neat.nn.FeedForwardNetwork.create(
                        EnemyNets[choice - 1], actorconfig)
                else:
                    choice -= 15
                    actor = neat.nn.FeedForwardNetwork.create(
                        JumpNets[choice - 1], actorconfig)
            if (checkact % frameskip == 0):
                inputs = sim.get_displayinfo()
                #print(inputs)
                prev_inputs = inputs
                action = np.argmax(actor.activate(inputs))
                prev_action = action
            else:
                inputs = prev_inputs
                action = prev_action
            checkact += 1
            sim.running(action)
            hp = sim.map.python.HP
            score = sim.map.python.score
            x = sim.map.python.fpx
            y = sim.map.python.fpy
            if sim.game_state == 2 or sim.t >= simulation_seconds:
                bonus = -10.0
            elif sim.game_state == 3:
                clear = True
        if (clear == True):
            global trainend
            fitness += 150 + hp + score // 50 + sim.gametime // 60
            if (trainend == False):
                trainend = True
                clearinfo.append((nowtry, 100.0))
                #with open('Results/multifull'+str(stage)+'.txt','a') as f:
                #    f.write("("+str(nowtry)+","+str(100.0)+")")
                #    f.write('\n')
        #fitness = sim.map.python.HP/4.0+sim.map.python.score/100.0+bonus-math.sqrt((sim.map.python.rect.x-sim.map.goalx)**2+(sim.map.python.rect.y-sim.map.goaly)**2)/20.0
        else:
            fitness = fitness + bonus + hp // 20 + score // 50 + firstxdif - (
                math.sqrt(abs(sim.map.goalx - x) / 2.0))
            #print(hp,score,x,y)
        #math.sqrt((sim.map.python.rect.x-sim.map.goalx)**2+(sim.map.python.rect.y-sim.map.goaly)**2)/20.0
        if (nowBest < (x / sim.map.goalx) * 100):
            nowBest = (x / sim.map.goalx) * 100
        del sim
    #pygame.quit()
    #del sim
    #print(numtimes,fitness)
    #fitnesses.append(fitness)
    # The genome's fitness is its worst performance across all runs.
    if (len(cleared) > len(lastcleared)):
        lastcleared = cleared
    return fitness