Advertisement
Guest User

Population

a guest
Dec 13th, 2019
121
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 7.60 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6.  
  7. namespace ConsoleApp5
  8. {
  9.     public class RandomPopulation //Population must be odd num
  10.     {  
  11.         private int size = LoadFile.size; //52
  12.         private int[,] population;
  13.         private int[,] lenghts = LoadFile.pathsLenght;
  14.         private Random rand = new Random();
  15.  
  16.         public RandomPopulation()
  17.         {
  18.             population = new int[size, size]; // pierwszy size do wczytania
  19.             CreatePopulation();
  20.         }
  21.  
  22.         /// <summary>
  23.         /// Creates int[,] filled with with random unique numbers from [0,size).
  24.         /// </summary>
  25.         public void CreatePopulation()
  26.         {
  27.             int x;
  28.             int[] tmparray = new int[size];
  29.  
  30.             //fill with 2d array with numbers [0-52)          
  31.             //int[] numbers = Enumerable.Range(0, size).ToArray();
  32.             int[] numbers = new int[size];
  33.             for (int i = 0; i < size; i++)
  34.             {
  35.                 numbers[i] = i;
  36.             }      
  37.  
  38.             for (int i = 0; i < size; i++)
  39.             {
  40.                 x = size;
  41.                 numbers.CopyTo(tmparray, 0);
  42.  
  43.                 for (int j = 0; j < size; j++)
  44.                 {
  45.                     int r = rand.Next(x);
  46.                     population[i, j] = tmparray[r];
  47.                     tmparray[r] = tmparray[x - 1];
  48.                     x--;
  49.                 }
  50.             }
  51.         }
  52.  
  53.         public void ShowPopulation()
  54.         {
  55.             for (int i = 0; i < population.GetLength(0); i++)
  56.             {
  57.                 //int i = 0;
  58.                 Console.WriteLine("\n-" + i + "-x-");
  59.                 for (int j = 0; j < population.GetLength(1); j++)
  60.                 {
  61.  
  62.                     Console.Write(population[i, j] + " ");
  63.                 }
  64.                 Console.WriteLine("\n-" + i + "-x-");
  65.                
  66.             }
  67.         }
  68.  
  69.         /// <summary>
  70.         /// Returns sum of all roads lenght as int[]
  71.         /// </summary>
  72.         /// <returns></returns>
  73.         public int[] RatePopulation()
  74.         {
  75.             int[] ratings = new int[size]; // wpisany
  76.  
  77.             for (int i = 0; i < population.GetLength(0); i++) // wpisany
  78.             {
  79.                 for (int j = 0; j < population.GetLength(1) - 1; j++)
  80.                 {      
  81.                         ratings[i] += lenghts[population[i, j], population[i, j + 1]];
  82.                 }
  83.                 ratings[i] += lenghts[population[i, size - 1], population[i, 0]];
  84.                 //Console.WriteLine(ratings[i] + " na pozycji " + i);
  85.             }
  86.            
  87.             return ratings;
  88.         }
  89.  
  90.         public void ShowRatedPopulation()
  91.         {
  92.             Console.WriteLine();
  93.             int[] ratedPopulation = RatePopulation();
  94.             Console.WriteLine("Rated Population: ");
  95.             int i = 0;
  96.             foreach(int x in ratedPopulation)
  97.             {
  98.                 Console.Write("x "+i + " x \t");
  99.                 Console.Write(x + " ");
  100.                 Console.WriteLine("\t x " + i + " x");
  101.                 i++;
  102.             }
  103.         }
  104.  
  105.         //public void RateSinglePath(int row = 0)
  106.         //{
  107.         //    int sum = 0;
  108.         //    for (int i = 0; i < size - 1; i++)
  109.         //    {
  110.         //        sum += lenghts[population[row, i], population[row, i + 1]];
  111.                
  112.         //    }
  113.         //    sum += lenghts[population[row, size - 1], population[row, 0]];      
  114.         //}
  115.  
  116.         public void SelectionRoulette()
  117.         {
  118.             int[] ratedPopulation = RatePopulation();
  119.             int maxRating = ratedPopulation.Max();
  120.             // max value - current value + 1 so it can't be less than 0
  121.             for (int i = 0; i < ratedPopulation.Length; i++)
  122.             {
  123.                 ratedPopulation[i] = maxRating - ratedPopulation[i] + 1;
  124.                 Console.WriteLine(ratedPopulation[i]);
  125.             }
  126.  
  127.             int[,] specimenChoosed = new int[size, size];
  128.             int sumRates = ratedPopulation.Sum();
  129.  
  130.             int sumChances = 0;
  131.             Console.WriteLine("Po ocenie: " + sumRates);
  132.             Console.WriteLine("Nowa Populacja: ");
  133.             for (int i = 0; i < ratedPopulation.Length; i++)
  134.             {
  135.                 sumChances = 0;
  136.                 int randomNumber = rand.Next(0, sumRates);
  137.                 Console.Write("index: " + i);
  138.                 for (int j = 0; j < size; j++)
  139.                 {
  140.                     sumChances += ratedPopulation[j];
  141.                     if (sumChances >= randomNumber)
  142.                     {
  143.                         Array.Copy(population ,j * (size), specimenChoosed,  i * size, size);
  144.                         Console.WriteLine(" specimen:" + j + " ");
  145.                         break;
  146.                     }
  147.                 }
  148.  
  149.             }
  150.             population = specimenChoosed;
  151.             Console.WriteLine();
  152.             Console.WriteLine("SpecimenChoosed: ");
  153.             Console.WriteLine();
  154.  
  155.  
  156.             ShowPopulation();
  157.         } // ok
  158.  
  159.         public void SelectionTournament()
  160.         {
  161.             int[] ratedPopulation = RatePopulation();
  162.             int k = (int)((size * 0.08));
  163.             int[,] specimenChoosed = new int[size, size];
  164.             int selectedSpecimen;
  165.             int[] numbers = new int[size];
  166.             int[] tournamentSpecimen = new int[k];
  167.  
  168.             for (int l = 0; l < size; l++)
  169.             {
  170.                 int x = size;
  171.                 // fill numbers with [0,size)
  172.                 for (int i = 0; i < size; i++)
  173.                 {
  174.                     numbers[i] = i;
  175.                 }
  176.                 // Choose [k] tournament participants
  177.                 for (int i = 0; i < k; i++)
  178.                 {
  179.                     int r = rand.Next(x);
  180.                     tournamentSpecimen[i] = numbers[r];
  181.                     numbers[r] = numbers[x - 1];
  182.                     x--;
  183.                 }
  184.  
  185.                 //Console.WriteLine("\ntournamentSpecimen: ");
  186.                 //foreach (int z in tournamentSpecimen)
  187.                 //{
  188.                 //    Console.WriteLine(z + "value: " + ratedPopulation[z]);
  189.                 //}
  190.  
  191.                 // Select the best Specimen from k
  192.                 selectedSpecimen = tournamentSpecimen[0];
  193.                 for (int i = 0; i < k - 1; i++)
  194.                 {
  195.                     if (ratedPopulation[tournamentSpecimen[i]] > (ratedPopulation[tournamentSpecimen[i + 1]]))
  196.                     {
  197.                         selectedSpecimen = tournamentSpecimen[i + 1];
  198.                     }
  199.                 }
  200.                 Console.WriteLine("bestspecimen: " + selectedSpecimen + " ");
  201.                 Array.Copy(population, selectedSpecimen * size , specimenChoosed, l * (size), size);
  202.             }
  203.             population = specimenChoosed;
  204.            
  205.         } // ok better for algorithm
  206.  
  207.         public void CrossoverTwoPoint(int crossingParameter = 75) // 75% - 95% best population must be odd
  208.         {
  209.             int[] chromosomeFirst = new int[size];
  210.             int[] chromosomeSecond = new int[size];
  211.            
  212.             for(int i = 0; i < size; i+=2)
  213.             {
  214.                 if(rand.Next(100) < crossingParameter)
  215.                 {
  216.                     Console.WriteLine("Changed: " + i + " " + i + 1);
  217.                     int crossoverFristPoint = rand.Next(0, size); // [1-51)
  218.                     int crossoverSecondPoint = rand.Next(crossoverFristPoint, size);
  219.                 }
  220.             }
  221.         }
  222.  
  223.     }
  224. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement