问题描述
我目前正在为 Doodle Jump 开发 NEAT-Python AI,但由于某种原因,我的 AI 根本无法学习......当它达到新高度时,我会给基因组适应度,并在它死亡时移除适应度
networks = []
ge = []
players = []
# Set Neural Networks
for _,g in genomes:
network = neat.nn.FeedForwardNetwork.create(g,config)
networks.append(network)
players.append(Player(200,200))
g.fitness = 0
ge.append(g)
win = pygame.display.set_mode((WINDOW_WIDTH,WINDOW_HEIGHT))
clock = pygame.time.Clock()
platforms = generateInitialPlatforms()
platform_i = MAX_PLATFORMS
current_height = 0
run = True
score = 0
old_score = 0
stagnation_timer = 0
while run:
clock.tick(60)
# Quit Game
for event in pygame.event.get():
if event.type == pygame.QUIT:
run = False
pygame.quit()
quit()
# Stop Generation when there are no players left
if len(players) <= 0:
run = False
return
platform_data = []
# Refresh Platforms
for platform in platforms:
if platform.y > WINDOW_HEIGHT:
platforms.remove(platform)
platforms.append(Platform(
platform_i,random.randrange(
FIELD_MARGIN,WINDOW_WIDTH - platform.width - FIELD_MARGIN
),-platform.height
))
platform_i += 1
platform_data.append(platform.x)
platform_data.append(platform.y)
for index,player in enumerate(players):
player_data = []
player.move()
player_data.append(player.x)
player_data.append(player.y)
player_data.append(player.veLocity_x)
player_data.append(player.veLocity_y)
output = networks[index].activate(platform_data + player_data)
# Move Player based on Neural Network Output
if output[0] > 0.5:
player.moveLeft()
elif output[1] > 0.5:
player.moveRight()
# Move Platforms if Player Y is above Jump Threshold
if player.y <= JUMP_THRESHOLD:
player.y = JUMP_THRESHOLD
current_height = -round(player.vy)
# Check Player - Platform Collision
if player.collide(platforms) and index < len(ge):
player.jump()
# Player Death
if player.y >= WINDOW_HEIGHT and index < len(ge):
ge[index].fitness -= 1
players.pop(index)
networks.pop(index)
ge.pop(index)
# Increase fitness if player reaches new height
if current_height > 1 and index < len(ge):
ge[index].fitness += 0.3
player.stagnation_timer = 0
else:
player.stagnation_timer += 1
if player.stagnation_timer > MAX_STAGNATION:
player.stagnation_timer = 0
ge[index].fitness -= 1
players.pop(index)
networks.pop(index)
ge.pop(index)
if current_height > SCROLLING_VELociTY:
current_height = SCROLLING_VELociTY
score += current_height
# Move platforms when Player reaches above Jump Threshold
for platform in platforms:
platform.move(current_height)
draw_window(win,players,platforms,score)
old_score = score
这是配置,我已经在其中尝试了不同的突变率、激活函数等...
[NEAT]
fitness_criterion = max
fitness_threshold = 2000
pop_size = 50
reset_on_extinction = True
[DefaultGenome]
# node activation options
activation_default = tanh
activation_mutate_rate = 0.05
activation_options = tanh
#abs clamped cube exp gauss hat identity inv log relu sigmoid sin softplus square tanh
# node aggregation options
aggregation_default = random
aggregation_mutate_rate = 0.05
aggregation_options = sum product min max mean median maxabs
# node bias options
bias_init_mean = 0.05
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.5
Feed_forward = True
#initial_connection = unconnected
initial_connection = partial_nodirect 0.5
# node add/remove rates
node_add_prob = 0.5
node_delete_prob = 0.2
# network parameters
num_hidden = 0
num_inputs = 18
num_outputs = 2
# node response options
response_init_mean = 1.0
response_init_stdev = 0.05
response_max_value = 30.0
response_min_value = -30.0
response_mutate_power = 0.1
response_mutate_rate = 0.75
response_replace_rate = 0.1
# connection weight options
weight_init_mean = 0.1
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 = 2.5
[DefaultStagnation]
species_fitness_func = max
max_stagnation = 30
species_elitism = 0
[DefaultReproduction]
elitism = 3
survival_threshold = 0.3
完整源代码:https://github.com/maartenwolfsen/NEAT-Doodle-Jump
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)