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())
class StandardEnergyExchangeTestCase(unittest.TestCase): 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_transfer_energy(self): self.solution_a.transfer_energy_to(self.solution_b, 10) self.assertEqual(self.solution_b.energy, 50) self.assertEqual(self.solution_a.energy, 10) def test_transfer_energy_bad_amount(self): self.assertRaises(AssertionError, self.solution_a.transfer_energy_to, self.solution_b, 50) def test_standard_transfer_energy(self): exchange_operator = FractionEnergyExchange(exchange_fraction=0.5) exchange_operator.execute([self.solution_a, self.solution_b]) self.assertEqual(self.solution_a.energy, 10) self.assertEqual(self.solution_b.energy, 50)
def evaluate(self, solution: BinarySolution): instances = solution.variables[0] attributes = solution.variables[1] X = self.Xtrain[instances, :] X = X[:, attributes] Y = self.Ytrain[instances] noInst = np.shape(X)[0] noAttr = np.shape(X)[1] if (noAttr <= 1): return solution model = SVC(gamma=self.gamma, C=self.C, degree=self.degree, kernel=self.kernel) model.fit(X=X, y=Y) solution.objectives[0] = (1 - model.score(X, Y)) solution.objectives[1] = len(model.support_) solution.objectives[2] = noInst solution.objectives[3] = noAttr return solution
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_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 evaluate(self, solution: BinarySolution): instances = solution.variables[0] attributes = solution.variables[1] # Generate masks # Crop by characteristics and instances X = self.Xtrain[instances, :] X = X[:, attributes] Y = self.Ytrain[instances] noInst = np.shape(X)[0] noAttr = np.shape(X)[1] #The number of classes has to be greater than one; got 1 if (noAttr <= 1): return solution model = SVC(gamma=self.gamma, C=self.C, degree=self.degree, kernel=self.kernel) model.fit(X=X, y=Y) # y_hat = model.predict(self.Xtrain) error = 1 - model.score(X, Y) noSV = len(model.support_) solution.objectives[0] = error solution.objectives[1] = noSV solution.objectives[2] = noInst solution.objectives[3] = noAttr return solution
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 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 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_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 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 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_the_solution_remain_unchanged(self): operator = NullCrossover() 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_solution_remain_unchanged_if_the_probability_is_zero(self): operator = SP(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 = SP(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 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) -> 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 evaluate(self, solution: BinarySolution) -> BinarySolution: # g = self.eval_g(solution) # h = self.eval_h(solution.variables[0], g) obj = 1000000 for index, var in enumerate(solution.variables[0]): obj += var solution.objectives[0] = solution.variables[0] solution.objectives[1] = self.number_of_objectives return 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 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 evaluate(self, solution: BinarySolution) -> BinarySolution: counter_of_ones = 0 counter_of_zeroes = 0 for bits in solution.variables[0]: if bits: counter_of_ones += 1 else: counter_of_zeroes += 1 solution.objectives[0] = -1.0 * counter_of_ones solution.objectives[1] = -1.0 * counter_of_zeroes return 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 evaluate(self, solution: BinarySolution) -> BinarySolution: budget = 0 objectives = self.number_of_objectives * [0.0] for index, bits in enumerate(solution.variables[0]): if bits: budget += self.instance_.projects[index][0] for obj in range(0, self.number_of_objectives): objectives[obj] += self.instance_.projects[index][obj + 3] solution.objectives = [-obj for obj in objectives] solution.constraints = [self.budget - budget] return solution
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 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=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 evaluate(self, solution: BinarySolution) -> None: counter_of_ones = 0 for bits in solution.variables[0]: if bits: counter_of_ones += 1 solution.objectives[0] = -1.0 * counter_of_ones
def evaluate(self, solution: BinarySolution) -> BinarySolution: global playout_count global dummy_count run_env = copy.deepcopy(env) val_reward = 0 individual = solution.variables[0] for act in individual: _, val_reward, done = run_env.step(act) if done: break behv = (run_env.posx, run_env.posy) #if(behv[0] == 0 and behv[1] == 0): # r = -1 #else: r = n_s.get_approx_novelty(behv, k=5, done=True) # r=n_s.get_novelty_simple(behv,done=True) r = r * 10 n_s.set_behavior_in_archive(behv, n_s.behavior_archive, True) playout_count += 1 debug(r, playout_count, run_env) #solution.objectives[0] = val_reward*-1 solution.objectives[0] = abs(100 - r) return solution
def evaluate(self, solution: BinarySolution) -> BinarySolution: current_budget = Interval(0) objectives = self.number_of_objectives * [Interval(0)] for index, bits in enumerate(solution.variables[0]): if bits: current_budget += self.instance_.projects[index][0] for obj in range(0, self.number_of_objectives): objectives[obj] += self.instance_.projects[index][obj + 1] poss = self.budget.poss_greater_than_or_eq(current_budget) if poss < self.get_preference_model(0).chi: solution.constraints = [self.budget - current_budget] else: solution.constraints = [0] solution.budget = current_budget solution.objectives = objectives return solution
def execute(self, solution: BinarySolution) -> BinarySolution: for i in range(solution.number_of_variables): for j in range(len(solution.variables[i])): rand = random.random() if rand <= self.probability: solution.variables[i][j] = True if solution.variables[i][j] == False else False return solution
def execute(self, solution: BinarySolution) -> BinarySolution: for i in range(solution.number_of_variables): for j in range(len(solution.variables[i])): rand = random.random() if rand <= self.probability: solution.variables[i][j] = True if solution.variables[i][j] is False else False return solution
def evaluate(self, solution: BinarySolution) -> BinarySolution: total_objective1 = 0.0 total_objective2 = 0 total_weigths = 0.0 for index, bits in enumerate(solution.variables[0]): if bits: total_objective1 += self.objective1[index] total_objective2 += self.objective2[index] total_weigths += self.weights[index] if total_weigths > self.capacity: total_objective1 = 0.0 total_objective2 = 0.0 solution.objectives[0] = -1.0 * total_objective1 solution.objectives[1] = -1.0 * total_objective2 return solution
def evaluate(self, solution: BinarySolution) -> BinarySolution: counter_of_ones = 0 for bits in solution.variables[0]: if bits: counter_of_ones += 1 solution.objectives[0] = -1.0 * counter_of_ones return solution
def evaluate(self, solution: BinarySolution) -> BinarySolution: total_sum = 0.0 number_of_objects = 0 for index, bits in enumerate(solution.variables[0]): if bits: total_sum += self.W[index] number_of_objects += 1 if total_sum > self.C: total_sum = self.C - total_sum * 0.1 if total_sum < 0.0: total_sum = 0.0 solution.objectives[0] = -1.0 * total_sum solution.objectives[1] = number_of_objects return solution
def test_should_the_operator_work_with_a_solution_with_three_binary_variables(self, random_call): operator = SP(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_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 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=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