HasanRasulov

8-Queens.java

Dec 9th, 2019
571
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 12.07 KB | None | 0 0
  1.  
  2.  
  3. import java.util.*;
  4. import java.util.function.Supplier;
  5. import javafx.util.Pair;
  6.  
  7. class Individual {
  8.  
  9.     boolean[][] gene;
  10.     int number_of_attacks;
  11.     Pair<Integer, Integer> subInterval;
  12.     int fitness;
  13.     final int number_of_queens = 8;
  14.  
  15.     public Individual() {
  16.  
  17.         number_of_attacks = 0;
  18.         fitness = 0;
  19.         subInterval = new Pair<>(0, 0);
  20.         gene = new boolean[number_of_queens][number_of_queens];
  21.  
  22.     }
  23.  
  24.     public Individual(int number_of_attacks, int fitness, boolean[][] queens) {
  25.  
  26.         this.number_of_attacks = number_of_attacks;
  27.         this.fitness = fitness;
  28.         this.gene = queens;
  29.  
  30.     }
  31.  
  32. }
  33.  
  34. public class Queens {
  35.  
  36.     static int population_size = 7;
  37.  
  38.     static final int maxNumberOfClashes = 28;
  39.  
  40.     static float pro_mutation = 0.0005f;
  41.  
  42.     static ArrayList<Individual> population = new ArrayList<>();
  43.  
  44.     static ArrayList<Individual> fittest_individuals = new ArrayList<>();
  45.  
  46.     static final int number_of_queens = 8;
  47.  
  48.     static Random random = new Random();
  49.  
  50.     static int sumOfFit = 0;
  51.  
  52.     static void print_population() {
  53.  
  54.         System.out.println("-----------------------------------------------");
  55.         System.out.println("-----------------------------------------------");
  56.  
  57.         population.stream().forEach((ind) -> {
  58.             System.out.println("fitness:" + ind.fitness);
  59.             System.out.println("from:" + ind.subInterval.getKey() + "\tto:" + ind.subInterval.getValue());
  60.             for (boolean[] booleans : ind.gene) {
  61.                 System.out.println(Arrays.toString(booleans));
  62.             }
  63.             System.out.println("--------------------------------------------------------");
  64.  
  65.         });
  66.  
  67.         System.out.println("-----------------------------------------------");
  68.         System.out.println("-----------------------------------------------");
  69.     }
  70.  
  71.     static void init_population() {
  72.  
  73.         for (int i = 0; i < population_size; i++) {
  74.  
  75.             boolean queens[][] = new boolean[number_of_queens][number_of_queens];
  76.  
  77.             for (int j = 0; j < number_of_queens; j++) {
  78.  
  79.                 for (int k = 0; k < number_of_queens; k++) {
  80.  
  81.                     queens[j][k] = false;
  82.                 }
  83.  
  84.                 queens[j][random.nextInt(number_of_queens)] = true;
  85.  
  86.             }
  87.  
  88.             population.add(new Individual(number_of_attacks(queens), fittnes(number_of_attacks(queens)), queens));
  89.  
  90.         }
  91.  
  92.     }
  93.  
  94.     static void subInterval(ArrayList<Individual> inds) {
  95.  
  96.         sumOfFit = population.stream().map((ind) -> ind.fitness).reduce(0, (a, b) -> a + b);
  97.  
  98.         int to = 0, from = 0;
  99.         int i = 0;
  100.  
  101.         while (i < inds.size()) {
  102.  
  103.             to += inds.get(i).fitness;
  104.  
  105.             inds.get(i).subInterval = new Pair<>(from, to);
  106.             from = to;
  107.             i++;
  108.         }
  109.  
  110.     }
  111.  
  112.     static int numberOfAttacksOfOneQueen(boolean[][] queens, Pair<Integer, Integer> pos) {
  113.  
  114.         int count = 0;
  115.  
  116.         if (!queens[pos.getKey()][pos.getValue()]) {
  117.             return count;
  118.         }
  119.  
  120.         //column
  121.         for (boolean[] queen : queens) {
  122.             if (queen[pos.getValue()]) {
  123.                 count++;
  124.             }
  125.         }
  126.         //row
  127.         for (int j = 0; j < queens.length; j++) {
  128.  
  129.             if (queens[pos.getKey()][j]) {
  130.                 count++;
  131.             }
  132.  
  133.         }
  134.         //left of principal
  135.         for (int i = 0; i < queens.length; i++) {
  136.  
  137.             for (int j = 0; j < queens[i].length; j++) {
  138.  
  139.                 if ((pos.getKey() + pos.getValue() == i + j) && queens[i][j] && pos.getKey() != i && pos.getValue() != j) {
  140.                     count++;
  141.                 }
  142.  
  143.             }
  144.  
  145.         }
  146.         //right of principal up -part
  147.         for (int k = pos.getKey() - 1, z = pos.getValue() - 1; k >= 0 && z >= 0; k--, z--) {
  148.  
  149.             if (queens[k][z]) {
  150.                 count++;
  151.             }
  152.  
  153.         }
  154.         //right of principal down -part
  155.         for (int k = pos.getKey() + 1, z = pos.getValue() + 1; k < queens.length && z < queens.length; k++, z++) {
  156.  
  157.             if (queens[k][z]) {
  158.                 count++;
  159.             }
  160.  
  161.         }
  162.  
  163.         return count - 2;
  164.     }
  165.  
  166.     static int number_of_attacks(boolean[][] queens) {
  167.         int count = 0;
  168.  
  169.         for (int i = 0; i < queens.length; i++) {
  170.             label:
  171.             for (int j = 0; j < queens[i].length; j++) {
  172.                 if (queens[i][j]) {
  173.                     count += numberOfAttacksOfOneQueen(queens, new Pair<>(i, j));
  174.                     continue label;
  175.                 }
  176.             }
  177.  
  178.         }
  179.         return count / 2;
  180.     }
  181.  
  182.     static int fittnes(int count) {
  183.         return maxNumberOfClashes - count;
  184.     }
  185.  
  186.     static Pair<Individual, Individual> selection() {
  187.  
  188.         System.out.println("-----------------Selection------------------------");
  189.  
  190.         int rand_m = random.nextInt(sumOfFit);
  191.         int rand_f = random.nextInt(sumOfFit);
  192.  
  193.         Individual father = new Individual();
  194.         Individual mother = new Individual();
  195.  
  196.         for (int i = 0; i < population.size(); ++i) {
  197.  
  198.             Individual ind = population.get(i);
  199.  
  200.             if (rand_f >= ind.subInterval.getKey() && rand_f < ind.subInterval.getValue()) {
  201.                 father.fitness = ind.fitness;
  202.                 father.number_of_attacks = ind.number_of_attacks;
  203.  
  204.                 for (int j = 0; j < number_of_queens; j++) {
  205.  
  206.                     System.arraycopy(ind.gene[j], 0, father.gene[j], 0, ind.gene[j].length);
  207.  
  208.                 }
  209.  
  210. //                continue;
  211.             }
  212.             if (rand_m >= ind.subInterval.getKey() && rand_m < ind.subInterval.getValue()) {
  213.  
  214.                 mother.fitness = ind.fitness;
  215.                 mother.number_of_attacks = ind.number_of_attacks;
  216.  
  217.                 for (int j = 0; j < number_of_queens; j++) {
  218.  
  219.                     System.arraycopy(ind.gene[j], 0, mother.gene[j], 0, ind.gene[j].length);
  220.  
  221.                 }
  222.  
  223.             }
  224.  
  225.         }
  226.  
  227.         return new Pair<>(father, mother);
  228.  
  229.     }
  230.  
  231.     static Pair<Individual, Individual> crossover(Pair<Individual, Individual> selectedParents) {
  232.  
  233.         System.out.println("--------------Crossover---------------------------");
  234.  
  235.         int croosPoint = random.nextInt(number_of_queens);
  236.  
  237.         Individual father = selectedParents.getKey();
  238.         Individual mother = selectedParents.getValue();
  239.  
  240.         boolean[][] father_cp = new boolean[number_of_queens][number_of_queens];
  241.         boolean[][] mother_cp = new boolean[number_of_queens][number_of_queens];
  242.  
  243.         for (int i = 0; i < number_of_queens; i++) {
  244.  
  245.             System.arraycopy(father.gene[i], 0, father_cp[i], 0, father.gene[i].length);
  246.             System.arraycopy(mother.gene[i], 0, mother_cp[i], 0, mother.gene[i].length);
  247.  
  248.         }
  249.  
  250.         for (int i = croosPoint; i < number_of_queens; i++) {
  251.  
  252.             System.arraycopy(mother_cp[i], 0, father.gene[i], 0, mother_cp[i].length);
  253.             System.arraycopy(father_cp[i], 0, mother.gene[i], 0, father_cp[i].length);
  254.         }
  255.  
  256.         return selectedParents;
  257.     }
  258.  
  259.     static Pair<Individual, Individual> mutation(Pair<Individual, Individual> parents) {
  260.  
  261.         System.out.println("----------------Mutation-------------------------");
  262.  
  263.         Individual father = parents.getKey();
  264.         Individual mother = parents.getValue();
  265.  
  266.         Supplier<Boolean> r = () -> random.nextFloat() < pro_mutation;
  267.  
  268.         for (int i = 0; i < number_of_queens; i++) {
  269.  
  270.             if (random.nextInt(100) == 1) {
  271.                 Arrays.fill(father.gene[i], false);
  272.                 father.gene[i][random.nextInt(number_of_queens)] = true;
  273.             }
  274.  
  275.         }
  276.  
  277.         for (int i = 0; i < number_of_queens; i++) {
  278.  
  279.             if (r.get()) {
  280.                 Arrays.fill(mother.gene[i], false);
  281.                 mother.gene[i][random.nextInt(number_of_queens)] = true;
  282.             }
  283.  
  284.         }
  285.  
  286.         father.number_of_attacks = number_of_attacks(father.gene);
  287.         father.fitness = fittnes(father.number_of_attacks);
  288.  
  289.         mother.number_of_attacks = number_of_attacks(mother.gene);
  290.         mother.fitness = fittnes(mother.number_of_attacks);
  291.  
  292.         return parents;
  293.     }
  294.  
  295.     static void fittest() {
  296.  
  297.         for (int i = 0; i < population.size(); i++) {
  298.             if (population.get(i).fitness == maxNumberOfClashes) {
  299.                 fittest_individuals.add(population.get(i));
  300.             }
  301.         }
  302.  
  303.     }
  304.  
  305.     static void printMatingPool(Pair<Individual, Individual> parents) {
  306.  
  307.         System.out.println("--------------------MatingPool-----------------");
  308.         System.out.println("-------------------------------Father-------------------------------");
  309.         for (boolean[] booleanses : parents.getKey().gene) {
  310.             System.out.println(Arrays.toString(booleanses));
  311.         }
  312.  
  313.         System.out.println("-------------------------------Mother-------------------------------");
  314.         for (boolean[] booleanses : parents.getValue().gene) {
  315.             System.out.println(Arrays.toString(booleanses));
  316.         }
  317.         System.out.println("-----------------------------------------------------------------");
  318.     }
  319.  
  320.     static boolean check(Pair<Individual, Individual> pair) {
  321.  
  322.         for (int i = 0; i < pair.getKey().gene.length; i++) {
  323.  
  324.             boolean f = false;
  325.  
  326.             for (int j = 0; j < pair.getKey().gene[i].length; j++) {
  327.  
  328.                 if (!pair.getKey().gene[i][j]) {
  329.                     f = true;
  330.                 }
  331.  
  332.             }
  333.  
  334.             if (f) {
  335.                 return false;
  336.             }
  337.         }
  338.  
  339.         for (int i = 0; i < pair.getValue().gene.length; i++) {
  340.  
  341.             boolean f = false;
  342.  
  343.             for (int j = 0; j < pair.getValue().gene[i].length; j++) {
  344.  
  345.                 if (!pair.getValue().gene[i][j]) {
  346.                     f = true;
  347.                 }
  348.  
  349.             }
  350.  
  351.             if (f) {
  352.                 return false;
  353.             }
  354.         }
  355.  
  356.         return true;
  357.  
  358.     }
  359.  
  360.     public static void main(String[] args) {
  361.  
  362.         init_population();
  363.  
  364.         fittest();
  365.  
  366.         subInterval(population);
  367.  
  368.         if (!fittest_individuals.isEmpty()) {
  369.  
  370.             fittest_individuals.forEach((ind) -> {
  371.                 for (boolean[] booleanse : ind.gene) {
  372.                     System.out.println(Arrays.toString(booleanse));
  373.                 }
  374.                 System.out.println("-------------------------------------");
  375.             });
  376.             return;
  377.         }
  378.  
  379.         int gen = 0;
  380.         /*
  381.         System.out.println("----------------Population--size:" + population.size() + "--------------------------");
  382.         print_population();
  383.         System.out.println("-----------------------------------------");
  384.         System.out.println("-----------------------Mate------------------");
  385.  
  386.         printMatingPool(selection());
  387.          */
  388.  
  389.         while (fittest_individuals.isEmpty()) {
  390.  
  391.             System.out.println("----------------Population--size:" + population.size() + "--------------------------");
  392.             print_population();
  393.             System.out.println("-----------------------------------------");
  394.  
  395.             Pair<Individual, Individual> matingPool = selection();
  396.             printMatingPool(matingPool);
  397.  
  398.             matingPool = crossover(matingPool);
  399.             printMatingPool(matingPool);
  400.  
  401.             matingPool = mutation(matingPool);
  402.             printMatingPool(matingPool);
  403.  
  404.             population.add(matingPool.getKey());
  405.             population.add(matingPool.getValue());
  406.  
  407.             fittest();
  408.             subInterval(population);
  409.  
  410.             System.out.println("gen:" + gen++);
  411.         }
  412.  
  413.         System.out.println("-----------------Fittest Individuals------------------------");
  414.  
  415.         fittest_individuals.forEach((ind) -> {
  416.             for (boolean[] booleanse : ind.gene) {
  417.                 System.out.println(Arrays.toString(booleanse));
  418.             }
  419.             System.out.println("-------------------------------------");
  420.         });
  421.  
  422.     }
  423.  
  424. }
Advertisement
Add Comment
Please, Sign In to add comment