Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

I need help with implemented the serach cost and average time for both hill climbing and minConflicts. Also i want to only show only the

I need help with implemented the serach cost and average time for both hill climbing and minConflicts. Also i want to only show only the final board.This is my main class below, i also provided my NQueen class in the images:
main.java:
import java.util.Scanner;
public class Main {
public static void main(String[] args){
NQueen queen = new NQueen();
Scanner sc = new Scanner(System.in);
int n =1000; // Set a default value
int boardsize =8; //8 for project
int mode;
int debug;
int def =0;
boolean solved = false;
int end =0;
while (!solved){
prompt();
mode = Integer.parseInt(sc.nextLine());
promptDefault();
def = Integer.parseInt(sc.nextLine());
if(def ==1){
promptN();
n = Integer.parseInt(sc.nextLine());
promptSize();
boardsize = Integer.parseInt(sc.nextLine());
}
debug =1; // Always show completed boards
switch (mode){
case 1:
doSHC(queen, n, boardsize, debug);
break;
case 2:
doCSP(sc, queen, n, boardsize, debug, 0); // No customization
break;
default:
solved = false;
break;
}
promptFinished();
end = Integer.parseInt(sc.nextLine());
if (end ==0)
solved = true;
else if (end ==1)
solved = false;
}
}
private static void promptDebug(){
System.out.print("Show completed boards?: (0- NO,1- YES): ");
}
private static void promptDefault(){
System.out.print("Do you want to customize this run?
(1- yes
0- no): ");
}
private static void promptFinished(){
System.out.print("Run Again?? (0- NO,1- YES): ");
}
private static void printTime(NQueen queen){
System.out.println("Average time (ms): "+ queen.getTime());
}
/**
* This function will run the steepest hill climb algorithm
*/
private static void doSHC(NQueen queen, int n, int boardsize, int debug){
System.out.println("Running steepest hill climbing algorithm...");
queen.nTrials(n, boardsize, 1,0,0, debug);
System.out.println(queen.getReport());
printTime(queen);
}
/**
* This function will run the min conflict algorithm.
* If customization is chosen, the user will have the additional ability to set a maximum num
*/
private static void doCSP(Scanner sc, NQueen queen, int n, int boardsize, int debug, int def){
int maxsteps =0;
if(def ==1){
promptMaxSteps();
maxsteps = Integer.parseInt(sc.nextLine());
}
if(maxsteps ==0){
maxsteps =500; //default is set to 500
queen.nTrials(n, boardsize, 2, maxsteps, 0, debug);
System.out.println(queen.getReport());
printTime(queen);
}
}
private static void promptMaxSteps(){
System.out.print("Maximum number of steps?: (0- Default): ");
}
private static void promptSize(){
System.out.print("Size of the board?: ");
}
private static void promptN(){
System.out.print("How many puzzles to solve?: ");
}
private static void prompt(){
System.out.println("Enter type of search:
"
+"1) Steepest Hill Climbing
"
+"2) Minimum Conflicts (CSP)");
}
}
Board class:
public class Board {
private Tile[][] board;
private int size;
public Board(int n){
this.board = new Tile[n][n];
this.size = n;
initBoard();
}
public Board(Board copy){
this.size = copy.getSize();
this.board = copyBoard(copy);
}
private Tile[][] copyBoard(Board copy){
Tile[][] toReturn = new Tile[size][size];
for(int i =0; i size; i++){
for(int j =0; j size; j++){
toReturn[i][j]= new Tile(copy.board[i][j]);
}
}
return toReturn;
}
private void initBoard(){
for(int i =0; i size; i++){
for(int j =0; j size; j++){
board[i][j]= new Tile(i,j);
}
}
}
public Tile getTile(int i, int j){
return board[i][j];
}
public Tile getTile(NQueen.Coords coords){
return board[coords.x][coords.y];
}
public int getSize(){
return this.size;
}
public String toString(){
StringBuilder toReturn = new StringBuilder("");
for(int i =0; i size; i++){
for(int j =0; j size; j++){
toReturn.append("|Q|");
else
toReturn.append("|_|");
}
toReturn.append("
");
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

Beyond Big Data Using Social MDM To Drive Deep Customer Insight

Authors: Martin Oberhofer, Eberhard Hechler

1st Edition

0133509796, 9780133509793

More Books

Students also viewed these Databases questions