public Part1a(int swarm_size, int maxIterations, double c1, double c2, char type) { // store swarm size, iterations, c1, c2, part4 this.swarm_size = swarm_size; this.maxIterations = maxIterations; this.c1 = c1; this.c2 = c2; this.type = type; r = new Random(); // Weighted Velocity this.wmax = 1.0; // influence of current velocity this.wmin = 0.0; // on velocity update fxn // Vmax vmax = 5.0; // bound on particle velocity swarm = new LinkedList<Particle1a>(); for (int i = 0; i < this.swarm_size; i++) { Particle1a p = new Particle1a(); swarm.AddLast(p); p.SetPBest(p); } }
//TODO public Particle1a Clone() { Particle1a p = new Particle1a(); this.x.CopyTo(p.x, 0); this.velocity.CopyTo(p.velocity, 0); p.setZ(this.z); p.SetFitness(this.fitness); return p; }
public Particle1a() { this.r = new Random(); this.x[0] = (this.r.NextDouble() * 6) - 3; //Generate a random X [-3,+3] this.x[1] = (this.r.NextDouble() * 4) - 2; //Generate a random Y [-2,+2] this.velocity[0] = 0; this.velocity[1] = 0; this.pbest = null; this.lbest = null; this.calculateFitness(); }
public void SetPBest(Particle1a pbest) { this.pbest = pbest; }
public void SetLBest(Particle1a lbest) { this.lbest = lbest; }
public Particle1a RunPSO() { int iteration = 0; gbest = swarm.ElementAt(0); // first particle //while (iteration < this.maxIterations) do { foreach (Particle1a p in swarm) { p.calculateFitness(); //recalculate fitness if (p.GetFitness() < p.GetPBest().GetFitness()) //update personal best { p.SetPBest(p.Clone()); } } CalculateLocalBest(); // calculate gbest foreach (Particle1a p in swarm) { if (p.GetFitness() < gbest.GetFitness()) { gbest = p.Clone(); } } switch (type) { case 'x': // normal PSO - global best CalculateVelocity(); break; case 'y': // normal PSO - local best CalculateVelocityLBest(); break; case 'a': // Weighted Inertia - gbest // Console.WriteLine("Using Weighted Inertia Velocity Update with Global Best"); // calculate weighting function /* w = wMax-[(wMax-wMin) x iter]/maxIter where wMax= initial weight, wMin = final weight, maxIter = maximum iteration number, iter = current iteration number. */ double w = (double)(wmax - (((wmax - wmin) * (double)iteration) / (double)maxIterations)); // update velocity and position CalculateWeightedVelocityGBest(w); break; case 'b': // Vmax - Gbest //Console.WriteLine("Using Vmax Velocity Update with Global Best"); CalculateVmaxVelocityGBest(); break; case 'c': // Constriction factor - gbest // Console.WriteLine("Using Constriction Factor Velocity Update with Global Best"); // update velocity and position CalculateConstrictionFactorVelocityGBest(); break; case 'd': // Weighted Inertia - lbest // Console.WriteLine("Using Weighted Inertia Velocity Update with Local Best"); w = (double)(wmax - (((wmax - wmin) * (double)iteration) / (double)maxIterations)); // update velocity and position CalculateWeightedVelocityLBest(w); break; case 'e': // Vmax - lbest //Console.WriteLine("Using Vmax Velocity Update with Local Best"); CalculateVmaxVelocityLBest(); break; case 'f': // Constriction factor - lbest // update velocity and position CalculateConstrictionFactorVelocityLBest(); break; case 'g': // change random r - seed? int factor = maxIterations / 10; int remainder = 0; Math.DivRem(iteration, factor, out remainder); if (remainder == 0) // count is divisible by factor { r = new Random(iteration); } CalculateVelocity(); // use normal velocity break; default: CalculateVelocity(); //normal velocity break; } iteration++; // gbest.Print(count); // print solutions at 0, 10, 100, 1000, 10000 iterations if (iteration == 0 || iteration == 10 || iteration == 100 || iteration == 1000 || iteration == 10000) { // print gbest Console.WriteLine("\n\nIteration Best @ iteration #"+ iteration +" : "); Console.WriteLine(gbest.toString()); } } while (iteration < this.maxIterations && gbest.GetFitness() > -1.0316); return gbest; }
// OPTION B: Vmax Velocity w/ global best void CalculateVmaxVelocityGBest() { /* vnew = vold + c1*rand1*(pbest - xfitness) + c2*rand2*(gbest - xfitness) xfitness' = xfitness + vnew if currentv > vmax then currentv = vmax else if currentv < -vmax then currentv = -vmax end */ if (gbest == null) // if no gbest { gbest = swarm.ElementAt(0); //set gbest as first particle 0 } foreach (Particle1a p in swarm) { for (int i = 0; i < 2; i++) { double vnew = p.GetVelocity(i) + (c1 * r.NextDouble() * (p.GetPBest().GetX(i) - p.GetX(i))) + (c2 * r.NextDouble() * (gbest.GetX(i) - p.GetX(i))); // ensure vnew is within vmax bound if (vnew > vmax) { vnew = vmax; } else if (vnew < ((-1) * vnew)) { vnew = (-1) * vmax; } //store velocity p.SetVelocity(i, vnew); // update position double xnew = p.GetX(i) + vnew; p.SetX(i, xnew); } } return; }