/// <summary> /// Update the velocity of a particle /// </summary> /// <param name="particleIndex">index of the particle in the swarm</param> /// <param name="particlePosition">the particle current position vector</param> protected void UpdateVelocity(int particleIndex, double[] particlePosition) { int i = particleIndex; double[] vtmp = new double[particlePosition.Length]; // Standard PSO formula // inertia weight m_va.Mul(m_velocities[i], m_inertiaWeight); // cognitive term m_va.Copy(vtmp, m_bestVectors[i]); m_va.Sub(vtmp, particlePosition); m_va.MulRand(vtmp, m_c1); m_va.Add(m_velocities[i], vtmp); // social term if (i != m_bestVectorIndex) { m_va.Copy(vtmp, m_pseudoAsynchronousUpdate ? m_bestVectors[m_bestVectorIndex] : m_bestVector); m_va.Sub(vtmp, particlePosition); m_va.MulRand(vtmp, m_c2); m_va.Add(m_velocities[i], vtmp); } }
/** * 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 personal best position of a particle. /// </summary> /// <param name="particleIndex">Index of the particle in the swarm.</param> /// <param name="particlePosition">the particle current position vector.</param> protected void UpdatePersonalBestPosition(int particleIndex, double[] particlePosition) { // set the network weights and biases from the vector double score = _score.CalculateScore(_particles[particleIndex]); // update the best vectors (g and i) if ((_bestScores[particleIndex] == 0) || IsScoreBetter(score, _bestScores[particleIndex])) { _bestScores[particleIndex] = score; VectorAlgebra.Copy(_bestVectors[particleIndex], particlePosition); } }
/// <summary> /// Update the swarm's best position. /// </summary> protected void UpdateGlobalBestPosition() { bool bestUpdated = false; for (int i = 0; i < this._particles.Length; i++) { if ((_bestVectorIndex == -1) || IsScoreBetter(_bestScores[i], _bestScores[_bestVectorIndex])) { _bestVectorIndex = i; bestUpdated = true; } } if (bestUpdated) { VectorAlgebra.Copy(_bestVector, _bestVectors[_bestVectorIndex]); this.bestScore = _bestScores[_bestVectorIndex]; } }