示例#1
0
        /// <summary>
        /// Construct PSO trainer.
        /// </summary>
        /// <param name="theParticles">The particles to use.</param>
        /// <param name="theCalculateScore">The score object.</param>
        public TrainPSO(IMLMethod[] theParticles,
                        IScoreFunction theCalculateScore)
        {
            _particles = theParticles;
            _score     = theCalculateScore;
            int vectorSize    = theParticles[0].LongTermMemory.Length;
            int particleCount = theParticles.Length;

            _bestVectors = new double[particleCount][];
            _velocities  = new double[particleCount][];
            _bestScores  = new double[particleCount];

            for (int i = 0; i < particleCount; i++)
            {
                _bestVectors[i] = new double[vectorSize];
                _velocities[i]  = new double[vectorSize];
            }

            _bestVectorIndex = -1;

            _bestVector = new double[vectorSize];

            foreach (double[] velocity in _velocities)
            {
                VectorAlgebra.Randomise(_rnd, velocity, this.maxVelocity);
            }
        }
        /// <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);
        }