Advertisement
Guest User

Untitled

a guest
Jan 22nd, 2017
127
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 14.43 KB | None | 0 0
  1. package pl.smashed.berlin;
  2.  
  3. import java.io.File;
  4. import java.io.FileNotFoundException;
  5. import java.io.FileOutputStream;
  6. import java.io.IOException;
  7. import java.util.Scanner;
  8.  
  9. import org.apache.commons.codec.binary.StringUtils;
  10. import org.apache.poi.hssf.usermodel.HSSFCell;
  11. import org.apache.poi.hssf.usermodel.HSSFRow;
  12. import org.apache.poi.hssf.usermodel.HSSFSheet;
  13. import org.apache.poi.hssf.usermodel.HSSFWorkbook;
  14.  
  15. import com.csvreader.CsvWriter;
  16.  
  17. import java.util.Collections;
  18. import java.util.Random;
  19. import java.util.ArrayList;
  20. import java.util.Arrays;
  21.  
  22. public class Main {
  23.  
  24. private static final int NUMBER_OF_ITERATIONS = 400000;
  25. private static final int NUMBER_OF_TOURISTS = 40;
  26.  
  27. public static void main(String[] args) throws Exception {
  28. /*
  29. * ArrayList<Main> obiektyArr = new ArrayList<Main>(); Main obiekt = new
  30. * Main();
  31. */
  32.  
  33. /*
  34. * for (int i = 0; i < NUMBER_OF_ITERATIONS; i++) { obiektyArr.add(new
  35. * Main()); }
  36. */
  37.  
  38. String fileName = "att48.txt";
  39. String logRecord;
  40. File dataFile = new File(fileName);
  41. String[] splitLine = null;
  42. Scanner scanner = new Scanner(dataFile);
  43. int rowSize = Integer.valueOf(scanner.nextLine()); // pierwsza linia
  44. // wczytanego
  45. // dokumentu (ilosc
  46. // wierszy w pliku)
  47. int[][] tab = new int[rowSize][rowSize];
  48. int[][] tourist = new int[NUMBER_OF_TOURISTS][rowSize];
  49. int[] ocena = new int[NUMBER_OF_TOURISTS];
  50.  
  51. shuffleArrayList(rowSize, tourist);
  52.  
  53. int i = 0;
  54. while (scanner.hasNextLine()) {
  55. logRecord = scanner.nextLine();
  56. splitLine = logRecord.split("\\s");
  57.  
  58. for (int j = 0; j < splitLine.length; j++) {
  59. if (!"0".equals(splitLine[j])) {
  60. tab[i][j] = Integer.valueOf(splitLine[j]);
  61. tab[j][i] = Integer.valueOf(splitLine[j]);
  62. }
  63. }
  64. i++;
  65. }
  66.  
  67. int[][] touristChild = new int[NUMBER_OF_TOURISTS][rowSize];
  68. int[][] touristMutted = new int[NUMBER_OF_TOURISTS][rowSize];
  69. int[][] touristTMP = new int[NUMBER_OF_TOURISTS][rowSize];
  70.  
  71. for (int j = 0; j < tourist.length; j++) {
  72. System.arraycopy(tourist[j], 0, touristTMP[j], 0, touristTMP[j].length);
  73. }
  74.  
  75. int tmpOcena = Integer.MAX_VALUE;
  76.  
  77. // DUZA
  78. // PETLA------------------------------------------------------------------
  79. for (int k = 1; k < NUMBER_OF_ITERATIONS; k++) {
  80. int[] indeksySelekcja = new int[NUMBER_OF_TOURISTS];
  81. /*
  82. if(k<((3*NUMBER_OF_ITERATIONS)/4)){
  83.  
  84. indeksySelekcja = selekcjaTurniej(ocena); } else {
  85. */
  86.  
  87. if(k%20000<10000){
  88. System.out.println("Turniej");
  89. indeksySelekcja = selekcjaTurniej(ocena);
  90.  
  91. } else {
  92. System.out.println("Ruletka");
  93. indeksySelekcja = ruletka(ocena);
  94.  
  95. }
  96.  
  97.  
  98. /*System.out.println("\nRul: ");
  99. for (int j = 0; j < NUMBER_OF_TOURISTS; j++) {
  100. System.out.printf("%5d ", indeksySelekcja[j]);
  101. }*/
  102. // }
  103.  
  104. touristChild = crossOver(indeksySelekcja, touristTMP, rowSize);
  105.  
  106. System.arraycopy(touristChild, 0, touristTMP, 0, touristTMP.length);
  107.  
  108. touristMutted = mutacja(indeksySelekcja, touristTMP);
  109. // indeksySelekcja = null;
  110.  
  111. for (int d = 0; d < NUMBER_OF_TOURISTS; d++) {
  112.  
  113. ocena[d] = countDistance(tab, touristMutted[d]);
  114.  
  115. }
  116.  
  117. if (getMin(ocena) < tmpOcena) {
  118. tmpOcena = getMin(ocena);
  119. }
  120.  
  121. System.arraycopy(touristMutted, 0, touristTMP, 0, touristTMP.length);
  122.  
  123. System.out.println("Iteracja: " + (k + 1) + " -> Najkrótsza trasa: " + tmpOcena + "\n\n");
  124.  
  125. }
  126. System.out.println("--------------------------------------------");
  127. System.out.println("Najkrótsza trasa: " + tmpOcena + "\n\n");
  128. }
  129.  
  130. // --------------------------METODY----------------------------------------
  131.  
  132. private static void printMultiArr(int[][] crossOverArr) {
  133. for (int[] val : crossOverArr) {
  134. for (int v : val) {
  135. System.out.print(v + " ");
  136.  
  137. }
  138. System.out.println();
  139. }
  140. }
  141.  
  142. // Mieszanie wartości w tablicy
  143. private static void shuffleArrayList(int rowSize, int[][] tourist) {
  144. for (int i = 0; i < tourist.length; i++) {
  145.  
  146. ArrayList<Integer> numbers = new ArrayList<>();
  147. for (int q = 0; q < rowSize; q++) {
  148. numbers.add(q + 1);
  149. }
  150. Collections.shuffle(numbers);
  151. for (int j = 0; j < rowSize; j++) {
  152. tourist[i][j] = numbers.get(j);
  153. }
  154. }
  155. }
  156.  
  157. // Znajdź min z tablicy[]
  158. public static int getMin(int[] inputArray) {
  159. int minValue = inputArray[0];
  160. for (int i = 1; i < inputArray.length; i++) {
  161. if (inputArray[i] < minValue) {
  162. minValue = inputArray[i];
  163. }
  164. }
  165. return minValue;
  166. }
  167.  
  168. public static int getMax(int[] inputArray) {
  169. int maxValue = inputArray[0];
  170. for (int i = 1; i < inputArray.length; i++) {
  171. if (inputArray[i] > maxValue) {
  172. maxValue = inputArray[i];
  173. }
  174. }
  175. return maxValue;
  176. }
  177.  
  178. // Licz distans
  179. private static int countDistance(int[][] tab, int[] user) {
  180. int trasa = 0;
  181. int a, b;
  182. for (int i = 0; i < user.length; i++) {
  183. a = user[i];
  184. if (i == (user.length - 1)) {
  185. b = user[0];
  186. } else {
  187. b = user[i + 1];
  188. }
  189. trasa += tab[a - 1][b - 1];
  190. }
  191. return trasa;
  192. }
  193.  
  194. private static int[] selekcjaTurniej(int ocena[]) {
  195. Random r = new Random();
  196. int k = 2; // skacz co dwie oceny //120 7615
  197.  
  198. int[] indeksyTurniej = new int[NUMBER_OF_TOURISTS];
  199. for (int j = 0; j < NUMBER_OF_TOURISTS; j++) {
  200.  
  201. int ocenaMin = Integer.MAX_VALUE;
  202. int indexMin = -1; // -1 b ona pewno go nie ma, deklaracja
  203.  
  204. for (int i = 0; i < k; i++) {
  205. int randIndeks = r.nextInt(NUMBER_OF_TOURISTS);
  206. if (ocena[randIndeks] < ocenaMin) {
  207. indexMin = randIndeks;
  208. ocenaMin = ocena[randIndeks];
  209. }
  210. }
  211. indeksyTurniej[j] = indexMin;
  212. }
  213. int[] indTur = new int[indeksyTurniej.length];
  214. System.arraycopy(indeksyTurniej, 0, indTur, 0, indeksyTurniej.length);
  215.  
  216. return indTur;
  217.  
  218. }
  219.  
  220. // ----------------------Koło ruletki----------------------
  221.  
  222. /*
  223. * private static int[][] ruletka(int[] ocena, int[][] tourist, int[][]
  224. * touristChild) {
  225. *
  226. * int sumaPrawd = 0; for (int i = 0; i < ocena.length; i++) { sumaPrawd +=
  227. * ocena[i]; }
  228. *
  229. * Random r = new Random(); for (int i = 0; i < touristChild.length; i++) {
  230. * int numberRand = r.nextInt(sumaPrawd); int suma = 0; int j; for (j = 0; j
  231. * < tourist.length && suma < numberRand; j++) { suma += ocena[j]; } //
  232. * arraycopy(Object src, int srcPos, Object dest, int destPos, int //
  233. * length)
  234. *
  235. * System.out.println("tourist[j]"+tourist[j].length);
  236. * System.out.println("tourustChild[j]"+touristChild[j].length);
  237. * System.out.println("tourist[j]"+tourist[j].length);
  238. *
  239. * System.arraycopy(tourist[j], 0, touristChild[j], 0, tourist[j].length);
  240. * // bo // lecimy // po // wierszach
  241. *
  242. * // int [][] array = (int[][]) Arrays.stream(tourist).toArray();
  243. * //touristChild = tourist.clone();
  244. *
  245. * }
  246. *
  247. * return touristChild; }
  248. */
  249.  
  250. private static int[] ruletka(int distance[]) {
  251.  
  252.  
  253. int max = getMax(distance);
  254. int[] tmpDistance = new int[NUMBER_OF_TOURISTS];
  255. for (int i = 0; i < distance.length; i++) {
  256. tmpDistance[i] = max + 1 - distance[i];
  257. }
  258.  
  259.  
  260. int propabilitySum = 0;
  261. for (int i = 0; i < tmpDistance.length; i++) {
  262. propabilitySum += tmpDistance[i];
  263. }
  264.  
  265. int[] rouletteIndexes = new int[NUMBER_OF_TOURISTS];
  266. Random r = new Random();
  267.  
  268. for (int i = 0; i < NUMBER_OF_TOURISTS; i++) {
  269. int numberRand = r.nextInt(propabilitySum);
  270. int j = 0;
  271.  
  272. while (numberRand > 0) {
  273. numberRand = numberRand - tmpDistance[j];
  274. j++;
  275. }
  276. if(j>0){
  277. j=j-1;
  278. }
  279. rouletteIndexes[i] = j;
  280. }
  281.  
  282. return rouletteIndexes;
  283. }
  284.  
  285. /*
  286. * arr = [150, 200, 250, 400], s = 1000 prob = [15%, 20%, 25%, 40%]
  287. *
  288. * max = weź największy z arr (400) for i < arr.length; i++ { arr[i] = max +
  289. * 1 - arr[i]; } // max + 1 żeby nigdy nie wyszło 0
  290. *
  291. * arr = [251, 201, 151, 1] s = 604 prob = [42%, 33%, 25%, <1%]
  292. */
  293. /*
  294. * private static int[] ruletka(int ocena[]) {
  295. *
  296. * int sumaPrawd = 0; for (int i = 0; i < ocena.length; i++) { sumaPrawd +=
  297. * ocena[i]; }
  298. *
  299. * int[] indeksyRul = new int[NUMBER_OF_TOURISTS]; Random r = new Random();
  300. *
  301. * for (int i = 0; i < NUMBER_OF_TOURISTS; i++) { int numberRand =
  302. * r.nextInt(sumaPrawd); int j = 0;
  303. *
  304. * while (numberRand > 0) { numberRand = numberRand - ocena[j]; j++; }
  305. *
  306. * if (j > 0) { j = j - 1; }
  307. *
  308. * indeksyRul[i] = j; }
  309. *
  310. * return indeksyRul; }
  311. */
  312.  
  313. // ----------------------Krzyżowanie----------------------
  314.  
  315. // Porównanie indeksów ????????????
  316. private static int indeksOf(int[] tab, int el) {
  317. for (int i = 0; i < tab.length; i++) {
  318. if (tab[i] == el)
  319. return i;
  320. }
  321. return -1;
  322. }
  323.  
  324. // Krzyzowanie
  325. private static int[][] crossOver(int[] indeksySelekcja, int[][] tourist, int rowSize) {
  326. int[][] touristChild = new int[NUMBER_OF_TOURISTS][rowSize];
  327. int szansaKrzyzowania = 700;
  328. Random rand = new Random();
  329.  
  330. for (int i = 0; i < indeksySelekcja.length; i += 2) {
  331. if (rand.nextInt(1000) < szansaKrzyzowania) {
  332.  
  333. int p1 = indeksySelekcja[i];
  334. int p2 = indeksySelekcja[i + 1];
  335. Random r = new Random();
  336. int cross1 = r.nextInt(tourist[0].length - 3) + 1;
  337. int cross2 = r.nextInt(tourist[0].length - 1 - cross1) + cross1 + 1;
  338.  
  339. // zamiana środka
  340. for (int j = cross1; j <= cross2; j++) {
  341. /*System.out.println("\ni ->"+i);
  342. System.out.println("j ->"+j);
  343. System.out.println("p1 ->"+p1);
  344. System.out.println("p2 ->"+p2);*/
  345.  
  346. touristChild[i][j] = tourist[p1][j];
  347. touristChild[i + 1][j] = tourist[p2][j];
  348. }
  349.  
  350. // krzyżowanie PMX
  351. for (int j = 0; j < cross1; j++) {
  352. int city1 = tourist[p2][j];
  353. int ind;
  354. // zamiana nieistniejących środka
  355. while ((ind = indeksOf(touristChild[i], city1)) != -1) {
  356. city1 = tourist[p2][ind];
  357. }
  358. touristChild[i][j] = city1;
  359. }
  360.  
  361. for (int j = cross2 + 1; j < tourist[0].length; j++) {
  362. int city1 = tourist[p2][j];
  363. int ind;
  364.  
  365. // zamiana nieistniejących środka
  366. while ((ind = indeksOf(touristChild[i], city1)) != -1) {
  367. city1 = tourist[p2][ind];
  368. }
  369. touristChild[i][j] = city1;
  370. }
  371.  
  372. for (int j = 0; j < cross1; j++) {
  373. int city1 = tourist[p1][j];
  374. int ind;
  375. // zamiana nieistniejących środka
  376. while ((ind = indeksOf(touristChild[i + 1], city1)) != -1) {
  377. city1 = tourist[p1][ind];
  378. }
  379. touristChild[i + 1][j] = city1;
  380. }
  381.  
  382. for (int j = cross2 + 1; j < tourist[0].length; j++) {
  383. int city1 = tourist[p1][j];
  384. int ind;
  385. // zamiana nieistniejących środka
  386. while ((ind = indeksOf(touristChild[i + 1], city1)) != -1) {
  387. city1 = tourist[p1][ind];
  388. }
  389. touristChild[i + 1][j] = city1;
  390. }
  391. } else {
  392. for (int j = 0; j < tourist[i].length; j++) {
  393.  
  394. touristChild[i][j] = tourist[i][j];
  395. touristChild[i + 1][j] = tourist[i + 1][j];
  396.  
  397. }
  398. }
  399. }
  400.  
  401. return touristChild;
  402. }
  403.  
  404. // Mutacja
  405. private static int[][] mutacja(int[] indeksySelekcja, int[][] touristChild) {
  406. // losowanie przeciec
  407. int cross1 = 0;
  408. int cross2 = 0;
  409. int szansaMutacji = 5;
  410. Random rand = new Random();
  411.  
  412. for (int i = 0; i < indeksySelekcja.length; i++) {
  413. if (rand.nextInt(1000) < szansaMutacji) {
  414. Random r = new Random();
  415.  
  416. cross1 = r.nextInt(touristChild[0].length - 3) + 1;
  417. cross2 = r.nextInt(touristChild[0].length - 1 - cross1) + cross1 + 1;
  418.  
  419. ArrayList<Integer> touristChildTmpSrodek = new ArrayList<>();
  420.  
  421. // przypisujemy srodek z touristCHild
  422. for (int j = cross1; j <= cross2; j++) {
  423. touristChildTmpSrodek.add(touristChild[i][j]);
  424. }
  425.  
  426. Collections.reverse(touristChildTmpSrodek);
  427.  
  428. int tmpIndex = 0;
  429. for (int j = cross1; j <= cross2; j++) {
  430.  
  431. touristChild[i][j] = touristChildTmpSrodek.get(tmpIndex);
  432. tmpIndex++;
  433. }
  434. }
  435. }
  436.  
  437. return touristChild;
  438.  
  439. }
  440.  
  441. /*
  442. * private static String saveToExcel(int numberOfIterations, int
  443. * iterationRow, int rowNumber, ArrayList<Integer> cellValue, String
  444. * fileName) throws IOException {
  445. *
  446. * HSSFWorkbook wb = new HSSFWorkbook(); HSSFSheet sheet = wb.createSheet(
  447. * "new sheet");
  448. *
  449. *
  450. *
  451. *
  452. * for (int i = 0; i < numberOfIterations; i++) { HSSFRow row =
  453. * sheet.createRow(i);
  454. *
  455. *
  456. * HSSFCell cell = row.createCell(iterationRow); cell.setCellValue(i+1);
  457. * //numer iteracji
  458. *
  459. * HSSFCell cell2 = row.createCell(rowNumber);
  460. * cell2.setCellValue(cellValue.get(i)); }
  461. *
  462. * // Write the output to a file FileOutputStream fileOut = new
  463. * FileOutputStream(fileName); wb.write(fileOut); fileOut.close();
  464. *
  465. * wb.close();
  466. *
  467. * return "Excel created"; }
  468. */
  469.  
  470. /*
  471. * private static String saveToExcel(int numberOfIterations, int
  472. * iterationRow, int rowNumber, ArrayList<Integer> cellValue) {
  473. * //this.iloscIteracji = numberOfIterations;
  474. *
  475. * try { FileOutputStream fileOut = new FileOutputStream("wynik.xls");
  476. * HSSFWorkbook workbook = new HSSFWorkbook(); HSSFSheet worksheet =
  477. * workbook.createSheet("POI Worksheet");
  478. *
  479. * // index from 0,0... cell A1 is cell(0,0)
  480. *
  481. * for (int i = 0; i < numberOfIterations; i++) {
  482. *
  483. * HSSFRow row1 = worksheet.createRow(i); HSSFRow row2 =
  484. * worksheet.createRow(i); // ile wierszy
  485. *
  486. * HSSFCell cellA1 = row1.createCell(iterationRow); HSSFCell cellA2 =
  487. * row2.createCell(rowNumber);
  488. *
  489. * cellA1.setCellValue(i+1); //nr iteracji w wierszu
  490. * cellA2.setCellValue(cellValue.get(i)); // wartosc
  491. *
  492. * //HSSFCellStyle cellStyle = workbook.createCellStyle();
  493. *
  494. * }
  495. *
  496. * workbook.write(fileOut); fileOut.flush(); fileOut.close(); } catch
  497. * (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e)
  498. * { e.printStackTrace(); }
  499. *
  500. * return "Excel Created";
  501. *
  502. * }
  503. */
  504.  
  505. private static void saveToExcel(String[] val, String outputFile) {
  506.  
  507. try {
  508.  
  509. CsvWriter csvOutput = new CsvWriter(outputFile);
  510.  
  511. for (int i = 0; i < NUMBER_OF_ITERATIONS; i++) {
  512.  
  513. // write out a few records
  514. csvOutput.write(val[i]);
  515. // csvOutput.write("sdsds");
  516. csvOutput.endRecord();
  517. }
  518.  
  519. csvOutput.close();
  520. } catch (IOException e) {
  521. e.printStackTrace();
  522. }
  523. }
  524.  
  525. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement