def check_sum(self): '''ensure that the sum of weights of literals is equal to the objective (debug purpose).''' p1, p2, p3 = math.log(etcetera.jointProbability(self.literals)), sum([ v.obj for v in self.solver.gm.getVars() if v.X > 0.5 ]), self.solver.gm.objVal if abs(p1 - p2) >= 1e-6 or abs(p2 - p3) >= 1e-6: print self.solver.gm.Status logging.info(" FATAL ERROR: %f, %f, %f" % (math.exp(p1), math.exp(p2), math.exp(p3))) logging.info(parse.display(self.raw)) return False return True
def incremental1(obs, kb, maxdepth, n, w, b, skolemize=True): # n = n-best # w = window size # b = beam of running candidate interpretations indexed_kb = abduction.index_by_consequent_predicate(kb) previous = [] # proofs of the previous while len(obs) > 0: window = obs[0:w] obs = obs[w:] listoflists = [ abduction.and_or_leaflists([ob], indexed_kb, maxdepth) for ob in window ] if len(previous) > 0: listoflists.append(previous) pr2beat = 0.0 nbest = [] # solutions nbestPr = [] # probabilities for u in itertools.product(*listoflists): u = list(itertools.chain.from_iterable(u)) if etcetera.bestCaseProbability(u) > pr2beat: for solution in abduction.crunch(u): jpr = etcetera.jointProbability(solution) if jpr > pr2beat: insertAt = bisect.bisect_left(nbestPr, jpr) nbest.insert(insertAt, solution) nbestPr.insert(insertAt, jpr) if len(nbest) > b: nbest.pop(0) nbestPr.pop(0) pr2beat = nbestPr[0] # only if full nbest.reverse() # [0] is now highest previous = nbest if skolemize: return [unify.skolemize(r) for r in previous[0:n]] # only skolemize nbest else: return previous[0:n]
def incremental2(obs, kb, maxdepth, n, w, b, skolemize=True): # n = n-best # w = window size # b = beam of running candidate interpretations iteration = 1 # count for skolem constants indexed_kb = abduction.index_by_consequent_predicate(kb) previous = [] # proofs of the previous remaining = obs[:] # obs yet to be interpretated # first, interpret the first window as normal window = remaining[0:w] remaining = remaining[w:] listoflists = [ abduction.and_or_leaflists([ob], indexed_kb, maxdepth) for ob in window ] pr2beat = 0.0 nbest = [] # solutions nbestPr = [] # probabilities for u in itertools.product(*listoflists): u = list(itertools.chain.from_iterable(u)) if etcetera.bestCaseProbability(u) > pr2beat: for solution in abduction.crunch(u): jpr = etcetera.jointProbability(solution) if jpr > pr2beat: insertAt = bisect.bisect_left(nbestPr, jpr) nbest.insert(insertAt, solution) nbestPr.insert(insertAt, jpr) if len(nbest) > b: nbest.pop(0) nbestPr.pop(0) pr2beat = nbestPr[0] # only if full nbest.reverse() # [0] is now highest previous = nbest pre = "$" + str(iteration) + ":" previous = [unify.skolemize(r, prefix=pre) for r in previous] # skolemize the past (required) # next, interpret remaining windows in a special way while len(remaining) > 0: iteration += 1 window = remaining[0:w] remaining = remaining[w:] pr2beat = 0.0 nbest = [] # solutions nbestPr = [] # probabilities for previousSolution in previous: previousSolutionJpr = etcetera.jointProbability(previousSolution) context = getContext(previousSolution, obs, kb) listoflists = [ contextual_and_or_leaflists([ob], indexed_kb, maxdepth, context) for ob in window ] for u in itertools.product(*listoflists): u = list(itertools.chain.from_iterable(u)) if etcetera.bestCaseProbability( u) * previousSolutionJpr > pr2beat: for solution in abduction.crunch(u): jpr = etcetera.jointProbability( solution) * previousSolutionJpr if jpr > pr2beat: insertAt = bisect.bisect_left(nbestPr, jpr) nbest.insert(insertAt, previousSolution + solution) # joined nbestPr.insert(insertAt, jpr) if len(nbest) > b: nbest.pop(0) nbestPr.pop(0) pr2beat = nbestPr[0] # only if full nbest.reverse() # [0] is now highest previous = nbest pre = "$" + str(iteration) + ":" previous = [unify.skolemize(r, prefix=pre) for r in previous] # skolemize the past (required) return previous[0:n]
else: if args.ilp: import etcetera_ilp # import may take a while. time_start = time.time() solutions = etcetera_ilp.nbest_ilp(obs, indexed_kb, args.depth, args.nbest, args.ilp_verbose, args.ilp_use_cnf, not args.ilp_no_relreason, args.ilp_show_nonetc) else: solutions = etcetera.nbest(obs, kb, indexed_kb, args.depth, args.nbest) logging.info(str(len(solutions)) + " solutions.") logging.info("It took %.2f sec to find the solutions." % (time.time() - time_start)) if args.graph: solution = solutions[args.solution - 1] print(forward.graph(solution, forward.forward(solution, kb), targets=obs), file=args.outfile) else: for solution in solutions: print("; prob=%s\n%s" % (etcetera.jointProbability(solution), parse.display(solution)), file=args.outfile) # To do: enable skolemize as an option