/** * Update the velocity of a particle * * @param particleIndex index of the particle in the swarm */ protected void UpdateVelocity(int particleIndex) { double[] particlePosition = _particles[particleIndex].LongTermMemory; double[] vtmp = new double[particlePosition.Length]; // Standard PSO formula // inertia weight VectorAlgebra.Mul(_velocities[particleIndex], inertiaWeight); // cognitive term VectorAlgebra.Copy(vtmp, _bestVectors[particleIndex]); VectorAlgebra.Sub(vtmp, particlePosition); VectorAlgebra.MulRand(_rnd, vtmp, this.c1); VectorAlgebra.Add(_velocities[particleIndex], vtmp); // social term if (particleIndex != _bestVectorIndex) { VectorAlgebra.Copy(vtmp, _bestVector); VectorAlgebra.Sub(vtmp, particlePosition); VectorAlgebra.MulRand(_rnd, vtmp, c2); VectorAlgebra.Add(_velocities[particleIndex], vtmp); } }
/// <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); }
/// <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); }