/// <summary>
        /// Update the velocity, position and personal
        /// best position of a particle.
        /// </summary>
        /// <param name="particleIndex">index of the particle in the swarm</param>
        /// <param name="init">if true, the position and velocity
        ///                          will be initialised. </param>
        public void UpdateParticle(int particleIndex, bool init)
        {
            int i = particleIndex;

            double[] particlePosition = null;
            if (init)
            {
                // Create a new particle with random values.
                // Except the first particle which has the same values
                // as the network passed to the algorithm.
                if (m_networks[i] == null)
                {
                    m_networks[i] = (BasicNetwork)m_bestNetwork.Clone();
                    if (i > 0)
                    {
                        m_randomizer.Randomize(m_networks[i]);
                    }
                }
                particlePosition = GetNetworkState(i);
                m_bestVectors[i] = particlePosition;

                // randomise the velocity
                m_va.Randomise(m_velocities[i], m_maxVelocity);
            }
            else
            {
                particlePosition = GetNetworkState(i);
                UpdateVelocity(i, particlePosition);

                // velocity clamping
                m_va.ClampComponents(m_velocities[i], m_maxVelocity);

                // new position (Xt = Xt-1 + Vt)
                m_va.Add(particlePosition, m_velocities[i]);

                // pin the particle against the boundary of the search space.
                // (only for the components exceeding maxPosition)
                m_va.ClampComponents(particlePosition, m_maxPosition);

                SetNetworkState(i, particlePosition);
            }
            UpdatePersonalBestPosition(i, particlePosition);
        }
Esempio n. 2
0
        /// <summary>
        /// Update the velocity, position and personal
        /// best position of a particle.
        /// </summary>
        /// <param name="particleIndex">index of the particle in the swarm</param>
        protected void UpdateParticle(int particleIndex)
        {
            double[] particlePosition = _particles[particleIndex].LongTermMemory;

            UpdateVelocity(particleIndex);

            // velocity clamping
            VectorAlgebra.ClampComponents(_velocities[particleIndex], maxVelocity);

            // new position (Xt = Xt-1 + Vt)
            VectorAlgebra.Add(particlePosition, _velocities[particleIndex]);

            // pin the particle against the boundary of the search space.
            // (only for the components exceeding maxPosition)
            VectorAlgebra.ClampComponents(particlePosition, maxPosition);

            UpdatePersonalBestPosition(particleIndex, particlePosition);
        }