0

I have a problem that I need to use DFS to solve. This is my function so far and according to the autograder I'm provided with, it works on 4/5 tests and only fails at backtracking situations:

def depthFirstSearch(problem):

    stack=Stack()
    if problem.isGoalState(start):
    return actions
while stack:
    parent=stack.pop()
    if flag==1:
            action=actionStack.pop()
    if parent in visited: continue
    visited.add(parent)
    if problem.isGoalState(parent):
                    while parent!=None:
                            actions.append(action)
                            parent=parentMap[parent]
                            action=actionMap[parent]
                    return actions
    children=problem.getSuccessors(parent)
    for child in children:
            stack.push(child[0])
            actionStack.push(child[1])
            parentMap[child]=parent
            if flag==1:
                    actionMap[child] = child[1]
    flag=1
util.raiseNotDefined()

getSuccessors returns a list of triples (state, action, cost) and I need to return a list of actions to guide an agent from the start to a goal. Sorry in advance, I am new to python. Any hints?

edit: This is the tree that it fails at

FAIL: test_cases/q1/graph_backtrack.test graph:

     B   
     ^
     |
    *A --> C --> G
     |
     V
     D

    A is the start state, G is the goal.  Arrows mark 
    possible state transitions.  This tests whether
    you extract the sequence of actions correctly even
    if your search backtracks.  If you fail this, your
    nodes are not correctly tracking the sequences of
    actions required to reach them.
student solution:       ['2:A->D', '1:A->C', '0:C->G']
student expanded_states:    ['A', 'D', 'C']

correct solution:       ['1:A->C', '0:C->G']
correct expanded_states:    ['A', 'D', 'C']
correct rev_solution:       ['1:A->C', '0:C->G']
correct rev_expanded_states:    ['A', 'B', 'C']

1 Answers1

1

you have only one set of actions. not an actions per step. if you were travelling below going to 7 then your actions would be 1,2,3,4,5,6,7 when it should be 1,2,3,6,7

1-2-3-4-5
0-0-6-0-0
0-0-7-0-0

Don't be afraid to package the current state with each next move. unless you have a ridiculously large solution space you should be fine.

Ajurna
  • 561
  • 3
  • 16
  • I'm not sure i understood that, sorry. I extend my actions list on each parent node i expand (that is not visited). – Δημήτρης Μπακίρης Nov 03 '16 at 15:38
  • yes but it is global to all actions ever performed. you should have a list of actions that accompanies each current position that shows how you got there. that way when you start pruning dead paths you aren't bringing that data with you. – Ajurna Nov 03 '16 at 15:40
  • oh i see you are talking about something similar to this i think? http://stackoverflow.com/questions/12864004/tracing-and-returning-a-path-in-depth-first-search i wasnt sure how to implement it the first time i saw it – Δημήτρης Μπακίρης Nov 03 '16 at 15:43
  • yes, best to use tuple of (actions, next_action) or whatever you feel more comfortable with – Ajurna Nov 03 '16 at 15:46
  • if you could mark it as answered that would be great :) – Ajurna Nov 03 '16 at 15:55
  • i feel really stupid for not being able to do it even after your explanation.. i did it with dictionaries but it doesnt work.. i will edit what i changed and will surely mark it as answered when i manage to do it.. – Δημήτρης Μπακίρης Nov 03 '16 at 16:52
  • be careful using lists as they often pass refernce, your path should be in a tuple becasue it is immutable. on your stack.push it should be (path, child). – Ajurna Nov 03 '16 at 17:28
  • http://stackoverflow.com/questions/40439810/bfs-and-ucs-algorithms-my-bfs-implementation-works-but-my-ucs-doesnt-cant-fi i need you over here :( – Δημήτρης Μπακίρης Nov 05 '16 at 16:37