Answered step by step
Verified Expert Solution
Link Copied!

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 N-Queen 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 Coords(int 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 =0;
success =0;
map = new HashMap>();
queens = new ArrayList();
debug = false;
}
public NQueen(int n){
trials =0;
success =0;
debug = false;
makeBoard(n);
}
public void makeBoard(int n){
this.board = new Board(n);
map = new HashMap>(n);
queens = new ArrayList(n);
}
public void genSemiRandConfig(){
Random rand = new Random();
int randY;
for(int i =0; i board.getSize(); i++){
randY = Math.abs(rand.nextInt()%board.getSize());
if(!checkQ(i,randY))
placeQ(i, randY);
else
i--;
}
}
public void nTrials(int n, int size, int mode, int additional1, double additional2, int debug){
long startTime;
long endTime;
long time;
totalTime =0;
avgTime =0;
trials =0;
success =0;
if(debug ==1)
this.debug = true;
else if (debug ==0)
this.debug = false;
if(board == null){
makeBoard(size);
genSemiRandConfig();
}
for(int i =0; i n; i++){
switch(mode){
case 1:
startTime = System.currentTimeMillis();
steepestHillClimbing();
endTime = System.currentTimeMillis();
time = endTime - startTime;
totalTime += time;
break;
case 2:
startTime = System.currentTimeMillis();
minConflicts(additional1);
endTime = System.currentTimeMillis();
time = endTime - startTime;
totalTime += time;
break;
default:
continue;
}
avgTime = totalTime/n;
board = new Board(size);
queens.clear();
map.clear();
if(boardQ != null)
boardQ.clear();
genSemiRandConfig();
}
}
public String getReport(){
String toReturn;
toReturn = "Trials: "+(int)trials+"
Success Rate: "+(double)(success/trials)*100+"%";
return toReturn;
}
private void replaceQ(Tile variable, Tile value){
removeQ(variable);
placeQ(value);
}
public boolean checkQ(int x, int y){
return board.getTile(x, y).hasQ();
}
public void placeQ(int x, int y){
if(board.getTile(x, y).hasQ())
return;
else{
board.getTile(x, y).placeQ();
queens.add(x, new Coords(x,y));
}
}
public Node steepestHillClimbing(){
Node current;
Node neighbor;
current = new Node(board, getAttackers(),(ArrayList) queens);
boardQ = new PriorityQueue();
while(true){
neighbor = makeNeighbor(current);
if(neighbor.value >= current.value){
trials++;
if(current.value ==0){
success++;
if(debug)
System.out.println(current.board.toString());
}
return current;
}
current = neighbor;
}
} public int getAttackers(){
int allAttackers =0;
for(int i =0; i board.getSize(); i++){
for(int j =0; j board.getSize(); j++){
if(board.getTile(i,j).hasQ())
allAttackers += getAttackers(i,j);
}
}
map.clear();
return allAttackers;
}
public long getTime(){
return this.avgTime;
}
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_2

Step: 3

blur-text-image_3

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

SQL Server Query Performance Tuning

Authors: Sajal Dam, Grant Fritchey

4th Edition

1430267429, 9781430267423

More Books

Students also viewed these Databases questions

Question

6.5 Identify at least 10 methods used for external recruitment.

Answered: 1 week ago

Question

6.6 Explain two strategies used to recruit nonpermanent staff.

Answered: 1 week ago