private void Generate() { double fi1, fi2, psi, w = psi = 1; _swarm.Clear(); if (double.TryParse(textBoxFi1.Text, out fi1) && double.TryParse(textBoxFi1.Text, out fi2)) { if (algorithmType == EAlgorithm.Base || algorithmType == EAlgorithm.Weight && double.TryParse(textBoxFactor.Text, out w) || algorithmType == EAlgorithm.Constriction && double.TryParse(textBoxFactor.Text, out psi)) { _swarm = new Swarm(functionType, Population.Value, fi1, fi2, w, psi, panelMap.Width, panelMap.Height); panelMap.Image = Swarm.Map; panelMap.Refresh(); } } }
public void Run(int swarmSize, Function func) { var watch = System.Diagnostics.Stopwatch.StartNew(); Initialize(swarmSize, func); double vMax = Math.Abs(Func.BoundUpper - Func.BoundLower) * .1; const double wLow = 0.1; var best = new Particle(); int lastImprovementOn = 0; for (int iter = 0; iter < func.IterationsNumber; iter++) { double w = func.MaxVelocity - (func.MaxVelocity - wLow) * iter / func.IterationsNumber; foreach (Particle particle in Swarm.Particles) { for (int d = 0; d < Func.Dimensions; d++) { double rp = Random.NextDouble(); double rg = Random.NextDouble(); particle.V[d] = w * particle.V[d] + PhiP * rp * (particle.P[d] - particle.X[d]) + PhiG * rg * (Swarm.G[d] - particle.X[d]); particle.V[d] = Math.Min(particle.V[d], vMax); particle.V[d] = Math.Max(particle.V[d], -vMax); particle.X[d] += particle.V[d]; particle.X[d] = Math.Max(particle.X[d], Func.BoundLower); particle.X[d] = Math.Min(particle.X[d], Func.BoundUpper); } particle.Fx = Func.F(particle.X); if (particle.Fx < particle.Fp) { particle.UpdateP(); } else if (Random.NextDouble() <= func.KillProbability) { particle.Generate(func, Random); } if (particle.Fp < Swarm.Fg) { Swarm.UpdateG(particle.P, Func); lastImprovementOn = iter; best = particle; } } Console.WriteLine($"#{iter,-4} Best G = {Swarm.Fg,-16:0.000000000000} W = {w,-7:0.00000}"); } watch.Stop(); Console.WriteLine($"\nLast improvement was on iteration #{lastImprovementOn}. Time elapsed: {watch.Elapsed}"); Console.WriteLine("Coordinates of the best"); foreach (double x in best.X) { Console.Write($"{x,20}"); } Console.WriteLine(); }
private void buttonSim_Click(object sender, EventArgs e) { Buttons(false); StringBuilder results = new StringBuilder(); double[,] Fi = { { 1, 3 }, { 2, 2 }, { 3, 1 } }; double x = 0, y = 0, best; List<Particle> particles = new List<Particle>(Population.Value); for (int particle = 0; particle < Population.Value; ++particle) particles.Add(new Particle(panelMap.Width, panelMap.Height)); using (FileStream stream = File.OpenWrite("sim")) { _serializer.Serialize(stream, particles); } foreach (EFunction function in Enum.GetValues(typeof(EFunction))) foreach (EAlgorithm algorithm in Enum.GetValues(typeof(EAlgorithm))) foreach (ETopology topology in Enum.GetValues(typeof(ETopology))) for (int k = 0; k < Fi.GetLength(0); ++k) { best = double.MaxValue; for (int i = 0; i < 10; ++i) { switch (algorithm) { case EAlgorithm.Base: _swarm = new Swarm(function, Population.Value, Fi[k, 0], Fi[k, 1], 1d, 1d, panelMap.Width, panelMap.Height); break; case EAlgorithm.Weight: _swarm = new Swarm(function, Population.Value, Fi[k, 0], Fi[k, 1], 0.4, 1d, panelMap.Width, panelMap.Height); break; case EAlgorithm.Constriction: _swarm = new Swarm(function, Population.Value, Fi[k, 0], Fi[k, 1], 1d, 0.6d, panelMap.Width, panelMap.Height); break; } using (FileStream stream = File.OpenRead("sim")) { _swarm.Clear(); _swarm.AddRange((List<Particle>)_serializer.Deserialize(stream)); } for (int j = 0; j < 500; ++j) switch (topology) { case ETopology.Full: _swarm.SimulateStep(Delay.Value); break; case ETopology.Ring: _swarm.SimulateStep(Delay.Value, 2); break; case ETopology.N4: _swarm.SimulateStep(Delay.Value, 4); break; } foreach (Particle p in _swarm.Particles) { if (best > p.Best) { best = p.Best; x = p.BestX; y = p.BestY; } } } results.Append(function + "," + algorithm + "," + topology + "," + Fi[k, 0] + "," + Fi[k, 1] + "," + x.ToString("N4") + "," + y.ToString("N4") + "," + best.ToString("N4") + Environment.NewLine); } File.WriteAllText("my.csv", results.ToString()); Buttons(true); }