Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- namespace ConsoleApp5
- {
- public class RandomPopulation //Population must be odd num
- {
- private int size = LoadFile.size; //52
- private int[,] population;
- private int[,] lenghts = LoadFile.pathsLenght;
- private Random rand = new Random();
- public RandomPopulation()
- {
- population = new int[size, size]; // pierwszy size do wczytania
- CreatePopulation();
- }
- /// <summary>
- /// Creates int[,] filled with with random unique numbers from [0,size).
- /// </summary>
- public void CreatePopulation()
- {
- int x;
- int[] tmparray = new int[size];
- //fill with 2d array with numbers [0-52)
- //int[] numbers = Enumerable.Range(0, size).ToArray();
- int[] numbers = new int[size];
- for (int i = 0; i < size; i++)
- {
- numbers[i] = i;
- }
- for (int i = 0; i < size; i++)
- {
- x = size;
- numbers.CopyTo(tmparray, 0);
- for (int j = 0; j < size; j++)
- {
- int r = rand.Next(x);
- population[i, j] = tmparray[r];
- tmparray[r] = tmparray[x - 1];
- x--;
- }
- }
- }
- public void ShowPopulation()
- {
- for (int i = 0; i < population.GetLength(0); i++)
- {
- //int i = 0;
- Console.WriteLine("\n-" + i + "-x-");
- for (int j = 0; j < population.GetLength(1); j++)
- {
- Console.Write(population[i, j] + " ");
- }
- Console.WriteLine("\n-" + i + "-x-");
- }
- }
- /// <summary>
- /// Returns sum of all roads lenght as int[]
- /// </summary>
- /// <returns></returns>
- public int[] RatePopulation()
- {
- int[] ratings = new int[size]; // wpisany
- for (int i = 0; i < population.GetLength(0); i++) // wpisany
- {
- for (int j = 0; j < population.GetLength(1) - 1; j++)
- {
- ratings[i] += lenghts[population[i, j], population[i, j + 1]];
- }
- ratings[i] += lenghts[population[i, size - 1], population[i, 0]];
- //Console.WriteLine(ratings[i] + " na pozycji " + i);
- }
- return ratings;
- }
- public void ShowRatedPopulation()
- {
- Console.WriteLine();
- int[] ratedPopulation = RatePopulation();
- Console.WriteLine("Rated Population: ");
- int i = 0;
- foreach(int x in ratedPopulation)
- {
- Console.Write("x "+i + " x \t");
- Console.Write(x + " ");
- Console.WriteLine("\t x " + i + " x");
- i++;
- }
- }
- //public void RateSinglePath(int row = 0)
- //{
- // int sum = 0;
- // for (int i = 0; i < size - 1; i++)
- // {
- // sum += lenghts[population[row, i], population[row, i + 1]];
- // }
- // sum += lenghts[population[row, size - 1], population[row, 0]];
- //}
- public void SelectionRoulette()
- {
- int[] ratedPopulation = RatePopulation();
- int maxRating = ratedPopulation.Max();
- // max value - current value + 1 so it can't be less than 0
- for (int i = 0; i < ratedPopulation.Length; i++)
- {
- ratedPopulation[i] = maxRating - ratedPopulation[i] + 1;
- Console.WriteLine(ratedPopulation[i]);
- }
- int[,] specimenChoosed = new int[size, size];
- int sumRates = ratedPopulation.Sum();
- int sumChances = 0;
- Console.WriteLine("Po ocenie: " + sumRates);
- Console.WriteLine("Nowa Populacja: ");
- for (int i = 0; i < ratedPopulation.Length; i++)
- {
- sumChances = 0;
- int randomNumber = rand.Next(0, sumRates);
- Console.Write("index: " + i);
- for (int j = 0; j < size; j++)
- {
- sumChances += ratedPopulation[j];
- if (sumChances >= randomNumber)
- {
- Array.Copy(population ,j * (size), specimenChoosed, i * size, size);
- Console.WriteLine(" specimen:" + j + " ");
- break;
- }
- }
- }
- population = specimenChoosed;
- Console.WriteLine();
- Console.WriteLine("SpecimenChoosed: ");
- Console.WriteLine();
- ShowPopulation();
- } // ok
- public void SelectionTournament()
- {
- int[] ratedPopulation = RatePopulation();
- int k = (int)((size * 0.08));
- int[,] specimenChoosed = new int[size, size];
- int selectedSpecimen;
- int[] numbers = new int[size];
- int[] tournamentSpecimen = new int[k];
- for (int l = 0; l < size; l++)
- {
- int x = size;
- // fill numbers with [0,size)
- for (int i = 0; i < size; i++)
- {
- numbers[i] = i;
- }
- // Choose [k] tournament participants
- for (int i = 0; i < k; i++)
- {
- int r = rand.Next(x);
- tournamentSpecimen[i] = numbers[r];
- numbers[r] = numbers[x - 1];
- x--;
- }
- //Console.WriteLine("\ntournamentSpecimen: ");
- //foreach (int z in tournamentSpecimen)
- //{
- // Console.WriteLine(z + "value: " + ratedPopulation[z]);
- //}
- // Select the best Specimen from k
- selectedSpecimen = tournamentSpecimen[0];
- for (int i = 0; i < k - 1; i++)
- {
- if (ratedPopulation[tournamentSpecimen[i]] > (ratedPopulation[tournamentSpecimen[i + 1]]))
- {
- selectedSpecimen = tournamentSpecimen[i + 1];
- }
- }
- Console.WriteLine("bestspecimen: " + selectedSpecimen + " ");
- Array.Copy(population, selectedSpecimen * size , specimenChoosed, l * (size), size);
- }
- population = specimenChoosed;
- } // ok better for algorithm
- public void CrossoverTwoPoint(int crossingParameter = 75) // 75% - 95% best population must be odd
- {
- int[] chromosomeFirst = new int[size];
- int[] chromosomeSecond = new int[size];
- for(int i = 0; i < size; i+=2)
- {
- if(rand.Next(100) < crossingParameter)
- {
- Console.WriteLine("Changed: " + i + " " + i + 1);
- int crossoverFristPoint = rand.Next(0, size); // [1-51)
- int crossoverSecondPoint = rand.Next(crossoverFristPoint, size);
- }
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement