Answered step by step
Verified Expert Solution
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:
CS Project : NQueen n
Local search algorithms are very efficient in solving nQueen problems. You are asked to implement the following two algorithms to solve the nqueen problem:
Time elapsed:
Straightforward steepestascent hill climbing as described on page of our lecture "local search".
Your choice of one of the following algorithms: simulated annealing search algorithm page of our local search lecture; the genetic algorithm page of our "local search" lecture; or the MINCONFLICTS algorithm page of our CSP lecture
For analysis, you should generate a large number of nqueen instances 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 steepestascent hill climbing can only solve about 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 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 solveGeneticint population, int generations, double mutationRate
Comparator QueenBoardComparator new QueenBoardComparator;
PriorityQueue queue new PriorityQueue QueenBoardComparator;
fill population with random boards
for int i ; i population; i
int randomLayout QueenBoard.getRandomLayout;
QueenBoard organism new QueenBoardrandomLayout;
queue.offerorganism;
parent population is half the population
int parentPopulation population ;
PriorityQueue parents new PriorityQueue QueenBoardComparator;
for each generation, check if there is a solution, otherwise start breeding
for int g ; g generations; g
Systemout.printlnStarting generation g;
if queuepeekgetCrosses
System.out.printlnSolution found! queue.peektoString with queue.peekgetCrosses crosses.";
return;
for each generation, get the best organisms as parents
for int p ; p parentPopulation; p
parents.offerqueuepoll;
now mate them
PriorityQueue children new PriorityQueue QueenBoardComparator;
for int m ; m parentPopulation ; m
QueenBoard parent parents.poll;
QueenBoard parent parents.poll;
two offspring from each pair of parents
QueenBoard child mateparent parent mutationRate;
QueenBoard child mateparent parent mutationRate;
children.offerchild;
children.offerchild;
mating is done so set the queue to the new children population
parents.clear;
queue children;
System.out.printlnWasnt able to find a solution.";
public static QueenBoard mateQueenBoard p QueenBoard p double mutationRate
generate random crossover point
Random random new Random;
int crossoverPoint random.nextInt;
int childGenes new int;
int pGenes pgetQueens;
int pGenes pgetQueens;
generate child genes
for int i ; i crossoverPoint; i
childGenesi pGenesi;
for int j crossoverPoint; j ; j
childGenesj pGenesj;
generate possible mutation
if randomnextDouble mutationRate
int mutationIndex random.nextInt;
int newValue random.nextInt;
childGenesmutationIndex newValue;
return new QueenBoardchildGenes;
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