Exemplo n.º 1
0
 def neighbors(self, node):
     """enumerates the neighbors of node"""
     self.display(3,"finding neighbors of\n",node)
     if node.agenda:
         subgoal,act1 = node.agenda[0]
         self.display(2,"selecting",subgoal,"for",act1)
         new_agenda = node.agenda[1:]
         for act0 in node.actions:
             if (self.achieves(act0, subgoal) and 
                self.possible((act0,act1),node.constraints)):
                 self.display(2,"  reusing",act0)
                 consts1 = self.add_constraint((act0,act1),node.constraints)
                 new_clink = (act0,subgoal,act1)
                 new_cls = node.causal_links + [new_clink]
                 for consts2 in self.protect_cl_for_actions(node.actions,consts1,new_clink):
                     yield Arc(node, 
                               POP_node(node.actions,consts2,new_agenda,new_cls), 
                               cost=0)
         for a0 in self.planning_problem.prob_domain.strips_map:  #a0 is an action
             if self.achieves(a0, subgoal):
                 #a0 acheieves subgoal
                 new_a = Action_instance(a0)
                 self.display(2,"  using new action",new_a)
                 new_actions = node.actions + [new_a]
                 consts1 = self.add_constraint((self.start,new_a),node.constraints)
                 consts2 = self.add_constraint((new_a,act1),consts1)
                 preconds = self.planning_problem.prob_domain.strips_map[a0].preconditions
                 new_agenda = new_agenda + [(pre,new_a) for pre in preconds.items()]
                 new_clink = (new_a,subgoal,act1)
                 new_cls = node.causal_links + [new_clink]
                 for consts3 in self.protect_all_cls(node.causal_links,new_a,consts2):
                     for consts4 in self.protect_cl_for_actions(node.actions,consts3,new_clink):
                         yield Arc(node,
                                   POP_node(new_actions,consts4,new_agenda,new_cls),
                                   cost=1)
Exemplo n.º 2
0
 def neighbors(self,node):
     """returns a list of the arcs for the neighbors of node"""
     
     #TODO
     if (node == ("Farmer","hen","fox","grain")):
     	return [Arc(("Farmer","hen","fox","grain"),("fox","grain"),1,'Farmer takes hen to right side')]
     	#return ("fox","grain")
     elif (node == ("fox","grain")):
     	return [Arc(("fox","grain"),("Farmer","fox","grain"),1,'Farmer back to left side')]
     	#return ("Farmer","fox","grain")
     elif (node == ("Farmer","fox","grain")):
     	return [Arc(("Farmer","fox","grain"),("fox"),1,'Farmer takes grain to RS'),Arc(("Farmer","fox","grain"),("grain"),6,'Farmer takes fox to RS')]
     	#return {("fox"),("grain")}
     	#Arc(("Farmer","fox","grain"),("grain"),8)
     	#Arc(("Farmer","fox","grain"),("fox"),8)
     elif (node == ("fox")):
     	return [Arc(("fox"),("Farmer","hen","fox"),1,'Farmer takes hen back to LS')]
     	#return ("Farmer","hen","fox")
     elif (node == ("grain")):
     	return [Arc(("grain"),("Farmer","hen","grain"),1,'Farmer takes hen back to LS')]
     	#return ("Farmer","hen","grain")
     elif (node == ("Farmer","hen","fox")):
     	return [Arc(("Farmer","hen","fox"),("hen"),1,'Farmer take fox to RS')]
     	#return ("hen")
     elif (node == ("Farmer","hen","grain")):
     	return  [Arc(("Farmer","hen","grain"),("hen"),1,'Farmer take grain to RS')]
     	#return ("hen")
     elif (node == ("hen")):
     	return [Arc(("hen"),("Farmer","hen"),2,'Farmer back to LS')]
     	#return ("Farmer","hen")
     elif (node == ("Farmer","hen")):
     	return [Arc(("Farmer","hen"),("nothing on LS"),2,'Farmer take hen to RS')]
Exemplo n.º 3
0
 def neighbors(self, node):
     """returns a list of the neighbors of node.
        note that the neighbors are arcs.
     """
     ((x, y), c) = node
     return [
         Arc(node,
             (pos, True)) if self.at_coffee(pos) else Arc(node, (pos, c))
         for pos in ((x + dx, y + dy)
                     for (dx, dy) in [(-1, 0), (1, 0), (0, 1), (0, -1)])
         if self.legal(pos)
     ]
Exemplo n.º 4
0
 def neighbors(self, state):
     """returns neighbors of state in this problem"""
     return [
         Arc(state, self.effect(act, state.assignment), act.cost, act)
         for act in self.prob_domain.actions
         if self.possible(act, state.assignment)
     ]
Exemplo n.º 5
0
 def neighbors(self,subgoal):
     """returns a list of the arcs for the neighbors of subgoal in this problem"""
     cost = 1
     goal_asst = subgoal.assignment
     return [ Arc(subgoal,self.weakest_precond(act,goal_asst),cost,act)
              for act in self.prob_domain.actions
              if self.possible(act,goal_asst)]
Exemplo n.º 6
0
 def result(self, path1, path2):
 	current = path2
 	result = path1
 	while current.arc != None:
 		result = Path(result, Arc(current.arc.to_node, current.arc.from_node, 1, current.arc.action))
 		current = current.initial
 	#print(result)
 	return result
Exemplo n.º 7
0
 def neighbors(self, node):
     neighbours = list()
     arcs = list()
     cost = 1
     for i in range(self.dim):
         for j in range(self.dim):
             new_node = copy.deepcopy(node)
             self.flip_tile_set(i, j, new_node)
             arcs.append(Arc(node, new_node, cost))
     return arcs
Exemplo n.º 8
0
 def neighbors(self, node):
     """returns a list of the neighboring nodes of node.
     """
     var = self.variables[len(node)]  # the next variable
     res = []
     for val in self.csp.domains[var]:
         new_env = dict_union(node, {var: val})  #dictionary union
         if self.csp.consistent(new_env):
             res.append(Arc(node, new_env))
     return res
Exemplo n.º 9
0
 def neighbors(self, node):
     """
     Given a node, return a sequence of Arcs usable
     from this node. 
     """
     ACTIONS = tuple(('U', 'D', 'L', 'R'))
     lst = []
     for action in ACTIONS:
         next_pos = next_position(node, action)
         if Board.legal_position(self.board, next_pos):
             lst.append(Arc(node, next_pos, 1, action))
     return lst
Exemplo n.º 10
0
 def neighbors(self, node):
     """
   Given a node, return a sequence of Arcs usable
   from this node. 
   """
     arcs = []
     ACTIONS = tuple(('U', 'D', 'L', 'R'))
     for action in ACTIONS:
         next_pos = next_position(node, action)
         if Board.legal_position(self.board, next_pos):
             newArc = Arc(node, next_pos, cost=1, action=action)
             arcs.append(newArc)
     return arcs
Exemplo n.º 11
0
 def neighbors(self, node):
     """
     Given a node, return a sequence of Arcs usable
     from this node. 
     """
     arcs = []
     ACTIONS = tuple(('U', 'D', 'L', 'R'))
     for action in ACTIONS:
         neigh = bloxorz.next_position(node, action)
         if self.board.legal_position(neigh):
             arc = Arc(node, neigh, 1, action)
             arcs.append(arc)
     return arcs
Exemplo n.º 12
0
    def neighbors(self,node):
        """returns a list of the arcs for the neighbors of node"""
        
        valid_arcs = []
        temp_set = set("R")

        # Build valid_arcs list and return.

        if "R" in node.L:
            
            # Add the option of just moving the cart if it creates a valid state.
            temp_node = River_Node(node.L - temp_set, node.R.union(temp_set))
            if self.is_valid_node(temp_node):
                valid_arcs.append(Arc(node, temp_node, 1, "MOVE RAFT TO RIGHT"))

            # For each 
            for item in (node.L - set("R")):
                new_temp_set  = temp_set.union(set(item))
                new_temp_node = River_Node(node.L - new_temp_set, node.R.union(new_temp_set))

                if self.is_valid_node(new_temp_node):
                    valid_arcs.append(Arc(node, new_temp_node, 1, "PICKUP {0} AND MOVE TO RIGHT".format(item)))

        if "R" in node.R:

            temp_node = River_Node(node.L.union(temp_set), node.R - temp_set)
            if self.is_valid_node(temp_node):
                valid_arcs.append(Arc(node, temp_node, 1, "MOVE RAFT TO LEFT"))

            for item in (node.R - set("R")):
                new_temp_set  = temp_set.union(set(item))
                new_temp_node = River_Node(node.L.union(new_temp_set), node.R - new_temp_set)
                
                if self.is_valid_node(new_temp_node):
                    valid_arcs.append(Arc(node, new_temp_node, 1, "PICKUP {0} AND MOVE TO LEFT".format(item)))

        return valid_arcs
Exemplo n.º 13
0
    def neighbors(self,node):
      """
        Given a node, return a sequence of Arcs usable
        from this node. 
        """
      arc_output=[]
      Actions=tuple(("U","D","L","R"))
      #get new position from board class and check if that is legal or not 
      #and if that's  a legal add it to the arc list

      for a in Actions:
        next_poslist=next_position(node, a)
        if self.board.legal_position(next_poslist):
          arc=Arc(node, next_poslist, cost=1, action=a)
          arc_output.append(arc)
          
      return (arc_output)
Exemplo n.º 14
0
    def neighbors(self, node, forward=True):
        """
        Given a node, return a sequence of Arcs usable
        from this node.

        append list of arcs that are possible, use already built code in bloxorz.py
        as well as look at what is "possible"
        """
        arcs = []

        ACTIONS = tuple(('U', 'D', 'L', 'R'))
        for x in range(0, 4):
            new_node = next_position(node, action=ACTIONS[x], forward=True)
            if self.board.legal_position(new_node):
                new_arc = Arc(node, new_node)
                arcs.append(new_arc)

        return arcs
Exemplo n.º 15
0
 def neighbors(self,node):
     """returns the neighboring nodes of node.
     """
     neighs = []
     var = select(x for x in node if len(node[x])>1)
     if var:
         dom1, dom2 = partition_domain(node[var])
         self.display(2,"Splitting", var, "into", dom1, "and", dom2)
         to_do = self.cons.new_to_do(var,None)
         for dom in [dom1,dom2]:
             newdoms = copy_with_assign(node,var,dom)
             cons_doms = self.cons.make_arc_consistent(newdoms,to_do)
             if all(len(cons_doms[v])>0 for v in cons_doms):
                 # all domains are non-empty
                 neighs.append(Arc(node,cons_doms))
             else:
                 self.display(2,"...",var,"in",dom,"has no solution")
     return neighs
Exemplo n.º 16
0
    def neighbors(self, node, reverse_direction=False):
        """
        Given a node, return a sequence of Arcs usable
        from this node. 
        """
        #sequence of arcs
        seq = []
        #Actions
        ACTIONS = tuple(('U', 'D', 'L', 'R'))
        #Get next neighbor from current node position
        for item in ACTIONS:
            #print(item)
            neighbor = next_position(node, item, reverse_direction)

            #check if is a legal position on board
            if self.board.legal_position(neighbor):
                #Make arc
                arc = Arc(node, neighbor, 1, item)
                #print(neighbor)
                seq.append(arc)
        #return
        return seq
Exemplo n.º 17
0
    def neighbors(self,node):
        """returns a list of the arcs for the neighbors of node"""
        chicken = node[0]
        fox = node[1]
        grain = node[2]
        raft = node[3]
        neighbours = []
        if chicken == raft:
            if chicken == '0':
                if self.isValidNode('1'+ fox + grain + '1'):
                    neighbours.append(Arc(node, '1' + fox + grain + '1', 1))
            else:
                if self.isValidNode('0' + fox + grain + '0'):
                    neighbours.append(Arc(node, '0' + fox + grain + '0', 1))
        if fox == raft:
            if fox == '0':
                if self.isValidNode(chicken + '1' + grain + '1'):
                    neighbours.append(Arc(node, chicken + '1' + grain + '1',1))
            else:
                if self.isValidNode(chicken + '0' + grain + '0'):
                    neighbours.append(Arc(node, chicken + '0' + grain + '0',1))
        if grain == raft:
            if grain == '0':
                if self.isValidNode(chicken + fox + '1' + '1'):
                    neighbours.append(Arc(node, chicken + fox + '1' + '1', 1))
            else:
                if self.isValidNode(chicken + fox + '0' + '0'):
                    neighbours.append(Arc(node, chicken + fox + '0' + '0', 1))
        if raft == '0':
            if self.isValidNode(chicken + fox + grain + '1'):
                neighbours.append(Arc(node, chicken + fox + grain + '1', 1))
        else:
            if self.isValidNode(chicken + fox + grain + '0'):
                neighbours.append(Arc(node, chicken + fox + grain + '0', 1))

        return neighbours
Exemplo n.º 18
0
    def neighbors(self, node):
        """returns a list of the arcs for the neighbors of node"""

        if "Farmer" in node[0]:
            index = 0
        else:
            index = 1

        neighbors = []
        for item in node[index]:
            new_opposite = node[1 - index] + [item]
            new_origin = node[index][:]
            new_origin.remove(item)

            if item != "Farmer":
                new_opposite.append("Farmer")
                new_origin.remove("Farmer")

            if index == 0:
                new_node = (new_origin, new_opposite)
            else:
                new_node = (new_opposite, new_origin)

            # invalid situations
            if "Hen" in new_origin and ("Fox" in new_origin
                                        or "Grain" in new_origin):
                continue

            if item == "Farmer":
                action = "Move along"
            else:
                action = "Move " + item

            neighbors.append(Arc(node, new_node, 1, action))

        return neighbors
Exemplo n.º 19
0
    def start_node(self):
        """returns the start node for the search
        """

        return Arc({}, {}, 0)