Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Coordinate.java below /* * A simple class to represent a coordinate. Could be useful! * * DO NOT MODIFY THIS CLASS * * You can

Coordinate.java below /* * A simple class to represent a coordinate. Could be useful! * * DO NOT MODIFY THIS CLASS * * You can reference this class from Queens.java if you wish. * * Do not submit this class with your assignment. * * MH 2020 */ public class Coordinate { int x,y; public Coordinate(int x, int y) { this.x = x; this.y = y; } public int getX() { return x; } public int getY() { return y; } }
 QUEENS.java below import java.lang.Math; import java.util.*; /* YOU NEED TO ADD YOUR CODE TO THIS CLASS, REMOVING ALL DUMMY CODE * * DO NOT CHANGE THE NAME OR SIGNATURE OF ANY OF THE EXISTING METHODS * (Signature includes parameter types, return types and being static) * * You can add private methods to this class if it makes your code cleaner, * but this class MUST work with the UNMODIFIED Tester.java class. * * This is the ONLY class that you can submit for your assignment. * * MH 2020 */ public class Queens { private static int boardSize = 10; // creates a valid genotype with random values public static Integer [] randomGenotype() { Integer [] genotype = new Integer [boardSize]; // YOUR CODE GOES HERE // DUMMY CODE TO REMOVE: genotype = new Integer[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 }; // END OF YOUR CODE return genotype; } // swaps 2 genes in the genotype // the swap happens with probability p, so if p = 0.8 // then 8 out of 10 times this method is called, a swap happens public static Integer[] mutate(Integer[] genotype, double p) { // YOUR CODE GOES HERE // DUMMY CODE TO REMOVE: genotype = new Integer[]{ 1, 2, 8, 4, 5, 6, 7, 3, 9, 10 }; // END OF YOUR CODE return genotype; } // creates 2 child genotypes using the 'cut-and-crossfill' method public static Integer[][] crossover(Integer[] parent0, Integer[] parent1) { Integer [][] children = new Integer [2][boardSize]; // YOUR CODE GOES HERE // DUMMY CODE TO REMOVE: children[0] = new Integer[]{ 5, 4, 2, 6, 8, 1, 10, 9, 7, 3 }; children[1] = new Integer[]{ 6, 2, 5, 7, 3, 9, 1, 10, 4, 8 }; // END OF YOUR CODE return children; } // calculates the fitness of an individual public static int measureFitness(Integer [] genotype) { /* The initial fitness is the maximum pairs of queens * that can be in check (all possible pairs in check). * So we are using it as the maximum fitness value. * We deduct 1 from this value for every pair of queens * found to be in check. * So, the lower the score, the lower the fitness. * For a 10x10 board the maximum fitness is 45 (no checks), * and the minimum fitness is 0 (all queens in a line). */ int fitness = (int) (0.5 * boardSize * (boardSize - 1)); // YOUR CODE GOES HERE return fitness; } }
 TESTER.java below  /* * DO NOT SUBMIT THIS CLASS WITH YOUR ASSIGNMENT * * This class is only provided so that you can test your code. * * ALL your code MUST be contained in the Queens.java class. * * MH February 2020 */ public class Tester { public static void main(String[] args) { // we are using a 10x10 board size... Integer [] geno = new Integer [10]; // test 1: create 10 random genotypes System.out.println("1. Testing Random Genotype Creator: "); for (int count = 0; count  

image text in transcribedimage text in transcribed

You need to use the Queens.java and Tester.Java classes found in Resources > Queens to complete this assignment. You must only submit your completed version of the Queens.java class. No other classes or documentation! Read these instructions carefully. The 10 Queens Puzzle You are going to begin writing an evolutionary algorithm to solve the 10 Queens Puzzle. This is a variant on the 8 Queens puzzle, which instead uses a 10 by 10 chessboard and 10 queens. For this assignment you will need to write the code for 4 of the key components: Initialisation Mutation Crossover Fitness Evaluation Three classes have been provided for this task, Queens.java, Tester.java and Coordinate.java. Queens.java This class contains the four methods that you need to complete. At the moment they contain dummy code. This is what they need to do: randomGenotype () o create a valid genotype of size 10, using random values o a valid genotype contains each of the numbers 1 to 10 exactly once mutate() o swap the position of two genes integers) within the genotype o this happens with probability p so if p = 0.8, then 8 out of 10 times this method is called, a swap happens crossover() o take two parents and performs cut-and-crossfill recombination (see class slides) o the crossover point should be halfway in the genotype so the first 5 genes are directly copied from one parent, and the remaining 5 genes are filled from the other measureFitness () o measure the fitness of a genotype by counting the number of pairs of queens threatening each other o it begins with a maximum fitness value of 45, and then deducts 1 for every pair of queens found to be threatening each other You cannot change the signature of any of these methods! They must keep the same names, take exactly the same input parameters and provide exactly the same return types as they currently do. This is important for testing and assessing your code. You can only insert code in the places specified inside the methods. (See the actual code. You must remove all dummy code (which is clearly labelled) and replace it with your own. You are allowed to write private 'worker' methods that are called from within the existing methods if it helps. But you cannot create any new public methods. Each method is worth 5 marks (so a total of 20 marks for this assignment), but they are not of equal difficulty to code! Tester.java This class allows you to test the correctness of your code. When you first download the classes, compile them, run java Tester and observe. This class cannot be submitted with your assignment! You can modify it for testing purposes - for example, to try out your methods with different input genotypes - but it is not going to be assessed. Your Queens.java class must work correctly with the unmodified version of this class. Coordinate.java You can call this class in your code if you find that it's useful. Use of it is optional. This class cannot be modified or submitted with your assignment! The marker will use an unmodified version of this class if your code calls it. Note: all programming will be in Java and runnable on a Labnet machine using Linux without modification (i.e. the user can simply compile and run your code in console, they should not need to use an IDE) o see Compiling and Running java in Console for more guidance your code is going to assessed ONLY by its behaviour when run o we cannot debug your efforts to judge how 'close' you were to providing working code so please note that any code that cannot be run will score zero only submit your completed Queens.java class. Do not submit any other code because it will not be used for grading you must solve each of these 4 problems by yourself - neither the lecturer's nor your friend's ability to code EA is not being assessed here, yours is! You need to use the Queens.java and Tester.Java classes found in Resources > Queens to complete this assignment. You must only submit your completed version of the Queens.java class. No other classes or documentation! Read these instructions carefully. The 10 Queens Puzzle You are going to begin writing an evolutionary algorithm to solve the 10 Queens Puzzle. This is a variant on the 8 Queens puzzle, which instead uses a 10 by 10 chessboard and 10 queens. For this assignment you will need to write the code for 4 of the key components: Initialisation Mutation Crossover Fitness Evaluation Three classes have been provided for this task, Queens.java, Tester.java and Coordinate.java. Queens.java This class contains the four methods that you need to complete. At the moment they contain dummy code. This is what they need to do: randomGenotype () o create a valid genotype of size 10, using random values o a valid genotype contains each of the numbers 1 to 10 exactly once mutate() o swap the position of two genes integers) within the genotype o this happens with probability p so if p = 0.8, then 8 out of 10 times this method is called, a swap happens crossover() o take two parents and performs cut-and-crossfill recombination (see class slides) o the crossover point should be halfway in the genotype so the first 5 genes are directly copied from one parent, and the remaining 5 genes are filled from the other measureFitness () o measure the fitness of a genotype by counting the number of pairs of queens threatening each other o it begins with a maximum fitness value of 45, and then deducts 1 for every pair of queens found to be threatening each other You cannot change the signature of any of these methods! They must keep the same names, take exactly the same input parameters and provide exactly the same return types as they currently do. This is important for testing and assessing your code. You can only insert code in the places specified inside the methods. (See the actual code. You must remove all dummy code (which is clearly labelled) and replace it with your own. You are allowed to write private 'worker' methods that are called from within the existing methods if it helps. But you cannot create any new public methods. Each method is worth 5 marks (so a total of 20 marks for this assignment), but they are not of equal difficulty to code! Tester.java This class allows you to test the correctness of your code. When you first download the classes, compile them, run java Tester and observe. This class cannot be submitted with your assignment! You can modify it for testing purposes - for example, to try out your methods with different input genotypes - but it is not going to be assessed. Your Queens.java class must work correctly with the unmodified version of this class. Coordinate.java You can call this class in your code if you find that it's useful. Use of it is optional. This class cannot be modified or submitted with your assignment! The marker will use an unmodified version of this class if your code calls it. Note: all programming will be in Java and runnable on a Labnet machine using Linux without modification (i.e. the user can simply compile and run your code in console, they should not need to use an IDE) o see Compiling and Running java in Console for more guidance your code is going to assessed ONLY by its behaviour when run o we cannot debug your efforts to judge how 'close' you were to providing working code so please note that any code that cannot be run will score zero only submit your completed Queens.java class. Do not submit any other code because it will not be used for grading you must solve each of these 4 problems by yourself - neither the lecturer's nor your friend's ability to code EA is not being assessed here, yours is

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

Securing SQL Server Protecting Your Database From Attackers

Authors: Denny Cherry

3rd Edition

0128012757, 978-0128012758

More Books

Students also viewed these Databases questions

Question

What is the distribution of B(s) + B(t), s t?

Answered: 1 week ago

Question

Please make it fast 7 4 1 . .

Answered: 1 week ago