我正在尝试创建一个玩游戏的人工智能。这个游戏需要人工智能来躲避来袭的小行星。我使用整洁的图书馆,但由于某种原因,我不断得到一个列表索引错误,我似乎不明白为什么。旁注:我是新的整洁和遗传算法
我希望我的游戏一次只运行一个基因组
def main(genomes, config):
ge = [] # List to hold all genome objects
nets = [] # List to hold the NN associated with each genome
players = [] # List to hold the player objects (Contains x y coordinates and other attributes)
# To the three lists append each of the following objects: NNs, genomes and players
- such that they each match with their index position.
Example: players[1] has its genome stored at genome[1] and it's neural network at nets[1]
for __, g in genomes:
net = neat.nn.FeedForwardNetwork.create(g, config)
nets.append(net)
players.append(player())
g.fitness = 0
ge.append(g)
# Asteroid objects that are meant to be avoided
asteroid(img_num, max_y, speed, width, height, threshold_score)
asteroids = [0,
asteroid(1, playable_height-39, 10.5, 35, 39, 0),
asteroid(2, playable_height-25, 15, 25, 25, 10),
asteroid(3, playable_height-30, 12.5, 30, 30, 15),
asteroid(4, playable_height-35, 10, 35, 35, 20)]
for x in range(len(players)): # Suspected source of error
game_over = False
while not game_over:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
quit()
# Calculates the output of the current player playing using it's neural network
output = nets[x].activate(((players[x].y / 169),
GetDistance(1, players[x], asteroids),
GetDistance(2, players[x], asteroids),
GetDistance(3, players[x], asteroids),
GetDistance(4, players[x], asteroids)))
if output[0] > 0:
players[x].moveDOWN()
elif output[0] < 0:
players[x].moveUP()
if players[x].y > playable_height - players[x].height:
players[x].y = playable_height - players[x].height
elif players[x].y < 0:
players[x].y = 0
window.fill((0, 0, 0))
window.blit(bg_img, (0, 0))
players[x].draw()
# Checks to see if asteroid has been dodged and then increases fitness by 5 for that genome. If collision occurs remove the genome, NN and player from their lists.
for n in range(1, 5):
if asteroids[n].x < 0:
players[x].score += 1
ge[x].fitness += 5
asteroids[n].move(players[x])
if asteroids[n].collision(players[x]):
asteroids[n].reset(asteroids)
ge[x].fitness -= 1
del players[x]
del nets[x]
del ge[x]
game_over = True
Text_display("Score: " + str(players[x].score * 100), white, 0, 200)
Text_display("Asteroid 1: " + str(GetDistance(1, players[x], asteroids)), white, 0, 220)
Text_display("Asteroid 2: " + str(GetDistance(2, players[x], asteroids)), white, 0, 240)
Text_display("Asteroid 3: " + str(GetDistance(3, players[x], asteroids)), white, 0, 260)
Text_display("Asteroid 4: " + str(GetDistance(4, players[x], asteroids)), white, 0, 280)
Text_display("Space Ship Position: " + str(players[x].y / 169), white, 0, 300)
pygame.display.update()
# Set up function for the NEAT library
def run(config_path):
config = neat.config.Config(neat.DefaultGenome, neat.DefaultReproduction, neat.DefaultSpeciesSet, neat.DefaultStagnation, config_path)
p = neat.Population(config)
p.add_reporter(neat.StdOutReporter(True))
stats = neat.StatisticsReporter()
p.add_reporter(stats)
winner = p.run(main, 100)
if __name__ == "__main__":
local_dir = os.path.dirname(__file__)
config_path = os.path.join(local_dir, "NEATconfig.txt")
run(config_path)
目前没有回答
相关问题 更多 >
编程相关推荐