# Searching You are to write a search state class for the river crossing problem that will be used to solve the problem. The search algorithms are given to you, but you need to write the class that...

1 answer below »
# Searching
You are to write a search state class for the river crossing problem that will be used to solve the problem. The search algorithms are given to you, but you need to write the class that describes a state in the search space.
The river crossing problem, in this variant, involves a farmer who is trying to ferry a fox, a goat and a cabbage across a river. The farmer can only bring one of those things across the river at a time. The farmer can also cross the river without carrying anything.

The problem stems from the fact that the fox and goat cannot be left alone without the farmer and the goat and cabbage cannot be left alone without the farmer. How does the farmer ferry everything over without letting that happen?
You are to write a class that describes a state in the search space of this problem. A search state indicates which side of the river each item is on (including the farmer). A search state also needs a score indicating how close it seems to be to the solution (the heuristic).
You are given the skeleton of the class that uses booleans to indicate which side of the river each item is on. You need to write three methods:
calcScore() calculates the heuristic score and stores it in the score instance variable of the class. This score should simply be how many items are on the wrong side (remember lower scores are better).
generateNextStates() returns an ArrayList of SearchStates that should have all of the valid search states that can be reached from the current state. The farmer can cross the river (to either side) by himself or carrying one item. The item he is carrying must be on the same side the farmer is on. The farmer cannot leave the fox and goat alone or the goat and cabbage. Use the given helper method, isValid(), to test whether a given arrangement is valid. Only add valid search states to the ArrayList. Create the new search states using the constructor that takes an ancestor state. Return the ArrayList when you have added all of the valid states that can be reached from the current state.
isGoal() should return true if the current state is the goal state and false otherwise.
See the accompanying SlidePuzzle8.java file for an example of a search state class for another puzzle.
Sample Output:```Best-first Search:Found!States Generated:20 States Evaluated:10 Remaining Stack Size:10History (Size 7) :Farmer:Left Fox:Left Goat:Left Cabbage:LeftFarmer:Right Fox:Left Goat:Right Cabbage:LeftFarmer:Left Fox:Left Goat:Right Cabbage:LeftFarmer:Right Fox:Right Goat:Right Cabbage:LeftFarmer:Left Fox:Right Goat:Left Cabbage:LeftFarmer:Right Fox:Right Goat:Left Cabbage:RightFarmer:Left Fox:Right Goat:Left Cabbage:RightFarmer:Right Fox:Right Goat:Right Cabbage:Right```
Answered 1 days AfterMay 05, 2021

Answer To: # Searching You are to write a search state class for the river crossing problem that will be used...

Rushendra answered on May 07 2021
139 Votes
Searching/.classpath

    
        
            
        
    
    
    
Searching/.project

     Searching
    
    
    
    
        
             org.eclipse.jdt.core.javabuilder
            
            
        
    
    
         org.eclipse.jdt.core.javanature
    
Searching/.settings/org.eclipse.jdt.core.prefs
eclipse.preferences.version=1
org.eclipse.jdt.core.compiler.codegen.inlineJsrBytecode=enabled
org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.8
org.eclipse.jdt.core.compiler.codegen.unusedLocal=preserve
org.eclipse.jdt.core.compiler.compliance=1.8
org.eclipse.jdt.core.compiler.debug.lineNumber=generate
org.eclipse.jdt.cor
e.compiler.debug.localVariable=generate
org.eclipse.jdt.core.compiler.debug.sourceFile=generate
org.eclipse.jdt.core.compiler.problem.assertIdentifier=error
org.eclipse.jdt.core.compiler.problem.enumIdentifier=error
org.eclipse.jdt.core.compiler.release=disabled
org.eclipse.jdt.core.compiler.source=1.8
Searching/bin/Main.class
synchronized class Main {
void Main();
public static void main(String[]);
}
Searching/bin/README.md
# Searching
You are to write a search state class for the river crossing problem that will be used to solve the problem. The search algorithms are given to you, but you need to write the class that describes a state in the search space.
The river crossing problem, in this variant, involves a farmer who is trying to ferry a fox, a goat and a cabbage across a river. The farmer can only bring one of those things across the river at a time. The farmer can also cross the river without carrying anything.
The problem stems from the fact that the fox and goat cannot be left alone without the farmer and the goat and cabbage cannot be left alone without the farmer. How does the farmer ferry everything over without letting that happen?
You are to write a class that describes a state in the search space of this problem. A search state indicates which side of the river each item is on (including the farmer). A search state also needs a score indicating how close it seems to be to the solution (the heuristic).
You are given the skeleton of the class that uses booleans to indicate which side of the river each item is on. You need to write three methods:
calcScore() calculates the heuristic score and stores it in the score instance variable of the class. This score should simply be how many items are on the wrong side (remember lower scores are better).
generateNextStates() returns an ArrayList of SearchStates that should have all of the valid search states that can be reached from the current state. The farmer can cross the river (to either side) by himself or carrying one item. The item he is carrying must be on the same side the farmer is on. The farmer cannot leave the fox and goat alone or the goat and cabbage. Use the given helper method, isValid(), to test whether a given arrangement is valid. Only add valid search states to the ArrayList. Create the new search states using the constructor that takes an ancestor state. Return the ArrayList when you have added all of the valid states that can be reached from the current state.
isGoal() should return true if the current state is the goal state and false otherwise.
See the accompanying SlidePuzzle8.java file for an example of a search state class for another puzzle.
Sample Output:
```
Best-first Search:
Found!
States Generated:20 States Evaluated:10 Remaining Stack Size:10
History (Size 7) :
Farmer:Left Fox:Left Goat:Left Cabbage:Left
Farmer:Right Fox:Left Goat:Right Cabbage:Left
Farmer:Left Fox:Left Goat:Right Cabbage:Left
Farmer:Right Fox:Right Goat:Right Cabbage:Left
Farmer:Left Fox:Right Goat:Left Cabbage:Left
Farmer:Right Fox:Right Goat:Left Cabbage:Right
Farmer:Left Fox:Right Goat:Left Cabbage:Right
Farmer:Right Fox:Right Goat:Right Cabbage:Right
```
Searching/bin/RiverCrossingState.class
synchronized class RiverCrossingState extends SearchState {
public boolean farmer;
public boolean fox;
public boolean goat;
public boolean cabbage;
void RiverCrossingState();
private void calcScore();
void RiverCrossingState(RiverCrossingState, boolean, boolean, boolean, boolean);
public String toString();
static boolean isValid(boolean, boolean, boolean, boolean);
static String boolToLR(boolean);
java.util.ArrayList generateNextStates();
boolean isGoal();
}
Searching/bin/Search.class
public synchronized class Search {
public void Search();
static void breadthFirstSearch(SearchState);
static void depthFirstSearch(SearchState);
static void bestFirstSearch(SearchState);
static void aStarSearch(SearchState);
}
Searching/bin/SearchState.class
public abstract synchronized class SearchState {
java.util.ArrayList history;
int score;
int distance;
public void SearchState();
abstract java.util.ArrayList generateNextStates();
abstract boolean isGoal();
String historyToString();
}
Searching/bin/SlidePuzzle8State.class
synchronized class SlidePuzzle8State extends SearchState {
int[] squares;
void SlidePuzzle8State();
void SlidePuzzle8State(SlidePuzzle8State, int, int);
public String toString();
void calcScore();
java.util.ArrayList generateNextStates();
boolean isGoal();
}
Searching/src/Main.java
Searching/src/Main.java
class Main {
  public static void main(String[] args) {
    // Run to try the slide puzzle search
    //Search.bestFirstSearch(new SlidePuzzle8State());
    // Best-first works well for this problem
    Search.aStarSearch(new RiverCrossingState());
  }
}
Searching/src/README.md
# Searching
You are to write a search state class for the river crossing problem that will be used to solve the problem. The search algorithms are given to you, but you need to write the class that describes a state in the search space.
The river crossing problem, in this variant, involves a farmer...
SOLUTION.PDF

Answer To This Question Is Available To Download

Related Questions & Answers

More Questions »

Submit New Assignment

Copy and Paste Your Assignment Here