示例#1
0
 private void UpdatePositionAndVelocity(Particle particle)
 {
     UpdateVelocity(particle);
     UpdatePosition(particle);
 }
示例#2
0
        private void UpdateVelocity(Particle particle)
        {
            var newVelocity = new double[_vectorSize];

            Parallel.For(0, newVelocity.Length, i =>
            {
                CognitiveRandomizer1 = _random.NextDouble();
                CognitiveRandomizer2 = _random.NextDouble();

                newVelocity[i] = (InertiaWeight * particle.Properties.Velocity[i]) +
                                 (CognitiveWeight * CognitiveRandomizer1 *
                                  (particle.Properties.BestPosition[i] - particle.Properties.Position[i])) +
                                 (SocialWeight * CognitiveRandomizer2 * (BestGlobalPosition[i] - particle.Properties.Position[i]));
            });

            Array.Copy(newVelocity, particle.Properties.Velocity, newVelocity.Length);
        }
示例#3
0
        private void UpdatePosition(Particle particle)
        {
            var newPosition = new double[_vectorSize];

            Parallel.For(0, newPosition.Length, i =>
            {
                newPosition[i] = particle.Properties.Position[i] + particle.Properties.Velocity[i];

                if (newPosition[i] < _configuration.DimensionMinimum)
                {
                    newPosition[i] = _configuration.DimensionMinimum;
                }

                if (newPosition[i] > _configuration.DimensionMaximum)
                {
                    newPosition[i] = _configuration.DimensionMaximum;
                }
            });

            Array.Copy(newPosition, particle.Properties.Position, newPosition.Length);
        }
示例#4
0
        private void UpdateParticleError(NeuralNetworkConfiguration configuration, double[][] dataVectors, Particle particle)
        {
            var newError = Error(configuration, dataVectors, particle.Properties.Position);
            particle.ParticleError.Error = newError;

            if (newError < particle.ParticleError.BestError)
            {
                var bestPosition = particle.Properties.BestPosition;
                Array.Copy(particle.Properties.Position, particle.Properties.BestPosition, bestPosition.Length);

                particle.ParticleError.BestError = newError;
            }

            if (!(newError < BestGlobalError))
                return;

            var position = particle.Properties.Position;

            Array.Copy(particle.Properties.Position, BestGlobalPosition, position.Length);
            BestGlobalError = newError;
        }
示例#5
0
        private void LiveOrDie(Particle particle, NeuralNetworkConfiguration configuration, double[][] dataVectors)
        {
            var currentLifeProbability = _random.NextDouble();

            if (!(currentLifeProbability < ParticleDeathProbability))
                return;

            var position = particle.Properties.Position;
            for (var i = 0; i < position.Length; i++)
                particle.Properties.Position[i] = (_configuration.DimensionMaximum - _configuration.DimensionMinimum) * _random.NextDouble() + _configuration.DimensionMinimum;

            particle.ParticleError.Error = Error(configuration, dataVectors, particle.Properties.Position);
            particle.ParticleError.BestError = particle.ParticleError.Error;

            Array.Copy(particle.Properties.Position, particle.Properties.BestPosition,position.Length);

            if (!(particle.ParticleError.Error < BestGlobalError))
                return;

            BestGlobalError = particle.ParticleError.Error;
            var particlePosition = particle.Properties.Position;
            Array.Copy(particle.Properties.Position, BestGlobalPosition, particlePosition.Length);
        }
示例#6
0
        private void CreateParticleForSwarm(NeuralNetworkConfiguration configuration, double[][] dataVectors, int i)
        {
            var properties = new ParticleProperties(_vectorSize);
            var randomPosition = InitializeRandomPosition();

            var error = Error(configuration, dataVectors, randomPosition);

            var particleError = new ParticleError
            {
                Error = error,
                BestError = error
            };

            var randomVelocity = InitializeRandomVelocity();

            Array.Copy(randomPosition, properties.Position, randomPosition.Length);
            Array.Copy(randomPosition, properties.BestPosition, randomPosition.Length);
            Array.Copy(randomVelocity, properties.Velocity, randomVelocity.Length);

            _swarm[i] = new Particle(particleError, properties);
        }
示例#7
0
        Particle[] CreateParticles(int swarmSize)
        {
            Particle[] swarm = new Particle[swarmSize];
            for (int i = 0; i < swarmSize; i++)
            {
                swarm[i] = new Particle (this);
            }

            return swarm;
        }