本文整理汇总了Python中pycosat.solve函数的典型用法代码示例。如果您正苦于以下问题:Python solve函数的具体用法?Python solve怎么用?Python solve使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了solve函数的20个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于我们的系统推荐出更棒的Python代码示例。
示例1: sat
def sat(self, additional=None, includeIf=False, names=False, limit=0):
"""
Calculate a SAT solution for the current clause set.
Returned is the list of those solutions. When the clauses are
unsatisfiable, an empty list is returned.
"""
if self.unsat:
return None
if not self.m:
return set() if names else []
if additional:
additional = list(map(lambda x: tuple(map(self.varnum, x)), additional))
clauses = chain(self.clauses, additional)
else:
clauses = self.clauses
try:
solution = pycosat.solve(clauses, vars=self.m, prop_limit=limit)
except TypeError:
# pycosat 0.6.1 should not require this; pycosat 0.6.0 did, but we
# have made conda dependent on pycosat 0.6.1. However, issue #2276
# suggests that some people are still seeing this behavior even when
# pycosat 0.6.1 is installed. Until we can understand why, this
# needs to stay. I still don't want to invoke it unnecessarily,
# because for large clauses lists it is slow.
clauses = list(map(list, clauses))
solution = pycosat.solve(clauses, vars=self.m, prop_limit=limit)
if solution in ("UNSAT", "UNKNOWN"):
return None
if additional and includeIf:
self.clauses.extend(additional)
if names:
return set(nm for nm in (self.indices.get(s) for s in solution) if nm and nm[0] != '!')
return solution
开发者ID:conda,项目名称:libconda,代码行数:35,代码来源:logic.py
示例2: sat
def sat(self, additional=None, includeIf=False, names=False, limit=0):
"""
Calculate a SAT solution for the current clause set.
Returned is the list of those solutions. When the clauses are
unsatisfiable, an empty list is returned.
"""
if self.unsat:
return None
if not self.m:
return set() if names else []
clauses = self.clauses
if additional:
def preproc(eqs):
def preproc_(cc):
for c in cc:
c = self.names.get(c, c)
if c is False:
continue
yield c
if c is True:
break
for cc in eqs:
cc = tuple(preproc_(cc))
if not cc:
yield cc
break
if cc[-1] is not True:
yield cc
additional = list(preproc(additional))
if additional:
if not additional[-1]:
return None
clauses = chain(clauses, additional)
try:
solution = pycosat.solve(clauses, vars=self.m, prop_limit=limit)
except TypeError:
# pycosat 0.6.1 should not require this; pycosat 0.6.0 did, but we
# have made conda dependent on pycosat 0.6.1. However, issue #2276
# suggests that some people are still seeing this behavior even when
# pycosat 0.6.1 is installed. Until we can understand why, this
# needs to stay. I still don't want to invoke it unnecessarily,
# because for large clauses lists it is slow.
clauses = list(map(list, clauses))
solution = pycosat.solve(clauses, vars=self.m, prop_limit=limit)
if solution in ("UNSAT", "UNKNOWN"):
return None
if additional and includeIf:
self.clauses.extend(additional)
if names:
return set(nm for nm in (self.indices.get(s) for s in solution) if nm and nm[0] != "!")
return solution
开发者ID:Korijn,项目名称:conda,代码行数:56,代码来源:logic.py
示例3: pycoSAT
def pycoSAT(expr):
"""Check satisfiability of an expression.
Given a CNF expression, returns a model that causes the input expression
to be true. Returns false if it cannot find a satisfible model.
A model is simply a dictionary with Expr symbols as keys with corresponding values
that are booleans: True if that symbol is true in the model and False if it is
false in the model.
Calls the pycosat solver: https://pypi.python.org/pypi/pycosat
>>> ppsubst(pycoSAT(A&~B))
{A: True, B: False}
>>> pycoSAT(P&~P)
False
"""
assert is_valid_cnf(expr), "{} is not in CNF.".format(expr)
clauses = conjuncts(expr)
# Load symbol dictionary
symbol_dict = mapSymbolAndIndices(clauses)
# Convert Expr to integers
clauses_int = exprClausesToIndexClauses(clauses, symbol_dict)
model_int = pycosat.solve(clauses_int)
if model_int == 'UNSAT' or model_int == 'UNKNOWN':
return False
model = indexModelToExprModel(model_int, symbol_dict)
return model
开发者ID:KKsan,项目名称:logit_plan,代码行数:30,代码来源:logic.py
示例4: solve
def solve(grid):
#solve a Sudoku problem
clauses = sudoku_clauses()
for i in range(1, 10):
for j in range(1, 10):
d = grid[i - 1][j - 1]
# For each digit already known, a clause (with one literal).
if d:
clauses.append([v(i, j, d)])
# Print number SAT clause
numclause = len(clauses)
print "P CNF " + str(numclause) +"(number of clauses)"
# solve the SAT problem
start = time.time()
sol = set(pycosat.solve(clauses))
end = time.time()
print("Time: "+str(end - start))
def read_cell(i, j):
# return the digit of cell i, j according to the solution
for d in range(1, 10):
if v(i, j, d) in sol:
return d
for i in range(1, 10):
for j in range(1, 10):
grid[i - 1][j - 1] = read_cell(i, j)
开发者ID:taufanardi,项目名称:sudoku-sat-solver,代码行数:29,代码来源:Sudoku.py
示例5: solve
def solve(sudoku_mat, sudoku_sz):
# Setting params (size, size-square, root-size)
set_params(sudoku_sz)
# Adding all clauses to the list clause_set
clause_set = sudoku_vals(sudoku_mat);
for i in range(sudo_size):
row_clause(i, clause_set)
col_clause(i, clause_set)
element_clause(clause_set)
for i in range(sudo_size_sqrt):
for j in range(sudo_size_sqrt):
block_clause(i*sudo_size_sqrt,j*sudo_size_sqrt,clause_set)
print len(clause_set)
# We would also like to print a cnf file 'sudoku.cnf' of the clauses so we canconveniently use it with other SAT solvers
outfile = file('sudoku.cnf','w')
outfile.write('p cnf '+str(sudo_size**3)+' '+str(len(clause_set)))
for clause in clause_set:
string = ''
for var in clause:
string = string + str(var) + ' '
string = string[:-1]
outfile.write('\n'+string+' 0')
# Solving the sudoku using pycosat SAT solver for python, which is based on PicoSAT
sol = set(pycosat.solve(clause_set))
# Editing the original matrix to reflect the solved sudoku
def read_cell(i,j):
for d in range(1,sudo_size+1):
if v(i,j,d) in sol:
return d
for i in range(sudo_size):
for j in range(sudo_size):
sudoku_mat[i][j] = read_cell(i,j)
开发者ID:ashwinkachhara,项目名称:sat-sudoku-solver,代码行数:33,代码来源:test1.py
示例6: sat
def sat(clauses):
"""
Calculate a SAT solution for `clauses`.
Returned is the list of those solutions. When the clauses are
unsatisfiable, an empty list is returned.
"""
try:
import pycosat
except ImportError:
sys.exit('Error: could not import pycosat (required for dependency '
'resolving)')
try:
pycosat.itersolve({(1,)})
except TypeError:
# Old versions of pycosat require lists. This conversion can be very
# slow, though, so only do it if we need to.
clauses = list(map(list, clauses))
solution = pycosat.solve(clauses)
if solution == "UNSAT" or solution == "UNKNOWN": # wtf https://github.com/ContinuumIO/pycosat/issues/14
return []
# XXX: If solution == [] (i.e., clauses == []), the result will have
# boolean value of False even though the clauses are not unsatisfiable)
return solution
开发者ID:Bbouley,项目名称:conda,代码行数:27,代码来源:logic.py
示例7: sat
def sat(self, additional=None, includeIf=False, names=False, limit=0):
"""
Calculate a SAT solution for the current clause set.
Returned is the list of those solutions. When the clauses are
unsatisfiable, an empty list is returned.
"""
if self.unsat:
return None
if not self.m:
return set() if names else []
if additional:
additional = list(map(lambda x: tuple(map(self.varnum, x)), additional))
clauses = chain(self.clauses, additional)
else:
clauses = self.clauses
clauses = list(clauses)
solution = pycosat.solve(clauses, vars=self.m, prop_limit=limit)
if solution in ("UNSAT", "UNKNOWN"):
return None
if additional and includeIf:
self.clauses.extend(additional)
if names:
return set(nm for nm in (self.indices.get(s) for s in solution) if nm and nm[0] != '!')
return solution
开发者ID:NoriVicJr,项目名称:conda,代码行数:26,代码来源:logic.py
示例8: pycoSAT
def pycoSAT(expr_list):
"""Check satisfiability of an expression list.
Given a list of CNF expressions, returns a model that causes all input expressions
to be true. Returns false if it cannot find a satisfible model.
A model is simply a dictionary with Expr symbols as keys with corresponding values
that are booleans: True if that symbol is true in the model and False if it is
false in the model.
Each CNF expression in the input list should be relatively short. Long expression
will cause this method to become incredibly slow.
Calls the pycosat solver: https://pypi.python.org/pypi/pycosat
>>> ppsubst(pycoSAT([A&~B]))
{A: True, B: False}
>>> pycoSAT([P&~P])
False
"""
clauses = []
for expr in expr_list:
clauses += conjuncts(expr)
# Load symbol dictionary
symbol_dict = mapSymbolAndIndices(clauses)
# Convert Expr to integers
clauses_int = exprClausesToIndexClauses(clauses, symbol_dict)
model_int = pycosat.solve(clauses_int)
if model_int == 'UNSAT' or model_int == 'UNKNOWN':
return False
model = indexModelToExprModel(model_int, symbol_dict)
return model
开发者ID:eabartlett,项目名称:cs188,代码行数:32,代码来源:logic.py
示例9: solve
def solve(grid):
"""
solve a Sudoku grid inplace
"""
clauses = sudoku_clauses()
for i in range(1, 10):
for j in range(1, 10):
d = grid[i - 1][j - 1]
# For each digit already known, a clause (with one literal).
# Note:
# We could also remove all variables for the known cells
# altogether (which would be more efficient). However, for
# the sake of simplicity, we decided not to do that.
if d:
clauses.append([v(i, j, d)])
# solve the SAT problem
start = time.clock()
sol = set(pycosat.solve(clauses))
t = time.clock() - start
print 'pycosat clauses:', len(clauses), ' solution time:', t
def read_cell(i, j):
# return the digit of cell i, j according to the solution
for d in range(1, 10):
if v(i, j, d) in sol:
return d
for i in range(1, 10):
for j in range(1, 10):
grid[i - 1][j - 1] = read_cell(i, j)
开发者ID:jayshans,项目名称:Satoku,代码行数:32,代码来源:pycosatSudoku.py
示例10: sat
def sat(self, formula=None):
if formula:
added_clause, new_atoms = self._add(formula)
ret = pycosat.solve(self.cnfs)
if formula:
self._roll_back(added_clause, new_atoms)
# FIXME
return ret not in ('UNSAT', 'UNKNOWN')
开发者ID:skydark,项目名称:spdlr,代码行数:8,代码来源:plogic.py
示例11: solve_nqueen
def solve_nqueen(board, NUM_ROWS, NUM_COLUMNS):
nqueen_clauses = get_nqueen_clauses(NUM_ROWS, NUM_COLUMNS)
solution = set(pycosat.solve(nqueen_clauses))
for row in range(1,NUM_ROWS+1):
for column in range(1,NUM_COLUMNS+1):
board[row-1][column-1] = get_cell_solution(solution,row, column)
开发者ID:macartur,项目名称:programming_ai,代码行数:8,代码来源:n-queens.py
示例12: py_itersolve
def py_itersolve(clauses):
while True:
sol = pycosat.solve(clauses)
if isinstance(sol, list):
yield sol
clauses.append([-x for x in sol])
else: # no more solutions -- stop iteration
return
开发者ID:ContinuumIO,项目名称:pycosat,代码行数:8,代码来源:test_pycosat.py
示例13: solve
def solve(self):
solution = pycosat.solve(self.clauses)
if solution == 'UNSAT':
return 'UNSAT'
else:
#print solution
solution = [x for x in solution if -x not in self.all_units]
solution.extend(list(self.all_units))
return sorted(list(set(solution)), key=abs)
开发者ID:michal3141,项目名称:sat,代码行数:9,代码来源:model.py
示例14: _solve_sat
def _solve_sat(self,cnf):
"""
Calls the SAT solver.
"""
start = time.time()
sol = pycosat.solve(cnf)
elapsed = time.time()-start
if self.verbose:
print('\t- Took %.3f ms to solve SAT problem.'%(1000.0*elapsed))
return sol
开发者ID:phrqas,项目名称:rao-star,代码行数:10,代码来源:pysat.py
示例15: solve
def solve(n):
global N
N = n
clauses = queens_clauses()
# solve the SAT problem
sol = set(pycosat.solve(clauses))
for i in range(N):
print ''.join('Q' if v(i, j) in sol else '.' for j in range(N))
print n, len(clauses)
开发者ID:NacimKACEL,项目名称:misc,代码行数:10,代码来源:nqueens.py
示例16: solution
def solution(self):
"""
Return arbitrary solution to the currently described satisfiability
problem.
"""
solution = pycosat.solve(self.clauses)
if solution == "UNSAT":
raise UnsatisfiableConstraints("Constraints are unsatisfiable")
elif solution == "UNKNOWN":
raise SolutionNotFound("Search limits exhausted without solution")
else:
return self.remap_solution(solution)
开发者ID:ericpruitt,项目名称:a9bsp,代码行数:13,代码来源:a9bsp.py
示例17: proper
def proper(self):
global baseClauses
totalClauses = baseClauses + self.givens_to_dimacs_list()
sol = set(pycosat.solve(totalClauses))
def read_cell(i, j):
for d in range(1, 10):
if v(i, j, d) in sol:
return [v(i, j, d),d]
negation = []
for i in range(1, 10):
for j in range(1, 10):
negation.append(-read_cell(i, j)[0])
totalClauses.append(negation)
res = pycosat.solve(totalClauses)
if res == "UNSAT":
return 1
else:
return 0
开发者ID:erikvanegmond,项目名称:krcourse2015sat,代码行数:22,代码来源:sudoku.py
示例18: solve_sudoku
def solve_sudoku(sudoku_board):
"""
Generate a sudoku clauses, apply in a pycosat and get sudoku solution
"""
sudoku_clauses = get_sudoku_clauses()
single_clauses = get_single_clauses(sudoku_board)
sudoku_clauses.extend(single_clauses)
sudoku_solution = set(pycosat.solve(sudoku_clauses))
for row in range(1, NUM_DIGITS+1):
for column in range(1, NUM_DIGITS+1):
sudoku_board[row-1][column-1] = get_cell_solution(
sudoku_solution, row, column)
return sudoku_board
开发者ID:macartur,项目名称:programming_ai,代码行数:15,代码来源:sudoku.py
示例19: sat
def sat(clauses, iterator=False):
"""
Calculate a SAT solution for `clauses`.
Returned is the list of those solutions. When the clauses are
unsatisfiable, an empty list is returned.
"""
if pycosat_prep:
clauses = list(map(list,clauses))
if iterator:
return pycosat.itersolve(clauses)
solution = pycosat.solve(clauses)
if solution == "UNSAT" or solution == "UNKNOWN":
return None
return solution
开发者ID:ARF1,项目名称:conda,代码行数:16,代码来源:logic.py
示例20: sat
def sat(clauses):
"""
Calculate a SAT solution for `clauses`.
Returned is the list of those solutions. When the clauses are
unsatisfiable, an empty list is returned.
"""
try:
import pycosat
except ImportError:
sys.exit('Error: could not import pycosat (required for dependency '
'resolving)')
solution = pycosat.solve(clauses)
if solution == "UNSAT" or solution == "UNKNOWN": # wtf https://github.com/ContinuumIO/pycosat/issues/14
return []
return solution
开发者ID:certik,项目名称:conda,代码行数:18,代码来源:logic.py
注:本文中的pycosat.solve函数示例由纯净天空整理自Github/MSDocs等源码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。 |
请发表评论