FJK Home CPE/CSC 480 Syllabus Schedule Lecture Notes Assignments Labs Project Teams
CPE/CSC 480 Artificial Intelligence Fall 2009

CPE/CSC 480-F09 Artificial Intelligence Bot Search Tutorial

Making an Agent for the Bot Environment

The Bot Environment is designed for easy creation of agents. This tutorial will instruct how to begin creating an agent by giving some information about useful methods and classes to use.

Creating the Agent

All agents for the Bot Environment must be part of the "Agents" package. In addition, they must import the base classes for agent control as well. To do this, place the following lines of code at the top of your agent's Java file:

package Agents;
import BotEnvironment.SearchBot.*;

Also, make sure your agent extends the Bot base class:

public class MyAgent extends Bot

The step Method

Whenever the agent performs some action, it is considered a "step." This could be anything from final actions, such as making a calculated movement, or individual actions, such as analyzing a node, making a turn, or making a movement. How much that is done in a step is up to the programmer, but the simpler the action per step, the clearer the agent's actions are when viewed. Keep in mind that while movement / turning costs and searching costs are accrued when the agent does certain things, there is no difference in costs between if the agent does a large amount of actions in one step and if the agent splits their actions over several steps.

When programming the agent, place all computation in the step() method, and the GUI will call this method to determine what the agent sees and where it moves to. Try to make each step action as modular as possible so that the agent's actions are clearly understandable (in other words, do not attempt to find the map goal all in one step, but instead, take it one node at a time).

Moving the Agent

Now that the agent is set up, you can begin using motion controls on the agent. The basic controls for the agent are forward motion and turning. They can be invoked with the following methods:

moveForward()
Moves the agent one tile forward in the current direction it is facing.
turnLeft()
Turns the agent 90 degrees left from the current direction it is facing.
turnRight()
Turns the agent 90 degrees right from the current direction it is facing.
turnTo(int)
Turns the agent to the direction given, where the integer values for north, east, south, and west are defined in the SBConstants class.

Each time the agent moves and turns, movement and turning costs are accrued which are used in some applications, such as comparing efficiency between agents.

Other Agent Methods

In order to intelligently manuever through a map, the agent will need to analyze its surroundings and determine what to do next, or even know if it has found the goal. In addition, it may want to let the user know what it is thinking at a given time. Finally, it may be needed to know how much it cost to travel the map. The following methods are helpful for these tasks:

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.
getGoalReached()
Returns true if the agent has reached the goal of the map, false otherwise.
log(String)
Logs a message, which appears in the Agent Log in the GUI. This can be used to let the user know why the agent is performing a certain task, to display the state of its knowledge of the map, etc. The log can be saved as well by the GUI.
getMovementCost()
Returns the total integer movement cost, where moving and turning costs are defined in sbcproperties.dat.

Also, when a map is opened or reset, the reset() method is called in the agent. If the programmer has added any variables or settings to the agent, they should be reset in this method, making sure to call super.reset() first.

Learning About the Environment

In order to find the goal intelligently, the agent needs to understand its environment. It needs to know what paths it can take, what the cost is of traveling through the map, and whether it has passed this way before. The following methods can be used to learn about a particular Node object:

getIsWall()
Returns true if the node is a wall, false otherwise.
getIsGoal()
Returns true if the node is a goal, false otherwise.
getCost()
Returns the integer additional cost it takes to travel through the node.
getX(), getY()
Returns the integer x or y coordinate of the node in the map.
getNorth(),
getEast(),
getSouth(),
getWest()
Returns the corresponding adjacent Node objects to the agent's location.
getIsViewed()
Returns true if the node has ever been seen (i.e. the agent has called getNorth(), getEast(), etc. to get the node), false otherwise.
getIsTraveled()
Returns true if the agent has traveled on the node before, false otherwise.
getBotIsHere()
Returns true if the agent is currently on this node, false otherwise.

In addition to these, an agent can ask for hints on where the goal is. The SBFunctions class has a few static methods to give a hint on where the goal is:

getDistanceToGoal(Node)
Returns the integer number of nodes away the goal is from the given node, disregarding walls or additional costs.
getDirectionOfGoal(Node)
Returns the integer direction of where the goal is, where the integer values for north, east, south, and west are defined in the SBConstants class.

In some cases, the instructor may ask the programmer to either not use hints or to add an additional cost value to the total costs of movement and searching for each hint used. Before using these hints in an assignment, please ask the instructor whether they are allowed to be used.

Search Algorithm Agents

A special case of agents are those that use searching algorithms to find the goal. The Bot Environment allows for the agent to perform an off-line search, or a search in which the agent searches for and finds the goal before it begins moving. To create a search algorithm agent, extend the BotSearch class instead of the Bot class. The BotSearch class is a base class for search algorithm agents and it is an extension of the Bot class, so it has all the functionality as stated before.

When using a search algorithm agent, the agent will need to use a fringe, or a list of nodes that are to be evaluated. Depending on the search algorithm, these nodes may be ordered differently. The fringe in the Bot Environment is saved as a LinkedList object, and the following are several methods in the BotSearch class that are available to manage and use the fringe:

addToFringe(Node)
Adds the Node object to the end of the fringe.
addToFringe(Node, int)
Adds the Node object to the fringe at the specified index.
sortFringe(Comparator)
Sorts the fringe by the criteria in the Comparator object. In order to use this, the programmer must write a class that implements the Comparator interface and state how to determine the order in which to list nodes using the compare(Object, Object) method. In addition, since the agent is in the "Agents" package, the Comparator needs to be, too.
fringeContains(Node)
Returns true if the Node object is in the fringe, false otherwise.
getNextFringeNode()
Returns the first Node object in the fringe.
getFringeString()
Returns the contents of the fringe as a String in the form: {[x1,y1], [x2,y2], ... ,[xn,yn]}. This is sometimes useful to log while watching the agent's actions.
getFringe()
Returns the LinkedList object of the fringe. This is only useful if the programmer plans to do something else with the fringe that the BotSearch class does not.

The following methods are also in the BotSearch class and have to do with performing a 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.
moveSearchLocation(Node)
Moves the search location to the Node object given. This is most usefully coupled with the Node returned from getNextFringeNode().
getSearchCost()
Returns the total integer searching cost, where the cost to view a node is defined in sbcproperties.dat.
getNorthOfSearchLocation(),
getEastOfSearchLocation(),
getSouthOfSearchLocation(),
getWestOfSearchLocation()
Returns the corresponding adjacent Node objects to the search's location.

The most important change when using a search algorithm agent is that the step() method has been split into two step methods: a searchStep() method and a movementStep() method. In the searchStep() method, the programmer should put all computation steps when searching for the goal, and in the movementStep() method, the programmer should put all computation steps when moving the agent to the goal. The programmer does not need a step() method in the agent's class because it has been taken care of in the BotSearch class, so he/she only needs to write a searchStep() and movementStep() method for stepping in the agent's class.

Finally, some additional methods are available in the Node class for use by search algorithm agents:

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.
getSearchIsHere()
Returns true if the search is currently on this node, false otherwise.
FJK Home CPE/CSC 480 Syllabus Schedule Lecture Notes Assignments Labs Project Teams
Bot-Tutorial.html
last modified:
Thursday, October, 8, 2009, 13:49:25 PDT
© 2000-2025
Franz Kurfess