Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package pkg8.queens;
- import java.util.ArrayList;
- import java.util.Arrays;
- import java.util.Random;
- import javafx.util.Pair;
- public class Queens {
- static int population_size = 4;
- static int maxNumberOfClashes = 7 * population_size;
- static ArrayList<Boolean[][]> population = new ArrayList<>();
- static ArrayList<Integer> fittneses = new ArrayList<>();
- static ArrayList<Pair<Integer, Integer>> pro = new ArrayList<>();
- static final int number_of_queens = 8;
- static Random random = new Random();
- static void print_population(){
- System.out.println("-----------------------------------------------");
- System.out.println("-----------------------------------------------");
- for (Boolean[][] booleanses : population) {
- for (Boolean[] booleanse : booleanses) {
- System.out.println(Arrays.toString(booleanse));
- }
- System.out.println("-----------------------------------------------");
- }
- System.out.println("-----------------------------------------------");
- System.out.println("-----------------------------------------------");
- }
- static void init_population_and_fitnes() {
- for (int i = 0; i < population_size; i++) {
- Boolean queens[][] = new Boolean[number_of_queens][number_of_queens];
- for (int j = 0; j < number_of_queens; j++) {
- for (int k = 0; k < number_of_queens; k++) {
- queens[j][k] = random.nextBoolean();
- }
- }
- population.add(queens);
- }
- for (Boolean[][] booleans : population) {
- fittneses.add(fittnes(booleans));
- }
- int sum = 0, key = 0,i=0;
- while (i < fittneses.size()) {
- sum += fittneses.get(i++);
- pro.add(new Pair<>(key, sum));
- key=sum;
- }
- }
- static int numberOfAttacks(Boolean[][] queens, Pair<Integer, Integer> pos) {
- int count = 0;
- if (!queens[pos.getKey()][pos.getValue()]) {
- return count;
- }
- //column
- for (int j = 0; j < queens.length; j++) {
- if (queens[j][pos.getValue()]) {
- count++;
- }
- }
- //row
- for (int j = 0; j < queens.length; j++) {
- if (queens[pos.getKey()][j]) {
- count++;
- }
- }
- //left of principal
- for (int i = 0; i < queens.length; i++) {
- for (int j = 0; j < queens[i].length; j++) {
- if ((pos.getKey() + pos.getValue() == i + j) && queens[i][j] && pos.getKey() != i && pos.getValue() != j) {
- count++;
- }
- }
- }
- //right of principal up -part
- for (int k = pos.getKey() - 1, z = pos.getValue() - 1; k >= 0 && z >= 0; k--, z--) {
- if (queens[k][z]) {
- count++;
- }
- }
- //right of principal down -part
- for (int k = pos.getKey() + 1, z = pos.getValue() + 1; k < queens.length && z < queens.length; k++, z++) {
- if (queens[k][z]) {
- count++;
- }
- }
- return count - 2;
- }
- static int fittnes(Boolean[][] queens) {
- int count = 0;
- for (int i = 0; i < queens.length; i++) {
- for (int j = 0; j < queens[i].length; j++) {
- count += numberOfAttacks(queens, new Pair<>(i, j));
- }
- }
- return maxNumberOfClashes - count / 2;
- }
- static Pair<Boolean[][],Boolean[][]> selection(){
- int sumOfFitness=fittneses.stream().reduce(0, (a,b)->a+b);
- int rand_f = random.nextInt(sumOfFitness);
- int rand_m = random.nextInt(sumOfFitness);
- Boolean[][] father=null,mother=null;
- for (int i = 0; i < pro.size(); i++) {
- if(rand_f>=pro.get(i).getKey()&&rand_f<=pro.get(i).getValue())
- father = population.get(i);
- if(rand_m>=pro.get(i).getKey()&&rand_m<=pro.get(i).getValue())
- mother = population.get(i);
- }
- return new Pair<>(father,mother);
- }
- public static void main(String[] args) {
- init_population_and_fitnes();
- //print_population();
- /*
- for (Integer f : fittneses) {
- System.out.print(f+"\t");
- }
- System.out.println("");
- for (Pair<Integer, Integer> pair : pro) {
- System.out.println(pair + "\t");
- }
- */
- for (Boolean[] booleans : population.get(0)) {
- System.out.println(Arrays.toString(booleans));
- }
- System.out.println(fittnes(population.get(0)));
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement