def solverForSample(sample): # set up solver with clauses from original formula solver = pycryptosat.Solver() for clause in clauses: solver.add_clause(clause) # assert the assignment to the maximization variables for literal in sample: solver.add_clause([literal]) return solver
def testLiteral(literal, clauses): s = pycryptosat.Solver() s.add_clauses(clauses) sat, solution = s.solve([literal]) if not sat: return False sat, solution = s.solve([-literal]) if not sat: return True return None
def solve(nbits): sat = pycryptosat.Solver(threads=4) with open('cnf.dimacs', 'r') as f: print(f.readline().strip()) for line in f: if line.startswith('c'): print('comment, break') break sat.add_clause(list(map(int, line[:-3].split()))) is_sat, res = sat.solve() if not is_sat: raise ValueError('UNSAT') else: init = frombools(res[1:2 * nbits + 1][::2]) print(f"{init:0{nbits}b}") return init
def find_solution(original_assignment): """ Find a valid solution (if it exists) from a given CNF. :param original_assignment: input CNF. :return: a tuple of a SAT solution from the input CNF. """ solver = pycryptosat.Solver() for i in range(0, len(original_assignment)): solver.add_clause(original_assignment[i]) solve_return = solver.solve() if not solve_return[1]: print(' There is no valid solution. Exiting...\n') exit(1) else: return solve_return[1][1:]
def find_all_solutions(original_assignment): """ Find all possible solution from a given CNF. :param original_assignment: input CNF. :return: list of SAT solutions from the input CNF. """ solutions = list() solver = pycryptosat.Solver() for i in range(0, len(original_assignment)): solver.add_clause(original_assignment[i]) while True: solver_solution = solver.solve() # print('Solution found!') if not solver_solution[0]: break solutions.append(solver_solution[1][1:]) constraint_clauses = solve_to_vars_assignment(solver_solution[1]) solver.add_clause(constraint_clauses) return solutions
def __init__(self, nbits, pol, threads=4): self.nbits = nbits self.state = [[i + 1] for i in range(nbits)] self.state.insert(0, None) self.pol = toidx(pol, nbits) self.slvr = pycryptosat.Solver(threads=threads)
def cms_setup(clauses, m): solver = pycryptosat.Solver() solver.add_clauses(clauses, max_var=m) return solver
clause_input_cnf = re.search('^\s*((-)*\d+\s*)*(0)(\\n)*$', line) # detect clause if header is not None: num_variables = int(line.split()[2]) if comment is None and clause_input_cnf is None: print("Invalid input:", line) elif clause_input_cnf is not None: raw_clause = clause_input_cnf.group(0).split() raw_clause.pop() clause = [int(numeric_string) for numeric_string in raw_clause] cnf.append(clause) return cnf solver = pycryptosat.Solver() N = 150 R = np.arange(0.2, 10.2, 0.2) K = [3,4,5] for k in K: for r in R: num_total = 0 num_sat = 0 # Generate 50 formulas for i in range(50): K = CNF(k,N,N*r) DIMACS_format = K.dimacs(export_header=False) lines = DIMACS_format.split("\n")
def testKb(clauses): s = pycryptosat.Solver() s.add_clauses(clauses) sat, solution = s.solve() return sat