Example #1
0
 def evolve(self):
     
     self.population = self.utils.create_initial_population()
     self.utils.fast_nondominated_sort(self.population)
     for front in self.population.fronts:
         self.utils.calculate_crowding_distance(front)
     children = self.utils.create_children(self.population)
     returned_population = None 
     for i in range(self.num_of_generations):
         self.population.extend(children)
         self.utils.fast_nondominated_sort(self.population)
         new_population = Population()
         front_num = 0
         while len(new_population) + len(self.population.fronts[front_num]) <= self.num_of_individuals:
             self.utils.calculate_crowding_distance(self.population.fronts[front_num])
             new_population.extend(self.population.fronts[front_num])
             front_num += 1
             
         sorted(self.population.fronts[front_num], cmp=self.utils.crowding_operator)
         new_population.extend(self.population.fronts[front_num][0:self.num_of_individuals-len(new_population)])
         returned_population = self.population
         self.population = new_population
         children = self.utils.create_children(self.population)
         for fun in self.on_generation_finished:
             fun(returned_population, i)
     return returned_population.fronts[0]
Example #2
0
 def create_initial_population(self):
     population = Population()
     for _ in range(self.num_of_individuals):
         individual = self.problem.generate_individual()
         self.problem.calculate_objectives(individual)
         population.append(individual)
     return population
Example #3
0
 def create_initial_population(self):
     population = Population()
     for i in range(self.num_of_individuals):
         print "Individual-", i
         individual = self.problem.generateIndividual()
         population.population.append(individual)
     return population
Example #4
0
 def create_initial_population(self):
     population = Population()
     for _ in xrange(self.num_of_individuals):
         individual = self.problem.generateIndividual()
         '''have been calculate when generate the individual, to see if i was wrong'''
         #self.problem.calculate_objectives(individual)
         population.population.append(individual)
         
     return population
Example #5
0
 def create_initial_population(self, MSR, RV, BI_size, nsol_K, nsol_label,
                               nsol):
     population = Population()
     for count in range(self.num_of_individuals):
         individual = self.problem.generateIndividual(
             MSR[count], RV[count], BI_size[count], nsol_K[count],
             nsol_label[count], nsol[count])
         self.problem.calculate_objectives(individual)
         population.population.append(individual)
     return population
Example #6
0
    def create_initial_population(self):
        
        """creates initial population

            Parameters
            ----------

            Returns
            -------
            Population
                initial population
        """
        
        population = Population()
        for _ in range(self.num_of_individuals):
            individual = self.problem.generate_individual()
            self.problem.calculate_objectives(individual)
            population.append(individual)
        return population
Example #7
0
    def evolve(self):

        self.population = self.utils.create_initial_population()
        self.utils.fast_nondominated_sort(self.population)
        for front in self.population.fronts:
            self.utils.calculate_crowding_distance(front)
        children = self.utils.create_children(self.population)
        returned_population = None
        for i in range(self.num_of_generations):
            self.population.extend(children)
            self.utils.fast_nondominated_sort(self.population)
            new_population = Population()
            front_num = 0
            while len(new_population) + len(self.population.fronts[front_num]
                                            ) <= self.num_of_individuals:
                self.utils.calculate_crowding_distance(
                    self.population.fronts[front_num])
                new_population.extend(self.population.fronts[front_num])
                front_num += 1

            sorted(self.population.fronts[front_num],
                   cmp=self.utils.crowding_operator)
            new_population.extend(
                self.population.fronts[front_num][0:self.num_of_individuals -
                                                  len(new_population)])
            returned_population = self.population
            self.population = new_population
            children = self.utils.create_children(self.population)
            for fun in self.on_generation_finished:
                fun(returned_population, i)
        return returned_population.fronts[0]
Example #8
0
 def evolve(self):
     random.seed(self.utils.problem.seed)  
     np.random.seed(self.utils.problem.seed)
     self.population = self.utils.create_initial_population()
     self.utils.fast_nondominated_sort(self.population)
     for front in self.population.fronts:
         self.utils.calculate_crowding_distance(front)
     children = self.utils.create_children(self.population)
     returned_population = None
     for i in range(self.num_of_generations):
         for indiv in self.population.population:
             indiv_list = list(indiv.features.items())
             criterion, max_depth, min_samples_split, max_leaf_nodes, class_weight = [item[1] for item in indiv_list]
             evolutions_aux = pd.DataFrame({'criterion': [criterion], 'max_depth': [max_depth], 'min_samples_split': [min_samples_split], 'max_leaf_nodes': [max_leaf_nodes], 'class_weight': [class_weight],  'error': indiv.objectives[0], 'dem_fp': indiv.objectives[1], 'generation': i+1, 'rank': indiv.rank, 'actual_depth': indiv.actual_depth, 'actual_leaves': indiv.actual_leaves, 'id': indiv.id, 'creation_mode': indiv.creation_mode, 'seed': self.utils.problem.seed})
             self.evolutions_df = pd.concat([self.evolutions_df, evolutions_aux])
         if i == (self.num_of_generations-1):
             self.evolutions_df.to_csv("./results/population/evolution_" + self.dataset_name + "_" + self.protected_variable + '_seed_' + str(self.utils.problem.seed) + "_gen_" + str(self.num_of_generations) + "_indiv_" + str(self.num_of_individuals) +  ".csv", index = False, header = True, columns = ['seed', 'id', 'generation', 'rank', 'creation_mode', 'error', 'dem_fp', 'criterion', 'max_depth', 'min_samples_split', 'max_leaf_nodes', 'class_weight', 'actual_depth', 'actual_leaves'])
         print("GENERATION:",i)
         self.population.extend(children)
         self.utils.fast_nondominated_sort(self.population)
         new_population = Population()
         front_num = 0
         while len(new_population) + len(self.population.fronts[front_num]) <= self.num_of_individuals:
             self.utils.calculate_crowding_distance(self.population.fronts[front_num])
             new_population.extend(self.population.fronts[front_num])
             front_num += 1
         self.utils.calculate_crowding_distance(self.population.fronts[front_num])
         self.population.fronts[front_num].sort(key=lambda individual: individual.crowding_distance, reverse=True)
         new_population.extend(self.population.fronts[front_num][0:self.num_of_individuals-len(new_population)])
         returned_population = self.population
         self.population = new_population
         children = self.utils.create_children(self.population)
     return returned_population.fronts[0]
Example #9
0
 def evolve(self):
     self.population = self.utils.create_initial_population()
     self.utils.fast_nondominated_sort(self.population)
     for front in self.population.fronts:
         self.utils.calculate_crowding_distance(front)
     children = self.utils.create_children(self.population)
     returned_population = None
     for i in range(self.num_of_generations):
         self.population.extend(children)
         self.utils.fast_nondominated_sort(self.population)
         new_population = Population()
         front_num = 0
         while len(new_population) + len(self.population.fronts[front_num]
                                         ) <= self.num_of_individuals:
             self.utils.calculate_crowding_distance(
                 self.population.fronts[front_num])
             new_population.extend(self.population.fronts[front_num])
             front_num += 1
         self.utils.calculate_crowding_distance(
             self.population.fronts[front_num])
         self.population.fronts[front_num].sort(
             key=lambda individual: individual.crowding_distance,
             reverse=True)
         new_population.extend(
             self.population.fronts[front_num][0:self.num_of_individuals -
                                               len(new_population)])
         returned_population = self.population
         self.population = new_population
         self.utils.fast_nondominated_sort(self.population)
         for front in self.population.fronts:
             self.utils.calculate_crowding_distance(front)
         children = self.utils.create_children(self.population)
     return returned_population.fronts[0]
Example #10
0
    def create_initial_population(self):
        population = Population()
        for _ in range(self.num_of_individuals):
            count = 0
            individual = self.problem.generateIndividual()
            self.problem.calculate_objectives(individual)
            while individual.violated_constraints > 0 and count < 10:
                individual = self.problem.generateIndividual()
                self.problem.calculate_objectives(individual)
                count += 1
            population.population.append(individual)

        return population
Example #11
0
    def evolve(self):
        """main evolving method

            Parameters
            ----------

            Returns
            -------
            array
                array of optimal portfolios
        """

        self.population = self.utils.create_initial_population()
        self.utils.fast_nondominated_sort(self.population)
        for front in self.population.fronts:
            self.utils.calculate_crowding_distance(front)
        children = self.utils.create_children(self.population)
        returned_population = None
        for i in range(self.num_of_generations):
            self.population.extend(children)

            for pop in self.population.population:
                self.problem.calculate_objectives(pop)

            #Eliminate duplicates of  after breeding
            self.population.population = self.utils.eliminate_duplicates(
                self.population.population)

            self.utils.fast_nondominated_sort(self.population)
            new_population = Population()

            front_num = 0
            while len(new_population) + len(self.population.fronts[front_num]
                                            ) <= self.num_of_individuals:
                self.utils.calculate_crowding_distance(
                    self.population.fronts[front_num])
                new_population.extend(self.population.fronts[front_num])
                front_num += 1
            self.utils.calculate_crowding_distance(
                self.population.fronts[front_num])
            self.population.fronts[front_num].sort(
                key=lambda individual: individual.crowding_distance,
                reverse=True)
            new_population.extend(
                self.population.fronts[front_num][0:self.num_of_individuals -
                                                  len(new_population)])

            #returned_population = self.population
            R = copy.deepcopy(self.population.fronts[0])

            self.population = new_population
            self.utils.fast_nondominated_sort(self.population)
            for front in self.population.fronts:
                self.utils.calculate_crowding_distance(front)
            children = self.utils.create_children(self.population)
        return R
Example #12
0
    def create_initial_population(self):
        population = Population()
        first_individual = True
        for k in range(self.num_of_individuals):
            if k == 0:
                individual = self.problem.generate_default_individual_gini()
                self.problem.calculate_objectives(individual, first_individual,
                                                  self.problem.seed)
                population.append(individual)
                first_individual = False

            elif k == 1:
                individual = self.problem.generate_default_individual_entropy()
                self.problem.calculate_objectives(individual, first_individual,
                                                  self.problem.seed)
                population.append(individual)
            else:
                individual = self.problem.generate_individual()
                self.problem.calculate_objectives(individual, first_individual,
                                                  self.problem.seed)
                population.append(individual)
        return population
Example #13
0
    def evolve(self):

        self.population = self.utils.create_initial_population()
        self.utils.fast_nondominated_sort(self.population)
        for front in self.population.fronts:
            self.utils.calculate_crowding_distance(front)
        children = self.utils.create_children(self.population, 1)
        returned_population = None
        print("-----------create the initial children----------- " +
              time.strftime("%H:%M:%S"))
        for i in xrange(self.num_of_generations):
            self.population.extend(children)
            self.utils.fast_nondominated_sort(self.population)
            print("-----------fast nondominated sort-----------  " +
                  time.strftime("%H:%M:%S"))
            new_population = Population()
            front_num = 0
            ## according to the sum(10), add individuals, front 0 come first, then the second front
            while len(new_population) + len(self.population.fronts[front_num]
                                            ) <= self.num_of_individuals:
                self.utils.calculate_crowding_distance(
                    self.population.fronts[front_num])
                new_population.extend(self.population.fronts[front_num])
                front_num += 1

            sorted(self.population.fronts[front_num],
                   cmp=self.utils.crowding_operator)
            new_population.extend(
                self.population.fronts[front_num][0:self.num_of_individuals -
                                                  len(new_population)])
            returned_population = self.population
            self.population = new_population

            print("-------------created the new children------------  " +
                  time.strftime("%H:%M:%S"))
            children = self.utils.create_children(self.population, i)
            print("-------------finished the new children------------  " +
                  time.strftime("%H:%M:%S"))
            for fun in self.on_generation_finished:
                fun(self.vardim, self.iteration, returned_population, i)
        return returned_population.fronts[0]
Example #14
0
    def evolve(self, solution, MSR, RV, BI_size, solution_K, new_solution,
               new_msr, new_rv, new_bisize, new_K, generation, self_population,
               population_label, children_label):
        #population, MSR, RV, BI_size, population_K,nsol, new_rv, new_msr, new_K,generation,self_population,population_label,children_label)

        if self_population is None:

            #self.population = self.utils.create_initial_population(sil_sco,DunnIndex,solution)
            self.population = self.utils.create_initial_population(
                MSR, RV, BI_size, solution_K, population_label, solution)
            self.utils.fast_nondominated_sort(self.population)
            for front in self.population.fronts:
                self.utils.calculate_crowding_distance(front)

        else:
            #print "SelfPOP", self_population, len(self_population), type(self_population), self_population.fronts
            self.population = self_population

        children = self.utils.create_children(new_solution, new_msr, new_rv,
                                              new_bisize, children_label,
                                              new_K)
        self.population.extend(children)
        self.utils.fast_nondominated_sort(self.population)

        new_population = Population()
        front_num = 0

        while len(new_population) + len(
                self.population.fronts[front_num]) <= self.num_of_individuals:
            self.utils.calculate_crowding_distance(
                self.population.fronts[front_num])
            new_population.extend(self.population.fronts[front_num])
            front_num += 1

        sorted(self.population.fronts[front_num],
               cmp=self.utils.crowding_operator)
        new_population.extend(
            self.population.fronts[front_num][0:self.num_of_individuals -
                                              len(new_population)])
        returned_population = self.population
        self.population = new_population

        nsol = []
        objective = []
        new_pop_label = {}
        new_pop_K = {}
        cc = 0

        for _ in new_population:
            x = (getattr(_, 'features'))
            obj = (getattr(_, 'objectives'))
            label = (getattr(_, 'label'))
            K1 = (getattr(_, 'K'))
            nsol.insert(cc, x)
            objective.insert(cc, obj)
            new_pop_label[cc] = label
            new_pop_K[cc] = K1
            cc += 1
        for fun in self.on_generation_finished:
            fun(returned_population, generation)

        # population_label_old=dict(population_label)
        # solution_K_old=dict(solution_K)
        # solution = list(solution)
        # new_solution = list(new_solution)
        #
        # for k in range(len(solution)):
        #     y = list(solution[k])
        #     if y in nsol:
        #         get_index = nsol.index(y)
        #         population_label[get_index] = population_label_old[k]
        #         solution_K[get_index]=solution_K_old[k]
        #
        #     elif (new_solution in nsol) and (y not in nsol):
        #         get_index = nsol.index(new_solution)
        #         population_label[get_index] = children_label
        #         solution_K[get_index] = new_K
        #         print "new solution replaced {0}th solution in population, old and new objectives:".format(get_index), "(",[MSR[k],RV[k],BI_size[k]], ")", objective[get_index]

        return nsol, objective, self.population, new_pop_label, new_pop_K