1
\ begingroup美元

我试图实现整洁的蛇游戏。我的游戏逻辑是准备好了,这是正常工作和NEAT配置。但即使经过100代,每一代有200个基因组,蛇的表现还是很差。它很少吃超过两种食物。下面是eval_genome函数的片段:

def eval_genome(genomes, config): clock = pygame.time.Clock() win = pygame.display。set_mode((WIN_WIDTH, WIN_HEIGHT)) for genome_id, g in genomes: net = clean .nn. feedforwardnetwork . set_mode((WIN_WIDTH, WIN_HEIGHT));创建(g,配置)g.fitness = 0 =蛇蛇()食物= (snake.body)运行= True上上= = =左右= MOVE_SNAKE = False moveToFood得分= 0 = 0 moveCount = 0而运行:pygame.time.delay (50) clock.tick_busy_loop(10)的事件pygame.event.get():如果事件。= = pygame类型。QUIT: run = False蛇头x = snake。body[0]['x'] snakeHeadY =蛇。['y'] snakeTailX = snake.body[len(snake.body)-1]['x'] snakeTailY = snake.body[len(snake.body)-1]['y'] snakeLength = len(snake.body) snakeheaddist = WIN_HEIGHT - snakeHeadY - STEP snakeHeadX - STEP foodbottom = WIN_HEIGHT - food。y - STEP foodRightDist = WIN_WIDTH - food。x - STEP snakeFoodDistEuclidean =数学。sqrt((snakeHeadX - food.x)**2 + (snakeHeadY - food.y)**2) snakeFoodDistManhattan = abs(snakeHeadX - food.x) + abs(snakeHeadY - food.y) viewDirections = snake;checkDirections(food, UP, DOWN, LEFT, RIGHT) deltaFoodDist = snakeFoodDistEuclidean outputs = net。激活snakeHeadX, snakeHeadY, snakeHeadBottomDist, snakeHeadRightDist, snakeTailX, snakeTailY, snakeLength, moveCount, moveToFood, food。x,食物。y, foodBottomDist, fooddrightdist, snakefooddistelidean, snakeFoodDistManhattan, viewDirections[0], viewDirections[1], viewDirections[2], viewDirections[3], viewDirections[4], viewDirections[5], viewDirections[6], viewDirections[7], deltaFoodDist)如果(outputs[0] == max(outputs) and not DOWN):snake.setDir(0,-1) UP = True LEFT = False RIGHT = False MOVE_SNAKE = True elif (outputs[2] == max(outputs) and not RIGHT):输出[2]== max(outputs) and not RIGHT):snake.setDir(1,0)左= True上= False下= False MOVE_SNAKE = True elif(输出[3]= = max(输出),而不是左):snake.setDir (1,0) = False下= True右= False MOVE_SNAKE = True elif(不是MOVE_SNAKE):如果(输出[0]= = max(输出)):snake.setDir (0,1) = True MOVE_SNAKE = True elif(输出[1]= = max(输出)): snake.setDir(0,1) DOWN = True MOVE_SNAKE = True elif (outputs[2] == max(outputs)): snake.setDir(-1,0) LEFT = True MOVE_SNAKE = True elif (outputs[3] == max(outputs)): snake.setDir(1,0) RIGHT = True MOVE_SNAKE = True win.fill((0, 0, 0)) food.showFood(win) if(MOVE_SNAKE): snake.update() newSnakeHeadX = snake.body[0]['x'] newSnakeHeadY = snake.body[0]['y'] newFoodDist = math.sqrt((newSnakeHeadX - food.x)**2 + (newSnakeHeadY - food.y)**2) deltaFoodDist = newFoodDist - snakeFoodDistEuclidean moveCount += 1 if (newFoodDist <= snakeFoodDistEuclidean): g.fitness += 1 else: g.fitness -= 10 snake.show(win) if(snake.collision()): if score != 0: print('FINAL SCORE IS: '+ str(score)) g.fitness -= 50 break if(snake.eat(food,win)): g.fitness += 15 score += 1 if score == 1 : moveToFood = moveCount # foodEatenMove = pygame.time.get_ticks()/1000 else: moveToFood = moveCount - moveToFood food.foodLocation(snake.body) food.showFood(win)

此外,我将放入checkDirections函数的定义。它所做的是返回一个大小为8对应于8个方向的数组,每个值可以是0(没有食物或身体),1(找到食物但没有身体),3(找到身体但没有食物),或4(找到身体和食物)。

def checkDirections(self, food, up, down, left, right): " ' x+STEP, y-STEP x-STEP, y-STEP x-STEP, y+STEP x+STEP, y x, y-STEP x, y+STEP x-STEP, y x, y-STEP x, y+STEP x-STEP, y " ' view = [] x = self。Xdir y = self。ydir view.append(自我。检查(x, y, STEP, -STEP, food。x, food.y)) view.append(自我。检查(x, y, STEP, STEP, food。x, food.y)) view.append(自我。检查(x, y, -STEP, -STEP, food。x, food.y)) view.append(自我。检查(x, y, -STEP, STEP, food。x, food.y)) view.append(自我。检查(x, y, STEP, 0,食物。x,食物。y)) view.append(self.check(x, y, 0, -STEP, food.x, food.y)) view.append(self.check(x, y, 0, STEP, food.x, food.y)) view.append(self.check(x, y, -STEP, 0, food.x, food.y)) if up == True: view[6] = -999 elif down == True: view[5] = -999 elif left == True: view[4] == -999 elif right == True: view[7] == -999 return view def check(self, x, y, xIncrement, yIncrement, foodX, foodY): value = 0 foodFound = False bodyFound = False while (x >= 0 and x <= WIN_WIDTH and y >= 0 and y <= WIN_HEIGHT): x += xIncrement y += yIncrement if (not foodFound): if (foodX == x and foodY == y): foodFound = True if (not bodyFound): for i in range(1, len(self.body)): if ((x == self.body[i]['x']) and (y == self.body[i]['y'])): bodyFound = True if (not bodyFound and not foodFound): value = 0 elif (not bodyFound and foodFound): value = 1 elif (bodyFound and not foodFound): value = 2 else: value = 3 return value

我用sigmoid作为激活函数。虽然我也试过坦和雷卢,但运气不好。下面是我正在使用的NEAT配置文件:

(整洁)fitness_criterion = max fitness_threshold = 10000 pop_size = 200 reset_on_extinction = False (DefaultGenome) #节点激活选项activation_default =乙状结肠activation_mutate_rate = 0.0 activation_options =乙状结肠#节点聚合选择aggregation_default =和aggregation_mutate_rate = 0.0 aggregation_options = #节点和偏见选择bias_init_mean = 0.0 bias_init_stdev = 1.0 # 30最大和-30年最小偏差bias_max_value = 100.0 bias_min_value = -100.0 bias_mutate_power = 0.5 bias_mutate_rate = 0.7 bias_replace_rate = 0.3 #基因组兼容性选择compatibility_disjoint_coefficient = 1.0 compatibility_weight_coefficient = 0.5 #连接添加/删除率conn_add_prob = 0.8 conn_delete_prob = 0.56 #连接启用选项enabled_default = True #下面0.01 enabled_mutate_rate = 0.3 feed_forward = True initial_connection =全node_add_prob #节点添加/删除率= 0.7 node_delete_prob = 0.4 #网络参数num_hidden = 0 num_inputs = 24 num_outputs = 4 #节点响应选项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重量选择weight_init_mean = 0.0 = 0.0 #连接weight_init_stdev = 1.0 weight_max_value = 30 weight_min_value = -30 weight_mutate_power = 0.5[DefaultSpeciesSet] compatibility_threshold = 3.0 [DefaultStagnation] species_fitness_func = max max_stagnation = 20 species_elitism = 2 [defaulttreproduction] elitism = 2 survival_threshold = 0.2

如果有人有任何可以帮助提高蛇AI性能的见解或想法,请告诉我。

\ endgroup美元
    0
    \ begingroup美元

    正如我所看到的,在每一帧中,如果蛇靠近食物,你将提供1个适合度,如果蛇远离食物,你将移除10个适合度。我说的对吗?另外,当蛇死亡时,你将移除50个健康值,当蛇吃食物时,你将给予15个健康值?

    试着在每一帧移除蛇远离食物的-10适应度,这是没有必要的。不健身也是一种惩罚,你不需要夸大惩罚,因为它会让你的-50健身变得微不足道。正因为如此,快速死亡总比在不靠近食物的情况下存活6帧要好。所以你的ai将发现的第一个策略便是快速死亡(游戏邦注:因为生存对它们来说是件坏事)。这不是你想要的。

    但这些只是假设,尝试一下,告诉我它是否有效!

    \ endgroup美元
    1
    • \ begingroup美元 谢谢你!我会试着回复你… \ endgroup美元- - - - - -东印度缎木Chowdhury 4月4日8点29分

    你的答案

    点击“发表您的答案”,表示您同意我们的服务条款隐私政策饼干的政策

    不是你想要的答案?浏览标记的其他问题必威英雄联盟必威电竞问你自己的问题