示例#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))
示例#2
0
文件: test_ga.py 项目: S-Ercan/GA
    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:])
        )
示例#3
0
文件: ga.py 项目: 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
示例#4
0
文件: test_ga.py 项目: S-Ercan/GA
    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))
示例#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))
示例#6
0
文件: test_ga.py 项目: S-Ercan/GA
    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))
示例#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()))
示例#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))
示例#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)))
示例#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))
示例#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))