Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

I need help with the genetic algorithm. Whenever i select the genetic algorithm, it always displays no solution. How do I fix this, below i

I need help with the genetic algorithm. Whenever i select the genetic algorithm, it always displays no solution. How do I fix this, below i provided all my classes for this and the problem description. Also, how do i make a method so the board prints
this is my program description:
CS4200-- Project 2: N-Queen (n =8)
Local search algorithms are very efficient in solving n-Queen problems. You are asked to implement the following two algorithms to solve the n-queen problem:
Time elapsed: 52
1) Straight-forward steepest-ascent hill climbing as described on page 9 of our lecture "local search".
2) Your choice of one of the following algorithms: simulated annealing search algorithm (page 18 of our local search lecture); the genetic algorithm (page 26 of our "local search" lecture); or the MIN-CONFLICTS algorithm (page 42 of our "CSP" lecture).
For analysis, you should generate a large number of n-queen instances (>100) and solve them. Document the percentage of solved problems, search costs and the average running time. Explain why you get such results, for example, why the steepest-ascent hill climbing can only solve about 14% of the problems, or what kind of improvements have you made to make your algorithms more efficient.
If you choose to implement the genetic algorithm, then your goal is to be able to solve the problem. You need to show that your program is able to solve at least three cases. You will also get +10 bonus points for solving the problem using genetic algorithm.
below is my code for genetic.java:
import java.util.Comparator;
import java.util.PriorityQueue;
import java.util.Random;
public class Genetic {
public static void solveGenetic(int population, int generations, double mutationRate){
Comparator QueenBoardComparator = new QueenBoardComparator();
PriorityQueue queue = new PriorityQueue>(10, QueenBoardComparator);
// fill population with random boards
for (int i =0; i population; i++){
int[] randomLayout = QueenBoard.getRandomLayout();
QueenBoard organism = new QueenBoard(randomLayout);
queue.offer(organism);
}
// parent population is half the population
int parentPopulation = population /2;
PriorityQueue parents = new PriorityQueue>(10, QueenBoardComparator);
// for each generation, check if there is a solution, otherwise start breeding
for (int g =1; g = generations; g++){
//System.out.println("Starting generation "+ g);
if (queue.peek().getCrosses()==0){
System.out.println("Solution found! "+ queue.peek().toString()+" with "+ queue.peek().getCrosses()+" crosses.");
return;
}
// for each generation, get the best organisms as parents
for (int p =0; p parentPopulation; p++){
parents.offer(queue.poll());
}
// now mate them
PriorityQueue children = new PriorityQueue>(10, QueenBoardComparator);
for (int m =0; m parentPopulation /2; m++){
QueenBoard parent1= parents.poll();
QueenBoard parent2= parents.poll();
// two offspring from each pair of parents
QueenBoard child1= mate(parent1, parent2, mutationRate);
QueenBoard child2= mate(parent2, parent1, mutationRate);
children.offer(child1);
children.offer(child2);
}
// mating is done so set the queue to the new children population
parents.clear();
queue = children;
}
System.out.println("Wasn't able to find a solution.");
}
public static QueenBoard mate(QueenBoard p1, QueenBoard p2, double mutationRate){
// generate random crossover point
Random random = new Random();
int crossoverPoint = random.nextInt(7)+1;
int[] childGenes = new int[8];
int[] p1Genes = p1.getQueens();
int[] p2Genes = p2.getQueens();
// generate child genes
for (int i =0; i crossoverPoint; i++){
childGenes[i]= p1Genes[i];
}
for (int j = crossoverPoint; j 8; j++){
childGenes[j]= p2Genes[j];
}
// generate possible mutation
if (random.nextDouble() mutationRate){
int mutationIndex = random.nextInt(8);
int newValue = random.nextInt(8);
childGenes[mutationIndex]= newValue;
}
return new QueenBoard(childGenes);
}
}
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

OCA Oracle Database SQL Exam Guide Exam 1Z0-071

Authors: Steve O'Hearn

1st Edition

1259585492, 978-1259585494

More Books

Students also viewed these Databases questions

Question

Show that if u, v Rn, then det (I + uvt) = 1 + vtu.

Answered: 1 week ago

Question

What is the education level of your key public?

Answered: 1 week ago

Question

What are the cultural/ethnic/religious traits of your key public?

Answered: 1 week ago