def solve(self): queue = Priorityqueue([self.initial]) i = 0 maxheapsize = 1 h = 1 path = [] found = False a,b,c,d = -1,-1,-1,-1 while len(queue.heap) > 1: if len(self.visited) > 1000000: print "No. of Iterations crossed 10,00,000. Exiting.." sys.exit() curr = queue.popmax() if GlobalVariable.debug: print "iter = {0}, queue = {1}, heur = {2}, depth = {3}".format(i,len(queue.heap), curr.heur, curr.depth) i += 1 h -= 1 if curr.isgoalstate(): path = curr.tracebacks() path.append(curr) a,b,c,d = i, len(path), maxheapsize, len(self.visited) found = True if GlobalVariable.debug: k = 1 print " Solution: In Place Blocks Heuristic " for x in path: print "________________STATE {0}______________________".format(str(k)) for stack in x.vertex.stacks: print "{0}".format(str(stack)) k += 1 print "Iterations: %d" % (i) print "Path Length: %d" % len(path) print "Maximum queue size: %d" % (maxheapsize) print "Number of states visited: %d" % (len(self.visited)) return i, len(path), maxheapsize, len(self.visited), path for x in curr.successors(): if not self.visited.has_key(x.vertex.gethash()): self.visited[x.vertex.gethash()] = x.depth queue.insert(x) h += 1 elif self.visited[x.vertex.gethash()] > x.depth: del self.visited[x.vertex.gethash()] self.visited[x.vertex.gethash()] = x.depth queue.insert(x) if h > maxheapsize: maxheapsize = h #sprint i, maxheapsize, len(self.visited) if GlobalVariable.prun: if len(queue.heap) > 1000: del queue.heap[100:] print "Iterations: %d" % (a+1) print "Path Length: %d" % b print "Maximum queue size: %d" % c print "Number of states visited: %d" % d return a, b, c, d, path
class Solver: def __init__(self, strPathToInputFile): self.strPathToinputFile = strPathToInputFile self.clauses = Clauses(strPathToInputFile) self.clauses.parse() self.candidates = Priorityqueue(self.clauses) self.heapDict = {} def getInitialCandidates(self): for i in range(self.clauses.lastClauseID + 1): for j in range(i+1,self.clauses.lastClauseID + 1): if i != j and self.clauses.isResovable(i,j): self.candidates.insert([i,j]) def solveByResolution(self): self.getInitialCandidates() iter = 1 maxquesize = 0 while (len(self.candidates.heap) > 1): if len(self.candidates.heap)-1 > maxquesize: maxquesize = len(self.candidates.heap) -1 pairToBeResolved = self.candidates.popmax() listOfResolvent = self.clauses.resolvePair(pairToBeResolved) if Global.debug: print "Iteration {0}, queue size {1}, resolution on {2} and {3}".format(iter, len(self.candidates.heap), pairToBeResolved[0], pairToBeResolved[1]) print "Resolving {0} and {1}".format(self.clauses.toString(self.clauses.clauses[pairToBeResolved[0]].value), self.clauses.toString(self.clauses.clauses[pairToBeResolved[1]].value)) for resolvent in listOfResolvent: self.clauses.addClause(resolvent,pairToBeResolved) #Add clause takes care of duplicate clauses if len(resolvent) == 0: print "Maximum Queue size: {0}".format(maxquesize) return True for i in range(self.clauses.lastClauseID): if self.clauses.isResovable(i, self.clauses.lastClauseID): listOfResolvent = self.clauses.resolvePair([i,self.clauses.lastClauseID]) for resolvent in listOfResolvent: key = str(i) + "," + str(self.clauses.lastClauseID) if not (self.clauses.candidateDict.has_key(''.join(resolvent)) and self.heapDict.has_key(key)): if not self.heapDict.has_key(key): self.candidates.insert([i, self.clauses.lastClauseID]) self.heapDict[key] = True #Todo iter += 1 print "Maximum queue size {0}".format(maxquesize) return False def printResult(self,id): self.printPattern(id,0) def printPattern(self,id,depth): clause = self.clauses.clauses[id].value strClause = self.clauses.toString(clause) parent= "" if self.clauses.clauses[id].parentOne == -1: parent = "[input]" else: parent = "{0}".format([self.clauses.clauses[id].parentOne, self.clauses.clauses[id].parentTwo]) if strClause == "": strClause = "[]" for i in range(depth): print " ", print "{0}: {1} {2}".format(id, strClause, parent) if self.clauses.clauses[id].parentOne == -1: return self.printPattern(self.clauses.clauses[id].parentOne, depth + 1) self.printPattern(self.clauses.clauses[id].parentTwo, depth + 1) return