Advertisement
Guest User

Untitled

a guest
Jun 24th, 2017
49
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.78 KB | None | 0 0
  1. import java.util.Scanner;
  2. import java.util.Random;
  3. import java.util.Arrays;
  4.  
  5. class Genetic{
  6. public static void main(String args[]){
  7. // To solve n-queen problem
  8. Scanner scanner = new Scanner(System.in);
  9. Random rand = new Random();
  10.  
  11. int n = scanner.nextInt();
  12.  
  13. // number of boards to try
  14. final int BOARD_COUNT = 50;
  15.  
  16. // number of replaces
  17. final int REPLACE_COUNT = 2;
  18.  
  19. int[][][] boards = new int[2][BOARD_COUNT][n];
  20. int[] fitness = new int[BOARD_COUNT];
  21.  
  22. // fill up the table with random data
  23. for (int i = 0; i < BOARD_COUNT ; i++){
  24. for(int j = 0; j < n; j++){
  25. boards[0][i][j] = rand.nextInt(n) + 1;
  26. }
  27. }
  28.  
  29. int temp = 0;
  30. int iteration = 0;
  31.  
  32. int first = 0;
  33. int second = 0;
  34.  
  35. boolean found = false;
  36.  
  37. // do the mutations until solution foundConst
  38. while(!found){
  39. // get the fitness values
  40. for(int i = 0; i < BOARD_COUNT; i ++){
  41. temp = getFitness(boards[iteration][i], n);
  42.  
  43. // check solution found
  44. if (temp == (n * (n-1))/2){
  45. System.out.println(Arrays.toString(boards[iteration][i]));
  46. found = true;
  47. break;
  48. }
  49.  
  50. fitness[i] = temp;
  51. }
  52.  
  53. // calculate the selection probabilities based on fitnes
  54. for(int i = 1; i < BOARD_COUNT; i++){
  55. fitness[i] += fitness[i-1];
  56. }
  57.  
  58. // do the mutations based on the probabilities
  59.  
  60. for(int mutations = 0 ; mutations < BOARD_COUNT; mutations++){
  61. // first value
  62. temp = rand.nextInt(fitness[fitness.length - 1]);
  63. first = getLesserIndex(fitness, temp, 0, fitness.length - 1);
  64.  
  65. // find the second
  66. temp = rand.nextInt(fitness[fitness.length - 1]);
  67. second = getLesserIndex(fitness, temp, 0, fitness.length - 1);
  68.  
  69. // cut location
  70. temp = rand.nextInt(n);
  71.  
  72. for(int i = 0; i < temp; i++){
  73. boards[(iteration+1)%2][mutations][i] = boards[iteration][first][i];
  74. }
  75.  
  76. for(int i = temp; i < n; i++){
  77. boards[(iteration+1)%2][mutations][i] = boards[iteration][second][i];
  78. }
  79.  
  80. // Do random replacing
  81. for(int i = 0; i < REPLACE_COUNT; i ++){
  82. boards[(iteration+1)%2][mutations][rand.nextInt(n)] = rand.nextInt(n) + 1;
  83. }
  84. }
  85.  
  86. iteration ++;
  87. iteration %= 2;
  88. }
  89. }
  90.  
  91. // Calculate the fitness
  92. public static int getFitness(int[] board, int n){
  93. // number of non attacking pairs
  94. int fitness = (n * (n - 1))/2;
  95.  
  96. // check for each queen in column
  97. for(int i = 0; i < board.length ; i++){
  98. int pos = board[i];
  99. for(int j = 1 ; j < board.length - i; j++){
  100. // check horizontal
  101. if (board[i+j] == pos || board[i+j] == pos - j || board[i+j] == pos + j){
  102. fitness--;
  103. continue;
  104. }
  105. }
  106. }
  107.  
  108. return fitness;
  109. }
  110.  
  111. public static int getLesserIndex(int[] numbers, int value, int i, int j){
  112. if (i==j){
  113. return i;
  114. }
  115.  
  116. if (j-i == 1){
  117. return (numbers[i]<value) ? i : j;
  118. }
  119.  
  120. int loc = i + (j - i + 1) / 2;
  121.  
  122. if (numbers[loc] == value)
  123. return loc;
  124.  
  125. if (numbers[loc] < value){
  126. return getLesserIndex(numbers, value, loc, j);
  127. }else{
  128. return getLesserIndex(numbers, value, i, loc);
  129. }
  130.  
  131. }
  132. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement