Advertisement
Guest User

Untitled

a guest
Jan 16th, 2017
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 10.97 KB | None | 0 0
  1. package pl.smashed.berlin;
  2.  
  3. import java.io.File;
  4. import java.util.Scanner;
  5.  
  6. import javax.swing.plaf.metal.MetalIconFactory.FolderIcon16;
  7.  
  8. import java.util.Collections;
  9. import java.util.HashMap;
  10. import java.util.List;
  11. import java.util.Map.Entry;
  12. import java.util.Random;
  13. import java.util.ArrayList;
  14. import java.util.Arrays;
  15. import java.util.Collection;
  16.  
  17. public class Main {
  18.  
  19. private static final int NUMBER_OF_ITERATIONS = 200000;
  20. private static final int NUMBER_OF_TOURISTS = 20;
  21.  
  22. public static void main(String[] args) throws Exception {
  23.  
  24. String fileName = "att48.txt";
  25. String logRecord;
  26. File dataFile = new File(fileName);
  27. String[] splitLine = null;
  28. Scanner scanner = new Scanner(dataFile);
  29. int rowSize = Integer.valueOf(scanner.nextLine()); // pierwsza linia wczytanego dokumentu (ilosc wierszy w pliku)
  30. int[][] tab = new int[rowSize][rowSize];
  31. int[][] tourist = new int[NUMBER_OF_TOURISTS][rowSize];
  32. int[] ocena = new int[NUMBER_OF_TOURISTS];
  33.  
  34. shuffleArrayList(rowSize, tourist);
  35.  
  36. int i = 0;
  37. while (scanner.hasNextLine()) {
  38. logRecord = scanner.nextLine();
  39. splitLine = logRecord.split("\\s");
  40.  
  41. for (int j = 0; j < splitLine.length; j++) {
  42. if (!"0".equals(splitLine[j])) {
  43. tab[i][j] = Integer.valueOf(splitLine[j]);
  44. tab[j][i] = Integer.valueOf(splitLine[j]);
  45. }
  46. }
  47. i++;
  48. }
  49. //---------------------------------------------------------------------------------------------------------------------------------------
  50. for (int d = 0; d < NUMBER_OF_TOURISTS; d++) {
  51. System.out.printf("Wiersz %2d. ", d+1); // numerowanie wierszy
  52. for (int j = 0; j < rowSize; j++) {
  53. System.out.printf("%5d", tourist[d][j]); // wypisanie pojedynczego miasta (dlugosci)???
  54. }
  55. ocena[d] = countDistance(tab, tourist[d]);
  56. System.out.printf(" :: %s ", ocena[d]);
  57. System.out.println();
  58. }
  59. System.out.println("\nOceny: ");
  60. for (int j = 0; j < NUMBER_OF_TOURISTS; j++) {
  61. System.out.printf("%5d ", ocena[j]);
  62. }
  63. System.out.println("\n");
  64. System.out.println("Najkrótsza trasa: " + getMin(ocena) + "\n\n");
  65. // System.out.println("Najlepsza trasa: "+selekcjaTurniej(ocena));
  66. //----------------------------------------------------------------------------------------------------------
  67. // ----------------------------------------------------------------------------------------------------------
  68. //int minVal = 99999;
  69. //for (int j = 0; j < NUMBER_OF_ITERATIONS; j++) {
  70.  
  71.  
  72. int[][] touristTMP = tourist;
  73. int tmpOcena = Integer.MAX_VALUE;
  74.  
  75. for (int k = 0; k < NUMBER_OF_ITERATIONS; k++) {
  76.  
  77. int[] indeksySelekcja = selekcjaTurniej(ocena); //indekx najmniejszego elementu z ocena[]
  78.  
  79.  
  80.  
  81. int[][] touristChild = new int[NUMBER_OF_TOURISTS][rowSize];
  82.  
  83. touristChild = crossOver(indeksySelekcja, touristChild, touristTMP); /// mamy pokrzyzowana populacje (wynik tablica[][])
  84.  
  85.  
  86. touristChild = mutacja(indeksySelekcja, touristChild);
  87.  
  88.  
  89.  
  90. for (int d = 0; d < NUMBER_OF_TOURISTS; d++) {
  91.  
  92. ocena[d] = countDistance(tab, touristChild[d]);
  93.  
  94. }
  95.  
  96.  
  97.  
  98. if(getMin(ocena) < tmpOcena) {
  99. tmpOcena = getMin(ocena);
  100. }
  101.  
  102. touristTMP = touristChild;
  103.  
  104. }
  105. System.out.println("--------------------------------------------");
  106. System.out.println("Najkrótsza trasa: " + tmpOcena + "\n\n");
  107.  
  108. /*
  109.  
  110. int[][] krzyzowanieArr = crossOver(indeksySelekcja, touristChild, tourist);
  111. // --------------------------------------------------------------------
  112.  
  113. int[] ocena2 = new int[NUMBER_OF_TOURISTS];
  114.  
  115. //printMultiArr(crossOverArr); // wyswietla tablice
  116.  
  117. for (int d = 0; d < NUMBER_OF_TOURISTS; d++) {
  118. System.out.printf("Wiersz %2d. ", d+1); // numerowanie wierszy
  119. for (int j = 0; j < rowSize; j++) {
  120. System.out.printf("%5d", krzyzowanieArr[d][j]); // wypisanie pojedynczego miasta (dlugosci)???
  121. }
  122. ocena[d] = countDistance(tab, krzyzowanieArr[d]);
  123. System.out.printf(" :: %s ", ocena2[d]);
  124. System.out.println();
  125. }
  126. System.out.println("\nOceny: ");
  127. for (int j = 0; j < NUMBER_OF_TOURISTS; j++) {
  128. System.out.printf("%5d ", ocena2[j]);
  129. }
  130. System.out.println("\n");
  131. System.out.println("Najkrótsza trasa: " + getMin(ocena2) + "\n\n");
  132. // System.out.println("Najlepsza trasa: "+selekcjaTurniej(ocena));
  133.  
  134. System.out.println();
  135. System.out.println("-----------------------------------------------------");
  136. System.out.println();*/
  137.  
  138. //----------------------------------------------------------------------------------------------------------
  139. //----------------------------------------------------------------------------------------------------------
  140.  
  141.  
  142. //int[][] crossOverArr = crossOver(indeksySelekcja, touristChild, tourist);
  143.  
  144.  
  145. /*
  146. System.out.println("\nOceny: ");
  147. for (int j = 0; j < NUMBER_OF_TOURISTS; j++) {
  148. System.out.printf("%5d ", indeksySelekcja[j]);
  149. }
  150. System.out.println("\n");
  151. System.out.println("Najkrótsza trasa: " + getMin(indeksySelekcja) + "\n\n");
  152. }*/
  153.  
  154. }
  155.  
  156.  
  157.  
  158.  
  159.  
  160.  
  161. // }
  162. // --------------------------------------------------------------------
  163. // int mutedArr[][] = mutacja(indeksySelekcja, touristChild, touristChild, rowSize);
  164.  
  165. /*
  166. for (int[] val : touristChild) {
  167. for (int v : val) {
  168. System.out.print(v + " ");
  169.  
  170. }
  171. System.out.println();
  172. }
  173.  
  174. // --------------------------------------------
  175. for (int d = 0; d < NUMBER_OF_TOURISTS; d++) {
  176. System.out.printf("%2d. ", d);
  177. for (int j = 0; j < rowSize; j++) {
  178. System.out.printf("%5d", mutedArr[d][j]);
  179. }
  180. ocena[d] = countDistance(tab, mutedArr[d]);
  181. System.out.printf(" :: %s ", ocena[d]);
  182. System.out.println();
  183. }
  184. System.out.println("\nOcena: ");
  185. for (int j = 0; j < 20; j++) {
  186. System.out.printf("%5d ", ocena[j]);
  187. }
  188.  
  189. System.out.println("\n");
  190.  
  191. // minVal = getMin(ocena);
  192.  
  193. if (getMin(ocena) < minVal) {
  194. minVal = getMin(ocena);
  195.  
  196. }
  197. System.out.println(getMin(ocena));
  198. // }
  199. System.out.println("Najmniejsza ocena: " + minVal);*/
  200. //}
  201.  
  202.  
  203.  
  204. // --------------------------METODY----------------------------------------
  205.  
  206. private static void printMultiArr(int[][] crossOverArr) {
  207. for (int[] val : crossOverArr) {
  208. for (int v : val) {
  209. System.out.print(v + " ");
  210.  
  211. }
  212. System.out.println();
  213. }
  214. }
  215.  
  216. // Mieszanie wartości w tablicy
  217. private static void shuffleArrayList(int rowSize, int[][] tourist) {
  218. for (int i = 0; i < tourist.length; i++) {
  219.  
  220. ArrayList<Integer> numbers = new ArrayList<>();
  221. for (int q = 0; q < rowSize; q++) {
  222. numbers.add(q + 1);
  223. }
  224. Collections.shuffle(numbers);
  225. for (int j = 0; j < rowSize; j++) {
  226. tourist[i][j] = numbers.get(j);
  227. }
  228. }
  229. }
  230.  
  231. //Znajdź min z tablicy[]
  232. public static int getMin(int[] inputArray) {
  233. int minValue = inputArray[0];
  234. for (int i = 1; i < inputArray.length; i++) {
  235. if (inputArray[i] < minValue) {
  236. minValue = inputArray[i];
  237. }
  238. }
  239. return minValue;
  240. }
  241.  
  242. // Licz distans
  243. private static int countDistance(int[][] tab, int[] user) {
  244. int trasa = 0;
  245. int a, b;
  246. for (int i = 0; i < user.length; i++) {
  247. a = user[i];
  248. if (i == (user.length - 1)) {
  249. b = user[0];
  250. } else {
  251. b = user[i + 1];
  252. }
  253. trasa += tab[a - 1][b - 1];
  254. }
  255. return trasa;
  256. }
  257.  
  258. private static int[] selekcjaTurniej(int ocena[]) {
  259. Random r = new Random();
  260. int k = 2; // skacz co dwie oceny
  261.  
  262. int[] indeksyTurniej = new int[NUMBER_OF_TOURISTS];
  263. for (int j = 0; j < NUMBER_OF_TOURISTS; j++) {
  264. int ocenaMin = Integer.MAX_VALUE; //największa możliwa wartość, zamiast tego 9999
  265. int indexMin = -1; // -1 b ona pewno go nie ma, deklaracja
  266. for (int i = 0; i < k; i++) {
  267. int randIndeks = r.nextInt(NUMBER_OF_TOURISTS);
  268. if (ocena[randIndeks] < ocenaMin) {
  269. indexMin = randIndeks;
  270. ocenaMin = ocena[randIndeks];
  271. }
  272. }
  273. indeksyTurniej[j] = indexMin;
  274. }
  275.  
  276. return indeksyTurniej;
  277.  
  278. }
  279.  
  280. // ----------------------Krzyżowanie----------------------
  281.  
  282. // Porównanie indeksów ????????????
  283. private static int indeksOf(int[] tab, int el) {
  284. for (int i = 0; i < tab.length; i++) {
  285. if (tab[i] == el)
  286. return i;
  287. }
  288. return -1;
  289. }
  290.  
  291. // Krzyzowanie
  292. private static int[][] crossOver(int[] indeksySelekcja, int[][] touristChild, int[][] tourist) {
  293.  
  294. for (int i = 0; i < indeksySelekcja.length; i += 2) {
  295. int p1 = indeksySelekcja[i];
  296. int p2 = indeksySelekcja[i + 1];
  297. Random r = new Random();
  298. int cross1 = r.nextInt(tourist[0].length - 3) + 1;
  299. int cross2 = r.nextInt(tourist[0].length - 1 - cross1) + cross1 + 1;
  300.  
  301. // zamiana środka
  302. for (int j = cross1; j <= cross2; j++) {
  303. touristChild[i][j] = tourist[p1][j];
  304. touristChild[i + 1][j] = tourist[p2][j];
  305. }
  306.  
  307. // krzyżowanie PMX
  308. for (int j = 0; j < cross1; j++) {
  309. int city1 = tourist[p2][j];
  310. int ind;
  311. // zamiana nieistniejących środka
  312. while ((ind = indeksOf(touristChild[i], city1)) != -1) {
  313. city1 = tourist[p2][ind];
  314. }
  315. touristChild[i][j] = city1;
  316. }
  317.  
  318. for (int j = cross2 + 1; j < tourist[0].length; j++) {
  319. int city1 = tourist[p2][j];
  320. int ind;
  321.  
  322. // zamiana nieistniejących środka
  323. while ((ind = indeksOf(touristChild[i], city1)) != -1) {
  324. city1 = tourist[p2][ind];
  325. }
  326. touristChild[i][j] = city1;
  327. }
  328.  
  329. for (int j = 0; j < cross1; j++) {
  330. int city1 = tourist[p1][j];
  331. int ind;
  332. // zamiana nieistniejących środka
  333. while ((ind = indeksOf(touristChild[i + 1], city1)) != -1) {
  334. city1 = tourist[p1][ind];
  335. }
  336. touristChild[i + 1][j] = city1;
  337. }
  338.  
  339. for (int j = cross2 + 1; j < tourist[0].length; j++) {
  340. int city1 = tourist[p1][j];
  341. int ind;
  342. // zamiana nieistniejących środka
  343. while ((ind = indeksOf(touristChild[i + 1], city1)) != -1) {
  344. city1 = tourist[p1][ind];
  345. }
  346. touristChild[i + 1][j] = city1;
  347. }
  348. }
  349.  
  350. return touristChild;
  351. }
  352.  
  353. // Mutacja
  354. private static int[][] mutacja(int[] indeksySelekcja, int[][] touristChild) {
  355. // losowanie przeciec
  356. int cross1 = 0;
  357. int cross2 = 0;
  358.  
  359. for (int i = 0; i < indeksySelekcja.length; i++) {
  360.  
  361. Random r = new Random();
  362.  
  363. cross1 = r.nextInt(touristChild[0].length - 3) + 1;
  364. cross2 = r.nextInt(touristChild[0].length - 1 - cross1) + cross1 + 1;
  365.  
  366. ArrayList<Integer> touristChildTmpSrodek = new ArrayList<>();
  367.  
  368. // przypisujemy srodek z touristCHild
  369. for (int j = cross1; j <= cross2; j++) {
  370. touristChildTmpSrodek.add(touristChild[i][j]);
  371. }
  372.  
  373. Collections.reverse(touristChildTmpSrodek);
  374.  
  375.  
  376. int tmpIndex = 0;
  377. for (int j = cross1; j <= cross2; j++) {
  378.  
  379. touristChild[i][j] = touristChildTmpSrodek.get(tmpIndex);
  380. tmpIndex++;
  381.  
  382.  
  383. /* System.out.print("cross1 = "+cross1);
  384.  
  385. System.out.print("cross2 = "+cross2); System.out.println();*/
  386.  
  387. }
  388.  
  389. }
  390.  
  391. return touristChild;
  392.  
  393. }
  394.  
  395. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement