Exemplo n.º 1
0
 def __init__(self, filename):
     with open(filename, "r") as f:
         self.pop_size = int(f.readline())
         self.cp = float(f.readline())
         self.mp = float(f.readline())
         self.n_iterations = int(f.readline())
     self.population = Population(self.pop_size, self.cp, self.mp)
Exemplo n.º 2
0
def cool_test_once():
    population = Population(100, pb, 0.001)

    t_end = time.time() + 10
    while time.time() < t_end:
        population.get_next_gen()

    plt.plot(population.stats)
    plt.show()
Exemplo n.º 3
0
def cool_test_30():
    maxims = []
    for i in range(30):
        pop = Population(40, pb, 0.01)
        while pop.generation < 1000:
            pop.get_next_gen()
        maxims.append(pop.get_best())
        print(str(i) + " " + str(maxims[i]))
    plt.plot(maxims)
    plt.show()
Exemplo n.º 4
0
 def __init__(self, n, generationsNR, mutationChance, populationSize, breedCuts, selectionSample=None):
     self.__n = n
     self.__mutationChance = float(mutationChance)
     self.__generationsNR = generationsNR
     self.__populationSize = populationSize
     self.__generations = [Population(n, populationSize)]
     self.__breedCuts = breedCuts
     self.__selectionSample = selectionSample
     self.__generationCounter = 1
    def run(self):
        best = []
        p = Population(self.__n, self.__pop_size, self.__cross_prob,
                       self.__prob_mut)
        for i in range(self.__generations):
            p.iteration()
            best.append(p.getPop()[0].fitness())
            if i % 10 == 0:
                self.signal.emit([min(best), i])

        self.signal.emit([p.getPop()[0], p.getPop()[0].fitness(), "done"])
Exemplo n.º 6
0
class Algorithm:
    def __init__(self, filename):
        with open(filename, "r") as f:
            self.pop_size = int(f.readline())
            self.cp = float(f.readline())
            self.mp = float(f.readline())
            self.n_iterations = int(f.readline())
        self.population = Population(self.pop_size, self.cp, self.mp)

    def iterate(self):
        self.population.evaluate()
        self.population.selection()

    def run(self):
        num_iterations = 0
        while num_iterations < self.n_iterations:
            self.iterate()
            print("ITERATION: {} - FITTEST: {}".format(num_iterations, self.population.best().fitness_score))
            num_iterations += 1
        return self.population.best()

    def run_plot(self):
        best_fitness = []
        num_iterations = 0
        for e in self.population.individuals:
            print(e.fitness_score)

        while num_iterations < self.n_iterations:

            best_fitness.append(self.population.best())
            self.iterate()
            if self.population.best().fitness_score == 60:
                break
            print("ITERATION: {} - FITTEST: {}".format(num_iterations, self.population.best().fitness_score))
            num_iterations += 1
        return best_fitness
Exemplo n.º 7
0
 def __newGeneration(self, populationList=None):
     if populationList is not None:
         self.__generations.append(Population(self.__n, self.__populationSize, populationList))
     self.__generations.append(Population(self.__n, self.__populationSize, self.__generations[-1].getPopulation()))
 def run(self, generations, n, pop_size, cross_prob, prob_mut):
     p = Population(n, pop_size, cross_prob, prob_mut)
     for i in range(generations):
         p.iteration()
     return (p.getPop()[0], p.getPop()[0].fitness())