def test_set_value_for_literal_sets_value(self): mapping = {self.variable: False} v = Valuation(mapping) l = Literal(self.variable) v.set_value_for_literal(l, True) self.assertEqual(True, v.get_value_for_literal(l))
def test_create_offspring_creates_second_child_correctly_after_crossover_point(self): variables = self.rand_maxsat.variables crossover_index = int(len(variables) / 2) valuation1 = Valuation.init_random_from_variables(variables) valuation2 = Valuation.init_random_from_variables(variables) offspring = self.rand_ga.create_offspring(valuation1, valuation2, crossover_index=crossover_index) child2 = offspring[1] self.assertTrue( all(child2.get_value_for_variable(v) == valuation1.get_value_for_variable(v) for v in variables[crossover_index + 1:]) )
def generate_population(self): """ Generates a population of random candidate solutions. :return: list of 'self.population_size' amount of Valuation instances """ population = [] for i in range(self.population_size): population.append(Valuation.init_random_from_variables(self.maxsat.variables)) return population
def test_get_candidate_fitness_returns_half(self): v1 = Variable('a') c1 = Clause([Literal(v1, positive=True)]) c2 = Clause([Literal(v1, positive=False)]) maxsat = MAXSAT([v1], [c1, c2]) valuation = Valuation.init_random_from_variables([v1]) ga = GA(maxsat) self.assertEqual(0.5, ga.get_candidate_fitness(valuation))
def test_change_random_variable_changes_exactly_one_variable(self): v = Valuation.init_random_from_variables(self.variables) original_valuation = v.valuation.copy() v.change_value_for_random_variable() new_valuation = v.valuation changed_variables = [ variable for variable in self.variables if not original_valuation.get(variable) == new_valuation.get(variable) ] self.assertEqual(1, len(changed_variables))
def test_create_offspring_creates_two_children(self): valuation1 = Valuation.init_random_from_variables(self.rand_maxsat.variables) valuation2 = Valuation.init_random_from_variables(self.rand_maxsat.variables) offspring = self.rand_ga.create_offspring(valuation1, valuation2) self.assertEqual(2, len(offspring))
def test_init_random_creates_valuation_with_bools(self): v = Valuation.init_random_from_variables(self.variables) self.assertTrue(all(isinstance(k, bool) for k in v.valuation.values()))
def test_init_random_creates_valuation_of_correct_length(self): v = Valuation.init_random_from_variables(self.variables) self.assertEqual(len(self.variables), len(v.valuation))
def test_get_value_for_negated_literal_returns_correct_value(self): value = False mapping = {self.variable: value} v = Valuation(mapping) self.assertEqual(not value, v.get_value_for_literal(Literal(self.variable, positive=False)))
def test_set_value_for_variable_sets_value(self): mapping = {self.variable: False} v = Valuation(mapping) v.set_value_for_variable(self.variable, True) self.assertEqual(True, v.get_value_for_variable(self.variable))
def test_get_value_for_variable_returns_correct_value(self): value = False mapping = {self.variable: value} v = Valuation(mapping) self.assertEqual(value, v.get_value_for_variable(self.variable))