/// <summary> /// 1. Dynamic rate mute operator /// </summary> /// <param name="chrom">The individual to mutate</param> /// <returns>A mutated individual</returns> public Chromosome DRM(Chromosome chrom) { int nVar, nmu, it, MaxIt; int[] muLocation;// double a, b, sm; nVar = chrom.chromosome.Length; //double[] chromosomeTemp = chrom.chromosome; double[] Fai0; //Chromosome chromTemp =(Chromosome)DeepClone(chrom);// deep clone not work Chromosome chromTemp = Chromosome.Clone <Chromosome>(chrom);//use this method doing deep copy nmu = Convert.ToInt32(Math.Ceiling(chrom.MutationProbability * nVar)); if (nmu > nVar) { nmu = nVar; } muLocation = myRandom.NextUnique(1, nVar, nmu); //生成nmu个1~nVar之间的随机整数,突变的位置,突变一个或多个元素 Fai0 = myRandom.NextDouble(-1, 1, 10, nVar); //产生[-1, 1]之间随机数行向量 b = 1.0; a = 10.0; it = chrom.CurrentIteration; MaxIt = chrom.MaxIteration; double waveNum = 3; //sm = 1.0 / 3.0 * Math.Atan(a * Math.Pow((1.0 - 2.0 * (it/ MaxIt)), b)) + 0.5;//% 数 ,3 * it决定有3个波段 sm = 1.0 / 3.0 * Math.Atan(a * Math.Pow(1.0 - 2.0 * (((waveNum * it) % MaxIt) / MaxIt), b)) + 0.5;//% 数 ,3 * it决定有3个波段 for (int i = 0; i < muLocation.Length; i++) { for (int j = 0; j < nVar; j++) { if (muLocation[i] == j) { //chromosomeTemp[i] = chrom.chromosome[i] + 0.5 * sm * Fai0[i]* (chrom.UpperBd[i] - chrom.LowerBd[i]);// 0.5 *[1, 0] *[-1, 1] *[区间长度]; chromTemp.chromosome[i] = chrom.chromosome[i] + 0.5 * sm * Fai0[i] * (chrom.UpperBd[i] - chrom.LowerBd[i]);// 0.5 *[1, 0] *[-1, 1] *[区间长度]; } //chrom.chromosome[i] = chromosomeTemp[i];// } } chromTemp.Fit = double.MaxValue; chromTemp.CheckBoundary(); chromTemp.SetChromDecimalPlace(); return(chromTemp); }//DRM
/// <summary> /// 2. Simplex crossover:Da Ronco C C, Benini E. GeDEA-II: A simplex crossover based evolutionary algorithm /// including the genetic diversity as objective[J]. Engineering Letters, 2013, 21(1): 23-35. /// </summary> /// <param name="mom">parnet 1</param> /// <param name="dad">parent 2</param> /// <returns>two children</returns> public List <Chromosome> CrossOver_SPX(Chromosome mom, Chromosome dad) { List <Chromosome> result = new List <Chromosome>(); Chromosome child1 = Chromosome.Clone <Chromosome>(mom); //use this method doing deep copy Chromosome child2 = Chromosome.Clone <Chromosome>(dad); double Refl = myRandom.NextDouble(0, 0.5); //Generates a uniform random number between 0 and 0.5 double Refl2 = myRandom.NextDouble(0.5, 1); double n = 2.0; double M; if (mom.Fit < dad.Fit) { for (int i = 0; i < mom.ChromosomeSize; i++) { M = mom.chromosome[i] / n; child1.chromosome[i] = (1 + Refl) * M - Refl * dad.chromosome[i]; child2.chromosome[i] = (1 + Refl2) * M - Refl2 * dad.chromosome[i]; } } else { for (int i = 0; i < mom.ChromosomeSize; i++) { M = dad.chromosome[i] / n; child1.chromosome[i] = (1 + Refl) * M - Refl * mom.chromosome[i]; child2.chromosome[i] = (1 + Refl2) * M - Refl2 * mom.chromosome[i]; } } child1.Fit = double.MaxValue;//Fit need re-compute,so here give a great value child2.Fit = double.MaxValue; child1.CheckBoundary(); child1.SetChromDecimalPlace(); child2.CheckBoundary(); child2.SetChromDecimalPlace(); result.Add(child1); result.Add(child2); return(result); }
/// <summary> /// 1. Simulate binary crossover operator /// </summary> /// <param name="mom">parent 1</param> /// <param name="dad">parent 2</param> /// <returns>two children</returns> public List <Chromosome> CrossOver_SBX(Chromosome mom, Chromosome dad) { List <Chromosome> result = new List <Chromosome>(); //if (mom == dad) // Debug.WriteLine("Oh shet! are the mom and dad same!?"); //Chromosome child1 = (Chromosome)DeepClone(mom);//This method doesn't work //Chromosome child2 = (Chromosome)DeepClone(dad); Chromosome child1 = Chromosome.Clone <Chromosome>(mom);//use this method doing deep copy Chromosome child2 = Chromosome.Clone <Chromosome>(dad); double EPS = 1E-14; double etaC = mom.CurrentIteration / mom.MaxIteration * 5.0; //etaC=2~5 Random ran = new Random(Guid.NewGuid().GetHashCode()); //http://www.splaybow.com/post/csharp-generate-random-num.html double y1, y2, yl, yu, rnd, beta, alpha, betaq, c1, c2; for (int qq = 0; qq < mom.chromosome.Length; qq++) { if (ran.NextDouble() < 0.5) { if (Math.Abs(mom.chromosome[qq] - dad.chromosome[qq]) > EPS) { if (mom.chromosome[qq] < dad.chromosome[qq]) { y1 = mom.chromosome[qq]; y2 = dad.chromosome[qq]; } else { y1 = dad.chromosome[qq]; y2 = mom.chromosome[qq]; } yl = mom.LowerBd[qq]; yu = mom.UpperBd[qq]; rnd = ran.NextDouble(); beta = 1.0 + (2.0 * (y1 - yl) / (y2 - y1)); alpha = 2.0 - Math.Pow(beta, (-(etaC + 1.0))); if (rnd <= (1.0 / alpha)) { betaq = Math.Pow((rnd * alpha), (1.0 / (etaC + 1.0))); } else { betaq = Math.Pow((1.0 / (2.0 - rnd * alpha)), (1.0 / (etaC + 1.0))); } c1 = 0.5 * ((y1 + y2) - betaq * (y2 - y1)); beta = 1.0 + (2.0 * (yu - y2) / (y2 - y1)); alpha = 2.0 - Math.Pow(beta, (-(etaC + 1.0))); if (rnd <= (1.0 / alpha)) { betaq = Math.Pow((rnd * alpha), (1.0 / (etaC + 1.0))); } else { betaq = Math.Pow((1.0 / (2.0 - rnd * alpha)), (1.0 / (etaC + 1.0))); } c2 = 0.5 * ((y1 + y2) + betaq * (y2 - y1)); if (c1 < yl) { c1 = yl; } if (c2 < yl) { c2 = yl; } if (c1 > yu) { c1 = yu; } if (c2 > yu) { c2 = yu; } if (ran.NextDouble() <= 0.5) { child1.chromosome[qq] = c2; child2.chromosome[qq] = c1; } else { child1.chromosome[qq] = c1; child2.chromosome[qq] = c2; } } else { child1.chromosome[qq] = mom.chromosome[qq]; child2.chromosome[qq] = dad.chromosome[qq]; } } else { child1.chromosome[qq] = mom.chromosome[qq]; child2.chromosome[qq] = dad.chromosome[qq]; } } child1.Fit = double.MaxValue;//Fit need re-compute,so here give a great value child2.Fit = double.MaxValue; //child1.CheckBoundary(); child1.SetChromDecimalPlace(); //child2.CheckBoundary(); child2.SetChromDecimalPlace(); result.Add(child1); result.Add(child2); return(result); }
public Chromosome ChaoticLS(Chromosome chrom) //Jia D , Zheng G , Khan M K . An effective memetic differential evolution algorithm based on chaotic local search[J]. Information ences, 2011, 181(15):3175-3187. { Chromosome betterChrom = Chromosome.Clone <Chromosome>(chrom); //use this method doing deep copy double beta = myRandom.NextDouble(0.0, 1.0); double u = 4.0, m = 1000; double betac, lambda; int Sl = (int)Math.Round(chrom.ChromosomeSize / 5.0); if (Sl < 1) { Sl = 1; } int[] loc = myRandom.NextUnique(0, chrom.ChromosomeSize - 1, Sl); //Random ran = new Random(Guid.NewGuid().GetHashCode()); while (true) { if (beta != 0.25 && beta != 0.5 && beta != 0.75) { break; } beta = myRandom.NextDouble(0.0, 1.0); } for (int i = 0; i < chrom.PopulationSize * 5; i++) { beta = u * beta * (1 - beta); foreach (var j in loc) { betac = chrom.LowerBd[j] + beta * (chrom.UpperBd[j] - chrom.LowerBd[j]); if (chrom.Fit > 0.0) { lambda = 1 - Math.Pow(Math.Abs((chrom.Fit - 1) / chrom.Fit), m); } else { lambda = 1 - Math.Pow(Math.Abs((chrom.Fit + 1) / chrom.Fit), m); } betterChrom.chromosome[j] = (1 - lambda) * chrom.chromosome[j] + lambda * betac; } betterChrom.CheckBoundary(); betterChrom.SetChromDecimalPlace(); betterChrom.SolveFitness(); if (betterChrom.Fit < chrom.Fit) { //Console.WriteLine("local search find a better individuals!"); break; } } if (betterChrom.Fit < chrom.Fit) { return(betterChrom); } else { return(chrom); } }
public List <Chromosome> Execute() { //using select method Selection SL = new Selection(); SL.SelectMethodEvent += SL.Select_Tournament; //Chromosome result=SL.Select(Population pop); //using cross method Crossover CX = new Crossover(); CX.CrossMethodEvent += CX.CrossOver_MIX; // List<Chromosome> result=CX.Cross(Chromosome mom, Chromosome dad); //using mute method Mutation MU = new Mutation(); MU.MuteMethodEvent += MU.DRM; //Chromosome result = MU.Mute(Chromosome chrom); //using chaotic local search ChaoticLocalSearch LS = new ChaoticLocalSearch(); LS.CLSMethodEvent += LS.ChaoticLS; #region All population evolutionary algebraic processes //Iterative evolution Random ran = new Random(Guid.NewGuid().GetHashCode()); List <Chromosome> result1 = new List <Chromosome>(); Chromosome result2 = new Chromosome(); int maxSameIt = 0; for (int it = 0; it < MaxIteration; it++) { #region one population for (int popi = 0; popi < NumPopulation; popi++) { // Update iteration times PopsOfGa[popi].CurrentIteration = it;//Current iteration of evolution PopsOfGaCX[popi].CurrentIteration = it; PopsOfGaMU[popi].CurrentIteration = it; //HistBestChromOfPops[popi].CurrentIteration = it; for (int k = 0; k < PopsOfGa[popi].PopulationSize; k++) { PopsOfGa[popi].ChromsOfPop[k].CurrentIteration = it; } for (int k = 0; k < PopsOfGaCX[popi].ChromsOfPop.Count; k++) { PopsOfGaCX[popi].ChromsOfPop[k].CurrentIteration = it; } for (int k = 0; k < PopsOfGaMU[popi].ChromsOfPop.Count; k++) { PopsOfGaMU[popi].ChromsOfPop[k].CurrentIteration = it; } //select and cross for (int k = 0; k < numOffspringsOfCross[popi] - 1; k = k + 2) { //select operator int Chroms1Index = SL.Select(PopsOfGa[popi]); int Chroms2Index = SL.Select(PopsOfGa[popi]); //cross operator result1 = CX.Cross(PopsOfGa[popi].ChromsOfPop[Chroms1Index], PopsOfGa[popi].ChromsOfPop[Chroms2Index]); PopsOfGaCX[popi].ChromsOfPop.AddRange(result1); } PopsOfGaCX[popi].ChromsOfPop.RemoveRange(0, numOffspringsOfCross[popi]); //Console.WriteLine("PopsOfGaCX before solve:"); //PopsOfGaCX[popi].ChromsOfPopCWFitAndChroms(); PopsOfGaCX[popi].ChromsOfPopSolveFit();// * solve every individuals fitness //Console.WriteLine("PopsOfGaCX after solve:"); //PopsOfGaCX[popi].ChromsOfPopCWFitAndChroms(); //mute operator for (int k = 0; k < numPointOfMute[popi]; k++) { int Chroms1IndexMu = ran.Next(0, PopsOfGa[popi].ChromsOfPop.Count); result2 = MU.Mute(PopsOfGa[popi].ChromsOfPop[Chroms1IndexMu]); PopsOfGaMU[popi].ChromsOfPop.Add(result2); } PopsOfGaMU[popi].ChromsOfPop.RemoveRange(0, numPointOfMute[popi]); //Console.WriteLine("PopsOfGaMU before solve:"); //PopsOfGaMU[popi].ChromsOfPopCWFitAndChroms(); //Console.WriteLine("PopsOfGaMU after solve:"); PopsOfGaMU[popi].ChromsOfPopSolveFit(); //PopsOfGaMU[popi].ChromsOfPopCWFitAndChroms(); //combine three population for (int iCx = 0; iCx < PopsOfGaCX[popi].PopulationSize; iCx++) { PopsOfGa[popi].ChromsOfPop.Add(Chromosome.Clone <Chromosome>(PopsOfGaCX[popi].ChromsOfPop[iCx])); } for (int iMu = 0; iMu < PopsOfGaMU[popi].PopulationSize; iMu++) { PopsOfGa[popi].ChromsOfPop.Add(Chromosome.Clone <Chromosome>(PopsOfGaMU[popi].ChromsOfPop[iMu])); } //Console.WriteLine($"PopsOfGa+PopsOfGaCX+PopsOfGaMU[{popi}] solve but before sort:"); //PopsOfGa[popi].ChromsOfPopCWFitAndChroms(); //Console.WriteLine($"sort:"); //sort(ascending) with fitness PopsOfGa[popi].FitSort();// //PopsOfGa[popi].ChromsOfPopCWFitAndChroms(); // remove the bad ones PopsOfGa[popi].ChromsOfPop.RemoveRange(PopulationSize[popi], (numOffspringsOfCross[popi] + numPointOfMute[popi])); //Console.WriteLine($"PopsOfGa[{popi}] after remove:"); //PopsOfGa[popi].ChromsOfPopCWFitAndChroms(); //Local search PopsOfGa[popi].ChromsOfPop[0] = Chromosome.Clone <Chromosome>(LS.LocalSearch(PopsOfGa[popi].ChromsOfPop[0])); //elitism in a population if (HistBestChromOfPops[popi].Fit > PopsOfGa[popi].ChromsOfPop[0].Fit) { HistBestChromOfPops[popi] = Chromosome.Clone <Chromosome>(PopsOfGa[popi].ChromsOfPop[0]); } else { //PopsOfGa[popi].ChromsOfPop[0] = (Chromosome)DeepClone(HistBestChromOfPops[popi]);//elitism PopsOfGa[popi].ChromsOfPop[0] = Chromosome.Clone <Chromosome>(HistBestChromOfPops[popi]);//elitism } HistBestChromOfPops[popi].CurrentIteration = it; } #endregion popi //Take turns to swap the best:The best value of the next population gives the best value of the previous population if (NumPopulation == 1) { //HistBestChromOfGa[it] = (Chromosome)DeepClone(HistBestChromOfPops[0]); HistBestChromOfGa[it] = Chromosome.Clone <Chromosome>(HistBestChromOfPops[0]); } else { //Chromosome tmepChromo = (Chromosome)DeepClone(PopsOfGa[0].ChromsOfPop[0]); Chromosome tmepChromo = Chromosome.Clone <Chromosome>(PopsOfGa[0].ChromsOfPop[0]); for (int popi = 0; popi < NumPopulation; popi++) { if (HistBestChromOfGa[it].Fit > PopsOfGa[popi].ChromsOfPop[0].Fit) { //HistBestChromOfGa[it] = (Chromosome)DeepClone(PopsOfGa[popi].ChromsOfPop[0]); HistBestChromOfGa[it] = Chromosome.Clone <Chromosome>(PopsOfGa[popi].ChromsOfPop[0]); } else { //PopsOfGa[popi].ChromsOfPop[0] = (Chromosome)DeepClone(HistBestChromOfGa[it]); PopsOfGa[popi].ChromsOfPop[0] = Chromosome.Clone <Chromosome>(HistBestChromOfGa[it]); } //if (PopsOfGa[popi + 1] != null) if ((popi + 1) < NumPopulation) { //PopsOfGa[popi].ChromsOfPop[0] = (Chromosome)DeepClone(PopsOfGa[popi + 1].ChromsOfPop[0]); PopsOfGa[popi].ChromsOfPop[0] = Chromosome.Clone <Chromosome>(PopsOfGa[popi + 1].ChromsOfPop[0]); } if (popi == (NumPopulation - 1) && popi != 0) { //PopsOfGa[NumPopulation - 1].ChromsOfPop[0] = (Chromosome)DeepClone(tmepChromo); PopsOfGa[NumPopulation - 1].ChromsOfPop[0] = Chromosome.Clone <Chromosome>(tmepChromo); } } } if (it > (int)Math.Round(MaxIteration * 0.3)) { for (int itsame = 0; itsame < (int)Math.Round(MaxIteration * 0.3); itsame++) { if (Math.Abs(HistBestChromOfGa[it].Fit - HistBestChromOfGa[it - itsame - 1].Fit) < 1e-30) { maxSameIt++; } } if (maxSameIt > (int)Math.Round(MaxIteration * 0.2)) { break; } } // Console.WriteLine($"iteration:{HistBestChromOfGa[it].CurrentIteration},bestFit:{HistBestChromOfGa[it].Fit}"); foreach (double para in HistBestChromOfGa[it].chromosome) { Console.Write($"{para} "); } Console.WriteLine(); Console.WriteLine(); } #endregion return(HistBestChromOfGa); // return globalBestChrom; }