How to use the simpleai.search.convert_to_binary function in simpleai

To help you get started, we’ve selected a few simpleai examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github simpleai-team / simpleai / tests / search / test_hidden_variables.py View on Github external
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)
github simpleai-team / simpleai / tests / search / test_hidden_variables.py View on Github external
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,)])
github simpleai-team / simpleai / tests / search / test_hidden_variables.py View on Github external
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)
github simpleai-team / simpleai / samples / search / cryptarithmetic.py View on Github external
(('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
github simpleai-team / simpleai / samples / search / simple_nary_to_binary.py View on Github external
else:
        return values[1] % 2 == 0  # first odd, expect second to be even


# a constraint that requires one variable to be different than 1
def const_not_1(variables, values):
    return values[0] != 1

constraints = [
    (('A', 'B', 'C'), const_different),
    (('A', 'C'), const_one_bigger_other),
    (('A', 'C'), const_one_odd_one_even),
    (('A',), const_not_1)
]

variables, domains, constraints = convert_to_binary(variables, domains, constraints)
problem = CspProblem(variables, domains, constraints)
result = backtrack(problem)
print result
# result, {'A':2, 'B': 3, 'C': 1})
github simpleai-team / simpleai / samples / search / sudoku.py View on Github external
# -- 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)
print "Took %d seconds to finish using n-ary constraints" % elapsed
github simpleai-team / simpleai / samples / search / threesat.py View on Github external
(('X2', 'X5', 'X6'), lambda v, values: values[0] or values[1] or not values[2]),
]

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

start = time()
problem = CspProblem(variables, original_domains, original_constraints)
result = backtrack(problem)
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)
elapsed = time() - start
print result
print "Took %d seconds to finish using binary constraints" % elapsed