Lab Exercise 3: Breadth- and Depth-First Search
This lab exercise builds upon the previous one with the Bot Environment. Please get the latest version of CPE480-Lab3-BFSearch.zip from PolyLearn.
In this exercise, you need to systematically explore the environment by employing the
breadth-first and the depth-first search algorithms. This algorithm looks at the potential successor nodes of the current node, and selects the next node to be explored. In the case of our Bot Environment, a grid element corresponds to one node, and only those nodes that the agent can actually enter need to be considered.
The exploration of these nodes can be depicted as a
search tree, with the starting node as the root, the reachable grid elements as successor nodes, and nodes that have no successors as leaf nodes. The search is completed when either a specially marked node, the goal, is encountered, or when all reachable options have been exhausted.
Search Strategies
In order to perform a systematic search, the two fundamental strategies are breadth-first and depth-first. In addition, the agent needs to select among nodes at the same level in a systematic manner. The most frequently used selections here are left-to-right or right-to-left with respect to the way the nodes are listed in a graphical view. Depending on the type of the nodes, there is also often an obvious ordering, such as alphabetical or numerical. In this task, use a clockwise ordering starting from the "North" position (i.e. in the order North, East, South, West).
Breadth-First Search (BFS) Algorithm
In this strategy, all direct successor nodes of a given level are explored first, and then the successor nodes of the first node at this level are examined. In the tree, this means that all nodes at a given level are explored before proceeding to the next level.
Depth-First Search (DFS) Algorithm
This search strategy picks the first successor node of the current node, then the first successor node of that node, and so on. In the tree, this means that all nodes of a branch are explored until a leaf node is reached. Once a leaf node is reached, the algorithm "backtracks" (goes up one level), and selects the next node at that level as the new starting point of a sub-branch.
Tasks
Your task is to implement one agent each for the breadth-first strategy and the depth-first search strategy. For both search methods, the agent should first construct a path to the goal, and then carry out this path. Once the agent reaches the goal, print out the search cost and path cost to the logging window.
Search in the Bot Environment
In this lab, you will use the BotEnvironment, (similar to the Wumpus Environment). A key difference between the two environments is that instead of defining the step() function, you should define a searchStep() function and a movementStep() function.
To understand how this is implemented, note that in the
BotSearch class which your agent extends, the following function is called by clicking the step button:
public void step() {

if (!goalFound) {

searchStep();
}
else if (!getGoalReached()) {

movementStep();
}
}
Your searchStep() function should implement the Breadth First Search and the Depth First Search algorithms to find a path to the goal. The movementStep() function should carry out this path. You can test out yoPolyLearnur path planning and path following using the environment CPE480-Lab3-BFSearch.sbm. Note that the grader will use other maps as well to determine if your agent works.
Start by downloading the base code
CPE480-Lab3-BFSearch.zip from PolyLearn. A sample Windows-based agent compile batch file and run batch file are provided. Similar command lines can be used in Linux:
javac -classpath BotEnvironment.jar:.: ./Agents/MySearchAgent.java
java -classpath .:BotEnvironment.jar BotEnvironment.BotEnvironment
Here are some hints for programming the BFS and DFS algorithms:
Logging
You can use the log() function to print out debugging information. For example:
log("Adding east node " + eastNode.getX() + "," + eastNode.getY());
Resetting
You can reset all your variables in the function reset() that is called whenever the Map Reset drop-down menu is selected.
Node Class
The class Node has been implemented where each cell in the environment grid is represented by a node. These nodes can be used with the following functions:
getIsWall() Returns true if the node is a wall, false otherwise.

getIsEvaluated() Returns true if the node has been evaluated by the search, false otherwise.
A node is deemed evaluated if the search has moved its location to it at some time.
getX(), getY() Returns the integer x or y coordinate of the node in the map.

getIsGoal() Returns true if the node is a goal, false otherwise.

Search Functions
The following functions can be used for an offline search. Note that there is a difference between the bot's location (or actual location in the environment) versus the current location of the search node in the state space. This location is depicted with a "fairy" during the search.
getGoalFound() Returns true if the search has found the goal by the search, false otherwise.

getSearchLocation() Returns the Node object that the search is occupying. From here, you can use the methods in the Node class to find out more about this node and adjacent nodes.

getBotLocation() Returns the Node object that the agent is occupying. From here, the programmer can use the methods in the Node class to find out more about this node and adjacent nodes.

moveSearchLocation(Node n) Moves the search location to the Node object given. This is most usefully coupled with the Node returned from getNextFringeNode().

getNorthOfSearchLocation() Returns the corresponding Node objects adjacent to the search location.

getEastOfSearchLocation()

getSouthOfSearchLocation()

getWestOfSearchLocation()

getSearchCost() Returns the total integer searching cost, where the cost to view a node is defined in sbcproperties.dat.

getMovementCost() Returns the total integer movement cost, where moving and turning costs are defined in sbcproperties.dat.

SBConstants.NORTH, SBConstants.EAST, SBConstants.SOUTH, SBConstants.WEST Defined constants that can be used for directions.

Fringe
A variable fringe has been defined which is a linked list of nodes that works as a First In First Out (FIFO) stack.
addToFringe(Node n) Adds the Node object to the end of the fringe.

getNextFringeNode() Returns the first Node object in the fringe.

fringeContains(Node n) Returns true if the Node object is in the fringe, false otherwise.

For moving the bot in the moveStep() function, you can use turnTo(int direction) and moveForward() as in the previous lab.
More information on how to use the environment can be found by following the link to the
bot search tutorial. If you have problems with it, post your questions on the PolyLearn discussion forum.
Approach
I strongly recommend to look at the way the different search algorithms are developed from a generic search method in the textbook (this is also shown on the PowerPoint slides). Using this approach, it is fairly straightforward to modify one agent (e.g. breadth-first) in order to obtain an agent that behaves differently (e.g. depth-first). It is probably a little more difficult to understand and implement the first one this way. The alternative is to develop each agent separately from scratch, causing considerably more work for additional agents.
Administrative Aspects
Assignment Submission
This assignment must be submitted electronically via PolyLearn through the "Lab and Assignment Submission" menu entry by the deadline specified above.
You need to submit the following items:
  • observations about the behavior of your agents via a Web form
  • a plain text file (not a MS Word document) named README.txt with your name, a brief explanation of your program, and instructions for running your program
  • the Java source code for two agents implementing breadth-first and depth-first search
  • the Java executable (class file) for your agents
  • optionally additional files that may be relevant for the grading (e.g. make files, log files, or special environments you used

Collaboration
This is a "pair" assignment, and you can either do it individually, or together with one other student. Each pair must figure out and formulate their own answers, and write their own programs. It is fine with me to discuss general aspects of this lab with others (e.g. general aspects of the different search strategies).
Questions about the Assignment
If you have general questions or comments concerning the programming aspects of the homework, post them on the PolyLearn Discussion Forum for the assignment. The grader and I will check that forum on a regular basis, and try to answer your questions. If you know the answer to a support or clarification question posted by somebody else, feel free to answer it; this will count as extra participation credit.