Guest User

Untitled

a guest
Dec 13th, 2016
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 9.83 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. using System.Threading;
  7.  
  8. namespace ParallelDataProcessingCourseWorkA
  9. {
  10.    
  11.  
  12.     struct SchedulingTaskStruct
  13.     {
  14.         public int nProcs;
  15.         public int nTasks;
  16.         public int tMin;
  17.         public int tMax;
  18.         public int[,] T;
  19.  
  20.         public SchedulingTaskStruct(int nProcs, int nTasks, int tMin, int tMax)
  21.         {
  22.             this.nProcs = nProcs;
  23.             this.nTasks = nTasks;
  24.             this.tMin = tMin;
  25.             this.tMax = tMax;
  26.  
  27.             T = new int[nTasks, nProcs];
  28.  
  29.             Random rnd = new Random();
  30.  
  31.             for (int i = 0; i < nTasks; i++)
  32.             {
  33.                 for (int j = 0; j < nProcs; j++)
  34.                 {
  35.                     T[i, j] = rnd.Next(tMin, tMax);
  36.                 }
  37.             }
  38.         }
  39.     }
  40.  
  41.     class GenAlgoIslandModel
  42.     {
  43.         int nCreatures;
  44.         int nPopulations;
  45.         int pCross;
  46.         int pMut;
  47.  
  48.         int nGenes;
  49.  
  50.         int nGenerations;
  51.  
  52.         int migrationSize;
  53.         int migrationInterval;
  54.  
  55.         SchedulingTaskStruct schedulingTask;
  56.  
  57.         void Cross(int[] creature0, int[] creature1, ref int[] child0, ref int[] child1)
  58.         {
  59.             Random rnd = new Random();
  60.  
  61.             int cutPoint = rnd.Next(0, nGenes - 2);
  62.  
  63.             for (int i = 0; i < cutPoint + 1; i++)
  64.             {
  65.                 child0[i] = creature0[i];
  66.                 child1[i] = creature1[i];
  67.             }
  68.  
  69.             for (int i = cutPoint + 1; i < nGenes; i++)
  70.             {
  71.                 child0[i] = creature1[i];
  72.                 child1[i] = creature0[i];
  73.             }
  74.         }
  75.  
  76.         void Mutate(int[] individual, bool verbose = false)
  77.         {
  78.             Random rnd = new Random();
  79.             int iGene = rnd.Next(0, nGenes - 1);
  80.             int iBit = rnd.Next(0, 7);
  81.  
  82.             individual[iGene] ^= (1 << iBit);
  83.         }
  84.  
  85.         void CanonicalGAProcessPopulation(ref List<int[]> population, int pCross, int pMut, int nInds, int idx)
  86.         {
  87.             Random rnd = new Random();
  88.  
  89.             int[] fitValues = new int[population.Count];
  90.             for (int i = 0; i < population.Count; i++)
  91.             {
  92.                 fitValues[i] = CalculateFitness(population[i]);
  93.             }
  94.  
  95.             //implementing roulette selection
  96.             double iFitValsSum = 0;
  97.             for (int i = 0; i < population.Count; i++)
  98.             {
  99.                 iFitValsSum += 1.0f / fitValues[i];
  100.             }
  101.  
  102.             double[] probs = new double[nInds];
  103.             for (int i = 0; i < nInds; i++)
  104.             {
  105.                 probs[i] = ((1.0f / fitValues[i]) / iFitValsSum)/* * 100*/;
  106.             }
  107.  
  108.             List<int> matingPool = new List<int>();
  109.             for (int i = 0; i < nInds; i++)
  110.             {
  111.                 double roll = rnd.NextDouble();
  112.  
  113.                 double left = 0.0;
  114.  
  115.                 for (int j = 0; j < nInds; j++)
  116.                 {
  117.                     double right = left + probs[j];
  118.  
  119.                     if (roll >= left && roll <= right)
  120.                     {
  121.                         matingPool.Add(j);
  122.                         break;
  123.                     }
  124.                     left = right;
  125.                 }
  126.             }
  127.  
  128.             List<int[]> populationTemp = new List<int[]>(nInds);
  129.  
  130.             //getting new population
  131.             while (populationTemp.Count != nInds)
  132.             {
  133.                 int rCross = rnd.Next(0, 100);
  134.  
  135.                 if (rCross >= pCross)
  136.                 {
  137.                     continue;
  138.                 }
  139.  
  140.                 int iParent0 = rnd.Next(0, matingPool.Count - 1);
  141.                 int iParent1 = rnd.Next(0, matingPool.Count - 1);
  142.  
  143.                 int[] child0 = new int[nGenes];
  144.                 int[] child1 = new int[nGenes];
  145.  
  146.                 Cross(population[iParent0], population[iParent1], ref child0, ref child1);
  147.  
  148.                 int rMut = rnd.Next(0, 100);
  149.                 if (rMut < pMut)
  150.                 {
  151.                     Mutate(child0, verbose: true);
  152.                 }
  153.  
  154.                 rMut = rnd.Next(0, 100);
  155.                 if (rMut < pMut)
  156.                 {
  157.                     Mutate(child1, verbose: true);
  158.                 }
  159.  
  160.                 populationTemp.Add(child0);
  161.                 populationTemp.Add(child1);
  162.             }
  163.  
  164.             //replacing the parent population
  165.             population = populationTemp;
  166.         }
  167.  
  168.         List<List<int[]>> populations;
  169.  
  170.         List<Thread> threads;
  171.         List<Task> tasks;
  172.  
  173.         List<int> cGenerationList;
  174.  
  175.         public int CalculateFitness(int[] individual)
  176.         {
  177.             int maxLoad = 0;
  178.  
  179.             int nProcs = schedulingTask.nProcs;
  180.             int nTasks = schedulingTask.nTasks;
  181.  
  182.             int[] loads = new int[nProcs];
  183.  
  184.             for (int i = 0; i < nTasks; i++)
  185.             {
  186.                 int iProc = Math.Min(individual[i] / (255 / nProcs), nProcs - 1);
  187.                 loads[iProc] += schedulingTask.T[i, iProc];
  188.             }
  189.  
  190.             maxLoad = loads.Max();
  191.  
  192.             return maxLoad;
  193.         }
  194.  
  195.         void GenerateInitialPopulations()
  196.         {
  197.             Random rnd = new Random();
  198.             int nGenes = schedulingTask.nTasks;
  199.             for (int i = 0; i < nPopulations; i++)
  200.             {
  201.                 List<int[]> population = new List<int[]>(nCreatures);
  202.                 for (int j = 0; j < nCreatures; j++)
  203.                 {
  204.                     int[] creature = new int[nGenes];
  205.                     for (int k = 0; k < nGenes; k++)
  206.                     {
  207.                         creature[k] = rnd.Next(255);
  208.                     }
  209.  
  210.                     population.Add(creature);
  211.                 }
  212.  
  213.                 populations.Add(population);
  214.             }
  215.         }
  216.  
  217.         public GenAlgoIslandModel(int nCreatures, int nPopulations, int migrationSize, int migrationInterval, SchedulingTaskStruct schedulingTask, int pCross, int pMut, int nGenerations)
  218.         {
  219.             this.nCreatures = nCreatures;
  220.             this.nPopulations = nPopulations;
  221.             this.migrationSize = migrationSize;
  222.             this.migrationInterval = migrationInterval;
  223.             this.schedulingTask = schedulingTask;
  224.             this.pCross = pCross;
  225.             this.pMut = pMut;
  226.             this.nGenerations = nGenerations;
  227.  
  228.             cGenerationList = new List<int>(nPopulations);
  229.             for (int i = 0; i < nPopulations; i++)
  230.             {
  231.                 //cGenerationList[i] = nGenerations;
  232.                 cGenerationList.Add(nGenerations);
  233.             }
  234.  
  235.             this.nGenes = schedulingTask.nTasks;
  236.  
  237.             this.schedulingTask = schedulingTask;
  238.  
  239.             populations = new List<List<int[]>>();
  240.             GenerateInitialPopulations();
  241.             ManualResetEvent[] manualResetEvents = new ManualResetEvent[nPopulations];
  242.             for (int i = 0; i < nPopulations; i++)
  243.             {
  244.                 manualResetEvents[i] = new ManualResetEvent(false);
  245.             }
  246.             threads = new List<Thread>();
  247.             for (int i = 0; i < nPopulations; i++)
  248.             {
  249.                 int idx = i;
  250.                 List<int[]> population = populations[idx];
  251.                 Thread th = new Thread(delegate ()
  252.                 {
  253.                     int cGenerations = 0;
  254.                     while (cGenerations < nGenerations)
  255.                     {
  256.                         if (cGenerations % migrationInterval == 0)
  257.                         {
  258.                             Console.Write("\nPopulation {0} is being processed... cGeneration = {1}", idx, cGenerations);
  259.                             manualResetEvents[idx].Reset();
  260.                         }
  261.  
  262.                         CanonicalGAProcessPopulation(ref population, pCross, pMut, nCreatures, idx);
  263.                         cGenerations++;
  264.                         if (cGenerations % migrationInterval == 0)
  265.                         {
  266.                             manualResetEvents[idx].Set();
  267.                             Console.Write("\nPopulation {0}'s", idx);
  268.                             Console.Write("job is done, waiting for others... cGenerations = {0}\n", cGenerations);
  269.  
  270.                             WaitHandle.WaitAll(manualResetEvents);
  271.                         }
  272.                     }
  273.  
  274.                 });
  275.  
  276.                 threads.Add(th);
  277.             }
  278.  
  279.             for (int i = 0; i < nPopulations; i++)
  280.             {
  281.                 threads[i].Start();
  282.             }
  283.  
  284.             for (int i = 0; i < nPopulations; i++)
  285.             {
  286.                 threads[i].Join();
  287.             }
  288.  
  289.         }
  290.  
  291.     }
  292.  
  293.     class Program
  294.     {
  295.         static void Main(string[] args)
  296.         {
  297.             Console.BufferHeight = 1000;
  298.             Console.BufferWidth = 200;
  299.             int nProcs = 3;
  300.             int nTasks = 10;
  301.             int tMin = 1;
  302.             int tMax = 15;
  303.  
  304.             SchedulingTaskStruct st = new SchedulingTaskStruct(nProcs, nTasks, tMin, tMax);
  305.  
  306.             Console.WriteLine("Scheduling task: ");
  307.             for (int i = 0; i < st.nTasks; i++)
  308.             {
  309.                 for (int j = 0; j < st.nProcs; j++)
  310.                 {
  311.                     Console.Write("{0, 5}", st.T[i, j]);
  312.                 }
  313.                 Console.WriteLine();
  314.             }
  315.  
  316.             int nCreatures = 1000;
  317.             int nGenerations = 40;
  318.             int nPopulations = 4;
  319.             int migrationSize = 10;
  320.             int migrationInterval = 5;
  321.             int pMut = 1;
  322.             int pCross = 99;
  323.  
  324.             GenAlgoIslandModel gaga = new GenAlgoIslandModel(nCreatures, nPopulations, migrationSize, migrationInterval, st, pCross, pMut, nGenerations);
  325.  
  326.  
  327.             Console.Read();
  328.  
  329.         }  
  330.     }
  331. }
Add Comment
Please, Sign In to add comment