Answered step by step
Verified Expert Solution
Question
1 Approved Answer
when i run my program, the average time doesnt work. Also i want to implement the average search cost, how would i do that? Also,
when i run my program, the average time doesnt work. Also i want to implement the average search cost, how would i do that? Also, how do i show only the final board configuration, i dont want all the steps to show.
this is the nQueen class:
This file contains the implementations used to solve the trials for the NQueen problems
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.PriorityQueue;
import java.util.Random;
public class NQueen
class Coords
int x;
int y;
public Coordsint x int y
this.x x;
this.y y;
private Board board;
private List queens;
private Map map;
private PriorityQueue boardQ;
private double trials;
private double success;
private long totalTime;
private long avgTime;
private boolean debug;
public NQueen
trials ;
success ;
map new HashMap;
queens new ArrayList;
debug false;
public NQueenint n
trials ;
success ;
debug false;
makeBoardn;
public void makeBoardint n
this.board new Boardn;
map new HashMapn;
queens new ArrayListn;
public void genSemiRandConfig
Random rand new Random;
int randY;
forint i ; i board.getSize; i
randY Math.absrandnextIntboard.getSize;
ifcheckQirandY
placeQi randY;
else
i;
public void nTrialsint n int size, int mode, int additional double additional int debug
long startTime;
long endTime;
long time;
totalTime ;
avgTime ;
trials ;
success ;
ifdebug
this.debug true;
else if debug
this.debug false;
ifboard null
makeBoardsize;
genSemiRandConfig;
forint i ; i n; i
switchmode
case :
startTime System.currentTimeMillis;
steepestHillClimbing;
endTime System.currentTimeMillis;
time endTime startTime;
totalTime time;
break;
case :
startTime System.currentTimeMillis;
minConflictsadditional;
endTime System.currentTimeMillis;
time endTime startTime;
totalTime time;
break;
default:
continue;
avgTime totalTimen;
board new Boardsize;
queens.clear;
map.clear;
ifboardQ null
boardQ.clear;
genSemiRandConfig;
public String getReport
String toReturn;
toReturn "Trials: inttrials
Success Rate: doublesuccesstrials;
return toReturn;
private void replaceQTile variable, Tile value
removeQvariable;
placeQvalue;
public boolean checkQint x int y
return board.getTilex yhasQ;
public void placeQint x int y
ifboardgetTilex yhasQ
return;
else
board.getTilex yplaceQ;
queens.addx new Coordsxy;
public Node steepestHillClimbing
Node current;
Node neighbor;
current new Nodeboard getAttackersArrayList queens;
boardQ new PriorityQueue;
whiletrue
neighbor makeNeighborcurrent;
ifneighborvalue current.value
trials;
ifcurrentvalue
success;
ifdebug
System.out.printlncurrentboard.toString;
return current;
current neighbor;
public int getAttackers
int allAttackers ;
forint i ; i board.getSize; i
forint j ; j board.getSize; j
ifboardgetTileijhasQ
allAttackers getAttackersij;
map.clear;
return allAttackers;
public long getTime
return this.avgTime;
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started