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;
- using System.Threading;
- namespace ParallelDataProcessingCourseWorkA
- {
- struct SchedulingTaskStruct
- {
- public int nProcs;
- public int nTasks;
- public int tMin;
- public int tMax;
- public int[,] T;
- public SchedulingTaskStruct(int nProcs, int nTasks, int tMin, int tMax)
- {
- this.nProcs = nProcs;
- this.nTasks = nTasks;
- this.tMin = tMin;
- this.tMax = tMax;
- T = new int[nTasks, nProcs];
- Random rnd = new Random();
- for (int i = 0; i < nTasks; i++)
- {
- for (int j = 0; j < nProcs; j++)
- {
- T[i, j] = rnd.Next(tMin, tMax);
- }
- }
- }
- }
- class GenAlgoIslandModel
- {
- int nCreatures;
- int nPopulations;
- int pCross;
- int pMut;
- int nGenes;
- int nGenerations;
- int migrationSize;
- int migrationInterval;
- SchedulingTaskStruct schedulingTask;
- void Cross(int[] creature0, int[] creature1, ref int[] child0, ref int[] child1)
- {
- Random rnd = new Random();
- int cutPoint = rnd.Next(0, nGenes - 2);
- for (int i = 0; i < cutPoint + 1; i++)
- {
- child0[i] = creature0[i];
- child1[i] = creature1[i];
- }
- for (int i = cutPoint + 1; i < nGenes; i++)
- {
- child0[i] = creature1[i];
- child1[i] = creature0[i];
- }
- }
- void Mutate(int[] individual, bool verbose = false)
- {
- Random rnd = new Random();
- int iGene = rnd.Next(0, nGenes - 1);
- int iBit = rnd.Next(0, 7);
- individual[iGene] ^= (1 << iBit);
- }
- void CanonicalGAProcessPopulation(ref List<int[]> population, int pCross, int pMut, int nInds, int idx)
- {
- Random rnd = new Random();
- int[] fitValues = new int[population.Count];
- for (int i = 0; i < population.Count; i++)
- {
- fitValues[i] = CalculateFitness(population[i]);
- }
- //implementing roulette selection
- double iFitValsSum = 0;
- for (int i = 0; i < population.Count; i++)
- {
- iFitValsSum += 1.0f / fitValues[i];
- }
- double[] probs = new double[nInds];
- for (int i = 0; i < nInds; i++)
- {
- probs[i] = ((1.0f / fitValues[i]) / iFitValsSum)/* * 100*/;
- }
- List<int> matingPool = new List<int>();
- for (int i = 0; i < nInds; i++)
- {
- double roll = rnd.NextDouble();
- double left = 0.0;
- for (int j = 0; j < nInds; j++)
- {
- double right = left + probs[j];
- if (roll >= left && roll <= right)
- {
- matingPool.Add(j);
- break;
- }
- left = right;
- }
- }
- List<int[]> populationTemp = new List<int[]>(nInds);
- //getting new population
- while (populationTemp.Count != nInds)
- {
- int rCross = rnd.Next(0, 100);
- if (rCross >= pCross)
- {
- continue;
- }
- int iParent0 = rnd.Next(0, matingPool.Count - 1);
- int iParent1 = rnd.Next(0, matingPool.Count - 1);
- int[] child0 = new int[nGenes];
- int[] child1 = new int[nGenes];
- Cross(population[iParent0], population[iParent1], ref child0, ref child1);
- int rMut = rnd.Next(0, 100);
- if (rMut < pMut)
- {
- Mutate(child0, verbose: true);
- }
- rMut = rnd.Next(0, 100);
- if (rMut < pMut)
- {
- Mutate(child1, verbose: true);
- }
- populationTemp.Add(child0);
- populationTemp.Add(child1);
- }
- //replacing the parent population
- population = populationTemp;
- }
- List<List<int[]>> populations;
- List<Thread> threads;
- List<Task> tasks;
- List<int> cGenerationList;
- public int CalculateFitness(int[] individual)
- {
- int maxLoad = 0;
- int nProcs = schedulingTask.nProcs;
- int nTasks = schedulingTask.nTasks;
- int[] loads = new int[nProcs];
- for (int i = 0; i < nTasks; i++)
- {
- int iProc = Math.Min(individual[i] / (255 / nProcs), nProcs - 1);
- loads[iProc] += schedulingTask.T[i, iProc];
- }
- maxLoad = loads.Max();
- return maxLoad;
- }
- void GenerateInitialPopulations()
- {
- Random rnd = new Random();
- int nGenes = schedulingTask.nTasks;
- for (int i = 0; i < nPopulations; i++)
- {
- List<int[]> population = new List<int[]>(nCreatures);
- for (int j = 0; j < nCreatures; j++)
- {
- int[] creature = new int[nGenes];
- for (int k = 0; k < nGenes; k++)
- {
- creature[k] = rnd.Next(255);
- }
- population.Add(creature);
- }
- populations.Add(population);
- }
- }
- public GenAlgoIslandModel(int nCreatures, int nPopulations, int migrationSize, int migrationInterval, SchedulingTaskStruct schedulingTask, int pCross, int pMut, int nGenerations)
- {
- this.nCreatures = nCreatures;
- this.nPopulations = nPopulations;
- this.migrationSize = migrationSize;
- this.migrationInterval = migrationInterval;
- this.schedulingTask = schedulingTask;
- this.pCross = pCross;
- this.pMut = pMut;
- this.nGenerations = nGenerations;
- cGenerationList = new List<int>(nPopulations);
- for (int i = 0; i < nPopulations; i++)
- {
- //cGenerationList[i] = nGenerations;
- cGenerationList.Add(nGenerations);
- }
- this.nGenes = schedulingTask.nTasks;
- this.schedulingTask = schedulingTask;
- populations = new List<List<int[]>>();
- GenerateInitialPopulations();
- ManualResetEvent[] manualResetEvents = new ManualResetEvent[nPopulations];
- for (int i = 0; i < nPopulations; i++)
- {
- manualResetEvents[i] = new ManualResetEvent(false);
- }
- threads = new List<Thread>();
- for (int i = 0; i < nPopulations; i++)
- {
- int idx = i;
- List<int[]> population = populations[idx];
- Thread th = new Thread(delegate ()
- {
- int cGenerations = 0;
- while (cGenerations < nGenerations)
- {
- if (cGenerations % migrationInterval == 0)
- {
- Console.Write("\nPopulation {0} is being processed... cGeneration = {1}", idx, cGenerations);
- manualResetEvents[idx].Reset();
- }
- CanonicalGAProcessPopulation(ref population, pCross, pMut, nCreatures, idx);
- cGenerations++;
- if (cGenerations % migrationInterval == 0)
- {
- manualResetEvents[idx].Set();
- Console.Write("\nPopulation {0}'s", idx);
- Console.Write("job is done, waiting for others... cGenerations = {0}\n", cGenerations);
- WaitHandle.WaitAll(manualResetEvents);
- }
- }
- });
- threads.Add(th);
- }
- for (int i = 0; i < nPopulations; i++)
- {
- threads[i].Start();
- }
- for (int i = 0; i < nPopulations; i++)
- {
- threads[i].Join();
- }
- }
- }
- class Program
- {
- static void Main(string[] args)
- {
- Console.BufferHeight = 1000;
- Console.BufferWidth = 200;
- int nProcs = 3;
- int nTasks = 10;
- int tMin = 1;
- int tMax = 15;
- SchedulingTaskStruct st = new SchedulingTaskStruct(nProcs, nTasks, tMin, tMax);
- Console.WriteLine("Scheduling task: ");
- for (int i = 0; i < st.nTasks; i++)
- {
- for (int j = 0; j < st.nProcs; j++)
- {
- Console.Write("{0, 5}", st.T[i, j]);
- }
- Console.WriteLine();
- }
- int nCreatures = 1000;
- int nGenerations = 40;
- int nPopulations = 4;
- int migrationSize = 10;
- int migrationInterval = 5;
- int pMut = 1;
- int pCross = 99;
- GenAlgoIslandModel gaga = new GenAlgoIslandModel(nCreatures, nPopulations, migrationSize, migrationInterval, st, pCross, pMut, nGenerations);
- Console.Read();
- }
- }
- }
Add Comment
Please, Sign In to add comment