Ejemplo n.º 1
0
        public SwarmResult1 DoPSO() // Particle movement to achieve
        {                           // for particle swarm optimization
            for (int i = 0; i < SwarmDim; i++)
            {
                G[i] = PList[0].POSITION[i];
            }
            numberofinteration = 15000;

            for (int j = 0; j < 200; j++) // iterations
            {
                System.Diagnostics.Debug.WriteLine("interation:  " + j);

                // find best position in the swarm
                for (int i = 0; i < SwarmDim; i++)
                {
                    P[i] = PList[0].POSITION[i];
                }

                foreach (Particle2 pt in PList)
                {
                    if (Math.Abs(FunctionToSolve3(pt.POSITION)) < Math.Abs(FunctionToSolve3(P)))
                    {
                        for (int i = 0; i < SwarmDim; i++)
                        {
                            P[i] = pt.POSITION[i];
                        }
                    }
                }
                if (Math.Abs(FunctionToSolve3(P)) < Math.Abs(FunctionToSolve3(G)))
                {
                    for (int i = 0; i < SwarmDim; i++)
                    {
                        G[i] = P[i];
                    }
                }

                //update
                foreach (Particle2 pt in PList)
                {
                    pt.UpdateVelocity(P, G);
                    pt.UpdatePosition();
                }
            }
            SwarmResult1 sr = new SwarmResult1
            {
                SwarmId       = swarmNum,
                X             = G,
                FunctionValue = FunctionToSolve3(G)
            };

            return(sr);
        }
Ejemplo n.º 2
0
        public void ComputeGMM_ND2()
        {
            SwarmSystem1 ss = new SwarmSystem1(50, inputMatrix, k, dim);

            ss.Initialize();
            SwarmResult1 sr = ss.DoPSO();



            //===========Parse===========
            double[] x = sr.X;

            Debug.WriteLine("[{0}]", string.Join(", ", x));

            #region Parsing_array_to_Parameters
            int index = 0;
            //parsing phi
            for (int i = 0; i < k; i++)
            {
                phi[i] = x[index];
                index++;
                // Debug.WriteLine("[{0}]", string.Join(", ", phi));
            }
            //parsing mu
            for (int i = 0; i < k; i++)
            {
                for (int j = 0; j < dim; j++)
                {
                    mu[i][0, j] = x[index];
                    index++;
                }
            }
            //parsing sigma
            for (int i = 0; i < k; i++)
            {
                for (int j = 0; j < dim; j++)
                {
                    for (int h = 0; h < dim; h++)
                    {
                        sigma[i][j, h] = x[index];
                        index++;
                    }
                }
            }
            #endregion
            //===========================
            ////compute gamma
            for (int i = 0; i < inputMatrix.Rows; i++)
            {
                for (int k1 = 0; k1 < k; k1++)
                {
                    pdf[i, k1] = GaussianMV(inputMatrix, i, dim, mu[k1], sigma[k1]);
                }
            }
            double[] Gdenom = new double[inputMatrix.Rows];
            for (int i = 0; i < inputMatrix.Rows; i++) // denominator for Gamma
            {
                double sum = 0;
                for (int k1 = 0; k1 < k; k1++)
                {
                    sum = sum + phi[k1] * pdf[i, k1];
                }
                Gdenom[i] = sum;
            }

            for (int i = 0; i < inputMatrix.Rows; i++)
            {
                for (int k1 = 0; k1 < k; k1++)
                {
                    Gamma[i, k1] = (phi[k1] * pdf[i, k1]) / Gdenom[i];
                }
            }
            Debug.WriteLine(" Ending gamma");

            ///'ending compute gamma
        }