Ejemplo n.º 1
0
    def test_should_execute_work_properly_with_a_two_crossover_operators(self):
        operator = CompositeCrossover(
            [SBXCrossover(0.9, 20.0),
             IntegerSBXCrossover(0.1, 20.0)])

        float_solution1 = FloatSolution([2.0], [3.9], 3)
        float_solution1.variables = [3.0]
        float_solution2 = FloatSolution([2.0], [3.9], 3)
        float_solution2.variables = [4.0]
        integer_solution1 = IntegerSolution([2], [4], 3)
        integer_solution1.variables = [3.0]
        integer_solution2 = IntegerSolution([2], [7], 3)
        integer_solution2.variables = [4.0]

        composite_solution1 = CompositeSolution(
            [float_solution1, integer_solution1])
        composite_solution2 = CompositeSolution(
            [float_solution2, integer_solution2])

        children = operator.execute([composite_solution1, composite_solution2])

        self.assertIsNotNone(children)
        self.assertEqual(2, len(children))
        self.assertEqual(2, children[0].number_of_variables)
        self.assertEqual(2, children[1].number_of_variables)
Ejemplo n.º 2
0
    def create_solution(self) -> CompositeSolution: # INICIALIZAÇÂO DO PROBLEMA
        attributes_solution = IntegerSolution(self.int_lower_bound_attribute, self.int_upper_bound_attribute,
                                              self.number_of_objectives, self.number_of_constraints)

        labels_solution = IntegerSolution(self.int_lower_bound_label, self.int_upper_bound_label,
                                          self.number_of_objectives, self.number_of_constraints)

        points_solution = FloatSolution(self.lower_centroids, self.upper_centroids,
                                        self.number_of_objectives, self.number_of_constraints)
        if self.isSeed:
            attributes_solution.variables = self.antecedentes
            labels_solution.variables = self.consequentes
            points_solution.variables = self.centroids
            self.isSeed = False
        else:
            attributes_solution.variables = \
                [random.randint(self.int_lower_bound_attribute[i], self.int_upper_bound_attribute[i]) for i in
                 range(len(self.int_lower_bound_attribute))]

            labels_solution.variables = \
                [random.randint(self.int_lower_bound_label[i], self.int_upper_bound_label[i]) for i in
                 range(len(self.int_lower_bound_label))]

            points_solution.variables = \
                [random.uniform(self.lower_centroids[i], self.upper_centroids[i]) for i
                 in range(len(self.lower_centroids))]

        return CompositeSolution([attributes_solution, labels_solution, points_solution])
Ejemplo n.º 3
0
    def test_should_execute_raise_and_exception_if_the_types_of_the_solutions_do_not_match_the_operators(self):
        operator = CompositeCrossover([SBXCrossover(1.0, 5.0), SPXCrossover(0.9)])

        float_solution1 = FloatSolution([2.0], [3.9], 3)
        float_solution1.variables = [3.0]
        float_solution2 = FloatSolution([2.0], [3.9], 3)
        float_solution2.variables = [4.0]
        composite_solution1 = CompositeSolution([float_solution1, float_solution2])
        composite_solution2 = CompositeSolution([float_solution1, float_solution2])

        with self.assertRaises(InvalidConditionException):
            operator.execute([composite_solution1, composite_solution2])
Ejemplo n.º 4
0
    def test_should_execute_return_the_parents_if_the_crossover_probability_is_zero(self):
        crossover: SBXCrossover = SBXCrossover(0.0, 20.0)

        solution1 = FloatSolution([1, 2], [2, 4], 2, 2)
        solution2 = FloatSolution([1, 2], [2, 4], 2, 2)

        solution1.variables = [1.5, 2.7]
        solution2.variables = [1.7, 3.6]

        offspring = crossover.execute([solution1, solution2])

        self.assertEqual(2, len(offspring))
        self.assertEqual(solution1.variables, offspring[0].variables)
        self.assertEqual(solution2.variables, offspring[1].variables)
Ejemplo n.º 5
0
    def test_should_the_solution_change_if_the_probability_is_one(self):
        operator = SimpleRandomMutation(1.0)
        solution = FloatSolution([-5, -5, -5], [5, 5, 5], 1)
        solution.variables = [1.0, 2.0, 3.0]

        mutated_solution = operator.execute(solution)
        self.assertNotEqual([1.0, 2.0, 3.0], mutated_solution.variables)
Ejemplo n.º 6
0
    def test_should_the_solution_remain_unchanged_if_the_probability_is_zero(self):
        operator = Polynomial(0.0)
        solution = FloatSolution(2, 1, 0, [-5, -5, -5], [5, 5, 5])
        solution.variables = [1.0, 2.0, 3.0]

        mutated_solution = operator.execute(solution)
        self.assertEqual([1.0, 2.0, 3.0], mutated_solution.variables)
Ejemplo n.º 7
0
    def create_initial_solutions(self) -> List[S]:
        if self.problem.config.iteration_round == 0:
            # print(self.problem.config.iteration_round, self.population_size)
            return [
                self.population_generator.new(self.problem)
                for _ in range(self.population_size)
            ]

            # return [self.problem.create_solution() for _ in range(self.population_size)]  ## random generator
        else:
            population = [
                self.population_generator.new(self.problem)
                for _ in range(int(0.5 * self.population_size))
            ]
            # existing_population = []
            for i in range(self.population_size -
                           int(0.5 * self.population_size)):
                new_solution = FloatSolution(
                    self.problem.lower_bound, self.problem.upper_bound,
                    self.problem.number_of_objectives,
                    self.problem.number_of_constraints)
                new_solution.variables = self.initial_population[
                    self.problem.config.goal_selection_index][i]
                # print(i, self.initial_population[self.problem.config.goal_selection_index][i])
                population.append(new_solution)
            return population
Ejemplo n.º 8
0
    def create_solution(self) -> FloatSolution:
        new_solution = FloatSolution(self.number_of_variables, self.number_of_objectives, self.number_of_constraints,
                                     self.lower_bound, self.upper_bound)
        new_solution.variables = \
            [random.uniform(self.lower_bound[i]*1.0, self.upper_bound[i]*1.0) for i in range(self.number_of_variables)]

        return new_solution
Ejemplo n.º 9
0
    def create_solution(self) -> FloatSolution:
        new_solution = FloatSolution(self.number_of_variables, self.number_of_objectives, self.number_of_constraints,
                                     self.lower_bound, self.upper_bound)
        new_solution.variables = \
            [random.uniform(self.lower_bound[i]*1.0, self.upper_bound[i]*1.0) for i in range(self.number_of_variables)]

        return new_solution
Ejemplo n.º 10
0
def lorenz_map(swarm_size, number_of_variables, number_of_objectives,
               number_of_constraints, lower_bound, upper_bound):
    #lorenz map initialization
    chaos_limits = {
        "x_high": 22.0,
        "x_low": -22.0,
        "y_high": 30.0,
        "y_low": -30.0,
        "z_high": 55.0,
        "z_low": 0.0
    }
    lorenz_sets = number_of_variables // 3 + 1
    chaos_set = {}
    for it in range(lorenz_sets):
        chaos_set["xs_list_" + str(it + 1)] = []
        chaos_set["ys_list_" + str(it + 1)] = []
        chaos_set["zs_list_" + str(it + 1)] = []
        dt = 0.02
        xs = np.empty((swarm_size + 1))
        ys = np.empty((swarm_size + 1))
        zs = np.empty((swarm_size + 1))
        xs[0], ys[0], zs[0] = (np.random.rand(), np.random.rand(),
                               np.random.rand())
        for i in range(swarm_size):
            # Derivatives of the X, Y, Z state
            x_dot, y_dot, z_dot = lorenz(xs[i], ys[i], zs[i])
            xs[i + 1] = xs[i] + (x_dot * dt)
            ys[i + 1] = ys[i] + (y_dot * dt)
            zs[i + 1] = zs[i] + (z_dot * dt)
        chaos_set["xs_list_" + str(it + 1)].extend(xs)
        chaos_set["ys_list_" + str(it + 1)].extend(ys)
        chaos_set["zs_list_" + str(it + 1)].extend(zs)
    choices = list(chaos_set.keys())
    #print(chaos_set)
    random.shuffle(choices)
    result = []
    for i in range(swarm_size):
        temp = []
        for i_1 in range(number_of_variables):
            temp.append(((chaos_set[choices[i_1]][i] -
                          chaos_limits[choices[i_1][0] + "_low"]) /
                         (chaos_limits[choices[i_1][0] + "_high"] -
                          chaos_limits[choices[i_1][0] + "_low"])) *
                        (upper_bound[i_1] - lower_bound[i_1]) +
                        lower_bound[i_1])
            #print(chaos_set[choices[i_1]][i],chaos_limits[choices[i_1][0]+"_low"],(chaos_limits[choices[i_1][0]+"_high"],upper_bound[i_1],lower_bound[i_1]),((chaos_set[choices[i_1]][i] - chaos_limits[choices[i_1][0]+"_low"])/(chaos_limits[choices[i_1][0]+"_high"] - chaos_limits[choices[i_1][0]+"_low"])) * (upper_bound[i_1] - lower_bound[i_1]) + lower_bound[i_1])
            #input()
            #print(chaos_set[sel_list[i_1][0]+ "s_list_" + str(sel_list[i_1][1])][i_1])
        new_solution = FloatSolution(number_of_variables, number_of_objectives,
                                     number_of_constraints, lower_bound,
                                     upper_bound)
        #print(temp)
        #input()
        new_solution.variables = temp
        result.append(new_solution)
        #print(temp)
    #print(result)

    return result
Ejemplo n.º 11
0
    def test_should_the_solution_change_between_max_and_min_value(self):
        operator = SimpleRandom(1.0)
        solution = FloatSolution(4, 1, 0, [-1, 12, -3, -5], [1, 17, 3, -2])
        solution.variables = [-7.0, 3.0, 12.0, 13.4]

        mutated_solution = operator.execute(solution)
        for i in range(solution.number_of_variables):
            self.assertGreaterEqual(mutated_solution.variables[i], solution.lower_bound[i])
            self.assertLessEqual(mutated_solution.variables[i], solution.upper_bound[i])
Ejemplo n.º 12
0
 def create_solution(self):
     """ Creates a new solution based on the bounds and variables. """
     new_solution = FloatSolution(self.lower_bound, self.upper_bound,
                                  self.number_of_objectives,
                                  self.number_of_constraints)
     new_solution.variables = np.random.uniform(
         self.lower_bound, self.upper_bound, self.number_of_variables
     ).tolist(
     ) if not self._initial_solutions else self._initial_solutions.pop()
     return new_solution
Ejemplo n.º 13
0
    def test_should_the_solution_change__if_the_probability_is_one(self):
        operator = PolynomialMutation(1.0)
        solution = FloatSolution([-5, -5, -5], [5, 5, 5], 2)
        solution.variables = [1.0, 2.0, 3.0]
        FloatSolution.lower_bound = [-5, -5, -5]
        FloatSolution.upper_bound = [5, 5, 5]

        mutated_solution = operator.execute(solution)

        self.assertNotEqual([1.0, 2.0, 3.0], mutated_solution.variables)
Ejemplo n.º 14
0
 def generate_solution(self) -> FloatSolution:
     new_solution = FloatSolution(self.lower_bound, self.upper_bound,
                                  self.number_of_objectives,
                                  self.number_of_constraints)
     new_solution.variables = []
     for _ in range(self.number_of_objectives - 1):
         new_solution.variables.append(random.uniform(0, 1))
     for _ in range(self.number_of_objectives - 1,
                    self.number_of_variables):
         new_solution.variables.append(0.5)
     self.evaluate(new_solution)
     return new_solution
Ejemplo n.º 15
0
 def create_solution(self, swarm_size=1) -> FloatSolution:
     if swarm_size > 1:  #if not default swarm size, use lorenz map initalization
         return lorenz_map(swarm_size, self.number_of_variables,
                           self.number_of_objectives,
                           self.number_of_constraints, self.lower_bound,
                           self.upper_bound)
     else:
         new_solution = FloatSolution(self.number_of_variables,
                                      self.number_of_objectives,
                                      self.number_of_constraints,
                                      self.lower_bound, self.upper_bound)
         #### Chaos initialization start
         new_solution.variables = \
         [random.uniform(self.lower_bound[i]*1.0, self.upper_bound[i]*1.0) for i in range(self.number_of_variables)] #lorenz_map(self.lower_bound,self.upper_bound,self.number_of_variables)#[random.uniform(self.lower_bound[i]*1.0, self.upper_bound[i]*1.0) for i in range(self.number_of_variables)]
         return new_solution
Ejemplo n.º 16
0
    def test_should_execute_raise_and_exception_if_the_types_of_the_solutions_do_not_match_the_operators(
            self):
        operator = CompositeMutation(
            [PolynomialMutation(1.0, 5.0),
             PolynomialMutation(0.9, 25.0)])

        float_solution = FloatSolution([2.0], [3.9], 3)
        binary_solution = BinarySolution(1, 3, 0)
        float_solution.variables = [3.0]

        composite_solution = CompositeSolution(
            [float_solution, binary_solution])

        with self.assertRaises(InvalidConditionException):
            operator.execute(composite_solution)
Ejemplo n.º 17
0
 def create_initial_solutions(self) -> List[S]:
     if self.continue_flag == 1:
         population = []
         for i in range(self.population_size):
             new_solution = FloatSolution(
                 self.problem.lower_bound, self.problem.upper_bound,
                 self.problem.number_of_objectives,
                 self.problem.number_of_constraints)
             new_solution.variables = self.initial_population[i]
             population.append(new_solution)
         return population
     else:
         return [
             self.population_generator.new(self.problem)
             for _ in range(self.population_size)
         ]
Ejemplo n.º 18
0
    def test_should_copy_work_properly(self) -> None:
        solution = FloatSolution(2, 3, [0.0, 0.5], [1.0, 2.0])
        solution.variables = [1.24, 2.66]
        solution.objectives = [0.16, -2.34, 9.25]
        solution.attributes["attr"] = "value"

        new_solution = copy.copy(solution)

        self.assertEqual(solution.number_of_variables, new_solution.number_of_variables)
        self.assertEqual(solution.number_of_objectives, new_solution.number_of_objectives)
        self.assertEqual(solution.variables, new_solution.variables)
        self.assertEqual(solution.objectives, new_solution.objectives)
        self.assertEqual(solution.lower_bound, new_solution.lower_bound)
        self.assertEqual(solution.upper_bound, new_solution.upper_bound)
        self.assertIs(solution.lower_bound, solution.lower_bound)
        self.assertIs(solution.upper_bound, solution.upper_bound)
        self.assertEqual(solution.attributes, new_solution.attributes)
Ejemplo n.º 19
0
    def test_should_copy_work_properly(self) -> None:
        solution = FloatSolution(2, 3, 2, [0.0, 0.5], [1.0, 2.0])
        solution.variables = [1.24, 2.66]
        solution.objectives = [0.16, -2.34, 9.25]
        solution.attributes["attr"] = "value"

        new_solution = copy.copy(solution)

        self.assertEqual(solution.number_of_variables, new_solution.number_of_variables)
        self.assertEqual(solution.number_of_objectives, new_solution.number_of_objectives)
        self.assertEqual(solution.number_of_constraints, new_solution.number_of_constraints)
        self.assertEqual(solution.variables, new_solution.variables)
        self.assertEqual(solution.objectives, new_solution.objectives)
        self.assertEqual(solution.lower_bound, new_solution.lower_bound)
        self.assertEqual(solution.upper_bound, new_solution.upper_bound)
        self.assertIs(solution.lower_bound, solution.lower_bound)
        self.assertIs(solution.upper_bound, solution.upper_bound)
        self.assertEqual({}, new_solution.attributes)
Ejemplo n.º 20
0
    def create_solution(self) -> CompositeSolution:
        integer_solution = IntegerSolution(self.int_lower_bound,
                                           self.int_upper_bound,
                                           self.number_of_objectives,
                                           self.number_of_constraints)
        float_solution = FloatSolution(self.float_lower_bound,
                                       self.float_upper_bound,
                                       self.number_of_objectives,
                                       self.number_of_constraints)

        float_solution.variables = \
            [random.uniform(self.float_lower_bound[i] * 1.0, self.float_upper_bound[i] * .01) for i in
             range(len(self.int_lower_bound))]

        integer_solution.variables = \
            [random.uniform(self.float_lower_bound[i], self.float_upper_bound[i]) for i in
             range(len(self.float_lower_bound))]

        return CompositeSolution([integer_solution, float_solution])
Ejemplo n.º 21
0
 def generate_existing_solution(
         self,
         variables: List[float],
         is_objectives: bool = False) -> FloatSolution:
     new_solution = FloatSolution(self.lower_bound, self.upper_bound,
                                  self.number_of_objectives,
                                  self.number_of_constraints)
     if not is_objectives:
         new_solution.variables = [
             variables[index_var]
             for index_var in range(self.number_of_variables)
         ]
         self.evaluate(new_solution)
     else:
         new_solution.objectives = [
             variables[index_var]
             for index_var in range(self.number_of_objectives)
         ]
     return new_solution
Ejemplo n.º 22
0
    def create_solution(self) -> FloatSolution:

        new_solution = FloatSolution(
            lower_bound=self.lower_bound,
            upper_bound=self.upper_bound,
            number_of_objectives=self.number_of_objectives,
            number_of_constraints=self.number_of_constraints)

        new_solution.number_of_variables = self.number_of_variables
        new_solution.attributes = {
            "fitness": 0,
            "weights": np.zeros(self.number_of_objectives),
            "confort": 0,
            "temperatura": 0,
            "vt": 0
        }
        new_solution.variables = [
            np.random.uniform(self.lower_bound[i], self.upper_bound[i])
            for i in range(self.datos_externos.intervalos * 5)
        ]

        return new_solution
Ejemplo n.º 23
0
    def read_(self, absolute_path: str) -> DTLZInstance:
        with open(absolute_path) as f:
            content = f.readlines()
        # you may also want to remove whitespace characters like `\n` at the end of each line
        content = [x.strip() for x in content]
        index = 0
        self.n_obj = int(content[index].split()[0])
        index += 1
        self.n_var = int(content[index].split()[0])
        index += 1
        self.n_constraints = 0
        self.dms = int(content[index].split()[0])
        self.upper_bound = self.n_var * [0.0]
        self.lower_bound = self.n_var * [1.0]
        weight = []
        for dm in range(self.dms):
            index += 1
            line_split = clean_line(content[index])
            idx = 0
            w = []
            while idx < self.n_obj * 2:
                lower = float(line_split[idx])
                idx += 1
                upper = float(line_split[idx].replace(',', ''))
                idx += 1
                w.append(Interval(lower, upper))
            weight.append(w)
        veto = []
        for dm in range(self.dms):
            index += 1
            line_split = clean_line(content[index])
            idx = 0
            v = []
            while idx < self.n_obj * 2:
                lower = float(line_split[idx])
                idx += 1
                upper = float(line_split[idx].replace(',', ''))
                idx += 1
                v.append(Interval(lower, upper))
            veto.append(v)
        beta = []
        for i in range(self.dms):
            index += 1
            line_split = content[index].split()
            beta.append(
                Interval(float(line_split[0]),
                         float(line_split[1].replace(',', ''))))
        lambdas = []
        for i in range(self.dms):
            index += 1
            line_split = content[index].split()
            lambdas.append(
                Interval(float(line_split[0]),
                         float(line_split[1].replace(',', ''))))
        index += 1
        line = clean_line(content[index])
        if line[0] == 'TRUE':
            read_objectives = False
            if len(line) >= 2:
                read_objectives = line[1] == "TRUE"
            index += 1
            n = int(content[index].split()[0])
            best_compromise = []

            for i in range(n):
                index += 1
                line_split = content[index].split()
                if read_objectives:
                    best_compromise.append([
                        float(line_split[i].replace(',', ''))
                        for i in range(0, self.n_obj)
                    ])
                else:
                    best_compromise.append([
                        float(line_split[i].replace(',', ''))
                        for i in range(0, self.n_var)
                    ])
            self.attributes['best_compromise'] = best_compromise
        index += 1
        line = clean_line(content[index])
        if line[0] == "TRUE":
            is_interval = True
            if len(line) >= 2 and line[1] == "FALSE":
                is_interval = False
            r2_set = []
            r1_set = []
            frontiers = []
            for dm in range(self.dms):
                index += 1
                line = clean_line(content[index])
                n_size = int(int(line[0]) / 2)
                r2 = []
                r1 = []
                for n_row in range(n_size):
                    r2_ = []
                    index += 1
                    line = clean_line(content[index])
                    idx = 0
                    while idx < self.n_obj * (2 if is_interval else 1):
                        if is_interval:
                            r2_.append(Interval(line[idx], line[idx + 1]))
                            idx += 2
                        else:
                            r2_.append(Interval(line[idx], line[idx]))
                            idx += 1
                    r2.append(r2_)
                for n_row in range(n_size):
                    r1_ = []
                    index += 1
                    line = clean_line(content[index])
                    idx = 0
                    while idx < self.n_obj * (2 if is_interval else 1):
                        if is_interval:
                            r1_.append(Interval(line[idx], line[idx + 1]))
                            idx += 2
                        else:
                            r1_.append(Interval(line[idx], line[idx]))
                            idx += 1

                    r1.append(r1_)
                r2_set.append(r2)
                r1_set.append(r1)
                frontiers.append(r2 + r1)
            self.attributes['frontiers'] = frontiers
            self.attributes['r2'] = r2_set
            self.attributes['r1'] = r1_set

        index += 1
        if content[index].split()[0] == 'TRUE':
            index += 1
            n = int(content[index].split()[0])
            self.initial_solutions = []

            for i in range(n):
                index += 1
                line_split = content[index].split()
                solution = FloatSolution(lower_bound=self.lower_bound,
                                         upper_bound=self.upper_bound,
                                         number_of_objectives=self.n_obj)
                solution.variables = [
                    float(line_split[i].replace(',', ''))
                    for i in range(0, self.n_var)
                ]
                self.initial_solutions.append(solution)
        self.attributes['dms'] = self.dms
        models = []
        for dm in range(self.dms):
            model = OutrankingModel(weight[dm], veto[dm], 1.0, beta[dm],
                                    lambdas[dm], 1)
            models.append(model)
        self.attributes['models'] = models
        return self