Python-遗传算法突变函数不起作用

时间:2018-09-03 08:36:31

标签: python algorithm artificial-intelligence mutation genetic

我在python / pygame中创建了一个AI,但是即使经过数小时的调试,我仍然找不到为什么个体(点)没有突变的原因。几代人之后,所有个体彼此重叠并遵循相同的精确路径。但是突变后,它们的移动方式应该有所不同。

这是每2到3代后有10位人口的样子。

Image 1 Image 2 Image 3

正如您所看到的,几代人之后他们就重叠了,种群中的所有个体都沿着完全相同的路径一起移动!我们需要突变!!!

如果您能找到任何错误,我将非常感谢您。谢谢!

我从以下位置看到了代码:https://www.youtube.com/watch?v=BOZfhUcNiqk&t 并尝试在python中制作。这是我的代码

import pygame, random
import numpy as np

pygame.init()
width = 800
height = 600
screen = pygame.display.set_mode((width, height))
pygame.display.set_caption("The Dots")

FPS = 30
clock = pygame.time.Clock()
gameExit = False

grey = [30, 30, 30]
white = [255, 255, 255]
black = [0, 0, 0]
red = [255, 0, 0]

goal = [400, 10]

class Dot():
    def __init__(self):
        self.x = int(width/2)
        self.y = int(height - 150)
        self.r = 3
        self.c = black
        self.xVel = self.yVel = 0
        self.xAcc = 0
        self.yAcc = 0
        self.dead = False
        self.steps = 0
        self.reached = False
        self.brain = Brain(200)

    def show(self):
        pygame.draw.circle(screen, self.c, [int(self.x), int(self.y)], self.r)

    def update(self):
        if (self.x >= width or self.x <= 0 or self.y >= height or self.y <= 0):
            self.dead = True
        elif (np.sqrt((self.x-goal[0])**2 + (self.y-goal[1])**2) < 5):
            self.reached = True
        if not self.dead and not self.reached:
            if len(self.brain.directions) > self.steps:
                self.xAcc = self.brain.directions[self.steps][0]
                self.yAcc = self.brain.directions[self.steps][1]
                self.steps += 1

                self.xVel += self.xAcc
                self.yVel += self.yAcc
                if self.xVel > 5:
                    self.xVel = 5
                if self.yVel > 5:
                    self.yVel = 5
                self.x += self.xVel
                self.y += self.yVel
            else: self.dead = True

    def calculateFitness(self):
        distToGoal = np.sqrt((self.x-goal[0])**2 + (self.y-goal[1])**2)
        self.fitness = 1/(distToGoal**2)
        return self.fitness

    def getChild(self):
        child = Dot()
        child.brain = self.brain
        return child

class Brain():
    def __init__(self, size):
        self.size = size
        self.directions = []
        self.randomize()

    def randomize(self):
        self.directions.append((np.random.normal(size=(self.size, 2))).tolist())
        self.directions = self.directions[0]

    def mutate(self):
        for i in self.directions:
            rand = random.random()
            if rand < 1:
                i = np.random.normal(size=(1, 2)).tolist()[0]

class Population():
    def __init__(self, size):
        self.size = size
        self.dots = []
        self.fitnessSum = 0

        for i in range(self.size):
            self.dots.append(Dot())

    def show(self):
        for i in self.dots:
            i.show()

    def update(self):
        for i in self.dots:
            i.update()

    def calculateFitness(self):
        for i in self.dots:
            i.calculateFitness()

    def allDead(self):
        for i in self.dots:
            if not i.dead and not i.reached:
                return False
        return True

    def calculateFitnessSum(self):
        self.fitnessSum = 0
        for i in self.dots:
            self.fitnessSum += i.fitness

    def SelectParent(self):
        rand = random.uniform(0, self.fitnessSum)
        runningSum = 0
        for i in self.dots:
            runningSum += i.fitness
            if runningSum > rand:
                return i

    def naturalSelection(self):
        newDots = []
        self.calculateFitnessSum()
        for i in self.dots:
            parent = self.SelectParent()
            newDots.append(parent.getChild())

        self.dots = newDots

    def mutate(self):
        for i in self.dots:
            i.brain.mutate()

test = Population(100)

while not gameExit:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            gameExit = True
    screen.fill(white)

    if test.allDead():
        #Genetic Algorithm
        test.calculateFitness()
        test.naturalSelection()
        test.mutate()

    else:
        test.update()
        test.show()

    pygame.draw.circle(screen, red, goal, 4)
    clock.tick(FPS)
    pygame.display.update()
pygame.quit()

感谢您的帮助!

1 个答案:

答案 0 :(得分:0)

我没有阅读完整的代码,但是在这里

def mutate(self):
    for i in self.directions:
        rand = random.random()
        if rand < 1:
            i = np.random.normal(size=(1, 2)).tolist()[0]

您正在尝试为i(它是一个迭代器)分配一个新值,因此它不会发生任何变化,这说明了为什么您在处理突变时遇到了麻烦。

您应该具有以下内容:

def mutate(self):
    for i in range(len(self.directions)):
        rand = random.random()
        if rand < 1:
            self.directions[i] = np.random.normal(size=(1, 2)).tolist()[0]

或者您可以使用列表推导 https://docs.python.org/3/tutorial/datastructures.html#list-comprehensions