Example #1
0
    def simulate_evolution(self, pop_count=100, gen_count=150000, save_folder="test/"):
        # initializes population
        tot_fitness = 0  # total fitness of generation
        population = []
        pop_weight = []
        most_fit_org = circle_org()
        population.append(most_fit_org)
        pop_weight.append(most_fit_org.fit_score)
        tot_fitness += most_fit_org.fit_score

        for c in range(pop_count - 1):
            new_org = circle_org()
            population.append(new_org)
            pop_weight.append(new_org.fit_score)
            tot_fitness += new_org.fit_score
            if new_org.fit_score > most_fit_org.fit_score:
                most_fit_org = new_org

        # begins loop for evolution
        cond = True  # TODO: stop loop if certain fitscore is met
        for x in range(0, gen_count):
            # saving most fit
            most_fit_org.save_image("test/" + str(x) + " " + str(most_fit_org.fit_score) + ".png")
            print(most_fit_org.fit_score)
            most_fit_org.fit_score = 0  # TODO: DIRTY, FIX THIS

            temp_population = []  # temporary population buff
            new_pop_count = 0
            while new_pop_count < pop_count:
                # 2 new individuals
                p1 = weighted_choice(population, pop_weight)
                p2 = weighted_choice(population, pop_weight)
                # if yes_no(circ_org_handler.CROSS_RATE):
                # select 2 parents
                kids = self.gen_new_org(p1, p2)
                for kid in kids:
                    if kid.fit_score > p1.fit_score or kid.fit_score > p2.fit_score:
                        temp_population.append(kid)
                        new_pop_count += 1
                        if kid.fit_score >= most_fit_org.fit_score:
                            most_fit_org = kid
            population = temp_population
Example #2
0
 def gen_new_org(self, org1, org2):
     new_org_dna = self.__crossover__(org1.DNA, org2.DNA)
     for dna in new_org_dna:
         self.__mutate__(dna)
     # print (new_org_dna[1].bin)
     return (circle_org(new_org_dna[0], False), circle_org(new_org_dna[1], False))