Ejemplo n.º 1
0
def test_bfs():
    from numpy import array
    from search import best_first_graph_search, manhattan_dist
    from environment import Lightworld, expand_state
    maze_string2 = """\
111111111
1  1    1
1  1  1 1
1  1  1 1
1  1 11 1
1     1 1
1111111 1
1       1
111111111"""

    def arr_from_str(string):
        return array([[1 if z=='1' else 0 for z in l] for l in 
            string.split('\n')])

    structure = arr_from_str(maze_string2)
    h = len(structure[0])
    w = len(structure)
    print structure

    goal = (w-2, h-2)
    expand_state_partial = lambda s: expand_state(structure, s)
    manhattan_dist_partial = lambda s: manhattan_dist(s, goal)
    print best_first_graph_search((1,1), goal, manhattan_dist_partial, expand_state_partial)
Ejemplo n.º 2
0
 def choose_action(self, env, state):
     if self.room != state.r:
         self.room = state.r
         self.plan = None
     pos = (state.x, state.y)
     if self.plan is None or pos not in self.plan:
         priority_func = lambda s: manhattan_dist(s, env.goal)
         expand_partial = lambda s: expand_state(env.states, s)
         self.plan = search.best_first_graph_search(pos, env.goal, priority_func, expand_partial)
     for i, pathpos in enumerate(self.plan):
         if i == len(self.plan)-1:
             #print 'choose random option in door plan'
             return np.array([choice(env.actions)])
         elif pos == pathpos:
             fx,fy = self.plan[i+1]
             dx,dy = (fx-state.x, fy-state.y)
             #print 'move in direction',dx,dy,'for door plan'
             return np.array([env.movemap[dx,dy]])
Ejemplo n.º 3
0
 def choose_action_parameterized(self, env, state, field, action):
     if self.room != state.r:
         self.room = state.r
         self.plan = None
     pos = (state.x, state.y)
     if self.plan is None or pos not in self.plan:
         key_pos = filter_states(env.states, field)
         # Not positive if this is right move here.
         if not key_pos:
             #print 'RANDOM action for plan', action
             return np.array([choice(env.actions)])
         priority_func = lambda s: manhattan_dist(s, key_pos[0])
         expand_partial = lambda s: expand_state(env.states, s)
         self.plan = search.best_first_graph_search(pos, key_pos[0], priority_func, expand_partial)
     for i, pathpos in enumerate(self.plan):
         if i == len(self.plan)-1:
             #print 'action', action, 'for plan', field
             return np.array([action])
         elif pos == pathpos:
             fx,fy = self.plan[i+1]
             dx,dy = (fx-state.x, fy-state.y)
             #print 'move', dx,dy, 'for plan', field
             return np.array([env.movemap[dx,dy]])