Ejemplo n.º 1
0
class TestCSProblem(TestCase):
    def setUp(self):
        self.simple_csp = CSProblem()
        self.simple_csp.domains['x'] = set([1, 2, 3, 4, 5])
        self.simple_csp.domains['y'] = set([1, 2, 3, 4, 5])
        self.simple_csp.constraints.append(Constraint(['x', 'y'], 'x > 2*y', ['x', 'y']))

    def test_revise_x_domain(self):
        self.simple_csp.revise(('x', self.simple_csp.constraints[0]))
        self.assertEqual(self.simple_csp.domains['x'], set([3, 4, 5]))

    def test_finding_variable_with_smallest_domain(self):
        self.simple_csp.revise(('x', self.simple_csp.constraints[0]))
        self.assertEqual('x', self.simple_csp._find_variable_with_smallest_domain())

    def test_it_makes_the_right_number_of_successors(self):
        self.simple_csp.revise(('x', self.simple_csp.constraints[0]))
        children = self.simple_csp.get_successors()
        self.assertEqual(3, len(children))
Ejemplo n.º 2
0
    'b': [4, 5, 6, 7, 8, 9],
    'c': [1, 2, 3, 4, 5]
}

constraints = {
    ('a', 'b'): lambda a, b: a * 2 == b,
    ('b', 'a'): lambda b, a: b == 2 * a,
    ('a', 'c'): lambda a, c: a == c,
    ('c', 'a'): lambda c, a: c == a,
    ('b', 'c'): lambda b, c: b >= c - 2,
    ('b', 'c'): lambda b, c: b <= c + 2,
    ('c', 'b'): lambda c, b: b >= c - 2,
    ('c', 'b'): lambda c, b: b <= c + 2
}

problem = CSProblem(arcs, domains, constraints)
r = problem.arc_consistency()

print("Edge    | New Domain     | Edges to Reconsider")
print("--------|----------------|--------------------")

for step in r:

    if step == None:
        print('Inconsistent !')

    else:
        edge = step[0]

        if edge == None:
            print('Result : ', step[1])
Ejemplo n.º 3
0
 def setUp(self):
     self.simple_csp = CSProblem()
     self.simple_csp.domains['x'] = set([1, 2, 3, 4, 5])
     self.simple_csp.domains['y'] = set([1, 2, 3, 4, 5])
     self.simple_csp.constraints.append(Constraint(['x', 'y'], 'x > 2*y', ['x', 'y']))