Advertisement
Guest User

Untitled

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