def test_conver_to_binary_constraints_variables(self):
     v, d, c = convert_to_binary(self.variables, self.domains, self.constraints)
     var_tuples = map(fst, c)
     self.assertIn(('hidden0', 'A'), var_tuples)
     self.assertIn(('hidden0', 'B'), var_tuples)
     self.assertIn(('hidden0', 'C'), var_tuples)
     self.assertIn(('hidden1', 'A'), var_tuples)
Пример #2
0
 def test_conver_to_binary_constraints_variables(self):
     v, d, c = convert_to_binary(self.variables, self.domains,
                                 self.constraints)
     var_tuples = map(fst, c)
     self.assertIn(('hidden0', 'A'), var_tuples)
     self.assertIn(('hidden0', 'B'), var_tuples)
     self.assertIn(('hidden0', 'C'), var_tuples)
     self.assertIn(('hidden1', 'A'), var_tuples)
    def test_hidden_variable__domains_is_constraint_by_the_constraint_on_the_variable_it_hides(self):
        v, d, c = convert_to_binary(self.variables, self.domains, self.constraints)
        # hidden0 hides A, B, C
        domain = sorted(d['hidden0'])
        self.assertEqual(domain, [(1, 3, 2), (1, 4, 2), (2, 3, 1), (2, 4, 1), (3, 1, 2), (3, 4, 1), (3, 4, 2)])

        domain = sorted(d['hidden1'])
        self.assertEqual(domain, [(2,)])
Пример #4
0
    def test_hidden_variable__domains_is_constraint_by_the_constraint_on_the_variable_it_hides(
            self):
        v, d, c = convert_to_binary(self.variables, self.domains,
                                    self.constraints)
        # hidden0 hides A, B, C
        domain = sorted(d['hidden0'])
        self.assertEqual(domain, [(1, 3, 2), (1, 4, 2), (2, 3, 1), (2, 4, 1),
                                  (3, 1, 2), (3, 4, 1), (3, 4, 2)])

        domain = sorted(d['hidden1'])
        self.assertEqual(domain, [(2, )])
Пример #5
0
    def solve(self):

        print 'Please wait while AI is solving the puzzle'
        print
        print

        self.cellVariablesTuple, self.cellDomains, self.cellConstraints = convert_to_binary(self.cellVariablesTuple, self.cellDomains,
                                                                                self.cellConstraints) #convert fields to binary

        problem = CspProblem(self.cellVariablesTuple,self.cellDomains,self.cellConstraints) # initialize the csp problem
        self.solution = backtrack(problem) #solve the csp problem.
        print 'CSP Problem Is Solved'
        print
        self.printSolution() #print the solution

        return
        yallah = tuple(yallah)
        constraints.append((yallah, side_const_r))

for i in range(ri):
    for j in range(ci):
        yallah = []
        yallah.append('x' + str(i) + str(j))

        for a in range(len(cols[j])):
            yallah.append('c' + str(j) + str(a))

        yallah = tuple(yallah)
        constraints.append((yallah, side_const_c))

#binary problem convertion
variables, domains, constraints = convert_to_binary(variables, domains,
                                                    constraints)

#csp problem
my_problem = CspProblem(variables, domains, constraints)

#problem solved by backtrack
result = backtrack(my_problem,
                   variable_heuristic=MOST_CONSTRAINED_VARIABLE,
                   value_heuristic=LEAST_CONSTRAINING_VALUE)

#result output
for a in range(ri):
    for b in range(ci):
        if result['x' + str(a) + str(b)] == 1:
            print('X', end=' ')
        else:
Пример #7
0
def const_different(variables, values):
    return len(values) == len(set(values))  # remove repeated values and count

constraints = [
    (('F', 'T', 'U', 'W', 'R', 'O'), const_different),
    (('O', 'R', 'C_10'), lambda vars_, values: values[0] + values[0] == values[1] + 10 * values[2]),
    (('C_10', 'W', 'U', 'C_100'), lambda vars_, values: values[0] + values[1] + values[1] == values[2] + 10 * values[3]),
    (('C_100', 'T', 'O', 'C_1000'), lambda vars_, values: values[0] + values[1] + values[1] == values[2] + 10 * values[3]),
    (('C_1000', 'F'), lambda vars_, values: values[0] == values[1])
]

original_constraints = deepcopy(constraints)
original_domains = deepcopy(domains)

start = time()
problem = CspProblem(variables, original_domains, original_constraints)
result = backtrack(problem, variable_heuristic=MOST_CONSTRAINED_VARIABLE, value_heuristic=LEAST_CONSTRAINING_VALUE)
elapsed = time() - start
print result
print "Took %d seconds to finish using n-ary constraints" % elapsed


start = time()
variables, domains, constraints = convert_to_binary(variables, domains, constraints)
problem = CspProblem(variables, domains, constraints)
result = backtrack(problem, value_heuristic=LEAST_CONSTRAINING_VALUE)
elapsed = time() - start
print result
print "Took %d seconds to finish using binary constraints" % elapsed
Пример #8
0
# -- Hand made binary constraints --
constraints = mkconstraints()
start = time()
domains0 = deepcopy(domains)
my_problem = CspProblem(variables, domains0, constraints)
sol = backtrack(my_problem)
elapsed = time() - start
display_solution(sol)
print("Took %d seconds to finish using binary constraints" %
      elapsed)  # because of AC3 should be quick

# -- N-ary constraints made binary using hidden variables --
domains1 = deepcopy(domains)
start = time()
variables1, domains1, constraints = convert_to_binary(variables, domains1,
                                                      mknaryconstraints())
my_problem = CspProblem(variables1, domains1, constraints)
sol = backtrack(my_problem)
elapsed = time() - start
display_solution(sol)
print("Took %d seconds to finish using binary constraints (hidden variables)" %
      elapsed)

# -- N-ary constraints --
constraints = mknaryconstraints()
domains3 = deepcopy(domains)
start = time()
my_problem = CspProblem(variables, domains3, constraints)
sol = backtrack(my_problem)
elapsed = time() - start
display_solution(sol)
Пример #9
0
# -- Hand made binary constraints --
constraints = mkconstraints()
start = time()
domains0 = deepcopy(domains)
my_problem = CspProblem(variables, domains0, constraints)
sol = backtrack(my_problem)
elapsed = time() - start
display_solution(sol)
print "Took %d seconds to finish using binary constraints" % elapsed  # because of AC3 should be quick


# -- N-ary constraints made binary using hidden variables --
domains1 = deepcopy(domains)
start = time()
variables1, domains1, constraints = convert_to_binary(variables, domains1, mknaryconstraints())
my_problem = CspProblem(variables1, domains1, constraints)
sol = backtrack(my_problem)
elapsed = time() - start
display_solution(sol)
print "Took %d seconds to finish using binary constraints (hidden variables)" % elapsed


# -- N-ary constraints --
constraints = mknaryconstraints()
domains3 = deepcopy(domains)
start = time()
my_problem = CspProblem(variables, domains3, constraints)
sol = backtrack(my_problem)
elapsed = time() - start
display_solution(sol)
Пример #10
0
 def test_conver_to_binary_adds_variables(self):
     v, d, c = convert_to_binary(self.variables, self.domains,
                                 self.constraints)
     self.assertNotEqual(v, self.variables)
     self.assertIn('hidden0', v)
     self.assertIn('hidden1', v)
 def test_conver_to_binary_adds_variables(self):
     v, d, c = convert_to_binary(self.variables, self.domains, self.constraints)
     self.assertNotEqual(v, self.variables)
     self.assertIn('hidden0', v)
     self.assertIn('hidden1', v)