Ejemplo n.º 1
0
 def __init__(self, number_tables, table_size, guest_list):
     # arrangement_list is population
     self.arrangement_list = []
     self.table_size = table_size
     self.number_tables = number_tables
     self.guest_list = copy.deepcopy(guest_list)
     for i in range(POPULATION_SIZE):
         new_gl = copy.deepcopy(self.guest_list)
         self.arrangement_list.append(
             Arrangement(self.number_tables, self.table_size, new_gl))
     for arr_ in self.arrangement_list:
         arr_.create_tables()
         arr_.seat_guests()
Ejemplo n.º 2
0
    def evolve_one_generation_b(self):
        # for each window select parents
        range_of_arrangements = int(POPULATION_SIZE / WINDOW_SIZE)
        rand.shuffle(self.arrangement_list)
        diversity_for_avg = []
        new_arrangements = []
        for i in range(range_of_arrangements):
            window = copy.deepcopy(self.arrangement_list[:WINDOW_SIZE])
            del (self.arrangement_list[:WINDOW_SIZE])
            for arrangement in window:
                arrangement.get_fitness()
            window.sort(key=lambda arr: arr.fitness)  # get best 2 parents

            parent1, parent2 = window[0], window[1]  # PARENT SELECTION HERE

            # cross_over
            p1_master_list = copy.deepcopy(parent1.export_master_list())
            p2_master_list = copy.deepcopy(parent2.export_master_list())
            p1_for_child1 = copy.deepcopy(p1_master_list)
            p2_for_child1 = copy.deepcopy(p2_master_list)
            p1_for_child2 = copy.deepcopy(p1_master_list)
            p2_for_child2 = copy.deepcopy(p2_master_list)
            child_list1 = self.pmx_crossover(p1_for_child1, p2_for_child1,
                                             self.table_size)
            child_list2 = self.pmx_crossover(p1_for_child2, p2_for_child2,
                                             self.table_size)

            list_for_mutating = []  # list of lists

            for i_ in range(MUTANT_GROUPS):
                ch1_mutant = copy.deepcopy(child_list1)
                ch2_mutant = copy.deepcopy(child_list2)
                list_for_mutating.append(ch1_mutant)
                list_for_mutating.append(ch2_mutant)

            for new_mutant in list_for_mutating:
                self.mutate(new_mutant, LIMIT_MUTATIONS)

            list_of_new_mutants = []  # list of Arrangement objects
            for new_mutant in list_for_mutating:
                guest_list_copy = copy.deepcopy(self.guest_list)
                arrangement = Arrangement(self.number_tables, self.table_size,
                                          guest_list_copy)
                # break continutous list of guests back into their seperate tables
                now_seperate = [
                    new_mutant[x:x + self.table_size]
                    for x in range(0, len(new_mutant), self.table_size)
                ]
                table_list = []
                for index, list_ in enumerate(now_seperate):
                    table = Table(self.table_size, index)
                    table.guests = list_
                    table_list.append(table)
                arrangement.tables = table_list
                list_of_new_mutants.append(arrangement)

            # now list_of_new_mutants [] has mutated arrangement objects ready for survivor selection
            for arrangement in list_of_new_mutants:
                arrangement.get_fitness()
            parent1_group = []
            parent2_group = []
            for arr in list_of_new_mutants:
                div_1 = self.measure_diversity(arr, parent1)
                div_2 = self.measure_diversity(arr, parent2)
                if div_1 < div_2:
                    parent1_group.append(arr)
                else:
                    parent2_group.append(arr)

            parent1_group.sort(key=lambda arr: arr.fitness)
            parent2_group.sort(key=lambda arr: arr.fitness)

            # Attempting to add some diversity here, we want SOME suboptimal solutions to pass
            # so that they may end up evolving into even better solutions
            if parent1_group:
                self.add_randomness(parent1_group,
                                    LIMIT_CHANGES_SURVIVOR_SELECTION)
            if parent2_group:
                self.add_randomness(parent2_group,
                                    LIMIT_CHANGES_SURVIVOR_SELECTION)

            # for i in range(WINDOW_SIZE - 2):
            if WITH_REPLACEMENT:
                survivors = [parent1, parent2]
            else:
                survivors = []
            p1_index = 0
            p2_index = 0
            while len(survivors) < WINDOW_SIZE:
                if i % 2 == 0:
                    if parent1_group:
                        survivors.append(parent1_group[p1_index])
                        p1_index += 1
                    else:
                        survivors.append(parent2_group[p2_index])
                        p2_index += 1

                else:
                    if parent2_group:
                        survivors.append(parent2_group[p2_index])
                        p2_index += 1
                    else:
                        survivors.append(parent1_group[p1_index])
                        p1_index += 1
                i += 1
            for survivor in survivors:
                new_arrangements.append(survivor)
            diversity = self.measure_diversity(parent1, parent2)
            diversity_for_avg.append(diversity)
            print("Parents fitness(1, 2): {} {} Their Diversity: {}".format(
                parent1.fitness, parent2.fitness, diversity))

        avg_diversity = sum(diversity_for_avg) / len(diversity_for_avg)
        print("Average diversity between parents: ", avg_diversity)

        self.arrangement_list = new_arrangements
        top_three_diversity = self.top_three_diversity()
        print("Diversity for the top three in the next generation: ",
              top_three_diversity)
Ejemplo n.º 3
0
 def __init__(self):
     Arrangement.__init__(self, "baby's breath", 4)
     MothersDay.__init__(self, "white", 0, 0)
Ejemplo n.º 4
0
 def __init__(self):
     Arrangement.__init__(self, "poppies", 4)
     MothersDay.__init__(self, "red", 0, 0)
Ejemplo n.º 5
0
 def __init__(self):
     Arrangement.__init__(self, "daisies", 4)
     MothersDay.__init__(self, "white", 0, 0)
Ejemplo n.º 6
0
 def __init__(self):
     Arrangement.__init__(self, "rose", 7)
     ValentinesDay.__init__(self, "red", 0, 0)