Advertisement
Guest User

Untitled

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