Exemplo n.º 1
0
    def __init__(self,h):
        self.problem = BlocksWorld()

        # start with an empty plan
        self.plan = Plan()

        # h is a "heuristic" object
        self.h = h
Exemplo n.º 2
0
    def __init__(self,h):
        self.problem = BlocksWorld()

        # start with an empty plan
        self.plan = Plan()

        # h is a "heuristic" object
        self.h=h
Exemplo n.º 3
0
class ForwardSearch:
    def __init__(self,h):
        self.problem = BlocksWorld()

        # start with an empty plan
        self.plan = Plan()

        # h is a "heuristic" object
        self.h = h
    def search(self):
        import heapq
        from state import State
        from copy import deepcopy

        def Heuristic(state, problem):
        	return self.h.heuristic(state, problem)

        class PriorityQueue:
            def  __init__(self):
                self.heap = []
                self.count = 0

            def push(self, item, priority):
                # FIXME: restored old behaviour to check against old results better
                # FIXED: restored to stable behaviour
                entry = (priority, self.count, item)
                # entry = (priority, item)
                heapq.heappush(self.heap, entry)
                self.count += 1

            def pop(self):
                (_, _, item) = heapq.heappop(self.heap)
                #  (_, item) = heapq.heappop(self.heap)
                return item

            def isEmpty(self):
                return len(self.heap) == 0
                
        class AllActions():
	        def __init__(self):
		        self.actions = []
	        def addAction(self, action):
		        self.actions.append(action)
		        return self
	        def getActions(self):
		        return self.actions

        def getSuccessors(arg):
            """ now arg is a tuple"""
            actions = self.problem.getActions()
            successors = []
            for action in actions:
            	state = State(list(arg))
                precondition = action.getPreconditions()
                if state.entails(State(precondition)):
                    addList = action.getAddList()
                    deleteList = action.getDeleteList()
                    stateLiterals = state.getLiterals()
                    # delete literal from stateLiterals
                    for l1 in stateLiterals:
                    	for l2 in deleteList:
                    		if l2.equals(l1):
                    			stateLiterals.remove(l1)
                    			break
                    # add literal into stateLiterals
                    for l3 in addList:
                    	for l1 in stateLiterals:
                    		if l1.equals(l3):
                    			break
                    	else:
                    		stateLiterals.append(l3)

                    succState = tuple(stateLiterals)
                    successors.append((succState, action, 1, Heuristic(succState, self.problem)))   
            return successors

        def visitedState(state):
        	for k in Closed.keys():
        		visitedState = State(list(k))
        		if visitedState.equals(State(list(state))):
        			# Closed[k] is totalCost of state k in Closed
        			return (True, Closed[k])
        	return (False, None)

        startState = tuple(self.problem.getInitialState().getLiterals())
        Actions = AllActions()

        fringe, Closed = PriorityQueue(), dict()
        fringe.push((startState, Actions, 0, Heuristic(startState, self.problem)), 0 + Heuristic(startState, self.problem))

        while not fringe.isEmpty():
            toExpandState = fringe.pop()

            if self.problem.reachedGoal(State(toExpandState[0])):
                actions = toExpandState[1]
                break

            Closed[toExpandState[0]] = toExpandState[2] + Heuristic(toExpandState[0], self.problem)
            
            for triple in getSuccessors(toExpandState[0]):
                stepCost = toExpandState[2] + triple[2]
                currentTotalCost = stepCost + Heuristic(triple[0], self.problem)
                
                _visited, _cost = visitedState(triple[0])
                if _visited and currentTotalCost >= _cost:
                	continue

                triple = (triple[0], deepcopy(toExpandState[1]).addAction(triple[1]), stepCost, currentTotalCost)
                fringe.push(triple, triple[3])
                Closed[triple[0]] = triple[3]

        self.plan.actions = actions.getActions()
        # return the final plan
        return self.plan