public void fittnessCalculation() { // Grading all particles for (int a = 0; a < particles.Count; a++) { fittness_grading.Add(fittness.evalFunc(particles[a])); } //Grouping particles into Key-Value Pair for (int b = 0; b < fittness_grading.Count; b++) { particle_grade.Add(new KeyValuePair <int, string>(fittness_grading[b], particles[b])); } // Sorting all particles particle_grade.Sort(compareValue); }
public void spawnParticles()//Spawning new particles { int numParticle = Convert.ToInt16(numNumberOfParticles.Value); for (int i = 0; i < numParticle; i++) { string genParticle = string.Empty; genParticle = five_bin[random.Next(0, 5)]; //External Temperature genParticle = genParticle + five_bin[random.Next(0, 5)]; //Internal Temperature genParticle = genParticle + five_bin[random.Next(0, 5)]; //Cylnder Preasure genParticle = genParticle + five_bin[random.Next(0, 5)]; // Value Opening Preasure genParticle = genParticle + five_bin[random.Next(0, 5)]; //Load torque genParticle = genParticle + five_bin[random.Next(0, 5)]; // NOx emissions genParticle = genParticle + five_bin[random.Next(0, 5)]; //CO emmisions genParticle = genParticle + five_bin[random.Next(0, 5)]; // HC Emissions genParticle = genParticle + five_bin[random.Next(0, 5)]; //PM emmisions genParticle = genParticle + random.Next(0, 2); // UCLan ETD genParticle = genParticle + random.Next(0, 2); //UCLan FFD genParticle = genParticle + random.Next(0, 2); //UCLan EL genParticle = genParticle + random.Next(0, 2); //UCLan BMS genParticle = genParticle + random.Next(0, 2); //UCLan AFDM genParticle = genParticle + three_bin[random.Next(0, 3)]; //Fuel Injection Timing particle_fitness.Add(new KeyValuePair <int, string>(fitness.evalFunc(genParticle), genParticle)); } //Getting gBest Value particle_fitness.Sort(compareKey); gBest = particle_fitness[particle_fitness.Count - 1].Value; //Sotring pBest Fitness and pBest Particle for (int a = 0; a < particle_fitness.Count; a++) { pBest_fitness.Add(particle_fitness[a].Key); pBest_particle.Add(particle_fitness[a].Value); } }
public static Tuple <Individual, Individual> Crossover_and_Mutate(Individual Parent1, Individual Parent2, int Site, int mutationrate) { Individual child1 = new Individual(); Individual child2 = new Individual(); FitFunc newtest = new FitFunc(); // Initialise new Jagged Array for Children child1.Byte_Sequence = new byte[15][]; child2.Byte_Sequence = new byte[15][]; //First Crossover for (int i = 0; i < Parent1.Byte_Sequence.Length; i++) { if (i < Site) { child1.Byte_Sequence[i] = Parent1.Byte_Sequence[i]; child2.Byte_Sequence[i] = Parent2.Byte_Sequence[i]; } else { child1.Byte_Sequence[i] = Parent2.Byte_Sequence[i]; child2.Byte_Sequence[i] = Parent1.Byte_Sequence[i]; } } //Then Perform muation function (mutation will happen depending on mutation rate) Mutate(child1, mutationrate); Mutate(child2, mutationrate); //Updates Child's String_Sequence and fitness property child1.String_Sequence = ByteSeq_To_String(child1.Byte_Sequence); child1.Fitness = newtest.evalFunc(child1.String_Sequence); child2.String_Sequence = ByteSeq_To_String(child2.Byte_Sequence); child2.Fitness = newtest.evalFunc(child2.String_Sequence); //Returns two new children return(new Tuple <Individual, Individual>(child1, child2)); }
//To generate new individual Run sequence internal static Individual Random_Sequence() { FitFunc newtest = new FitFunc(); Individual I = new Individual(); string Indiv_s; byte[][] Indiv_b = new byte[15][]; for (int i = 0; i < 9; i++)// elements 0-8 reperesent parameter levels { bool[] L = new bool[5]; int lvl = rnd.Next(0, 5); // generate random parameter level L[lvl] = true; // set level to next a parameter and convert to byte array byte[] L_b = Array.ConvertAll(L, b => b ? (byte)1 : (byte)0); Indiv_b[i] = L_b; } for (int i = 9; i < 14; i++) // elements 9-13 reperent device presence { int D_presence = rnd.Next(0, 2); // Randomly decide if device during run bool[] D = new bool[1]; if (D_presence == 1) { D[0] = true; } else { D[0] = false; } byte[] D_b = Array.ConvertAll(D, b => b ? (byte)1 : (byte)0); Indiv_b[(i)] = D_b; } bool[] T = new bool[3]; int T_Level = rnd.Next(0, 3); //Randomly choose a Timer level T[T_Level] = true; byte[] T_b = Array.ConvertAll(T, b => b ? (byte)1 : (byte)0); Indiv_b[14] = T_b; // Element 14 Represents Timer Lels Indiv_s = ByteSeq_To_String(Indiv_b); //Set Properties Of new Individual I.Byte_Sequence = Indiv_b; I.String_Sequence = Indiv_s; I.Fitness = newtest.evalFunc(Indiv_s); return(I); }
public void Selection(int parentPoolSize) { FitFunc selection = new FitFunc(); int fitness; parentIndex = ((totalPopulation * parentPoolSize) / 100); for (int i = 0; i < chroms.Count; i++) { fitness = selection.evalFunc(chroms[i].Genes); chroms[i].Grade = fitness; } chroms.Sort((a, b) => b.CompareTo(a)); genBest = chroms[0].Grade; chroms.RemoveRange(parentIndex, totalPopulation - parentIndex); if (genBest > overallBest) { overallBest = genBest; bestChrom = chroms[0].Genes; } }
public void Initialise(int populationSize) { chroms.Clear(); Random random = new Random(); FitFunc grading = new FitFunc(); totalPopulation = populationSize; int fitness; string createChrom; for (int i = 0; i < populationSize; i++) { createChrom = alleleFive[random.Next(5)] + alleleFive[random.Next(5)] + alleleFive[random.Next(5)] + alleleFive[random.Next(5)] + alleleFive[random.Next(5)] + alleleFive[random.Next(5)] + alleleFive[random.Next(5)] + alleleFive[random.Next(5)] + alleleFive[random.Next(5)] + random.Next(0, 2) + random.Next(0, 2) + random.Next(0, 2) + random.Next(0, 2) + random.Next(0, 2) + alleleThree[random.Next(3)]; fitness = grading.evalFunc(createChrom); chroms.Add(new Chromosome { Genes = createChrom, Grade = fitness }); } }
//******************************************************************************************************************************** static void Main(string[] args) { int POPULATION_SIZE = 1000; List <string> chromosomeList = new List <string>(); var rand = new Random(); for (int n = 0; n < POPULATION_SIZE; n++) // here 10 is the population size { Random random = new Random(); //create a random object List <int> list1 = new List <int>(); //create a list for parameter 1 List <int> list2 = new List <int>(); //create a list for parameter 2 List <int> list3 = new List <int>(); //create a list for parameter 3 List <int> list4 = new List <int>(); //create a list for parameter 4 List <int> list5 = new List <int>(); //create a list for parameter 5 List <int> list6 = new List <int>(); //create a list for parameter 6 List <int> list7 = new List <int>(); //create a list for parameter 7 List <int> list8 = new List <int>(); //create a list for parameter 8 List <int> list9 = new List <int>(); //create a list for parameter 9 List <int> list10 = new List <int>(); //created a list10 correct for 10 - 14 parameters (5 elements) List <int> list11 = new List <int>(); //create a list 11 for 15th parameter (without only 1 rest 0) // Adding elements to List list1.Add(0); list1.Add(0); list1.Add(0); list1.Add(0); list1.Add(0); list1[random.Next() % 5] = 1; // changing one 0 to 1 at random position list2.Add(0); list2.Add(0); list2.Add(0); list2.Add(0); list2.Add(0); list2[random.Next() % 5] = 1; // changing one 0 to 1 at random position list3.Add(0); list3.Add(0); list3.Add(0); list3.Add(0); list3.Add(0); list3[random.Next() % 5] = 1; // changing one 0 to 1 at random position list4.Add(0); list4.Add(0); list4.Add(0); list4.Add(0); list4.Add(0); list4[random.Next() % 5] = 1; // changing one 0 to 1 at random position list5.Add(0); list5.Add(0); list5.Add(0); list5.Add(0); list5.Add(0); list5[random.Next() % 5] = 1; // changing one 0 to 1 at random position list6.Add(0); list6.Add(0); list6.Add(0); list6.Add(0); list6.Add(0); list6[random.Next() % 5] = 1; // changing one 0 to 1 at random position list7.Add(0); list7.Add(0); list7.Add(0); list7.Add(0); list7.Add(0); list7[random.Next() % 5] = 1; // changing one 0 to 1 at random position list8.Add(0); list8.Add(0); list8.Add(0); list8.Add(0); list8.Add(0); list8[random.Next() % 5] = 1; // changing one 0 to 1 at random position list9.Add(0); list9.Add(0); list9.Add(0); list9.Add(0); list9.Add(0); list9[random.Next() % 5] = 1; // changing one 0 to 1 at random position for (int i = 0; i < 5; i++) //loop 5 times { int r = random.Next(0, 2); //genearte random number 0 or 1 list10.Add(r); //add the random number to list10 } list11.Add(0); list11.Add(0); list11.Add(0); list11[random.Next() % 3] = 1; // changing one 0 to 1 at random position List <int> Individualslist = list1.Concat(list2).Concat(list3).Concat(list4).Concat(list5).Concat(list6).Concat(list7).Concat(list8).Concat(list9).Concat(list10).Concat(list11).ToList(); var chromosome = string.Join("", Individualslist); // concatenated chromosomeList is in List<int> data type and needs to be converted to string and this string needs to be stored in "chromosome" chromosomeList.Add(chromosome); } // of "for loop" of initialising Population // printing the population for (int i = 0; i < POPULATION_SIZE; i++) { Console.WriteLine(chromosomeList[i]); } List <string> testPopulationlist = new List <string>(); testPopulationlist.AddRange(chromosomeList); List <int> fitnesseslist = new List <int>(testPopulationlist.Count); // 100 is the constant population size // calculate fitness for test population. for (int i = 0; i < 1; ++i) // this loop evaluates the fitness grade of the engine configurations chromosome string { FitFunc generationtest = new FitFunc(); foreach (string chromosomeString in testPopulationlist) { int gradeValue = generationtest.evalFunc(chromosomeString); fitnesseslist.Add(gradeValue); } } List <string> topScorerList = Selection1(testPopulationlist, fitnesseslist); for (int i = 0; i < fitnesseslist.Count; i++) { if (testPopulationlist[i] == topScorerList[0]) { Console.WriteLine(topScorerList[0] + " scored the highest fitness grade of " + fitnesseslist[i]); break; } } testPopulationlist = topScorerList; //now contain only the top 10% student names //Clear the contents of the gradeslist completely and keep only fitnesseslist.Clear(); // clear this list List <string> runPopulationlist = new List <string>(); List <string> OptimumPopulationlist = new List <string>(); List <string> MutatePopulationlist = new List <string>(); List <string> Generationlist = new List <string>(); Generationlist.AddRange(testPopulationlist); int generation = 10; // continuously generate populations until number of iterations is met. for (int iter = 1; iter < generation; ++iter) { if (iter == generation) { break; // condition for breaking out of generation loop. } while (Generationlist.Count <= POPULATION_SIZE) { // crossover // Instantiating random number generator var random1 = new Random(); //OptimumPopulationlist which will store randomly selected parent strings as per calculations for crossover for (int c = 0; c < 10; c++) // random 10 chromosome strings (n = 10) { //generating a random index int index1 = random1.Next(Generationlist.Count); // adding the chromosome string present at randomly generated index in runPopulationlist to OptimumPopulationlist OptimumPopulationlist.Add(Generationlist[index1]); } //Console.Write("\n random 10 crossover parent strings: \n"); // loop to print the 9 random chromosome strings in OptimumPopulationlist //for (int i = 0; i < 10; i++) // { // Console.WriteLine(OptimumPopulationlist[i]); //} int[] array = { 5, 10, 15, 20, 25, 30, 35, 40, 45, 50 }; // array of crossover index points string[] list = OptimumPopulationlist.ToArray(); //Console.Write("\n crossover : \n"); string[] result = crossover(list, array); // send to crossover function //for (int i = 0; i < result.Length; i++) // Console.WriteLine(result[i] + " --> offspring " + (i + 1)); List <string> crossoverResultlist = new List <string>(result); runPopulationlist.AddRange(crossoverResultlist); //crossoverResultlist has 900 chromosomees for 10 randomly selected chromosome strings from OptimumPopulationlist (from top 5% of test Populationlist of 1000 population size) // Mutation Program p = new Program(); List <string> resultlist = p.Mutation(Generationlist); // strings in resultlist from 1 randomly selected string // resultlist has 50 strings //function to remove chromosomes strings in resultlist which are identical to OptimumPopulationlist RemoveIdenticalString(resultlist, Generationlist); //string is reference type, so resultList is updated on ReturnIdenticalString //MutatePopulationlist which will store randomly selected 50 mutated chromosome strings for 1000 population for (int m = 0; m < 50; m++) // random 1 string from runPopulationlist so 1 x 50 combinations is 50 for mutation { //generating a random index int index1 = random1.Next(resultlist.Count); // adding the chromosome string present at randomly generated index in runPopulationlist to MutatePopulationlist MutatePopulationlist.Add(resultlist[index1]); // 50 strings generated from one randomly selectedstring } // loop to print the 50 random mutated chromosome strings in MutatePopulationlist //for (int i = 0; i < 50; i++) //{ // Console.WriteLine(MutatePopulationlist[i]); // } resultlist.Clear(); //******************************************************************************************************************** Generationlist.AddRange(MutatePopulationlist); Generationlist.AddRange(runPopulationlist); } List <int> fitnesseslist1 = new List <int>(Generationlist.Count); // calculate fitness for test population. for (int i = 0; i < 1; ++i) // this loop evaluates the fitness grade of the engine configurations chromosome string { FitFunc generationtest1 = new FitFunc(); foreach (string chromosomeString in Generationlist) { int gradeValue1 = generationtest1.evalFunc(chromosomeString); fitnesseslist1.Add(gradeValue1); } } List <string> topScorerList1 = Selection2(Generationlist, fitnesseslist1); for (int i = 0; i < fitnesseslist1.Count; i++) { if (Generationlist[i] == topScorerList1[0]) { Console.WriteLine(topScorerList1[0] + " scored the highest fitness grade of " + fitnesseslist1[i] + " in generation:" + iter); break; } } Generationlist = topScorerList1;//now contain only the top N% chromosome strings. fitnesseslist1.Clear(); //Clear the contents of the fitnesseslist1 completely. runPopulationlist.Clear(); MutatePopulationlist.Clear(); } Console.WriteLine("Hello World!"); }
private void BruteWorker_DoWork(object sender, DoWorkEventArgs e) { string bruteExTemp = "", bruteInTemp = "", bruteCyPre = "", bruteVOP = "", bruteLoTor = "", bruteNOx = "", bruteCO = "", bruteHC = "", brutePM = "", bruteUCLan = "", bruteString = "", optimalSettings = ""; int gradeValue = 0, newGradeValue, progress = 0, Iterations = 0; //Extern temp for (int i = 0; i < 5; i++) { bruteExTemp = toBinary(2 ^ i); //Intern temp for (int j = 0; j < 5; j++) { bruteInTemp = bruteExTemp + toBinary(2 ^ j); //Cylinder Pressure for (int k = 0; k < 5; k++) { bruteCyPre = bruteExTemp + bruteInTemp + toBinary(2 ^ k); //Valve Opening Pressure for (int l = 0; l < 5; l++) { bruteVOP = bruteExTemp + bruteInTemp + bruteCyPre + toBinary(2 ^ l); //Load torque for (int m = 0; m < 5; m++) { bruteLoTor = bruteExTemp + bruteInTemp + bruteCyPre + bruteVOP + toBinary(2 ^ m); //NOx Emissions for (int n = 0; n < 5; n++) { bruteNOx = bruteExTemp + bruteInTemp + bruteCyPre + bruteVOP + bruteLoTor + toBinary(2 ^ n); //CO Emissions for (int o = 0; o < 5; o++) { bruteCO = bruteExTemp + bruteInTemp + bruteCyPre + bruteVOP + bruteLoTor + bruteNOx + toBinary(2 ^ o); //HC Emissions for (int p = 0; p < 5; p++) { bruteHC = bruteExTemp + bruteInTemp + bruteCyPre + bruteVOP + bruteLoTor + bruteNOx + bruteCO + toBinary(2 ^ p); //PM Emissions for (int q = 0; q < 5; q++) { brutePM = bruteExTemp + bruteInTemp + bruteCyPre + bruteVOP + bruteLoTor + bruteNOx + bruteCO + bruteHC + toBinary(2 ^ q); //UCLan Stuff for (int r = 0; r < 32; r++) { bruteUCLan = bruteExTemp + bruteInTemp + bruteCyPre + bruteVOP + bruteLoTor + bruteNOx + bruteCO + bruteHC + brutePM + toBinary(r); // for (int s = 0; s < 3; s++) { //temp if (s == 0) { //high bruteString = bruteExTemp + bruteInTemp + bruteCyPre + bruteVOP + bruteLoTor + bruteNOx + bruteCO + bruteHC + brutePM + bruteUCLan + "001"; } else if (s == 1) { //Middle bruteString = bruteExTemp + bruteInTemp + bruteCyPre + bruteVOP + bruteLoTor + bruteNOx + bruteCO + bruteHC + brutePM + bruteUCLan + "010"; } else { //low bruteString = bruteExTemp + bruteInTemp + bruteCyPre + bruteVOP + bruteLoTor + bruteNOx + bruteCO + bruteHC + brutePM + bruteUCLan + "100"; } //shows the progress in progress bar progress = (Iterations + 1 / 187500000); BruteWorker.ReportProgress(progress, BruteProgress); System.Threading.Thread.Sleep(1000); if (BruteWorker.CancellationPending) { e.Cancel = true; } FitFunc newtest = new FitFunc(); newGradeValue = newtest.evalFunc(bruteString); if (newGradeValue > gradeValue) { gradeValue = newGradeValue; optimalSettings = bruteString; } } } } } } } } } } } } //fin MessageBox.Show("Optimal Settings found: " + optimalSettings + Environment.NewLine + "With a grade of: " + gradeValue); Result_Box.Text = "Optimal Settings found: " + optimalSettings + Environment.NewLine + "With a grade of: " + gradeValue; }
public int[] GeneticAlgorythm(int PoolSize, int Cycles, int GeneNumber) { int[][] GeneInt = new int[PoolSize][]; //to store the config parts as integer array array int mutate; string[] GeneString = new string[PoolSize]; //to store the configurations FitFunc EvaluateGene = new FitFunc(); Random randNumGen = new Random(); //GENERATES POOL MEMBERS GeneInt = poolGen(PoolSize); //Genetic Algorythm code to follow: //EVALUATE FIRST BATCH for (int i = 0; i < PoolSize; i++) { GeneString[i] = ToBinary(GeneInt[i]); GeneInt[i][11] = EvaluateGene.evalFunc(GeneString[i]); } for (int i = 0; i < Cycles; i++) { //SORT ARRAY GeneInt = sort(GeneInt, PoolSize); Evaluation.Series["Genetic Algorythm"].Points.AddXY(i, GeneInt[0][11]); System.Threading.Thread.Sleep(1); //Thread sleep to give UI time to updatew with fresh graph //REPOPULATE BOTTOM 2/3 GeneInt = childmaker(GeneInt, PoolSize); if (i < 100) { mutate = randNumGen.Next(0, 101 - i); //non-Uniform mutation prevents later pools from being too random allowing better solutions pools to be kept } else { mutate = 0; } //MUTATE for (int m = PoolSize / GeneNumber; (m < mutate) && (m < PoolSize); m++) //random number of mutations { int r = randNumGen.Next(0, 11); //mutate random parameter GeneInt[m][r] = GeneInt[m][r] + randNumGen.Next(0, PoolSize); } //EVALUATE CHILDREN for (int j = 0; j < PoolSize; j++) { GeneString[j] = ToBinary(GeneInt[j]); GeneInt[j][11] = EvaluateGene.evalFunc(GeneString[j]); } } GeneInt = sort(GeneInt, PoolSize); Result_Box.Text = Result_Box.Text + "The Genetic Algorithm solution is: " + ToBinary(GeneInt[0]) + Environment.NewLine + "With a grade of: " + GeneInt[0][11] + Environment.NewLine; return(GeneInt[0]); }
public int[] ParticleSwarm(int PoolSize, int Cycles) { foreach (var series in ParticleVisual.Series) { series.Points.Clear(); } int[][][] Generation = new int[Cycles][][]; int[][] ParticleInt = new int[PoolSize][], ParticlePlot = new int[PoolSize][], ParticleLocalBest = new int[PoolSize][], temp = new int[PoolSize][], GlobalBest = new int[Cycles][]; double[][] velocity = new double[PoolSize][]; double random; string[] ParticleString = new string[PoolSize]; string particle; //unused was part of attampt to graph particle score over time(cycles) FitFunc EvaluateParticle = new FitFunc(); Random randNumGen = new Random(); //initial velocity for (int a = 0; a < PoolSize; a++) { velocity[a] = new double[12]; for (int b = 0; b < 12; b++) { velocity[a][b] = 0.1; } } //generate random pool of particles ParticleInt = poolGen(PoolSize); Generation[0] = ParticleInt; for (int a = 1; a < Cycles; a++) { Generation[a] = ParticleInt; } for (int i = 0; i < PoolSize; i++) { ParticleString[i] = ToBinary(Generation[0][i]); ParticleInt[i][11] = EvaluateParticle.evalFunc(ParticleString[i]); } ParticlePlot = sort(ParticleInt, PoolSize); GlobalBest[0] = ParticleInt[0]; for (int gen = 0; gen < Cycles; gen++) { GlobalBest[gen] = Generation[gen][0]; for (int i = 0; i < PoolSize; i++) { ParticleString[i] = ToBinary(Generation[gen][i]); Generation[gen][i][11] = EvaluateParticle.evalFunc(ParticleString[i]); } ParticleLocalBest = Generation[gen]; // Step 2 – for cycles many times do this for (int i = 0; i < Cycles; i++) { //System.Threading.Thread.Sleep(1); //Thread sleep to give UI time to updatew with fresh graph //PlotConvergence(i, Generation[gen][i][11]); // Step 3 - For each particle do this for (int j = 0; j < PoolSize; j++) { //temp = (sort(Generation[gen], PoolSize)); //Step 4 – Evaluate the particle’s position. ParticleString[j] = ToBinary(Generation[gen][j]); Generation[gen][j][11] = EvaluateParticle.evalFunc(ParticleString[j]); //Step 5 – Compare particle’s current position to particle’s best local position, if current position is better replace it. if (Generation[gen][j][11] > ParticleLocalBest[j][11]) { int[] temp2 = new int[12]; temp2 = Generation[gen][j]; for (int k = 0; k > 12; k++) { ParticleLocalBest[j][k] = temp2[k]; } } //Step 6 – Compare particle’s current position to global best position of the previous cycle, if better, replace if (gen > 0) { if (Generation[gen][j][11] > GlobalBest[gen - 1][11]) { for (int k = 0; k > 12; k++) { GlobalBest[gen][k] = Generation[gen][j][k]; } } else if (Generation[gen][j][11] < GlobalBest[gen - 1][11]) { for (int k = 0; k > 12; k++) { GlobalBest[gen][k] = Generation[gen - 1][j][k]; } } } // Step 7 - Calculate velocity vector for particles for (int k = 0; k < 11; k++) { random = randNumGen.Next(1, 101) / 100.00; if (gen < 1) { velocity[j][k] = Convert.ToDouble(velocity[j][k] + 2 + random * (ParticleLocalBest[j][k] - Generation[gen][j][k]) + 2 * random * (GlobalBest[gen][k] - Generation[gen][j][k])); } else { velocity[j][k] = Convert.ToDouble(velocity[j][k] + 2 + random * (ParticleLocalBest[j][k] - Generation[gen][j][k]) + 2 * random * (GlobalBest[gen - 1][k] - Generation[gen][j][k])); } // Step 8 - Move particle according to velocity vector. if (gen + 1 >= Cycles) { // } else if (k < 9) { if ((velocity[j][k] < 0) && (Generation[gen][j][k] != 1)) { Generation[gen + 1][j][k] = Generation[gen][j][k]--; } else if ((velocity[j][k] > 0) && (Generation[gen][j][k] != 5)) { Generation[gen + 1][j][k] = Generation[gen][j][k]++; } else if (gen + 1 >= Cycles) { // } else { } } else if (k == 9) { if ((velocity[j][k] < 0) && (Generation[gen][j][k] != 1)) { Generation[gen + 1][j][k] = Generation[gen][j][k]--; } else if ((velocity[j][k] > 0) && (Generation[gen][j][k] != 32)) { Generation[gen + 1][j][k] = Generation[gen][j][k]++; } else if (gen + 1 >= Cycles) { // } else { } } else if (k == 10) { if ((velocity[j][k] < 0) && (Generation[gen][j][k] != 1)) { Generation[gen + 1][j][k] = Generation[gen][j][k]--; } else if ((velocity[j][k] > 0) && (Generation[gen][j][k] != 3)) { Generation[gen + 1][j][k] = Generation[gen][j][k]++; } else if (gen + 1 >= Cycles) { // } else { } } //ParticleVisual.Series["Velocity per cycle"].Points.AddXY(k, velocity[j][k]); } } //Step 9 – Return to Step 2. //ParticleGraph.RunWorkerAsync(); } if (gen < 1) { Evaluation.Series["Particle Swarm"].Points.AddXY(gen, GlobalBest[gen][11]); } else { Evaluation.Series["Particle Swarm"].Points.AddXY(gen, GlobalBest[gen - 1][11]); } } Result_Box.Text = Result_Box.Text + "The Particle Swarm solition is: " + ToBinary(GlobalBest[Cycles - 1]) + Environment.NewLine + "With a grade of: " + GlobalBest[Cycles - 1][11] + Environment.NewLine; return(GlobalBest[0]); }
} // end of sorting function static void Main(string[] args) { int POPULATION_SIZE = 100; //List<List<int>> initialPopulationlist = new List<List<int>>(); List <string> chromosomeList = new List <string>(); var rand = new Random(); for (int n = 0; n < POPULATION_SIZE; n++) // here 10 is the population size { Random random = new Random(); //create a random object List <int> list1 = new List <int>(); //create a list for parameter 1 List <int> list2 = new List <int>(); //create a list for parameter 2 List <int> list3 = new List <int>(); //create a list for parameter 3 List <int> list4 = new List <int>(); //create a list for parameter 4 List <int> list5 = new List <int>(); //create a list for parameter 5 List <int> list6 = new List <int>(); //create a list for parameter 6 List <int> list7 = new List <int>(); //create a list for parameter 7 List <int> list8 = new List <int>(); //create a list for parameter 8 List <int> list9 = new List <int>(); //create a list for parameter 9 List <int> list10 = new List <int>(); //created a list10 correct for 10 - 14 parameters (5 elements) List <int> list11 = new List <int>(); //create a list 11 for 15th parameter (without only 1 rest 0) // Adding elements to List list1.Add(0); list1.Add(0); list1.Add(0); list1.Add(0); list1.Add(0); list1[random.Next() % 5] = 1; // changing one 0 to 1 at random position list2.Add(0); list2.Add(0); list2.Add(0); list2.Add(0); list2.Add(0); list2[random.Next() % 5] = 1; // changing one 0 to 1 at random position list3.Add(0); list3.Add(0); list3.Add(0); list3.Add(0); list3.Add(0); list3[random.Next() % 5] = 1; // changing one 0 to 1 at random position list4.Add(0); list4.Add(0); list4.Add(0); list4.Add(0); list4.Add(0); list4[random.Next() % 5] = 1; // changing one 0 to 1 at random position list5.Add(0); list5.Add(0); list5.Add(0); list5.Add(0); list5.Add(0); list5[random.Next() % 5] = 1; // changing one 0 to 1 at random position list6.Add(0); list6.Add(0); list6.Add(0); list6.Add(0); list6.Add(0); list6[random.Next() % 5] = 1; // changing one 0 to 1 at random position list7.Add(0); list7.Add(0); list7.Add(0); list7.Add(0); list7.Add(0); list7[random.Next() % 5] = 1; // changing one 0 to 1 at random position list8.Add(0); list8.Add(0); list8.Add(0); list8.Add(0); list8.Add(0); list8[random.Next() % 5] = 1; // changing one 0 to 1 at random position list9.Add(0); list9.Add(0); list9.Add(0); list9.Add(0); list9.Add(0); list9[random.Next() % 5] = 1; // changing one 0 to 1 at random position for (int i = 0; i < 5; i++) //loop 5 times { int r = random.Next(0, 2); //genearte random number 0 or 1 list10.Add(r); //add the random number to list10 } list11.Add(0); list11.Add(0); list11.Add(0); list11[random.Next() % 3] = 1; // changing one 0 to 1 at random position List <int> Individualslist = list1.Concat(list2).Concat(list3).Concat(list4).Concat(list5).Concat(list6).Concat(list7).Concat(list8).Concat(list9).Concat(list10).Concat(list11).ToList(); var chromosome = string.Join("", Individualslist); // concatenated chromosomeList is in List<int> data type and needs to be converted to string and this string needs to be stored in "chromosome" chromosomeList.Add(chromosome); } // of "for loop" of initialising Population for (int i = 0; i < 100; i++) { Console.WriteLine(chromosomeList[i]); } List <string> runPopulationlist = new List <string>(); runPopulationlist.AddRange(chromosomeList); // Create fitnesses list with as many elements as the chromosome strings in the OptimumPopulationlist; List <int> fitnesseslist = new List <int>(POPULATION_SIZE); // 100 is the constant population size // calculate fitness for initliased population. for (int i = 0; i < 1; ++i) // this loop evaluates the fitness grade of the engine configurations chromosome string { //fitnesseslist.Clear(); FitFunc generationtest = new FitFunc(); foreach (string chromosomeString in runPopulationlist) { int gradeValue = generationtest.evalFunc(chromosomeString); fitnesseslist.Add(gradeValue); } } List <string> topScorerList = Selection(runPopulationlist, fitnesseslist); for (int i = 0; i < fitnesseslist.Count; i++) { if (runPopulationlist[i] == topScorerList[0]) { Console.WriteLine(topScorerList[0] + " scored the highest fitness grade of " + fitnesseslist[i]); break; } } runPopulationlist = topScorerList; //now contain only the top 10% student names //Clear the contents of the gradeslist completely and keep only fitnesseslist.Clear(); // clear this list after each generation loop. Console.WriteLine("Hello World!"); }