def sat_kb(kb, val, var_list): """ Check a valuation satisfies a classical KB return true if val satisfies KB """ solver = minisolvers.MinisatSolver() new_kb = kb.copy() for i in range(len(kb)): tree = create_tree(kb[i]) tree = conv_impl(tree) tree = prop_neg(tree) tree = conv_orOfAnd(tree) new_kb[i]=tree.inorder() val_s = [] for i in range(len(val)): if val[i] =="0": val_s.append("-"+var_list[i]) else: val_s.append(var_list[i]) val="&".join(val_s) new_kb.append(val) clauses = sat_format(new_kb,var_list) for i in range(len(var_list)): solver.new_var() for clause in clauses: solver.add_clause(clause) return solver.solve()
def checkInterpretations(interpretations, knowB): """Checks if given interpretations are consistent with a given knowledge base . Returns True if it does.""" inte = conversion.SATSolverFormat(interpretations) kb = knowB m = minisolvers.MinisatSolver() uniqueVar = conversion.noOfUniqueAtomsLetters(kb) KBno = conversion.SATSolverFormat(kb) for i in range(uniqueVar): m.new_var() for clause in KBno: m.add_clause(clause) for clause2 in inte: m.add_clause(clause2) if (m.solve()): return True else: return False
def checkInterpretationsLM(interpretations, knowB, letters): """Checks if given interpretations are consistent with a given knowledge base. also takes a list of unique letters as input as well. Returns True if it is""" inte = conversion.SATSolverFormatLM(interpretations, letters) kb = knowB Letters = letters m = minisolvers.MinisatSolver() KBno = conversion.SATSolverFormatLM(kb, Letters) for i in range(len(Letters)): m.new_var() for clause in KBno: m.add_clause(clause) for clause2 in inte: m.add_clause(clause2) if (m.solve()): return True else: return False
def entail(s, val, var_list): """ Returns true if a statement is entailed by a valuation """ solver = minisolvers.MinisatSolver() s = create_tree(s) s = conv_impl(s) s = negate(s) s = conv_orOfAnd(s) s = s.inorder() val_s = [] for i in range(len(val)): if val[i] =="0": val_s.append("-"+var_list[i]) else: val_s.append(var_list[i]) val="&".join(val_s) kb= [s] + [val] clauses = sat_format(kb,var_list) for i in range(len(var_list)): solver.new_var() for clause in clauses: solver.add_clause(clause) return not solver.solve()
def checkEntailment(knowB, sentence): """Checks Classical Entailment without a Ranked Model. Returns True if it does entail""" KB = knowB uniqueVar = conversion.noOfUniqueAtomsLetters(KB) KBno = conversion.SATSolverFormat(KB) sen = sentence uni = conversion.listOfUniqueAtomsLetters(KB) Ent = minisolvers.MinisatSolver() uniNo = conversion.convertLettersListToNo(uni) check = addNegation(sen, uniNo, uni) #Sentence being checked is negated for i in check: #Check is already in SAT-SOlver format KBno.append(i) for i in range(uniqueVar): Ent.new_var() for clause in KBno: Ent.add_clause(clause) if (Ent.solve()): return False else: return True
def setUp(self): self.solver = minisolvers.MinisatSolver() self.clauses = [ [1], [-2], [3, 4], [-3, 5], [-4, 6], [-5, 4], [-6] ] self.numvars = max([max(cl) for cl in [[abs(x) for x in cl] for cl in self.clauses]])
def __init__(self): super().__init__() self._solver = minisolvers.MinisatSolver()
# -*- coding: utf-8 -*- """ Created on Wed Jul 18 13:21:12 2018 @author: Guy """ import conversion import minisolvers import itertools S = minisolvers.MinisatSolver() def createRankedModel(knowB): """Takes in a knowledge base as input and returns the LM-Minimal Ranked model that the LM Algorithm produces and returns it as a dictionary""" #Initialising Variables KB = knowB letters = conversion.listOfUniqueAtomsLetters(KB) interpretations = createInterpretations(KB) rankedModel = {} rankedModel[0] = interpretations if (len(rankedModel) == 1): #The First Level rankedModel[1] = moveInterpretations( rankedModel[0], KB) #Moving interpretations up a level rankedModel[0] = removeContradictoryInterpretations( rankedModel[0], KB) #Keeping the consistent ones if (len(rankedModel) > 1): counter = 1 # Represents level