public Particle(ParticleSwarmOptimization us, Random r, Job job, bool maximize) : this()
 {
     Maximize       = maximize;
     BestValue      = maximize ? float.MinValue : float.MaxValue;
     Velocity       = InitializeVelocity(us, r, job);
     BestParameters = InitializeBestParameters(job);
     Job            = job;
 }
            private static float[] InitializeVelocity(ParticleSwarmOptimization us, Random r, Job job)
            {
                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)((r.NextDouble() * 2.0) - 1.0);
                }
                return(velocity);
            }
            internal void UpdateVelocity(ParticleSwarmOptimization us, float[] globalBest, float[] bestInGeneration, Random r)
            {
                var parameters = us.Root.Parameters;
                var factor     = 1.0f - (us.Root.CurrentIteration / (us.Root.TotalIterations - 1.0f) * us.MovementReduction);

                for (int i = 0; i < Velocity.Length; i++)
                {
                    var bestParameterRandom = r.NextDouble();
                    var optimalRandom       = 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] = ((factor * us.Momentum * Velocity[i]) + (float)(globalBestV + localBestV + generationBestV));
                }
            }
Example #4
0
            internal Job UpdatePosition(ParticleSwarmOptimization 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;
                        if (us.Momentum > 0)
                        {
                            Velocity[i] = -Velocity[i];
                        }
                    }
                    if (parameters[i].Current > (parameters[i].Maximum = temp[i].Maximum))
                    {
                        parameters[i].Current = temp[i].Maximum;
                        if (us.Momentum > 0)
                        {
                            Velocity[i] = -Velocity[i];
                        }
                    }
                }
                return(Job = job);
            }
 internal void UpdateVelocity(ParticleSwarmOptimization 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(ParticleSwarmOptimization 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(ParticleSwarmOptimization us, Random rand)
            {
                var temp = Job.Parameters;
                var 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);
            }