def findTotalMisWithMCSAT(self, finalout): for key, value in self.weightsDic.items(): value['nii'] = 0 if isinstance(self.evidence, list): for i, evid in enumerate(self.evidence): mcASPObj = marginal_mcsat.mcSAT(finalout, evid, [], self.xorMode, self.max_mcsat_iteration) mcASPObj.args = self.args mcASPObj.runMCASP() samplesEvidenced = mcASPObj.sampleForReturn if self.args.verbosity > 4: print("Done sampling from " + str(i) + "th partial evidence") for sampleE in samplesEvidenced: for atom in sampleE: if (str(atom.name) == "neg" or str(atom.name) == "unsat") and "lpmlnneg_" in str( atom.arguments[0]): idx = eval(str(atom.arguments[0]).split('_')[1]) for key, value in self.weightsDic.items(): if value['wIndex'] == idx: value['nii'] += 1 for key, value in self.weightsDic.items(): value['nii'] = float(value['nii']) / float( self.max_mcsat_iteration) else: mcASPObj = marginal_mcsat.mcSAT(finalout, self.evidence, [], self.xorMode, self.max_mcsat_iteration) mcASPObj.args = self.args mcASPObj.runMCASP() samplesEvidenced = mcASPObj.sampleForReturn if self.args.verbosity > 4: print("Done samples evidenced") for sampleE in samplesEvidenced: for atom in sampleE: if (str(atom.name) == "neg" or str(atom.name) == "unsat") and "lpmlnneg_" in str( atom.arguments[0]): idx = eval(str(atom.arguments[0]).split('_')[1]) for key, value in self.weightsDic.items(): if value['wIndex'] == idx: value['nii'] += 1 for key, value in self.weightsDic.items(): value['nii'] = float(value['nii']) / float( self.max_mcsat_iteration)
def max_walk_sat_exact_sdd(self,save=None,load=None): solution = "" utility = -float("inf") inferObj = sdd_infer.sddInference("", ['utility']) inferObj.args = self.args if load is None and save is None: inferObj.sddConstructorFromLPMLN(self.aspContent) elif load is None and save is not None: inferObj.sddConstructorFromLPMLN(self.aspContent,True,save) else: inferObj.sddConstructorFromFile(load) for _ in range(1,self.mt): mcASPObj = marginal_mcsat.mcSAT(self.aspContent, "", [], self.xorMode,1) mcASPObj.runMCASP() firstSample = mcASPObj.getSamples() decisionAssignments = self.completeTruthAssignment(firstSample[0]) tempSoln = decisionAssignments tempUtility = self.max_walk_sat_exact_helper(inferObj,tempSoln) for _ in range(1,self.mf): vf="" deltCostVf = float("inf") orgUtility = self.max_walk_sat_exact_helper(inferObj,tempSoln) if self.p < random.random(): vf = tempSoln[random.randint(0, len(tempSoln) - 1)] newDecisionAssignments = self.flipDecisionAtom(tempSoln,vf) flippedUtility =self.max_walk_sat_exact_helper(inferObj,newDecisionAssignments) if flippedUtility != False: deltCostVf = orgUtility - flippedUtility else: deltaCost = [] for decToFlip in tempSoln: newDecisionAssignments = self.flipDecisionAtom(tempSoln, decToFlip) flippedUtility = self.max_walk_sat_exact_helper(inferObj,newDecisionAssignments) if flippedUtility != False: deltaCost.append((decToFlip,orgUtility - flippedUtility)) if len(deltaCost) != 0: minDeltaCost = min(deltaCost,key=lambda t:t[1]) else: continue vf = minDeltaCost[0] deltCostVf = minDeltaCost[1] if deltCostVf<0: tempSoln = self.flipDecisionAtom(tempSoln, vf) tempUtility = tempUtility - deltCostVf if tempUtility >utility: utility = tempUtility solution = tempSoln return solution,utility
def max_walk_sat(self): solution = "" utility = -float("inf") for mt_iter in range(1,self.mt): if self.args.verbosity >4: print("Starting one iteration: ",mt_iter) mcASPObj = marginal_mcsat.mcSAT(self.aspContent, "", [], self.xorMode,1) mcASPObj.runMCASP() firstSample = mcASPObj.getSamples() decisionAssignments = self.completeTruthAssignment(firstSample[0]) tempSoln = decisionAssignments tempUtility = self.expectedUtility(self.buildASPEvidenceForm(tempSoln)) for _ in range(1,self.mf): vf="" deltCostVf = float("inf") orgUtility = self.expectedUtility(self.buildASPEvidenceForm(tempSoln)) if self.p < random.random(): vf = tempSoln[random.randint(0, len(tempSoln) - 1)] newDecisionAssignments = self.flipDecisionAtom(tempSoln,vf) flippedUtility = self.expectedUtility(self.buildASPEvidenceForm(newDecisionAssignments)) if flippedUtility != False: deltCostVf = orgUtility - flippedUtility else: deltaCost = [] for decToFlip in tempSoln: newDecisionAssignments = self.flipDecisionAtom(tempSoln, decToFlip) flippedUtility = self.expectedUtility(self.buildASPEvidenceForm(newDecisionAssignments)) if flippedUtility != False: deltaCost.append((decToFlip,orgUtility - flippedUtility)) if len(deltaCost) != 0: minDeltaCost = min(deltaCost,key=lambda t:t[1]) else: continue vf = minDeltaCost[0] deltCostVf = minDeltaCost[1] if deltCostVf<0: tempSoln = self.flipDecisionAtom(tempSoln, vf) tempUtility = tempUtility - deltCostVf if tempUtility >utility: utility = tempUtility solution = tempSoln return solution,utility
def max_walk_sat_exact_sdd_2(self): solution = "" utility = -float("inf") r = run.run(self.aspContent,self.args) for _ in range(1,self.mt): mcASPObj = marginal_mcsat.mcSAT(self.aspContent, "", [], self.xorMode,1) mcASPObj.runMCASP() firstSample = mcASPObj.getSamples() decisionAssignments = self.completeTruthAssignment(firstSample[0]) tempSoln = decisionAssignments tempUtility = self.max_walk_sat_sdd2_helper(r, tempSoln) for _ in range(1,self.mf): vf="" deltCostVf = float("inf") orgUtility = self.max_walk_sat_sdd2_helper(r,tempSoln) if self.p < random.random(): vf = tempSoln[random.randint(0, len(tempSoln) - 1)] newDecisionAssignments = self.flipDecisionAtom(tempSoln,vf) flippedUtility =self.max_walk_sat_sdd2_helper(r,newDecisionAssignments) if flippedUtility != False: deltCostVf = orgUtility - flippedUtility else: deltaCost = [] for decToFlip in tempSoln: newDecisionAssignments = self.flipDecisionAtom(tempSoln, decToFlip) flippedUtility = self.max_walk_sat_sdd2_helper(r,newDecisionAssignments) if flippedUtility != False: deltaCost.append((decToFlip,orgUtility - flippedUtility)) if len(deltaCost) != 0: minDeltaCost = min(deltaCost,key=lambda t:t[1]) else: continue vf = minDeltaCost[0] deltCostVf = minDeltaCost[1] if deltCostVf<0: tempSoln = self.flipDecisionAtom(tempSoln, vf) tempUtility = tempUtility - deltCostVf if tempUtility >utility: utility = tempUtility solution = tempSoln return solution,utility
def expectedUtility(self,query_action): mcASPObj = marginal_mcsat.mcSAT(self.aspContent, query_action, [], self.xorMode,self.max_sample) mcASPObj.args = self.args mcASPObj.runMCASP() samplesEvidenced = mcASPObj.getSamples() #Given program combining with decision or evidence cannot lead to any stable model if len(samplesEvidenced) == 0: return False expectedU = 0.0 for sampleE in samplesEvidenced: for atom in sampleE: if str(atom.name) == "utility": expectedU+=eval(str(atom.arguments[0]).strip('\"')) return expectedU/len(samplesEvidenced)
def findTotalMisWithMCSAT(self, finalout): for key, value in self.weightsDic.items(): value['unsatEvi'] = 0 value['unsatNoEvi'] = 0 if isinstance(self.evidence, list): for i, evid in enumerate(self.evidence): if self.args.verbosity > 4: print("Start sample no evidenced") mcASPObj = marginal_mcsat.mcSAT(finalout, "", [], self.xorMode, self.max_mcsat_iteration) mcASPObj.args = self.args mcASPObj.runMCASP() samples = mcASPObj.sampleForReturn if self.args.verbosity > 4: print("Done sample no evidenced") for samplene in samples: for atom in samplene: counter = 0 if (atom.name == "neg" or atom.name == "unsat") and ( "lpmlnneg_" in str(atom.arguments[0])): idx = eval(str(atom.arguments[0]).split('_')[1]) counter += 1 for key, value in self.weightsDic.items(): if value['wIndex'] == idx: value['unsatNoEvi'] += 1 if self.args.verbosity > 4: if self.complete_evid: print("Start sampling from " + str(i) + "th complete evidenced") else: print("Start sampling from " + str(i) + "th partial evidenced") if self.complete_evid: mcASPObj = marginal_mcsat.mcSAT(finalout, evid, [], self.xorMode, 1) else: mcASPObj = marginal_mcsat.mcSAT(finalout, evid, [], self.xorMode, self.max_mcsat_iteration) mcASPObj.args = self.args mcASPObj.runMCASP() samplesEvidenced = mcASPObj.sampleForReturn if self.args.verbosity > 4: if self.complete_evid: print("Done sampling from " + str(i) + "th complete evidenced") else: print("Done sampling from " + str(i) + "th partial evidenced") for sampleE in samplesEvidenced: for atom in sampleE: if (str(atom.name) == "neg" or str(atom.name) == "unsat") and "lpmlnneg_" in str( atom.arguments[0]): idx = eval(str(atom.arguments[0]).split('_')[1]) for key, value in self.weightsDic.items(): if value['wIndex'] == idx: if self.complete_evid: value[ 'unsatEvi'] += 1 * self.max_mcsat_iteration else: value['unsatEvi'] += 1 else: if self.args.verbosity > 4: if self.complete_evid: print("Start sampling from complete evidenced") else: print("Start sampling from partial evidenced") if self.complete_evid: mcASPObj = marginal_mcsat.mcSAT(finalout, self.evidence, [], self.xorMode, 1) else: mcASPObj = marginal_mcsat.mcSAT(finalout, self.evidence, [], self.xorMode, self.max_mcsat_iteration) mcASPObj.args = self.args mcASPObj.runMCASP() samplesEvidenced = mcASPObj.sampleForReturn if self.args.verbosity > 4: if self.complete_evid: print("Done sample from complete evidenced") else: print("Done sample from partial evidenced") for sampleE in samplesEvidenced: for atom in sampleE: if (str(atom.name) == "neg" or str(atom.name) == "unsat") and "lpmlnneg_" in str( atom.arguments[0]): idx = eval(str(atom.arguments[0]).split('_')[1]) for key, value in self.weightsDic.items(): if value['wIndex'] == idx: if self.complete_evid: value[ 'unsatEvi'] += 1 * self.max_mcsat_iteration else: value['unsatEvi'] += 1 if self.args.verbosity > 4: print(self.weightsDic)
def learn(self): self.learn_ini() content = self.lpmln_to_lpmln_neg_parser( self.progranWithoutPlaceholder) content = self.lpmln_to_asp_parser(content) finalout = self.asp_domain_2_asp_parser(content) if isinstance(self.evidence, list): for l in self.evidence: mcASPObj = marginal_mcsat.mcSAT(finalout, l, [], self.xorMode, self.min_mcsat_interation) mcASPObj.args = self.args result = mcASPObj.runMCASP() if not result: print('Evidence and program not satisfiable. Exit.') return False else: mcASPObj = marginal_mcsat.mcSAT(finalout, l, [], self.xorMode, self.min_mcsat_interation) mcASPObj.args = self.args result = mcASPObj.runMCASP() if not result: print('Evidence and program not satisfiable. Exit.') return False # Begin: Learning Iterations actualNumIteration = 0 for iter_count in range(self.max_learning_iteration): iter_start_time = time.time() actualNumIteration += 1 self.updateWithWeightPlaceHolders() content = self.lpmln_to_lpmln_neg_parser( self.progranWithoutPlaceholder) content = self.lpmln_to_asp_parser(content) finalout = self.asp_domain_2_asp_parser(content) self.findTotalMisWithMCSAT(finalout) # End: Single learning iteration # Compute new weights total_gradient = 0 max_diff = 0 for key, value in self.weightsDic.items(): prob_gradient = float(-value['unsatEvi'] + float(value['unsatNoEvi'])) / float( self.max_mcsat_iteration) total_gradient += abs(prob_gradient) if self.args.verbosity > 4: print('Rule: ', key) print( '# False ground instances from Evidence: ', float(value['unsatEvi']) / float(self.max_mcsat_iteration)) print( 'Expected # false ground instances: ', float(value['unsatNoEvi']) / float(self.max_mcsat_iteration)) print('Gradient: ', prob_gradient) if max_diff < abs(self.lr * prob_gradient): max_diff = abs(self.lr * prob_gradient) value['weight'] += (self.lr * prob_gradient) value['weightSum'] += value['weight'] if self.args.verbosity > 4: print('New weight: ', key, ':', value['weight']) # End: Learning Iterations if self.args.verbosity > 4: print("max_diff: ", max_diff) if max_diff <= self.stopping_diff: break if self.args.verbosity > 4: print('============ Time for finishing iteration ' + str(actualNumIteration) + ': ' + str(time.time() - iter_start_time) + ' ============') # Begin: Store and save new weights self.updateWithWeightPlaceHolders() self.updatefinalWeight()
def solve(self): wholeInput = "" evidenceInput = "" if self.arg_list['nn_indictor']: wholeInput += nn_processor.process(self.arg_list['file_name'][-1]) self.arg_list['file_name'] = self.arg_list['file_name'][:-1] for lpmln_file in self.arg_list['file_name']: with open(lpmln_file, 'r') as lpmln_content: wholeInput += lpmln_content.read() lpmln_content.close() for evid_file in self.arg_list['evidence']: with open(evid_file, 'r') as evidence_content: evidenceInput += evidence_content.read() evidence_content.close() if self.arg_list['mode'] == "sdd": content = self.lpmln_to_asp_sdd_parser(wholeInput, False, self.arg_list['mf']) parsed_lpmln = self.asp_domain_2_asp_parser(content) elif self.arg_list['mode'] == "mcasp": content = self.lpmln_to_lpmln_neg_parser(wholeInput, self.arg_list['mf']) content = self.lpmln_to_asp_parser(content, False, self.arg_list['mf']) parsed_lpmln = self.asp_domain_2_asp_parser(content) elif self.arg_list['mode'] == "map": content = self.lpmln_to_asp_parser(wholeInput, self.arg_list['hard'], self.arg_list['mf'], True) parsed_lpmln = self.asp_domain_2_asp_parser(content) else: content = self.lpmln_to_asp_parser(wholeInput, self.arg_list['hard'], self.arg_list['mf'], True) parsed_lpmln = self.asp_domain_2_asp_parser(content) if self.args.verbosity > 4: print( "================== Parsed ASP Program ======================") print(parsed_lpmln) if self.arg_list['mode'] == "map": with open('asp.pl', 'w') as fw: fw.write(parsed_lpmln + evidenceInput) if self.arg_list['hard']: command = "clingo " + os.getcwd() + "/asp.pl --opt-mode=enum 0" else: command = "clingo " + os.getcwd() + "/asp.pl " os.system(command) elif self.arg_list['mode'] == "ex": warn_option = "--warn=none" thread_option = "-t 4" enumerateAll = "--opt-mode=enum" listAll = "0" warn = "--warn=no-atom-undefined" options = [warn_option, thread_option, enumerateAll, listAll, warn] exactInfer = exactProbInfer.exactProbInfer( parsed_lpmln + evidenceInput, options, self.arg_list['query'], self.arg_list['hard'], self.arg_list['all']) if self.arg_list['hard']: with open('asp.pl', 'w') as fw: fw.write(parsed_lpmln + evidenceInput) command = "clingo " + os.getcwd() + "/asp.pl --opt-mode=enum 0" os.system(command) exactInfer.solve() elif self.arg_list['mode'] == "mcasp": mcASPObj = marginal_mcsat.mcSAT(parsed_lpmln, evidenceInput, self.arg_list['query'], self.arg_list['xor_mode'], self.arg_list['sample']) mcASPObj.args = self.args mcASPObj.runMCASP() mcASPObj.printQuery() elif self.arg_list['mode'] == "kcor": marginal_coherent_obj = marginal_coherent.Marginal_coherent( parsed_lpmln, evidenceInput, self.arg_list['query'], self.arg_list['sample']) marginal_coherent_obj.args = self.args marginal_coherent_obj.run() marginal_coherent_obj.printQuery() elif self.arg_list['mode'] == "sdd": inferObj = sdd_infer.sddInference(evidenceInput, self.arg_list['query']) inferObj.args = self.args if self.arg_list['sddPara'][0] == 0: inferObj.sddConstructorFromLPMLN(parsed_lpmln, False) inferObj.inferencePrtQuery() elif self.arg_list['sddPara'][0] == 1: inferObj.sddConstructorFromFile(self.arg_list['sddPara'][1]) inferObj.inferencePrtQuery() else: inferObj.sddConstructorFromLPMLN(parsed_lpmln, True, self.arg_list['sddPara'][1]) inferObj.inferencePrtQuery() else: r = run.run(parsed_lpmln, self.args) result = r.infer(self.arg_list['query'], evidenceInput) for r in result: print(r[0].to_string(), ": ", r[1])