internal Job UpdatePosition(HybridPSOGD us) { var temp = Job.Parameters; Job job = new Job(); job.Processed = false; job.ProcessedBy = null; job.Processing = false; job.Value = float.NaN; var parameters = job.Parameters = new ParameterSetting[temp.Length]; for (int i = 0; i < temp.Length; i++) { parameters[i] = new ParameterSetting(); // we need to move in real parameter space instead of relative parameter space parameters[i].Current = temp[i].Current + Velocity[i] * (temp[i].Size); // clamp the value inside of parameter space if (parameters[i].Current < (parameters[i].Minimum = temp[i].Minimum)) { parameters[i].Current = temp[i].Minimum; Velocity[i] = -Velocity[i]; } if (parameters[i].Current > (parameters[i].Maximum = temp[i].Maximum)) { parameters[i].Current = temp[i].Maximum; Velocity[i] = -Velocity[i]; } } return(Job = job); }
public Particle(HybridPSOGD us, Job job, bool maximize, Random random) : this() { Maximize = maximize; BestValue = maximize ? float.MinValue : float.MaxValue; Velocity = InitializeVelocity(us, job, random); Job = job; BestParameters = InitializeBestParameters(job); }
private static float[] InitializeVelocity(HybridPSOGD us, Job job, Random random) { var parameters = job.Parameters; float[] velocity = new float[parameters.Length]; // initialize all of the velocities to [-1,1] since we work in relative parameter space for (int i = 0; i < velocity.Length; i++) { velocity[i] = us.MaxInitialVelocity * (float)((random.NextDouble() * 2.0) - 1.0); } return(velocity); }
internal void UpdateVelocity(HybridPSOGD us, float[] globalBest, float[] bestInGeneration, int ourIndex, Random r) { var parameters = us.Root.Parameters; for (int i = 0; i < Velocity.Length; i++) { var bestParameterRandom = r.NextDouble(); var optimalRandom = r.NextDouble(); var generationRandom = r.NextDouble(); var current = Job.Parameters[i].Current; var globalBestV = us.BestParameterWeight * bestParameterRandom * RelativeDistance(parameters[i], current, BestParameters[i]); var localBestV = us.OptimalWeight * optimalRandom * RelativeDistance(parameters[i], current, globalBest[i]); var generationBestV = us.GenerationOptimalWeight * RelativeDistance(parameters[i], current, bestInGeneration[i]); // we step our velocity by apply a momentum to the old velocity and then applying the new with the rest of the fraction Velocity[i] = (us.Momentum * Velocity[i]) + (float)(globalBestV + localBestV + generationBestV); } }
internal void UpdateVelocity(HybridPSOGD us, float[] globalBest, float[] bestInGeneration, int ourIndex, Random r) { var parameters = us.Root.Parameters; for(int i = 0; i < Velocity.Length; i++) { var bestParameterRandom = r.NextDouble(); var optimalRandom = r.NextDouble(); var generationRandom = r.NextDouble(); var current = Job.Parameters[i].Current; var globalBestV = us.BestParameterWeight * bestParameterRandom * RelativeDistance(parameters[i], current, BestParameters[i]); var localBestV = us.OptimalWeight * optimalRandom * RelativeDistance(parameters[i], current, globalBest[i]); var generationBestV = us.GenerationOptimalWeight * RelativeDistance(parameters[i], current, bestInGeneration[i]); // we step our velocity by apply a momentum to the old velocity and then applying the new with the rest of the fraction Velocity[i] = (us.Momentum * Velocity[i]) + (float)(globalBestV + localBestV + generationBestV); } }
internal Job UpdatePosition(HybridPSOGD us) { var temp = Job.Parameters; Job job = new Job(); job.Processed = false; job.ProcessedBy = null; job.Processing = false; job.Value = float.NaN; var parameters = job.Parameters = new ParameterSetting[temp.Length]; for(int i = 0; i < temp.Length; i++) { parameters[i] = new ParameterSetting(); // we need to move in real parameter space instead of relative parameter space parameters[i].Current = temp[i].Current + Velocity[i] * (temp[i].Size); // clamp the value inside of parameter space if(parameters[i].Current < (parameters[i].Minimum = temp[i].Minimum)) { parameters[i].Current = temp[i].Minimum; Velocity[i] = -Velocity[i]; } if(parameters[i].Current > (parameters[i].Maximum = temp[i].Maximum)) { parameters[i].Current = temp[i].Maximum; Velocity[i] = -Velocity[i]; } } return (Job = job); }
internal Job UpdatePosition(HybridPSOGD us, Random rand) { var temp = Job.Parameters; Job job = new Job { Processed = false, ProcessedBy = null, Processing = false, Value = float.NaN }; var parameters = job.Parameters = new ParameterSetting[temp.Length]; for (int i = 0; i < temp.Length; i++) { parameters[i] = new ParameterSetting { // we need to move in real parameter space instead of relative parameter space Current = temp[i].Current + Velocity[i] * (temp[i].Size) }; // clamp the value inside of parameter space if (parameters[i].Current < (parameters[i].Minimum = temp[i].Minimum)) { // reflect off the boundary instead of being absorbed switch (us.Bounce) { case BounceStrategy.Reflect: parameters[i].Current = Math.Min(temp[i].Minimum + (temp[i].Minimum - parameters[i].Current), temp[i].Maximum); break; case BounceStrategy.RandomReallocation: parameters[i].Current = (float)(rand.NextDouble() * (temp[i].Maximum - temp[i].Minimum) + temp[i].Minimum); break; case BounceStrategy.Absorb: parameters[i].Current = parameters[i].Minimum; break; } if (us.Momentum > 0) { Velocity[i] = -Velocity[i]; } } if (parameters[i].Current > (parameters[i].Maximum = temp[i].Maximum)) { // reflect off the boundary instead of being absorbed switch (us.Bounce) { case BounceStrategy.Reflect: parameters[i].Current = Math.Max(temp[i].Maximum - (parameters[i].Current - temp[i].Maximum), temp[i].Minimum); break; case BounceStrategy.RandomReallocation: parameters[i].Current = (float)(rand.NextDouble() * (temp[i].Maximum - temp[i].Minimum) + temp[i].Minimum); break; case BounceStrategy.Absorb: parameters[i].Current = parameters[i].Maximum; break; } if (us.Momentum > 0) { Velocity[i] = -Velocity[i]; } } } return(Job = job); }