3

I've been working on Berkeley's Pacman project for their A.I. course. I'm running into an issue figuring out how to find a path so that pacman touches all four corners of the pacman board. It uses a general breadth-first search algorithm. It only returns a path when there is one goal state, and not four.

The project for this particular question states:

In corner mazes, there are four dots, one in each corner. Our new search problem is to find the shortest path through the maze that touches all four corners (whether the maze actually has food there or not). Note that for some mazes like tinyCorners, the shortest path does not always go to the closest food first! Hint: the shortest path through tinyCorners takes 28 steps.

Implement the CornersProblem search problem in searchAgents.py. You will need to choose a state representation that encodes all the information necessary to detect whether all four corners have been reached.

BFS:

fringe = util.Queue()
fringe.push( (problem.getStartState(), [], 0) )    
visited = set()

while not fringe.isEmpty():
    curState, curAction, curCost = fringe.pop()

    if curState in visited:
        continue
    visited.add(curState)

    if problem.isGoalState(curState):
        return curAction

    for state, action, cost in problem.getSuccessors(curState):
        fringe.push( (state, curAction + [action], cost ) )

return []

util.raiseNotDefined()

Search Agent Start State:

def getStartState(self):
    """
    Returns the start state (in your state space, not the full Pacman state
    space)
    """
    "*** YOUR CODE HERE ***"
    return self.startingPosition
    util.raiseNotDefined()

SearchAgent Goal State

"""
Returns whether this search state is a goal state of the problem.
"""
"*** YOUR CODE HERE ***"

// TODO
util.raiseNotDefined()

I'm stuck on the returning the goal state so that the test passes. Currently, the function returns:

*** FAIL: test_cases/q5/corner_tiny_corner.test

*** Corners missed: [(1, 1), (1, 6), (6, 1), (6, 6)]

*** Tests failed.

Or I return only 1 out of the 4 corners.

Brandon Essler
  • 710
  • 8
  • 21
Monika
  • 41
  • 3
  • 7

1 Answers1

-1

What defines the goal state? When all 4 corners are touched.

Given that you have all 4 corner coordinates already, you can use a simple Python list to store the corners as you pass the state along in the successor, removing from the list of corners when you reach that state. (The check itself would go in the getSuccessors() function since you pass along a state containing your current position).

Then it should be simple for your goal condition to check this list.

aphrid
  • 589
  • 8
  • 25
  • ```if state in self.corners: self.corners = list(self.corners) self.corners.remove(state) print self.corners print len(self.corners) return len(self.corners) == 0``` – Monika Jan 10 '18 at 18:31
  • I tried the above code to remove the corners from self.corners when pacman reaches the corner coordinates, and then return true/false depending whether len(self.corners) == 0 condition is met outside the if statement. I still get the same error – Monika Jan 10 '18 at 19:51