1
$ \ begingroup $

我正在尝试为贪吃蛇游戏执行整洁。我的游戏逻辑已经准备好了,它工作正常,配置整洁。但即使经过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在基因组中:net = neat.nn.feedforwordnetwork.create(g,CONFIG)G.Fitness = 0 Snake = Snake()Food = Food = Food(Snake.Body)Run = True Up = Down = left = left = setm_snake = false movefood = 0 score = 0运行时= 0 moveCount = 0延迟(50)clock.tick_busy_loop(10)在pygame.event.get()中的事件:如果event.type == pygame.quit:run = false snake headx = snake.body [0] ['x'] snakeheady =蛇。Body [0] ['y'] snaketailx = snake.body [len(snake.body)-1] ['x'] snaketaily = snake.body [len(snake.body)-1] ['y']snakelength = len(snake.body)snakeheadbottomdist = win_height  -  snakeheady  -  Step snakeheadrightdist = win_width  -  snakeheadyx  -  step foodbottomdist = win_height  -  food.y  -  step food.y  -  Food.y  -  Food.y  -  Food.x  -  Step SnakeFoodDisteuClidean = Math.sqrt((SnakeHeadx  -  Food.x)** 2 +(Snakeheady  -  Food.y)** 2)Snake食品Distmanhattan = ABS(SnakeHeadX  -  Food.x)+Abs(SnakeHeady  -  Food.y)ViewDirections = Snake.CheckDirections(Food,Up,Down,Lefting,Right)Deltafooddist = SnakeFooddisteuClidean输出= Net.Activate((SnakeHeadX,Snakeheady,Snakeheady,Snakeheadbottomdist,SnakeeadrigryDist,Snaketailx,Snaketaily,Snakelength,MoveCount,movetofood,food.x,food.y,foodbott distdist,snakefooddisteuclidean,snake食品ddistmanhattan,viewdirections [0],ViewDirections [2],ViewDirections [3],ViewDirections [4],ViewDirections [5],ViewDirections [6],ViewDirections [7],deltafooddist))if(输出[0] == max(输出)而不是下降):snake.setdir(0,-1)向上= true左= false右= false view_snake = true elif(输出[1] == max(输出)and fun):snake.setdir(0,1)向下= true左= false右= false view_snake = true elif(输出[2] == max(输出)而不是右):snake.setdir(-1,0)left = true = false = false = false move_snake = true elif(输出[3] == max(输出)和不是左):snake.setdir(1,0)右=真正=假指数n = false move_snake = true elif(不是move_snake):if(输出[0] == max(输出)):snake.setdir(0,-1)向上= true move_snake = true elif(输出[1] == max(输出)):snake.setdir(0,1)向下= true move_snake = true elif(输出[2] == max(outputs)):snake.setdir(-1,0)left = true move_snake = true elif(输出[3] == max(输出)):snake.setdir(1,0)右= true move_snake = true win.fill((0,0,0))food.showfood(win)if(move_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(自我,食物,上,向下,左,右):'''x +步骤,y-step x +步骤,y +步x步,y-step x步,y +步x +步骤那y x, y-STEP x, y+STEP x-STEP, y ''' view = [] x = self.xdir y = self.ydir view.append(self.check(x, y, STEP, -STEP, food.x, food.y)) view.append(self.check(x, y, STEP, STEP, food.x, food.y)) view.append(self.check(x, y, -STEP, -STEP, food.x, food.y)) view.append(self.check(x, y, -STEP, STEP, food.x, food.y)) view.append(self.check(x, y, STEP, 0, food.x, food.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

我使用符切函数作为激活函数。虽然我已经尝试过Tanh和Relu也没有运气。以下是我正在使用的neat配置文件:

[neat] fitness_criterion = max fitness_threshold = 10000 pop_size = 200 reset_on_xextinction = false [default genome]#节点激活选项activation_default = sigmoid activation_mutate_rate = 0.0 activation_options = sigmoid#node聚合选项aggregation_default = sum聚合_mutate_rate = 0.0聚合= sum#节点偏置选项bias_init_mean =0.0 bias_init_stdev = 1.0#为30 max和-30 for min 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#基因组兼容性选项兼容性_disjoint_coeffirity = 1.0兼容性_weight_coeffiol = 0.5#connection添加/删除rates conn_add_prob = 0.8 conn_delete_prob= 0.56#Connection Enable选项Enabled_Default = True#下面是0.01 Enabled_mutate_rate = 0.3 Feed_Forward = True initial_connection = full#node add / refal ry event_add_prob = 0.7 node_delete_prob = 0.4 #wentrol parameters num_hidden = 0 num_inputs = 24 num_outputs = 4#节点响应选项response_init_stdev = 0.0 response_min_value = 30.0 response_mutate_power = 0.0 response_mutate_rate = 0.0 response_replate_rate = 0.0#connection权重选项权力_init_mean = 0.0 weight_init_stdev = 1.0 weight_min_value = 30 proce_min_value = -30 procex_mutate_power = 0.5 weight_power = 0.5 weight_mutate_rate= 0.8权重= 0.1 [defaultspeciesset]兼容性_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帧,而不会更接近食物。所以你的AIS将发现的第一个策略正在迅速旋转(因为幸存对他们不好)。这不是你想要的。

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

    $ \ endgroup $
    1
    • $ \ begingroup $ 谢谢..我会尝试这个并回复你... $ \ endgroup $-Ayan Chowdhury. 4月4日20分8:29分

    你的答案

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

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