Exemplo n.º 1
0
        public ParticleSwarm(int population_size, int dimension_count, CostEvaluationMethod evaluator, double[] lower_bounds, double[] upper_bounds)
        {
            mDimensionCount = dimension_count;

            mLowerBounds = lower_bounds;
            mUpperBounds = upper_bounds;

            mParticleGenerator = (constraints) =>
            {
                SimpleParticle p = new SimpleParticle(this, mDimensionCount, constraints);
                double         lower_bound;
                double         upper_bound;
                double         range;
                for (int d = 0; d < mDimensionCount; ++d)
                {
                    lower_bound = lower_bounds[d];
                    upper_bound = upper_bounds[d];
                    range       = upper_bound - lower_bound;
                    p[d, ParticleVectorType.Position] = lower_bound + range * RandomEngine.NextDouble();
                    p[d, ParticleVectorType.Velocity] = 0;
                }
                Particle cast_p = p as Particle;
                return(cast_p);
            };

            mParticles          = new Particle[population_size];
            mLocalBestParticles = new Particle[population_size];
            mEvaluator          = evaluator;
        }
Exemplo n.º 2
0
        public virtual Particle GenerateParticle(object info)
        {
            if (mParticleGenerator != null)
            {
                return(mParticleGenerator(info));
            }
            SimpleParticle p = new SimpleParticle(this, mDimensionCount, info);

            if (mLowerBounds != null && mUpperBounds != null)
            {
                double lower_bound;
                double upper_bound;
                double range;
                for (int i = 0; i < mDimensionCount; ++i)
                {
                    lower_bound = mLowerBounds[i];
                    upper_bound = mUpperBounds[i];
                    range       = upper_bound - lower_bound;

                    p[i, ParticleVectorType.Position] = lower_bound + range * RandomEngine.NextDouble();
                }
            }
            Particle cast_p = p as Particle;

            if (cast_p == null)
            {
                throw new ArgumentNullException();
            }
            return(cast_p);
        }
        public virtual SimpleParticle Clone()
        {
            SimpleParticle clone = new SimpleParticle(mSwarm, DimensionCount, mParticleGeneratorContext);

            clone.Copy(this);
            return(clone);
        }
Exemplo n.º 4
0
 public double Evaluate(SimpleParticle p)
 {
     if (mEvaluator == null)
     {
         throw new ArgumentNullException();
     }
     return(mEvaluator(p, mConstraints));
 }
 public virtual void Copy(SimpleParticle rhs)
 {
     if (rhs.DimensionCount != DimensionCount)
     {
         throw new ArgumentOutOfRangeException();
     }
     for (int i = 0; i < DimensionCount; ++i)
     {
         for (int j = 0; j < 2; ++j)
         {
             mData[i, j] = rhs.mData[i, j];
         }
     }
     mCost = rhs.mCost;
 }