//instrukcja do przycisku reset private void resetB_Click(object sender, EventArgs e) { i = 0; j = 0; avvelocity = new float[1]; avfitness = new float[1]; avbfitness = new float[1]; List <Particle> itemList = new List <Particle>(); population = new Populacja(populationestore.dim); population = (Populacja)populationestore.Clone(); foreach (Particle item in populationestore.population) { Particle tmp = new Particle(populationestore.dim); Particle.copy(item, tmp); population.population.Add(tmp); } model = new Thread(GenGraph2); model.IsBackground = true; model.Start(); List <Populacja> tmp1 = new PSO(numberIterations, inertiaw, c1, c2, r1r2, linearinertia).PSOALG(population); this.functionButtonSet(false); animace = new Thread(ShowParticleMove); animace.IsBackground = true; animace.Start(tmp1); }
private void ShowParticleOnGraph(Populacja tmp, int size = 10) { double best = tmp.population.Min(x => x.fitnessValue); foreach (Particle item in tmp.population) { ILArray <float> coords = new float[3]; coords[0] = (float)item.position[0]; coords[1] = (float)item.position[1]; coords[2] = (float)item.fitnessValue;// +1000; ILPoints bod = surface.Add(Shapes.Point); //surface.Colormap = Colormaps.Hot; if (item.fitnessValue == best) { bod.Color = Color.Red; bod.Size = 10; } else { bod.Color = Color.Black; bod.Size = size; } bod.Positions.Update(coords); surface.Add(bod); } }
public static Populacja Copy() { Populacja item = new Populacja(); item.maxX = maxX; item.minX = minX; item.populationSize = populationSize; item.type = type; item.dim = dim; return(item); }
private void GenerateGraph() { Invoke(new Action(() => ilgraf.Visible = true)); MaxEpoch = new Populacja(MaxEpochSize, Funkcje.FunctionName.type); MaxEpoch.SetRangeOfPopulation(); MaxEpoch.GenerateGraphPopulation(); MaxEpoch.ObliczPopulFitness(); population = new Populacja(PopulationSize, dim, Funkcje.FunctionName.type); population.SetRangeOfPopulation(); population.GeneratePopulation(dim); population.ObliczPopulFitness(); float[] newData = new float[MaxEpoch.population.Count * 3]; Parallel.For(0, MaxEpoch.population.Count, i => { newData[i] = (float)MaxEpoch.population[i].fitnessValue; // Z newData[i + MaxEpoch.population.Count] = (float)MaxEpoch.population[i].position[0]; // X newData[i + 2 * MaxEpoch.population.Count] = (float)MaxEpoch.population[i].position[1]; // Y }); int size = (int)Math.Sqrt(MaxEpoch.population.Count); data = ILNumerics.ILMath.array(newData, size, size, 3); surface = new ILSurface(data); surface.Fill.Markable = false; surface.Wireframe.Markable = false; //surface.Colormap = Colormaps.Copper; ILColorbar color = new ILColorbar() { Location = new PointF(.96f, 0.1f) }; surface.Children.Add(color); this.ShowParticleOnGraph(population); //this.ShowGraphChart(population); //this.ShowCharts(population); //this.RefreshModel(); }
private void StartBtn_Click(object sender, EventArgs e) { //string f = FunctionSelectionCombo.SelectedItem.ToString(); //if (!String.IsNullOrEmpty(funkcja)&&!funkcja.Equals("Proszę wybrać funkcję do optymalizacji")) //{ // ileCzastek = Convert.ToInt16(ParticleQuantityUpDown.Value); // maxEpochs = Convert.ToInt16(MaxEpochUpDown.Value); // optymalizacja = new PSO(dziedzinyFunkcji[funkcja], ileCzastek, maxEpochs, funkcja); // MessageBox.Show(string.Format("Znalezione minimum to {0} z błędem {1}", PSO.PSOSolution().Item1, PSO.PSOSolution().Item2),"Rezultat optymalizacji",MessageBoxButtons.OK,MessageBoxIcon.Information); // } // else MessageBox.Show("Nie wybrano funkcji do optymalizacji","BŁĄD!",MessageBoxButtons.OK,MessageBoxIcon.Error); // instrukcja do przycisku start List <Populacja> tmp = new PSO(numberIterations, inertiaw, c1, c2, r1r2, linearinertia).PSOALG(population); this.functionButtonSet(false); animace = new Thread(ShowParticleMove); animace.IsBackground = true; animace.Start(tmp); if (thesame == true) { resetB.Enabled = true; } double[] tab = new double[testnumber]; float[] sum = new float[numberIterations]; float[] best = new float[numberIterations]; float[] worst = new float[numberIterations]; float[] bgfworst = new float[numberIterations]; float[] bgfbest = new float[numberIterations]; float[] bgfav = new float[numberIterations]; float[] globalmin = new float[testnumber]; double wynik = 0; double bestresult = 0; double worstresult = 0; double percentsucess = 0; double tmpbest = 0; double tmpworst = 0; for (int i = 0; i < testnumber; ++i) { population = new Populacja(PopulationSize, dim, Funkcje.FunctionName.type); population.SetRangeOfPopulation(Funkcje.FunctionName.type, error); population.GeneratePopulation(dim); population.ObliczPopulFitness(Funkcje.FunctionName.type); //List<Populacja> tmp = new PSO(numberIterations, inertiaw, c1, c2, r1r2, linearinertia).PSOALG(population);//numberIterations, inertiaw tmp.Remove(tmp.Last()); tab[i] = tmp.Min((x => x.NajlepszaFitness)); //tablica wartości wyników-z tego obliczyc % sukcesów wynik += tab[i]; globalmin[i] = (float)tmp.Min((x => x.NajlepszaFitness)); if (Math.Abs(tab[i] - tmp.First().min) < tmp.First().exitError) { percentsucess++; } if (i == 0) { tmpbest = tab[i]; tmpworst = tab[i]; } int popnumber = 0; foreach (Populacja pop in tmp) { float b = 0; float c = 0; bgfav[popnumber] += (float)pop.NajlepszaFitness / testnumber; var scene = new ILScene(); scene.Screen.First <ILLabel>().Visible = false; foreach (Particle item in pop.population) { // MessageBox.Show(a.Length.ToString()+" " +tmp.populationSize.ToString()); b += (float)item.fitnessValue; } c = (b / pop.population.Count) / testnumber; sum[popnumber] += c; if (tab[i] <= tmpbest) { best[popnumber] = b / pop.population.Count; bgfbest[popnumber] = (float)pop.NajlepszaFitness; bestresult = pop.NajlepszaFitness; tmpbest = tab[i]; } if (tab[i] >= tmpworst) { worst[popnumber] = b / pop.population.Count; bgfworst[popnumber] = (float)pop.NajlepszaFitness; worstresult = pop.NajlepszaFitness; tmpworst = tab[i]; } popnumber++; } } frm.richTextBox1.AppendText("Średnie wartości funkci: " + wynik / testnumber + "\n" + "\n"); frm.richTextBox1.AppendText("Najlepsza wartość funkcji: " + bestresult + "\n" + "\n"); frm.richTextBox1.AppendText("Najgorsza wartość funkcji: " + worstresult + "\n" + "\n"); frm.richTextBox1.AppendText("Procent sukcesu: " + percentsucess / testnumber * 100 + "%" + "\n" + "\n"); var scena = new ILScene(); using (ILScope.Enter()) { ILArray <float> AV = sum; ILArray <float> BEST = best; ILArray <float> WORST = worst; ILArray <float> BGFworst = bgfworst; ILArray <float> BGFbest = bgfbest; ILArray <float> BGFav = bgfav; ILArray <float> GLOBAL = globalmin; var plot = scena.Add(new ILPlotCube() { ScreenRect = new RectangleF(0, 0, 1, 0.4f), Children = { new ILLinePlot(AV.T, lineColor:Color.Yellow), new ILLinePlot(BEST.T, lineColor:Color.Blue), new ILLinePlot(WORST.T, lineColor:Color.Red), } }); var plot1 = scena.Add(new ILPlotCube() { ScreenRect = new RectangleF(0, 0.33f, 1, 0.4f), Children = { new ILLinePlot(BGFav.T, lineColor:Color.Yellow), new ILLinePlot(BGFbest.T, lineColor:Color.Blue), new ILLinePlot(BGFworst.T, lineColor:Color.Red), }, }); var plot2 = scena.Add(new ILPlotCube() { ScreenRect = new RectangleF(0, 0.66f, 1, 0.3f), Children = { new ILLinePlot(GLOBAL.T, markerStyle: MarkerStyle.Diamond, lineColor: Color.Black) }, }); var dg2 = plot2.AddDataGroup(); dg2.Add(new ILLinePlot(GLOBAL.T, markerStyle: MarkerStyle.Diamond, lineColor: Color.Red));//,lineColor: Color.Red)); dg2.ScaleModes.YAxisScale = AxisScale.Logarithmic; var axisY2 = plot2.Axes.Add(new ILAxis(dg2) { AxisName = AxisNames.YAxis, Position = new Vector3(1, 0, 0), Label = { Text = "osiągnięte minimum (log)", Color = Color.Red }, Ticks = { DefaultLabel = { Color = Color.Red } } }); frm.ilgraf.Scene = scena; frm.Show(); } }
public List <Populacja> PSOALG(Populacja p) { Random ran = new Random(); double minBound = p.minX; double maxBound = p.maxX; double minV = -1.0 * maxBound; double maxV = maxBound; int iteracja = 0; double[] NajlepszaPozycja = new double[p.dim]; double NajlepszaFitness = double.MaxValue; List <Populacja> roj = new List <Populacja>(); roj.Add(p); foreach (Particle item in p.population) { if (item.fitnessValue < NajlepszaFitness) { NajlepszaFitness = item.fitnessValue; item.position.CopyTo(NajlepszaPozycja, 0); } p.NajlepszaPozycja = new double[p.dim]; NajlepszaPozycja.CopyTo(p.NajlepszaPozycja, 0); p.NajlepszaFitness = NajlepszaFitness; } double r1, r2; // poznawczy i ogólne spułczynniki randoma // tworzenie roju ->swam while (iteracja < nrIteracji) { ++iteracja; Populacja currP2 = new Populacja(p.type); double[] newVelocity = new double[p.dim]; double[] newPosition = new double[p.dim]; double newFitness; foreach (Particle item in roj.Last().population) { if (linearinertia) { this.w = setw(iteracja); } // główna pętla PSO przejście przez wszystkie iteracje for (int j = 0; j < item.velocity.Length; ++j) { if (r1r2) { r1 = ran.NextDouble(); r2 = ran.NextDouble(); } else { r1 = 1; r2 = 1; } newVelocity[j] = (w * item.velocity[j]) + (c1 * r1 * (item.bestPosition[j] - item.position[j])) + (c2 * r2 * (NajlepszaPozycja[j] - item.position[j])); if (newVelocity[j] < minV) { newVelocity[j] = minV; } else if (newVelocity[j] > maxV) { newVelocity[j] = maxV; } } newVelocity.CopyTo(item.velocity, 0); for (int j = 0; j < item.position.Length; ++j) { newPosition[j] = item.position[j] + newVelocity[j]; if (newPosition[j] < minBound) { newPosition[j] = minBound; } else if (newPosition[j] > maxBound) { newPosition[j] = maxBound; } } Particle best = new Particle(p.dim); newPosition.CopyTo(item.position, 0); newPosition.CopyTo(best.position, 0); Particle.ObliczIndiwFitness(best); newFitness = best.fitnessValue; item.fitnessValue = newFitness; if (newFitness < item.bestFitness) { newPosition.CopyTo(item.bestPosition, 0); item.bestFitness = newFitness; } if (newFitness < NajlepszaFitness) { newPosition.CopyTo(NajlepszaPozycja, 0); NajlepszaFitness = newFitness; } Particle tmp = new Particle(p.dim); //kopia Particle.copy(item, tmp); currP2.population.Add(tmp); roj.Last().NajlepszaPozycja = new double[p.dim]; NajlepszaPozycja.CopyTo(roj.Last().NajlepszaPozycja, 0); roj.Last().NajlepszaFitness = NajlepszaFitness; } roj.Add(currP2); } return(roj); }