Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

I need help adding h 1 time and h 2 time ( the time it takes for each heuristic ) . this is another class

I need help adding h1 time and h2 time (the time it takes for each heuristic). this is another class i have:
public class OutputData {//used to store the solution depth and search cost of a given puzzle solution for printing
public int solutionDepth;
public int searchCost;
public OutputData(int solutionDepth, int searchCost){
this.solutionDepth = solutionDepth;
this.searchCost = searchCost;
}
}
this is my main class: public class Main {
static Scanner sc = new Scanner(System.in);
static AStarSearch search = new AStarSearch();
public static void main(String[] args){
boolean validInput = false;
String prompt = "Select:
[1] Generate Random Puzzle
[2] Enter Puzzle
[3] Exit
", validChoices ="123";
int choice =-1;
while(!validInput){
System.out.println(prompt);
System.out.print("Enter choice: ");
choice = getChoice();
if(validChoices.contains(String.valueOf(choice)))//check if input is 1/2/3
validInput = true;
}
Main test = new Main();
switch (choice){//user enter puzzle or generate random puzzle based on user choice
case 1:
test.randomPuzzle();
break;
case 2:
test.userPuzzle();
break;
case 3:
System.out.println("Goodbye!");
sc.close();
System.exit(0);
break;
default:
break;
}
}
/**
*
*/
public void randomPuzzle(){
Map> completeData = new TreeMap>(); //treemap for storing data of multiple cases
int testCases =0;
do {
System.out.print("How many random puzzles would you like to solve? ");
testCases = getChoice(); //get amount of random puzzles to generate
} while (testCases =0);
for(int i =0; i testCases; i++){
Puzzle randomPuzzle = new Puzzle(); //generate random puzzle
OutputData solution = solve(randomPuzzle); //pass random puzzle into solve method to get corresponding data
if(!completeData.containsKey(solution.depth))//make new entry in data set if depth of random puzzle does not exist in the set
completeData.put(solution.depth, new ArrayList>());
completeData.get(solution.depth).add(solution); //add data to the specified depth
}
System.out.printf("Depth | Total Cases | Manhattan Search Cost | Manhattan Elapsed Time | Misplaced Search Cost | Misplaced Elapsed Time");
completeData.entrySet().stream().forEach((entry)->{
int h1Average =0, h1AvgTime =0, h2Average =0, h2AvgTime =0, total = entry.getValue().size();
for(int i =0; i entry.getValue().size(); i++){
OutputData data = entry.getValue().get(i);
h1Average += data.searchCostH1;
h1AvgTime += data.totalTimeH1;
h2Average += data.searchCostH2;
h2AvgTime += data.totalTimeH2;
}
System.out.println();
System.out.printf("%3d %10d %20d %20d ms %25d %20d ms", entry.getKey()+1, total, (h1Average / total),(h1AvgTime/total),(h2Average/total),(h2AvgTime/total));
});
}
public void userPuzzle(){
boolean puzzleCreated = false;
String puzzleString;
Integer[] temp ={-1,-1,-1,-1,-1,-1,-1,-1,-1};
Puzzle userPuzzle = new Puzzle(temp);
while(!puzzleCreated){
puzzleString = getPuzzle();
puzzleCreated = userPuzzle.createPuzzle(puzzleString);
}
search.aStar(userPuzzle.puzzle, "manhattan");
search.aStar(userPuzzle.puzzle, "misplaced");
}
public static int getChoice(){
int choice =-1;
try {
choice = Integer.parseInt(sc.nextLine());
} catch (Exception e){
System.out.println("Enter an integer.");
}
return choice;
}
public static String getPuzzle(){
String puzzleString ="";
int rows =3;
System.out.println("Enter puzzle: ");
for(int i =0; i rows; i++){
System.out.printf("Row %d: ", i +1);
puzzleString += sc.nextLine().replace("",""); //make the strin
image text in transcribed

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

The Database Experts Guide To SQL

Authors: Frank Lusardi

1st Edition

0070390029, 978-0070390027

More Books

Students also viewed these Databases questions