def test_should_the_operator_work_with_a_solution_with_three_binary_variables( self, random_call): operator = SPXCrossover(1.0) solution1 = BinarySolution(number_of_variables=3, number_of_objectives=1) solution1.variables[0] = [True, False, False, True, True, False] solution1.variables[1] = [True, False, False, True, False, False] solution1.variables[2] = [True, False, True, True, True, True] solution2 = BinarySolution(number_of_variables=3, number_of_objectives=1) solution2.variables[0] = [False, True, False, False, True, True] solution2.variables[1] = [True, True, False, False, True, False] solution2.variables[2] = [True, True, True, False, False, True] random_call.return_value = 8 offspring = operator.execute([solution1, solution2]) self.assertEqual([True, False, False, True, True, False], offspring[0].variables[0]) self.assertEqual([True, False, False, False, True, False], offspring[0].variables[1]) self.assertEqual([True, True, True, False, False, True], offspring[0].variables[2]) self.assertEqual([False, True, False, False, True, True], offspring[1].variables[0]) self.assertEqual([True, True, False, True, False, False], offspring[1].variables[1]) self.assertEqual([True, False, True, True, True, True], offspring[1].variables[2])
def test_should_the_solution_remain_unchanged_if_the_probability_is_zero(self): operator = SPXCrossover(0.0) solution1 = BinarySolution(number_of_variables=1, number_of_objectives=1) solution1.variables[0] = [True, False, False, True, True, False] solution2 = BinarySolution(number_of_variables=1, number_of_objectives=1) solution2.variables[0] = [False, True, False, False, True, False] offspring = operator.execute([solution1, solution2]) self.assertEqual([True, False, False, True, True, False], offspring[0].variables[0]) self.assertEqual([False, True, False, False, True, False], offspring[1].variables[0])
def test_should_the_operator_work_if_the_third_bit_is_selected(self, random_call): operator = SPXCrossover(1.0) solution1 = BinarySolution(number_of_variables=1, number_of_objectives=1) solution1.variables[0] = [True, False, False, True, True, False] solution2 = BinarySolution(number_of_variables=1, number_of_objectives=1) solution2.variables[0] = [False, True, False, False, True, True] random_call.return_value = 3 offspring = operator.execute([solution1, solution2]) self.assertEqual([True, False, False, False, True, True], offspring[0].variables[0]) self.assertEqual([False, True, False, True, True, False], offspring[1].variables[0])
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 test_should_the_solution_remain_unchanged(self): operator = Null() solution1 = BinarySolution(number_of_variables=1, number_of_objectives=1) solution1.variables[0] = [True, False, False, True, True, False] solution2 = BinarySolution(number_of_variables=1, number_of_objectives=1) solution2.variables[0] = [False, True, False, False, True, False] offspring = operator.execute([solution1, solution2]) self.assertEqual([True, False, False, True, True, False], offspring[0].variables[0]) self.assertEqual([False, True, False, False, True, False], offspring[1].variables[0])
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)
def create_solution(self) -> BinarySolution: new_solution = BinarySolution(number_of_variables=1, number_of_objectives=1) new_solution.variables[0] = \ [True if random.randint(0, 1) == 0 else False for _ in range(self.number_of_bits)] return new_solution
def test_should_the_solution_remain_unchanged_if_the_probability_is_zero(self): operator = BitFlip(0.0) solution = BinarySolution(number_of_variables=1, number_of_objectives=1) solution.variables[0] = [True, True, False, False, True, False] mutated_solution = operator.execute(solution) self.assertEqual([True, True, False, False, True, False], mutated_solution.variables[0])
def test_should_get_total_number_of_bits_return_the_right_value( self) -> None: solution = BinarySolution(number_of_variables=2, number_of_objectives=3) solution.variables[0] = [True, False] solution.variables[1] = [False, True, False] self.assertEqual(5, solution.get_total_number_of_bits())
def create_solution(self) -> BinarySolution: new_solution = BinarySolution(number_of_variables=3, number_of_objectives=1) new_solution.variables[0] = [ random.randint(0, self.number_of_variables) for _ in range(self.number_of_bits) ] return new_solution
def generate_existing_solution(self, variables: str) -> BinarySolution: new_solution = BinarySolution( number_of_variables=self.number_of_variables, number_of_objectives=self.number_of_objectives) new_solution.variables[0] = \ [True if variables[_] == '1' else False for _ in range( self.number_of_bits)] self.evaluate(new_solution) return new_solution
def test_should_the_solution_change_all_the_bits_if_the_probability_is_one(self): operator = BitFlip(1.0) solution = BinarySolution(number_of_variables=2, number_of_objectives=1) solution.variables[0] = [True, True, False, False, True, False] solution.variables[1] = [False, True, True, False, False, True] mutated_solution = operator.execute(solution) self.assertEqual([False, False, True, True, False, True], mutated_solution.variables[0]) self.assertEqual([True, False, False, True, True, False], mutated_solution.variables[1])
def create_from_string(self, variables: str) -> BinarySolution: new_solution = BinarySolution( number_of_variables=self.number_of_variables, number_of_objectives=self.number_of_objectives) new_solution.variables[0] = \ [True if variables[_] == '1' else False for _ in range( self.number_of_bits)] return new_solution
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 test_should_constructor_create_a_valid_solution(self) -> None: solution = BinarySolution(number_of_variables=2, number_of_objectives=3) solution.variables[0] = [True, False] solution.variables[1] = [False] 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([True, False], solution.variables[0]) self.assertEqual([False], solution.variables[1])
def create_solution(self) -> BinarySolution: solution = BinarySolution( self.number_of_variables, self.number_of_objectives ) notes = [np.random.choice(self.note_candidates[i]) for i in range(self.number_of_notes)] solution.variables[0] = notes solution.variables[1] = self.note_candidates return solution
def create_solution(self): sol = BinarySolution(number_of_variables=2, number_of_objectives=4) sol.variables[0] = [ True if random.randint(0, 1) == 0 else False for _ in range(self.instances) ] sol.variables[1] = [ True if random.randint(0, 1) == 0 else False for _ in range(self.attributes) ] return sol
def create_solution(self): new_solution = BinarySolution( number_of_variables=self.number_of_variables, number_of_objectives=self.number_of_objectives) new_solution.variables[0] = [ True if random.randint(0, 1) == 0 else False for _ in range(self.instances) ] new_solution.variables[1] = [ True if random.randint(0, 1) == 0 else False for _ in range(self.attributes) ] return new_solution
def __HYPE(self) -> None: reference_point = BinarySolution(self.__problem.number_of_variables, self.__problem.number_of_objectives, self.__problem.number_of_constraints) reference_point.objectives = [1.0 for _ in range(self.__problem.number_of_objectives)] self.__solver = HYPE( problem=self.__problem, reference_point=reference_point, population_size=POPULATION_SIZE, offspring_population_size=OFFSPRING_SIZE, #mutation=BitFlipMutation(probability=1.0/self.__problem.number_of_variables), mutation=BitFlipMutation(probability=0.035), crossover=SPXCrossover(probability=1.0), termination_criterion=StoppingByEvaluations(max_evaluations=MAX_EVALUATION) )
def create_solution(self) -> BinarySolution: random.seed(123) new_solution = BinarySolution( number_of_variables=self.number_of_variables, number_of_objectives=self.number_of_objectives, ) new_solution.variables[0] = [ True if random.randint(0, 1) == 0 else False for _ in range(self.number_of_tests) ] return new_solution
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)
def create_solution(self) -> BinarySolution: new_solution = BinarySolution( number_of_variables=self.number_of_variables, number_of_objectives=self.number_of_objectives) new_solution.variables[0] = [] budget = Interval(0) random.shuffle(self.positions) new_solution.variables[0] = self.number_of_bits * [False] for v in self.positions: tmp = budget + self.instance_.projects[v][0] poss = self.budget.poss_greater_than_or_eq(tmp) if poss >= self.get_preference_model(0).chi: new_solution.variables[0][v] = True budget = tmp return new_solution
def test_should_get_total_number_of_bits_return_zero_if_the_object_variables_are_not_initialized(self) -> None: solution = BinarySolution(number_of_variables=2, number_of_objectives=3) self.assertEqual(0, solution.get_total_number_of_bits())
def create_solution(self) -> BinarySolution: new_solution = BinarySolution(self.number_of_variables, self.number_of_objectives, self.number_of_constraints) new_solution.variables = \ [self.sf.get_random_individual() for i in range(self.number_of_variables)] return new_solution
def test_should_default_constructor_create_a_valid_solution(self) -> None: solution = BinarySolution(2, 3) self.assertEqual(2, solution.number_of_variables) self.assertEqual(3, solution.number_of_objectives)