Python NEAT似乎没有从上一代人那里学习

2024-05-18 07:33:52 发布

您现在位置:Python中文网/ 问答频道 /正文

我试图学习更多关于神经网络和NEAT的知识,并着手构建一个可以使用NEAT玩乒乓球的AI。然而,似乎在不同的世代中,神经网络的输出并没有改变。我尝试使用配置文件以及更改各种权重。然而,我已经没有主意了

def main_game(genomes,config):
    pygame.init()
    WIN = pygame.display.set_mode(SIZE)
    clock = pygame.time.Clock()
    running = True
    player1,player2 = Paddle(True), Paddle(False)
    ball = Ball()

    neat1 = neat.nn.FeedForwardNetwork.create(genomes[0][1],config)
    neat2 = neat.nn.FeedForwardNetwork.create(genomes[1][1],config)
    genomes[0][1].fitness = 0
    genomes[1][1].fitness = 0

    def activate(player1,ball,nn):
        output = nn.activate((player1.x,player1.y,ball.x,ball.y))[0]
        if output >= 0 and output <= 1/3:
            player1.up()
        elif output >= 1/3 and output <= 2/3:
            player1.down()


    while running:
        clock.tick(30)

        activate(player1,ball,neat1)
        activate(player2,ball,neat2)

        WIN.fill((0,0,0))
        lose = ball.move()
        if lose == 1:
            genomes[0][1].fitness -=500  #genomes[0] = None    
            running = False
        elif lose == 2:
            genomes[1][1].fitness -=500   # genomes[1] = None  
            running = False

        collide,who_collide = ball.check_collision(player1,player2)
        if collide:
            # print(who_collide)
            genomes[who_collide][1].fitness +=100


        player1.draw(WIN)
        player2.draw(WIN)
        ball.draw(WIN)


        pygame.display.flip()

    print(genomes)



def run(config_file):
    config = neat.config.Config(neat.DefaultGenome, neat.DefaultReproduction,
                         neat.DefaultSpeciesSet, neat.DefaultStagnation,
                         config_file)   

    p = neat.Population(config)

    p.add_reporter(neat.StdOutReporter(True))
    stats = neat.StatisticsReporter()
    p.add_reporter(stats)

     # Run for up to 50 generations.
    winner = p.run(main_game, 50)

    # show final stats
    print('\nBest genome:\n{!s}'.format(winner))

配置文件:


[NEAT]
fitness_criterion     = max
fitness_threshold     = 100000
pop_size              = 2
reset_on_extinction   = False

[DefaultGenome]
# node activation options
activation_default      = sigmoid
activation_mutate_rate  = 0
activation_options      = sigmoid

# node aggregation options
aggregation_default     = sum
aggregation_mutate_rate = 0.0
aggregation_options     = sum

# node bias options
bias_init_mean          = 0.0
bias_init_stdev         = 1.0
bias_max_value          = 30.0
bias_min_value          = -30.0
bias_mutate_power       = 0.5
bias_mutate_rate        = 0.7
bias_replace_rate       = 0.1

# genome compatibility options
compatibility_disjoint_coefficient = 1.0
compatibility_weight_coefficient   = 0.5

# connection add/remove rates
conn_add_prob           = 0.5
conn_delete_prob        = 0.5

# connection enable options
enabled_default         = True
enabled_mutate_rate     = 0.01

feed_forward            = True
initial_connection      = full

# node add/remove rates
node_add_prob           = 0.2
node_delete_prob        = 0.2

# network parameters
num_hidden              = 0
num_inputs              = 4
num_outputs             = 1

# node response options
response_init_mean      = 1.0
response_init_stdev     = 0.0
response_max_value      = 30.0
response_min_value      = -30.0
response_mutate_power   = 0.0
response_mutate_rate    = 0.0
response_replace_rate   = 0.0

# connection weight options
weight_init_mean        = 0.0
weight_init_stdev       = 1.0
weight_max_value        = 30
weight_min_value        = -30
weight_mutate_power     = 0.5
weight_mutate_rate      = 0.8
weight_replace_rate     = 0.1

[DefaultSpeciesSet]
compatibility_threshold = 3.0

[DefaultStagnation]
species_fitness_func = max
max_stagnation       = 20
species_elitism      = 2

[DefaultReproduction]
elitism            = 2
survival_threshold = 0.2

Tags: addconfignoderateinitresponseneatoptions