Esempio n. 1
0
    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))
Esempio n. 2
0
    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:])
        )
Esempio n. 3
0
File: ga.py Progetto: S-Ercan/GA
 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
Esempio n. 4
0
    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))
Esempio n. 5
0
    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))
Esempio n. 6
0
    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))
Esempio n. 7
0
 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()))
Esempio n. 8
0
 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))
Esempio n. 9
0
    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)))
Esempio n. 10
0
    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))
Esempio n. 11
0
    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))