/// <summary> /// Plot function with generation of individuals. /// </summary> /// <param name="selectedFuncId"></param> /// <param name="population"></param> private void PlotGeneration(int selectedFuncId, Individuals population) { ILArray <float> A = ILMath.zeros <float>(3, population.Population.Count - 1); ILArray <float> B = ILMath.zeros <float>(3, 0); var dim = _benchmark.GetById(selectedFuncId).Dimension; var best = population.GetBest(); int c = 0; foreach (var i in population.Population) { if (i == best) { continue; } A[0, c] = i.Dimension[0]; A[1, c] = i.Dimension[1]; A[2, c] = i.Z; c += 1; } // Best individual B[0, 0] = best.Dimension[0]; B[1, 0] = best.Dimension[1]; B[2, 0] = best.Z; _scene = new ILScene() { new ILPlotCube(twoDMode: false) { new ILSurface((x, y) => CallFunction(selectedFuncId, new float[] { x, y }), xmin: (float)genMin.Value, xmax: (float)genMax.Value, xlen: LENGTH, ymin: (float)genMin.Value, ymax: (float)genMax.Value, ylen: LENGTH, colormap: Colormaps.Hsv) { new ILColorbar() }, new ILPoints { Positions = ILMath.tosingle(B), Color = Color.Blue }, new ILPoints { Positions = ILMath.tosingle(A), Color = Color.DarkSlateGray } } }; ilPanel1.Scene = _scene; ilPanel1.Refresh(); return; }
private void btnPopulation_Click(object sender, EventArgs e) { if (popSize.Value == 0) { return; } var population = new Individuals(); population.GeneratePopulation((int)popSize.Value, _benchmark.GetById((int)comboFunctions.SelectedValue), _integer, (float)genMin.Value, (float)genMax.Value); //population.ComputeFitness(); PlotGeneration((int)comboFunctions.SelectedValue, population); }
public override Individuals Run(Individuals p, Function f, bool _integer, float?min = null, float?max = null) { if (min == null) { min = f.GetMin(); } if (max == null) { max = f.GetMax(); } Individual best = p.GetBest(); var result = new Individuals(); result.Population.Add(best); result.GeneratePopulation(p.Population.Count - 1, f, _integer, min, max); return(result); }
private async void btnRunAlgorithm_Click(object sender, EventArgs e) { if (popSize.Value == 0) { return; } var population = new Individuals(); population.GeneratePopulation((int)popSize.Value, _benchmark.GetById((int)comboFunctions.SelectedValue), _integer, (float)genMin.Value, (float)genMax.Value); population.ComputeFitness(); for (int i = 0; i < (int)numberOfGenerationsUpDown.Value; i++) { population = _algorithms.All.FirstOrDefault(a => a.Id == (int)comboAlgorithms.SelectedValue).Run(population, _benchmark.GetById((int)comboFunctions.SelectedValue), _integer, (float)genMin.Value, (float)genMax.Value); population.ComputeFitness(); PlotGeneration((int)comboFunctions.SelectedValue, population); await UpdateProgress(i); } }
public override Individuals Run(Individuals p, Function f, bool integer, float?min = null, float?max = null) { if (min == null) { min = f.GetMin(); } if (max == null) { max = f.GetMax(); } if (p.Population.Count < 4) // too small { return(p); } Random r = new Random(); Individuals result = new Individuals(); for (int i = 0; i < p.Population.Count; i++) //for each element { //get 3 different elements in interval int[] rands = new int[3]; Individual[] parents = new Individual[4]; Individual diff = new Individual(); Individual noisy = new Individual(); Individual trial = new Individual(); while (true) //until child in interval is found { do { rands[0] = r.Next(p.Population.Count); } while (rands[0] == i); do { rands[1] = r.Next(p.Population.Count); } while (rands[1] == i || rands[1] == rands[0]); do { rands[2] = r.Next(p.Population.Count); } while (rands[2] == i || rands[2] == rands[0] || rands[2] == rands[1]); parents[0] = p.Population[i]; parents[0].Fitness = f.EvaluateFitness(f.Id, parents[0].Dimension); parents[1] = p.Population[rands[0]]; parents[2] = p.Population[rands[1]]; parents[3] = p.Population[rands[2]]; //get noisy vector (mutation) diff = new Individual(); diff.Dimension = new float[] { parents[1].Dimension[0] - parents[2].Dimension[1], parents[1].Dimension[0] - parents[2].Dimension[1], 0 }; noisy = new Individual(); noisy.Dimension = new float[] { parents[3].Dimension[0] + F * diff.Dimension[0], parents[3].Dimension[1] + F * diff.Dimension[1], 0 }; //get trial element (intersection) trial = new Individual(); trial.Dimension = new float[] { (r.NextDouble() < CR ? noisy.Dimension[0] : parents[0].Dimension[0]), (r.NextDouble() < CR ? noisy.Dimension[1] : parents[0].Dimension[1]), 0 }; trial.Z = f.EvaluateFitness(f.Id, trial.Dimension); trial.Fitness = f.EvaluateFitness(f.Id, trial.Dimension); if (trial.IsOK((float)min, (float)max)) { break; } } //use parent or trial, whichever is better if (trial.Z < parents[0].Z) { result.Population.Add(trial); } else { result.Population.Add(parents[0]); } } return(result); }
public virtual Individuals Run(Individuals p, Function f, bool _integer, float?min = null, float?max = null) { return(null); }
public override Individuals Run(Individuals population, Function f, bool integer, float?min = null, float?max = null) { if (min == null) { min = f.GetMin(); } if (max == null) { max = f.GetMax(); } if (Migration <= 0) { return(population); } Individual leader = population.Population[0]; Individual worst = population.Population[0]; foreach (var e in population.Population) { if (e.Z < leader.Z) { leader = e; } if (e.Z > worst.Z) { worst = e; } } if (worst.Z - leader.Z < MinDiv) { return(population); } Random r = new Random(); Individuals result = new Individuals(); for (int k = 0; k < population.Population.Count; k++) { var e = population.Population[k]; if (e == leader) { result.Population.Add(e); continue; } var jumps = new Individuals(); for (int i = 0; i < PathLength / Step; i++) { var prtv = GetPRTVector(2, r); var leaderdiff = new Individual { Dimension = new float[] { leader.Dimension[0] - e.Dimension[0], leader.Dimension[1] - e.Dimension[1] }, Z = 0 }; var onejump = new Individual { Dimension = new float[] { e.Dimension[0] + leaderdiff.Dimension[0] * i * Step * prtv[0], e.Dimension[1] + leaderdiff.Dimension[1] * i * Step * prtv[1] }, Z = f.EvaluateFitness(f.Id, new float[] { e.Dimension[0] + leaderdiff.Dimension[0] * i * Step * prtv[0], e.Dimension[1] + leaderdiff.Dimension[1] * i * Step * prtv[1] }) }; if (onejump.IsOK((float)min, (float)max)) { jumps.Population.Add(onejump); } } int bestindex = -1; for (int j = 0; j < jumps.Population.Count; j++) { if (jumps.Population[j].Z < e.Z) { bestindex = j; } } result.Population.Add((bestindex == -1) ? population.Population[k] : jumps.Population[bestindex]); } Migration--; return(result); }
public override Individuals Run(Individuals population, Function f, bool integer, float?min = null, float?max = null) { if (min == null) { min = f.GetMin(); } if (max == null) { max = f.GetMax(); } Random r = new Random(); Individuals result = new Individuals(); result.Population.AddRange(population.Population); if (T >= Tf) { for (int i = 0; i < result.Population.Count; i++) // for each element { for (int j = 0; j < nT; j++) // reps { //generate list of neighbors Individuals neighbors = new Individuals(); float tempMax = ((float)max - (float)min) / 2; Individual parent = result.Population[i]; for (int k = 0; k < nT; k++) //number of neighbors often same as number of Metropolis repetitions { float x = parent.Dimension[0] + T * (float)(r.NextDouble() * tempMax - tempMax / 2); float y = parent.Dimension[1] + T * (float)(r.NextDouble() * tempMax - tempMax / 2); if (x < min) { x = (float)min; } else if (x > max) { x = (float)max; } if (y < min) { y = (float)min; } else if (y > max) { y = (float)max; } neighbors.Population.Add(new Individual() { Dimension = new float[] { x, y }, Z = f.EvaluateFitness(f.Id, new float[] { x, y }) }); } //choose randomly Individual choose = neighbors.Population[r.Next(neighbors.Population.Count - 1)]; // If better use it if (choose.Z < parent.Z) { result.Population[i] = choose; } else //If worse, rng might still choose it { float chance = (float)r.NextDouble(); float exp = (float)Math.Pow(Math.E, -(choose.Z - parent.Z) / T); if (chance < exp) { result.Population[i] = choose; } } } } T *= alpha; } return(result); }