コード例 #1
0
 def searchtop(self, n=10):
     """Return the top n best resulta (or possibly less if not enough is found)"""
     solutions = PriorityQueue([],
                               lambda x: x.score,
                               self.minimize,
                               length=n,
                               blockworse=False,
                               blockequal=False,
                               duplicates=False)
     for solution in self:
         solutions.append(solution)
     return solutions
コード例 #2
0
    def __iter__(self):
        """Generator yielding *all* valid goalstates it can find"""
        i = 0
        while len(self.fringe) > 0:
            i += 1
            if self.debug:
                print("\t[pynlpl debug] *************** STARTING ROUND #" +
                      str(i) + " ****************",
                      file=stderr)

            b = 0
            #Create a new empty fixed-length priority queue (this implies there will be pruning if more items are offered than it can hold!)
            successors = PriorityQueue([],
                                       lambda x: x.score,
                                       self.minimize,
                                       length=self.beamsize,
                                       blockworse=False,
                                       blockequal=False,
                                       duplicates=self.duplicates)

            while len(self.fringe) > 0:
                b += 1
                if self.debug:
                    print("\t[pynlpl debug] *************** ROUND #" + str(i) +
                          " BEAM# " + str(b) + " ****************",
                          file=stderr)
                #if self.debug: print >>stderr,"\t[pynlpl debug] FRINGE: ", self.fringe

                state = self.poll(self.fringe)()
                if self.debug:
                    try:
                        print("\t[pynlpl debug] CURRENT STATE (depth " +
                              str(state.depth()) + "): " + str(state),
                              end="",
                              file=stderr)
                    except AttributeError:
                        print("\t[pynlpl debug] CURRENT STATE: " + str(state),
                              end="",
                              file=stderr)
                    print(" hash=" + str(hash(state)), file=stderr)
                    try:
                        print(" score=" + str(state.score()), file=stderr)
                    except:
                        pass

                if not self.usememory or (self.usememory and
                                          not hash(state) in self._visited):

                    self.traversed += 1
                    #Evaluate state
                    if state.test(self.goalstates):
                        if self.debug:
                            print("\t[pynlpl debug] Valid goalstate, yielding",
                                  file=stderr)
                        self.solutions += 1  #counts the number of solutions
                        yield state
                    elif self.debug:
                        print("\t[pynlpl debug] (no goalstate, not yielding)",
                              file=stderr)

                    if self.eager:
                        score = state.score()

                    #Expand the specified state and offer to the fringe

                    statecount = offers = 0
                    for j, s in enumerate(state.expand()):
                        statecount += 1
                        if self.debug >= 2:
                            print("\t[pynlpl debug] (Round #" + str(i) +
                                  " Beam #" + str(b) + ") Expanded state #" +
                                  str(j + 1) +
                                  ", offering to successor pool: " + str(s),
                                  end="",
                                  file=stderr)
                            try:
                                print(s.score(), end="", file=stderr)
                            except:
                                print("ERROR SCORING!", end="", file=stderr)
                                pass
                        if not self.maxdepth or s.depth() <= self.maxdepth:
                            if not self.eager:
                                #use all successors (even worse ones than the current state)
                                offers += 1
                                accepted = successors.append(s)
                            else:
                                #use only equal or better successors
                                if s.score() >= score:
                                    offers += 1
                                    accepted = successors.append(s)
                                else:
                                    accepted = False
                            if self.debug >= 2:
                                if accepted:
                                    print(" ACCEPTED", file=stderr)
                                else:
                                    print(" REJECTED", file=stderr)
                        else:
                            if self.debug >= 2:
                                print(" REJECTED, MAXDEPTH EXCEEDED.",
                                      file=stderr)
                            elif self.debug:
                                print(
                                    "\t[pynlpl debug] Not offered to successor pool, maxdepth exceeded",
                                    file=stderr)
                    if self.debug:
                        print("\t[pynlpl debug] Expanded " + str(statecount) +
                              " states, " + str(offers) +
                              " offered to successor pool",
                              file=stderr)
                    if self.keeptraversal: self._traversal.append(state)
                    if self.usememory: self._visited[hash(state)] = True
                    self.prune(
                        state
                    )  #calls prune method (does nothing by default in this search!!!)

                else:
                    if self.debug:
                        print(
                            "\t[pynlpl debug] State already visited before, not expanding again... (hash="
                            + str(hash(state)) + ")",
                            file=stderr)
            #AFTER EXPANDING ALL NODES IN THE FRINGE/BEAM:

            #set fringe for next round
            self.fringe = successors

            #Pruning is implicit, successors was a fixed-size priority queue
            if self.debug:
                print("\t[pynlpl debug] (Round #" + str(i) +
                      ") Implicitly pruned with beamsize " +
                      str(self.beamsize) + "...",
                      file=stderr)
            #self.fringe.prune(self.beamsize)
            if self.debug:
                print(" (" + str(offers) + " to " + str(len(self.fringe)) +
                      " items)",
                      file=stderr)

        if self.debug:
            print("\t[pynlpl debug] Search complete: " + str(self.solutions) +
                  " solution(s), " + str(self.traversed) +
                  " states traversed in " + str(i) + " rounds with " + str(b) +
                  "  beams",
                  file=stderr)
コード例 #3
0
ファイル: search.py プロジェクト: 493238731/pynlpl
    def __iter__(self):
        """Generator yielding *all* valid goalstates it can find"""
        i = 0
        while len(self.fringe) > 0:
            i +=1 
            if self.debug: print("\t[pynlpl debug] *************** STARTING ROUND #" + str(i) + " ****************",file=stderr)
            
            b = 0
            #Create a new empty fixed-length priority queue (this implies there will be pruning if more items are offered than it can hold!)
            successors = PriorityQueue([], lambda x: x.score, self.minimize, length=self.beamsize, blockworse=False, blockequal=False,duplicates= self.duplicates)
            
            while len(self.fringe) > 0:
                b += 1
                if self.debug: print("\t[pynlpl debug] *************** ROUND #" + str(i) + " BEAM# " + str(b) + " ****************",file=stderr)
                #if self.debug: print >>stderr,"\t[pynlpl debug] FRINGE: ", self.fringe

                state = self.poll(self.fringe)()
                if self.debug:
                    try:
                        print("\t[pynlpl debug] CURRENT STATE (depth " + str(state.depth()) + "): " + str(state),end="",file=stderr)
                    except AttributeError:
                        print("\t[pynlpl debug] CURRENT STATE: " + str(state),end="",file=stderr)
                    print(" hash="+str(hash(state)),file=stderr)
                    try:
                        print(" score="+str(state.score()),file=stderr)
                    except:
                        pass


                if not self.usememory or (self.usememory and not hash(state) in self._visited):
                    
                    self.traversed += 1
                    #Evaluate state
                    if state.test(self.goalstates):
                        if self.debug: print("\t[pynlpl debug] Valid goalstate, yielding",file=stderr)
                        self.solutions += 1 #counts the number of solutions
                        yield state
                    elif self.debug:
                        print("\t[pynlpl debug] (no goalstate, not yielding)",file=stderr)

                    if self.eager:
                        score = state.score()                    

                    #Expand the specified state and offer to the fringe
                    
                    statecount = offers = 0
                    for j, s in enumerate(state.expand()):
                        statecount += 1
                        if self.debug >= 2:
                            print("\t[pynlpl debug] (Round #" + str(i) +" Beam #" + str(b) + ") Expanded state #" + str(j+1) + ", offering to successor pool: " + str(s),end="",file=stderr)
                            try:
                                print(s.score(),end="",file=stderr)
                            except:
                                print("ERROR SCORING!",end="",file=stderr)
                                pass
                        if not self.maxdepth or s.depth() <= self.maxdepth:
                            if not self.eager:
                                #use all successors (even worse ones than the current state)
                                offers += 1
                                accepted = successors.append(s)
                            else:
                                #use only equal or better successors
                                if s.score() >= score:
                                    offers += 1
                                    accepted = successors.append(s)
                                else:
                                    accepted = False
                            if self.debug >= 2:
                                if accepted:
                                    print(" ACCEPTED",file=stderr)
                                else:
                                    print(" REJECTED",file=stderr)
                        else:                            
                            if self.debug >= 2:
                                print(" REJECTED, MAXDEPTH EXCEEDED.",file=stderr)
                            elif self.debug:
                                print("\t[pynlpl debug] Not offered to successor pool, maxdepth exceeded",file=stderr)
                    if self.debug:
                        print("\t[pynlpl debug] Expanded " + str(statecount) + " states, " + str(offers) + " offered to successor pool",file=stderr)
                    if self.keeptraversal: self._traversal.append(state)
                    if self.usememory: self._visited[hash(state)] = True
                    self.prune(state) #calls prune method (does nothing by default in this search!!!)

                else:
                    if self.debug:
                        print("\t[pynlpl debug] State already visited before, not expanding again... (hash=" + str(hash(state))  +")",file=stderr)
            #AFTER EXPANDING ALL NODES IN THE FRINGE/BEAM:
            
            #set fringe for next round
            self.fringe = successors

            #Pruning is implicit, successors was a fixed-size priority queue
            if self.debug: 
                print("\t[pynlpl debug] (Round #" + str(i) + ") Implicitly pruned with beamsize " + str(self.beamsize) + "...",file=stderr)
            #self.fringe.prune(self.beamsize)
            if self.debug: print(" (" + str(offers) + " to " + str(len(self.fringe)) + " items)",file=stderr)
        
        if self.debug:
            print("\t[pynlpl debug] Search complete: " + str(self.solutions) + " solution(s), " + str(self.traversed) + " states traversed in " + str(i) + " rounds with " + str(b) + "  beams",file=stderr)            
コード例 #4
0
ファイル: search.py プロジェクト: 493238731/pynlpl
 def searchtop(self,n=10):
     """Return the top n best resulta (or possibly less if not enough is found)"""            
     solutions = PriorityQueue([], lambda x: x.score, self.minimize, length=n, blockworse=False, blockequal=False,duplicates=False)
     for solution in self:
         solutions.append(solution)
     return solutions