In this exercise you will:
- Add the provided graph package to
your ProblemSolver framework and test it
- Add a Solution class to the framework that can iterate
through the results of graph searches
- Use the framework to create a FarmerGraph class
representing the farmer, wolf, goat, and cabbage (FWGC)
problem and solve it
Note that the testing of this lab will use
graph searching algorithms
that will be described later. However, your tasks in this lab can be
accomplished without understanding the algorithms at this time.
These provided files are required by the lab.
These are the lecture presentations in which the concepts for this lab
were introduced.
First, add the new packages below to your
ProblemSolver by
right-clicking on
Source Packages in the project tree and
selecting
New > Java Package... as shown below on the left.
- domains.example
- framework.graph
After, your project structure should look like that on the right.
Now add the following classes to their respective packages.
These classes have been discussed previously and are available from the
menu on the left under
Files.
- Add the Vertex, Graph, and GraphSearcher
classes from the graph framework to the new framework.graph
package.
- Add the ExampleGraph class from the graph example to
the new domains.example package.
- Add the Solution class to
the existing framework.solution package.
To add a class to a package, right-click the package and slect
New > Java Class... as shown below on the left.
After, your project structure should like that on the right.
Now add the provided test classes
ExampleGraphTest
and
SolutionTest to your project. Do this in two steps:
- Create a default test class under the Test Packages handle
of your ProblemSolver project
- Replace the default content of the test with the content
provided here and available under Files in the menu
For example, to create
ExampleGraphTest.java,
right-click
ExampleGraph.java under
Source Packages and
select
Tools > Create/Update Tests, as shown below.
This will create the corresponding test class under
Test
Packages whose default content you can then change.
After, your test packages structure should like that on the right.
Now test your project by running the graph example test as shown
below left. Right-click
ExampleGraphTest.java and select
Run
File.
The test output should look like that below right.
The example graph class and test class are shown in the menu at left.
The test class runs some graph searches, the nature of which are
described in the next module.
For now, look at
ExampleGraphTest and
note how the searches result in search predecessor information
being set in the vertices and made available through
the
getPredecessor method in the
Vertex class.
Breadth-first and depth-first graph searches generate predecessor trees
that can be used to generate problem solutions, as described
in
Problem Solutions in the menu on the left.
It is not necessary at this time to know precisely how the graph
searches work, only that they set vertex predecessor information during
the search. This information can be used to construct solutions to
problems.
Implement the described
Solution class and test it by running
the
SolutionTest class.
The test output should look like that below.
These files include the ExampleGraph class and its test
class ExampleGraphTest.
These files include the Solution class, which you must complete,
and a SolutionTest class.
These files make up the graph framework, which contains
the Vertex, Graph, and GraphSearcher classes.
This section presents a Java framework for representing and searching
graphs. The framework is comprised of the following classes, whose
relationships are shown below. Note that the graph search operations
are discussed in
the
Graph Search Algorithms presentation:
Note that the
search method employs a double-ended queue
(
java.util.LinkedList) so that it can be used for both
breadth-first and depth-first search.
GraphSearcher.java
In this exercise you have three tasks:
- Add a Solution class to the framework.solution package
to support observing solutions once they have been found.
- Install a new framework.graph package that is provided
for you.
- Use the graph framework to represent and solve the FWGC problem.
You will create a
Solution class that shows the path from
a
start vertex to an
end vertex in a graph search.
This task is slightly complicated by the fact that although the output is
from
start to
end, you must begin your solution
processing with
end.
- Why? Because once a graph has been searched
using breadth-first or depth-first search, its vertices are linked
with predecessor information that can be used to show a problem
solution.
- Thus, a stack is a natural structure for processing
solutions:
- Starting at end, push vertices onto the stack
while following predecessor vertices, stopping when there are no
more predecessors.
- The stack will then have the start
vertex on the top, and the vertices from start
to end can be processed by popping the stack.
To begin, create a Java class file called
Solution.java in
the
framework.solution package:
The API for the
Solution class is described in the Javadoc
comments of the class template given below.
Create a test file for
Solution.java and populate it with this
SolutionTest.java file, whose
content is shown in the menu.
The output is shown below. Note that although 3 states are shown,
the length of the solution, that is, the number of moves to achieve the
solution, is 2.
Move to the next step when your
Solution class passes the test.
| |
| | F
| | W
G | |
| | C
| |
| |
F | |
| | W
G | |
| | C
| |
| |
| | F
| | W
| | G
| | C
| |
-- --
--
See the "Framework Code" menu item to the left for a description of the
graph framework code.
Create a new source package called
framework.graph in
your
ProblemSolver project and
install the
Vertex.java,
Graph.java,
and
GraphSearcher.java files in the package.
After installing the graph framework files, your project structure
should look like that below.
Using
ExampleGraph as a model, create a
FarmerGraph class
that represents the full FWGC problem:
FarmerGraph should be in the
domains.farmer package as
shown to the right. Note:
- Like ExampleGraph, FarmerGraph should extend
the Graph class
- FarmerGraph should create ten vertices. For example, to
create the vertex representing the start state:
- The FarmerGraph constructor should add edges among the
vertices, as in ExampleGraph.
Add a new class called
FarmerGraph to your
domains.farmer
package:
Here is a template for the file:
All the work is done by the
FarmerGraph constructor.
First create the ten (10) possible
FarmerState objects and store
them as vertices.
For example, to create the vertex representing the start state:
Next, use the
addEdge method inherited from the
Graph
class to link the vertices.
Note: It is not necessary to create a
Graph object for
this. Since
FarmerGraph extends
Graph, it
is
a
Graph and inherits the
addEdge method. To get rid of
the NetBeans warning about overridable method calls in the constructor,
use
super.addEdge(...).
Recall that in the FWGC problem, if there is an edge
from
v1 to
v2, then there is an
edge from
v2 to
v1.
To test your graph, there will need to be access to the FWGC problem's
start and end vertices.
Complete the
getStart and
getEnd methods.
In the last step,
create a test class for
FarmerGraph and replace its default
contents with the
FarmerGraphTest.java file shown in the menu at
left.
This test does four searches on your graph:
- Breadth-first search from the start vertex
- Breadth-first search from the end vertex
- Depth-first search from the start vertex
- Depth-first search from the end vertex
A successful test results in:
Additionally, for each search, the test file displays the path traced
from the start (or end) to the end (or start). The path must show a
valid solution. Example solutions are shown in the menu.
Important:
- The graph framework relies on vertices being stored
on and retrieved from a hash table represented in Java using
a HashMap.
- The hash table will work only if the vertices, in
this case FarmerState objects, have successfully overridden
the hashCode method built into every Java object.
- This has been discussed previously (see Overriding hashCode in
the menu) with respect to PuzzleState objects, but similar
considerations hold for FarmerState objects.
- If your FarmerState class does not have a hashCode
method defined, NetBeans can create one if you hover the cursor
over the icon over the line
number of your equals method.
After adding the
FarmerGraphTest class shown below,
your test packages structure should look like that on the right.
FarmerGraphTest.java
BFS from start to end:
| |
F | |
W | |
G | |
C | |
| |
| |
| | F
W | |
| | G
C | |
| |
| |
F | |
W | |
| | G
C | |
| |
| |
| | F
| | W
| | G
C | |
| |
| |
F | |
| | W
G | |
C | |
| |
| |
| | F
| | W
G | |
| | C
| |
| |
F | |
| | W
G | |
| | C
| |
| |
| | F
| | W
| | G
| | C
| |
BFS from end to start:
| |
| | F
| | W
| | G
| | C
| |
| |
F | |
| | W
G | |
| | C
| |
| |
| | F
| | W
G | |
| | C
| |
| |
F | |
| | W
G | |
C | |
| |
| |
| | F
| | W
| | G
C | |
| |
| |
F | |
W | |
| | G
C | |
| |
| |
| | F
W | |
| | G
C | |
| |
| |
F | |
W | |
G | |
C | |
| |
DFS from start to end:
| |
F | |
W | |
G | |
C | |
| |
| |
| | F
W | |
| | G
C | |
| |
| |
F | |
W | |
| | G
C | |
| |
| |
| | F
W | |
| | G
| | C
| |
| |
F | |
W | |
G | |
| | C
| |
| |
| | F
| | W
G | |
| | C
| |
| |
F | |
| | W
G | |
| | C
| |
| |
| | F
| | W
| | G
| | C
| |
DFS from end to start:
| |
| | F
| | W
| | G
| | C
| |
| |
F | |
| | W
G | |
| | C
| |
| |
| | F
| | W
G | |
| | C
| |
| |
F | |
W | |
G | |
| | C
| |
| |
| | F
W | |
| | G
| | C
| |
| |
F | |
W | |
| | G
C | |
| |
| |
| | F
W | |
| | G
C | |
| |
| |
F | |
W | |
G | |
C | |
| |
When your
Solution and
FarmerGraph classes are working correctly:
- Make sure your identifying information is in the class comments
of your files
- Export
your ProblemSolver project to
ProblemSolver.zip.
- Submit ProblemSolver.zip by going to
.
Note the general
Submission Policy in the menu at left.
Your project will be inspected, tested, and run by the lab instructor. Grading criteria:
- (4 points) Successful implementation and testing of the Solution
class.
- (6 points) Successful implementation and testing of the FarmerGraph
class.