为什么我的程序在结束之前就停止了?

时间:2019-12-25 04:59:53

标签: python artificial-intelligence

当我在程序运行到结束时运行程序时,它只是挂起,就像停止一样。有人可以帮我解决这个问题吗?我正在尝试开发一种进化算法,以找到通往目标的途径

Stackoverflow说我的问题主要是代码,因此请忽略下一行文本

堆栈溢出堆栈溢出堆栈溢出堆栈溢出堆栈溢出堆栈溢出堆栈溢出堆栈溢出堆栈溢出堆栈溢出堆栈溢出堆栈溢出堆栈溢出堆栈溢出堆栈溢出堆栈溢出堆栈溢出堆栈溢出堆栈溢出堆栈溢出堆栈溢出堆栈溢出堆栈溢出堆栈溢出过流堆栈过流堆栈过流堆栈过流堆栈过流堆栈过流堆栈过流堆栈过流堆栈过流堆栈过流堆栈过流堆栈过流

这是我的代码:

import turtle
import random
import time
import math



wn = turtle.Screen()
wn.tracer(0)

goal = turtle.Turtle()
goal.shape('circle')
goal.speed(0)
goal.color('red')
goal.penup()
goal.goto(0, 300)
wn.update()





def isCollision(t1, t2):
    d = math.sqrt(math.pow(t1.xcor()-t2.xcor(),2) + math.pow(t1.ycor()-t2.ycor(),2))
    if d < 15:
        return True
    else:
        return False




class Brain:
    def __init__(self, length, rate):
        self.directions = []
        self.step = 0
        self.steps = length
        self.mutation_rate = rate
        self.mutated_list = []

    def brain(self):
        randomize()

    def randomize(self):
        for i in range(self.steps):
            self.directions.append(random.randint(0, 360))

    def clone(self):
        return self.directions

    def mutate(self):

        for move in self.directions:

            if round(random.uniform(0, 1), 3) < self.mutation_rate:
                #print('MUTATAAAAAAA')
                self.mutated_list.append(random.randint(0, 360))
            else:
                self.mutated_list.append(move)

        return self.mutated_list



class Dot:



    def __init__(self):
        self.brain = Brain(500, 0.01)
        self.brain.randomize()
        self.dead = False
        self.reachedGoal = False
        self.isBest = False
        self.fitness = float()
        self.dot = turtle.Turtle()
        self.reset()
        self.update_()

    def reset(self):
        #print('reset')
        self.dot.speed(0)
        self.dot.shape('circle')
        self.dot.shapesize(0.5, 0.5)
        self.dot.penup()
        self.dot.goto(0, -290)




    def update_(self):
        #wn.update()
        if self.dot.xcor() > 310:
            self.dead = True

        if self.dot.xcor() < -310:
            self.dead = True

        if self.dot.ycor() > 310:
            self.dead = True

        if self.dot.ycor() < -310:
            self.dead = True

        if isCollision(self.dot, goal):
            reachedGoal = True
            #dead = True




    def move(self):
        for move in self.brain.directions:
            if not self.dead:
                self.dot.setheading(move)
                self.dot.forward(12)
                self.update_()

                #time.sleep(0.05)


class Population:
    def __init__(self, size):
        self.population = []
        self.size = size
        self.fitness = 10000
        self.best_fitness = 10000
        self.fittest_dot = 0
        self.bad_dead = []
        for i in range(self.size):
            self.population.append(Dot())
        for dot in self.population:
            dot.reset()
            dot.move()
            wn.update()



    def create_new_population(self):
        self.bad_dead = []

        for dot in self.population:
            dot.dead = False

        for dot in self.population:
            dot.reset()
            dot.brain.directions = self.population[self.fittest_dot].brain.directions
            dot.brain.directions = dot.brain.mutate()
            dot.move()
            wn.update()



    def getFittest(self):
        count = 0
        for dot in self.population:

            if not dot.dead:
                self.fitness = math.sqrt((goal.xcor()-dot.dot.xcor())*(goal.xcor()-dot.dot.xcor())+(goal.ycor()-dot.dot.ycor())*(goal.ycor()-dot.dot.ycor()))
                if self.fitness < self.best_fitness:
                    self.best_fitness = self.fitness
                    self.fittest_dot = count
            count += 1
            dot.dead = True


        wn.update()

    def check_for_end(self):
        for dot in self.population:
            #print(dot.dead)
            self.bad_dead.append(dot.dead)

        nTemp = self.bad_dead[0]
        bEqual = True


        for item in self.bad_dead:
          if nTemp != item:
            bEqual = False
            break
          else:
            bad_dead = []
        return bEqual





population = Population(50)
#population.getFittest()

population.getFittest()

if population.check_for_end():
    print('pin')
    population.create_new_population()
print('fit')
population.getFittest()

if population.check_for_end():
    print('pin')
    population.create_new_population()

print('done')


wn.update()


wn.mainloop()

1 个答案:

答案 0 :(得分:-1)

您需要包括某种循环,否则程序将只运行然后退出。我建议一个while循环,该循环将在条件不满足时运行。 例如

while True:
  population.getFittest()

  if population.check_for_end():
    print('pin')
    population.create_new_population()
  print('fit')
  population.getFittest()

  if population.check_for_end():
    print('pin')
    population.create_new_population()

  print('done')


  wn.update()

  wn.mainloop()

要在模拟完成时退出循环,应添加一个break语句,该语句将退出循环。