예제 #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)
예제 #2
0
    def test_should_dominance_comparator_return_zero_if_the_two_solutions_have_one_objective_with_the_same_value(self):
        solution = FloatSolution(3, 1, 0, [], [])
        solution2 = FloatSolution(3, 1, 0, [], [])

        solution.objectives = [1.0]
        solution2.objectives = [1.0]

        self.assertEqual(0, self.comparator.compare(solution, solution2))
예제 #3
0
    def test_should_dominance_comparator_return_minus_one_if_the_two_solutions_have_one_objective_and_the_first_one_is_lower(self):
        solution = FloatSolution(3, 1, 0, [], [])
        solution2 = FloatSolution(3, 1, 0, [], [])

        solution.objectives = [1.0]
        solution2.objectives = [2.0]

        self.assertEqual(-1, self.comparator.compare(solution, solution2))
예제 #4
0
    def test_should_dominance_comparator_work_properly_case_3(self):
        """ Case d: solution1 has objectives [-1.0, 5.0, 9.0] and solution2 has [-2.0, 5.0, 10.0]
        """
        solution = FloatSolution(3, 3, 0, [], [])
        solution2 = FloatSolution(3, 3, 0, [], [])

        solution.objectives = [-1.0, 5.0, 9.0]
        solution2.objectives = [-2.0, 5.0, 10.0]

        self.assertEqual(0, self.comparator.compare(solution, solution2))
예제 #5
0
    def test_should_dominance_comparator_work_properly_case_a(self):
        '''
        Case A: solution1 has objectives [-1.0, 5.0, 9.0] and solution2 has [2.0, 6.0, 15.0]
        '''
        solution = FloatSolution(3, 3, 0, [], [])
        solution2 = FloatSolution(3, 3, 0, [], [])

        solution.objectives = [-1.0, 5.0, 9.0]
        solution2.objectives = [2.0, 6.0, 15.0]

        self.assertEqual(-1, self.comparator.compare(solution, solution2))
예제 #6
0
    def test_should_dominance_comparator_work_properly_with_constrains_case_2(self):
        """ Case 2: solution1 has a lower degree of constraint violation than solution 2
        """
        solution1 = FloatSolution(3, 3, 0, [], [])
        solution2 = FloatSolution(3, 3, 0, [], [])
        solution1.attributes["overall_constraint_violation"] = -0.3
        solution2.attributes["overall_constraint_violation"] = -0.1

        solution1.objectives = [-1.0, 5.0, 9.0]
        solution2.objectives = [-2.0, 5.0, 10.0]

        self.assertEqual(1, self.comparator.compare(solution1, solution2))
예제 #7
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])
예제 #8
0
    def test_should_raise_an_exception_when_format_is_not_supported(self):
        solution1, solution2 = FloatSolution(1, 2, 0, [], []), FloatSolution(
            1, 2, 0, [], [])
        solution1.objectives[0], solution2.objectives[0] = 5.0, 1.0  # x values
        solution1.objectives[1], solution2.objectives[1] = 3.0, 6.0  # y values
        solution_list = [solution1, solution2]

        plot = ScatterPlot(plot_title="title", animation_speed=1 * 10e-10)

        with self.assertRaises(Exception):
            plot.simple_plot(solution_list=solution_list,
                             file_name="file",
                             fmt="null",
                             save=True)
예제 #9
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)
예제 #10
0
    def test_should_constructor_raise_an_exception_if_the_number_of_objectives_is_not_coherent(
            self):
        float_solution: FloatSolution = FloatSolution([1.0], [3.0], 3)
        integer_solution: IntegerSolution = IntegerSolution([2], [4], 2)

        with self.assertRaises(InvalidConditionException):
            CompositeSolution([float_solution, integer_solution])
예제 #11
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])
예제 #12
0
    def test_should_copy_work_properly(self):
        number_of_objectives = 3
        number_of_constraints = 1
        float_solution: FloatSolution = FloatSolution([1.0], [3.0],
                                                      number_of_objectives,
                                                      number_of_constraints)
        integer_solution: IntegerSolution = IntegerSolution(
            [2], [4], number_of_objectives, number_of_constraints)

        solution: CompositeSolution = CompositeSolution(
            [float_solution, integer_solution])
        new_solution: CompositeSolution = copy.deepcopy(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[0].number_of_variables,
                         new_solution.variables[0].number_of_variables)
        self.assertEqual(solution.variables[1].number_of_variables,
                         new_solution.variables[1].number_of_variables)
        self.assertEqual(solution.variables[0], new_solution.variables[0])
        self.assertEqual(solution.variables[1], new_solution.variables[1])

        self.assertEqual(solution.variables[0].variables,
                         new_solution.variables[0].variables)
        self.assertEqual(solution.variables[1].variables,
                         new_solution.variables[1].variables)
예제 #13
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)
예제 #14
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
예제 #15
0
    def test_should_constructor_create_a_valid_soltion_composed_of_a_float_and_an_integer_solutions(
            self):
        number_of_objectives = 3
        number_of_constraints = 1
        float_solution: FloatSolution = FloatSolution([1.0], [3.0],
                                                      number_of_objectives,
                                                      number_of_constraints)
        integer_solution: IntegerSolution = IntegerSolution(
            [2], [4], number_of_objectives, number_of_constraints)

        solution: CompositeSolution = CompositeSolution(
            [float_solution, integer_solution])

        self.assertIsNotNone(solution)
        self.assertEqual(2, solution.number_of_variables)
        self.assertEqual(number_of_objectives, solution.number_of_objectives)
        self.assertEqual(number_of_constraints, solution.number_of_constraints)
        self.assertEqual(number_of_objectives,
                         solution.variables[0].number_of_objectives)
        self.assertEqual(number_of_objectives,
                         solution.variables[1].number_of_objectives)
        self.assertEqual(number_of_constraints,
                         solution.variables[0].number_of_constraints)
        self.assertEqual(number_of_constraints,
                         solution.variables[1].number_of_constraints)
        self.assertTrue(type(solution.variables[0] is FloatSolution))
        self.assertTrue(type(solution.variables[1] is IntegerSolution))
예제 #16
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
예제 #17
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)
예제 #18
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
예제 #19
0
 def test_should_default_constructor_create_a_valid_solution(self) -> None:
     solution = FloatSolution(2, 3, [0.0, 0.5], [1.0, 2.0])
     self.assertEqual(2, solution.number_of_variables)
     self.assertEqual(3, solution.number_of_objectives)
     self.assertEqual(2, len(solution.variables))
     self.assertEqual(3, len(solution.objectives))
     self.assertEqual([0.0, 0.5], solution.lower_bound)
     self.assertEqual([1.0, 2.0], solution.upper_bound)
예제 #20
0
    def test_should_print_solution_points_correctly(self):
        solution1, solution2 = FloatSolution(1, 2, 0, [], []), FloatSolution(
            1, 2, 0, [], [])
        solution1.objectives[0], solution2.objectives[0] = 5.0, 1.0  # x values
        solution1.objectives[1], solution2.objectives[1] = 3.0, 6.0  # y values
        solution_list = [solution1, solution2]

        plot = ScatterPlot(plot_title="title", animation_speed=1 * 10e-10)
        plot.simple_plot(solution_list=solution_list,
                         file_name="test",
                         save=False)

        x_values, y_values = plot.sc.get_xdata(), plot.sc.get_ydata()

        self.assertEqual(solution1.objectives[0], x_values[0])
        self.assertEqual(solution2.objectives[0], x_values[1])
        self.assertEqual(solution1.objectives[1], y_values[0])
        self.assertEqual(solution2.objectives[1], y_values[1])
예제 #21
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])
예제 #22
0
    def test_should_execute_with_an_invalid_solution_list_size_raise_an_exception(self):
        crossover: SBXCrossover = SBXCrossover(0.1, 20.0)

        solution = FloatSolution([1, 2], [2, 4], 2, 2)
        with self.assertRaises(Exception):
            crossover.execute([solution])

        with self.assertRaises(Exception):
            crossover.execute([solution, solution, solution])
예제 #23
0
    def create_solution(self) -> FloatSolution:
        new_solution = FloatSolution(
            self.lower_bound,
            self.lower_bound,
            number_of_objectives=self.number_of_objectives)
        new_solution.variables[0] = random.uniform(self.lower_bound[0],
                                                   self.upper_bound[0])

        return new_solution
예제 #24
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)
예제 #25
0
 def setUp(self):
     self.solution_a = BinarySolution(number_of_objectives=1,
                                      number_of_variables=1,
                                      initial_energy=20)
     self.solution_a.objectives = [-50.]  # solution_a is worse
     self.solution_b = FloatSolution(lower_bound=[0.1],
                                     upper_bound=[0.5],
                                     number_of_objectives=1,
                                     initial_energy=40)
     self.solution_b.objectives = [-100.]  # solution_b is better
 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
예제 #27
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
 def setUp(self):
     self.solution_a = BinarySolution(number_of_objectives=1,
                                      number_of_variables=1,
                                      initial_energy=20)
     self.solution_b = FloatSolution(lower_bound=[0.1],
                                     upper_bound=[0.5],
                                     number_of_objectives=1,
                                     initial_energy=40)
     self.solution_c = BinarySolution(number_of_objectives=1,
                                      number_of_variables=1,
                                      initial_energy=30)
     self.solutions = [self.solution_a, self.solution_b, self.solution_c]
 def setUp(self) -> None:
     self.solution_a = BinarySolution(number_of_objectives=1,
                                      number_of_variables=1,
                                      initial_energy=20)
     self.solution_b = FloatSolution(lower_bound=[0.1],
                                     upper_bound=[0.5],
                                     number_of_objectives=1,
                                     initial_energy=40)
     self.solution_c = BinarySolution(number_of_objectives=1,
                                      number_of_variables=2,
                                      initial_energy=30)
     self.solution_d = BinarySolution(number_of_objectives=1,
                                      number_of_variables=2,
                                      initial_energy=30)
     self.neighbours_operator = RandomNeighbours(seed=1)
예제 #30
0
def read_front_from_file_as_solutions(file_path: str):
    """ Reads a front from a file and returns a list of solution objects.

    :return: List of solution objects.
    """
    front = []
    with open(file_path) as file:
        for line in file:
            vector = [float(x) for x in line.split()]
            solution = FloatSolution(2, 2, 0, [], [])
            solution.objectives = vector

            front.append(solution)

    return front