Advertisement
HasanRasulov

8-queens-selection.java

Nov 25th, 2019
196
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5.02 KB | None | 0 0
  1. package pkg8.queens;
  2.  
  3. import java.util.ArrayList;
  4. import java.util.Arrays;
  5. import java.util.Random;
  6. import javafx.util.Pair;
  7.  
  8. public class Queens {
  9.  
  10.     static int population_size = 4;
  11.  
  12.     static int maxNumberOfClashes = 7 * population_size;
  13.  
  14.     static ArrayList<Boolean[][]> population = new ArrayList<>();
  15.  
  16.     static ArrayList<Integer> fittneses = new ArrayList<>();
  17.  
  18.     static ArrayList<Pair<Integer, Integer>> pro = new ArrayList<>();
  19.  
  20.     static final int number_of_queens = 8;
  21.  
  22.     static Random random = new Random();
  23.  
  24.     static void print_population(){
  25.        
  26.         System.out.println("-----------------------------------------------");
  27.         System.out.println("-----------------------------------------------");
  28.        
  29.         for (Boolean[][] booleanses : population) {
  30.             for (Boolean[] booleanse : booleanses) {
  31.                 System.out.println(Arrays.toString(booleanse));      
  32.             }
  33.             System.out.println("-----------------------------------------------");
  34.         }
  35.        
  36.         System.out.println("-----------------------------------------------");
  37.         System.out.println("-----------------------------------------------");
  38.     }
  39.    
  40.     static void init_population_and_fitnes() {
  41.  
  42.         for (int i = 0; i < population_size; i++) {
  43.  
  44.             Boolean queens[][] = new Boolean[number_of_queens][number_of_queens];
  45.  
  46.             for (int j = 0; j < number_of_queens; j++) {
  47.  
  48.                 for (int k = 0; k < number_of_queens; k++) {
  49.  
  50.                     queens[j][k] = random.nextBoolean();
  51.  
  52.                 }
  53.             }
  54.             population.add(queens);
  55.  
  56.         }
  57.  
  58.         for (Boolean[][] booleans : population) {
  59.             fittneses.add(fittnes(booleans));
  60.         }
  61.  
  62.         int sum = 0, key = 0,i=0;
  63.        
  64.         while (i < fittneses.size()) {
  65.             sum += fittneses.get(i++);
  66.            
  67.             pro.add(new Pair<>(key, sum));
  68.             key=sum;
  69.            
  70.         }
  71.     }
  72.  
  73.     static int numberOfAttacks(Boolean[][] queens, Pair<Integer, Integer> pos) {
  74.  
  75.         int count = 0;
  76.  
  77.         if (!queens[pos.getKey()][pos.getValue()]) {
  78.             return count;
  79.         }
  80.  
  81.         //column
  82.         for (int j = 0; j < queens.length; j++) {
  83.  
  84.             if (queens[j][pos.getValue()]) {
  85.                 count++;
  86.             }
  87.  
  88.         }
  89.         //row
  90.         for (int j = 0; j < queens.length; j++) {
  91.  
  92.             if (queens[pos.getKey()][j]) {
  93.                 count++;
  94.             }
  95.  
  96.         }
  97.         //left of principal
  98.         for (int i = 0; i < queens.length; i++) {
  99.  
  100.             for (int j = 0; j < queens[i].length; j++) {
  101.  
  102.                 if ((pos.getKey() + pos.getValue() == i + j) && queens[i][j] && pos.getKey() != i && pos.getValue() != j) {
  103.                     count++;
  104.                 }
  105.  
  106.             }
  107.  
  108.         }
  109.         //right of principal up -part
  110.         for (int k = pos.getKey() - 1, z = pos.getValue() - 1; k >= 0 && z >= 0; k--, z--) {
  111.  
  112.             if (queens[k][z]) {
  113.                 count++;
  114.             }
  115.  
  116.         }
  117.         //right of principal down -part
  118.         for (int k = pos.getKey() + 1, z = pos.getValue() + 1; k < queens.length && z < queens.length; k++, z++) {
  119.  
  120.             if (queens[k][z]) {
  121.                 count++;
  122.             }
  123.  
  124.         }
  125.  
  126.         return count - 2;
  127.     }
  128.  
  129.     static int fittnes(Boolean[][] queens) {
  130.  
  131.         int count = 0;
  132.  
  133.         for (int i = 0; i < queens.length; i++) {
  134.             for (int j = 0; j < queens[i].length; j++) {
  135.  
  136.                 count += numberOfAttacks(queens, new Pair<>(i, j));
  137.             }
  138.  
  139.         }
  140.         return maxNumberOfClashes - count / 2;
  141.     }
  142.  
  143.     static Pair<Boolean[][],Boolean[][]> selection(){
  144.        
  145.         int sumOfFitness=fittneses.stream().reduce(0, (a,b)->a+b);
  146.        
  147.         int rand_f = random.nextInt(sumOfFitness);
  148.         int rand_m = random.nextInt(sumOfFitness);
  149.        
  150.         Boolean[][] father=null,mother=null;
  151.        
  152.         for (int i = 0; i < pro.size(); i++) {
  153.            
  154.             if(rand_f>=pro.get(i).getKey()&&rand_f<=pro.get(i).getValue())
  155.                 father = population.get(i);
  156.              if(rand_m>=pro.get(i).getKey()&&rand_m<=pro.get(i).getValue())
  157.                 mother = population.get(i);
  158.            
  159.         }
  160.        
  161.         return new Pair<>(father,mother);
  162.        
  163.     }
  164.    
  165.     public static void main(String[] args) {
  166.  
  167.         init_population_and_fitnes();
  168.        
  169.         //print_population();
  170. /*
  171.         for (Integer f : fittneses) {
  172.             System.out.print(f+"\t");
  173.         }
  174.         System.out.println("");
  175.        
  176.         for (Pair<Integer, Integer> pair : pro) {
  177.              System.out.println(pair + "\t");
  178.         }
  179.        */
  180.              
  181.         for (Boolean[] booleans : population.get(0)) {
  182.                     System.out.println(Arrays.toString(booleans));
  183.  
  184.         }
  185.  
  186.  
  187.         System.out.println(fittnes(population.get(0)));
  188.  
  189.        
  190.        
  191.     }
  192.  
  193. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement