Ejemplo n.º 1
0
        // ===========================================================
        static Result PSO(Parameters parameters, IProblem pb, int level)
        {
            int added; // For information
            Fitness error = new Fitness(Constants.fMax);
            Fitness errorInit = new Fitness(Constants.fMax);  // Just for information
            Fitness errorPrev = new Fitness(Constants.fMax);
            double errorTot;

            int g;
            int sBest; // Rank in g of the best of the bests
            int improvTot; // Number of particles that have improved their previous best
            int[] index = new int[Constants.SMax];
            int initLinks;	// Flag to (re)init or not the information links
            int initLinkNb;
            int iterBegin;
            int[,] LINKS = new int[Constants.SMax, Constants.SMax];	// Information links
            int m;
            int moved;
            int n;
            int noStop;
            Result R = new Result();
            int removed; // For information

            int s0 = 0;
            int s, s1, s2;
            int spread;
            int stagnation = 0;
            int swarmMod;
            int sWorst;

            XV xvNorm = new XV();

            // -----------------------------------------------------
            // INITIALISATION

            R.SW.S = parameters.S; // Initial size of the swarm
            memPos.Rank = 0; 					// Rank (in M) where to memorise a new position
            memPos.Size = 0; 					// Number of memorised positions

            // Positions
            for (s = 0; s < R.SW.S; s++)
            {
                R.SW.X[s] = Position.Initialize(pb.SwarmSize);
                memPos.memSave(R.SW.X[s]); 		// Save the position
            }

            // Velocities
            for (s = 0; s < R.SW.S; s++)
            {
                R.SW.V[s] = Velocity.Initialize(R.SW.X[s], pb.SwarmSize);
            }

            // Discrete values
            // Note: may be removed if you are sure that the initialisation
            //        takes discretisation into account (or if there is none)
            for (s = 0; s < R.SW.S; s++)
            {
                R.SW.X[s] = Position.Discrete(R.SW.X[s], pb);
            }

            // Note: at this point no confinement is needed:
            // initialisation is supposed to be OK from this point of view
            // (but some constraints may be not respected)

            // First evaluations
            for (s = 0; s < R.SW.S; s++)
            {
                R.SW.X[s].f = pb.Evaluate(R.SW.X[s]);
                R.SW.P[s] = R.SW.X[s];	// Best position = current one
            }

            // Save the positions
            for (s = 0; s < R.SW.S; s++) memPos.memSave(R.SW.X[s]);

            // Find the best
            R.SW.best = best(R.SW);
            error = R.SW.P[R.SW.best].f;

            // Display the best
            Console.WriteLine("Best value after init. {0} ", R.SW.P[R.SW.best].f.f[0]);
            if (pb.Constraint > 0)
            {
                Console.WriteLine("Constraints (should be < 0) ");
                for (n = 0; n < pb.Constraint; n++) Console.WriteLine("{0} ", error.f[n + 1]);
            }

            //fprintf(f_run,"\n Best value after init. %f ", errorPrev );
            //printf( "\n Position :\n" );
            //for ( d = 0; d < pb.SwarmSize.D; d++ ) printf( " %f", R.SW.P[R.SW.best].x[d] );

            initLinks = 1;		// So that information links will beinitialized
            initLinkNb = 0; // Count the number of iterations between two reinit of the links
            iter = 0;
            nEval = 0;
            noStop = 0;
            added = 0; removed = 0; // For information
            spread = spreadIter(parameters.spreadProba, R.SW.S, parameters.formula); // Number of iterations
            // needed to "spread" the information
            errorInit = error; // For information

            // ---------------------------------------------- ITERATIONS
            while (noStop == 0)
            {

                //printf("\niter %i",iter);
                //fprintf(f_run,"\niter %i",iter);
                iter = iter + 1;
                errorPrev = error;
                Alea.Shuffle(index, R.SW.S); // Random numbering of the particles

                if (initLinks == 1)	// Bidirectional ring topology. Randomly built
                {
                    initLinks = 0;
                    initLinkNb = 0; // Count the number of iterations since the last re-init
                    // of the links

                    // Init to zero (no link)
                    for (s = 0; s < R.SW.S; s++)
                    {
                        for (m = 0; m < R.SW.S; m++) LINKS[m, s] = 0;
                    }

                    // Information links (bidirectional ring)
                    for (s = 0; s < R.SW.S - 1; s++)
                    {
                        LINKS[index[s], index[s + 1]] = 1;
                        LINKS[index[s + 1], index[s]] = 1;
                    }

                    LINKS[index[0], index[R.SW.S - 1]] = 1;
                    LINKS[index[R.SW.S - 1], index[0]] = 1;

                    // Each particle informs itself
                    for (m = 0; m < R.SW.S; m++) LINKS[m, m] = 1;
                }

                // Loop on particles, for move
                improvTot = 0;

                for (s0 = 0; s0 < R.SW.S; s0++)
                {
                    s = index[s0];

                    // Find the best informant
                    g = s;
                    for (m = 0; m < R.SW.S; m++)
                    {
                        if (m == s) continue;
                        if (LINKS[m, s] == 1 && betterThan(R.SW.P[m].f, R.SW.P[g].f) == 1)
                            g = m;
                    }

                    // Move
                    xvNorm = move(R, s, g, pb, parameters);
                    xvNorm.x = Position.Discrete(xvNorm.x, pb);

                    // Confinement and evaluation
                    xvNorm.Confinement(pb);

                    // New position and new velocity
                    R.SW.X[s] = xvNorm.x;
                    R.SW.V[s] = xvNorm.v;

                    // Update the best previous position
                    if (betterThan(R.SW.X[s].f, R.SW.P[s].f) == 1) // Improvement of the previous best
                    {
                        R.SW.P[s] = R.SW.X[s].Clone();
                        improvTot++; // Increase the number of improvements during this iteration

                        // Memorise the improved position
                        memPos.memSave(R.SW.P[s]);

                        // Update the best of the bests
                        if (betterThan(R.SW.P[s].f, R.SW.P[R.SW.best].f) == 1) R.SW.best = s;
                    }

                    // Decide to stop or not
                    errorTot = R.SW.P[R.SW.best].f.errorFC();

                    if (errorTot > pb.Epsilon && nEval < pb.EvaluationMaximum)
                        noStop = 0;	// Won't stop
                    else // Failure
                    {
                        noStop = 1;	// Will stop
                        goto end;
                    }

                }			// End of "for (s0=0 ...  "	= end of the iteration (move)

                /*-------------------------------------------------- Adaptations
                 Rule 1:
                 Check every "spread" iterations after each re-init of the links
                 If no improvement of the global best
                 => reinit links before the next iteration

                 Rule 2:
                 if no improvement of the global best during "spread" iterations
                 => Try to add a particle (and initialise it in a non-searched area)
                 => re-init links before the next iteration
                 Note that the condition is slightly different from the one of Rule 1

                 Rule 3:
                 if "enough" local improvements during the iteration
                 => try to remove a particle (keep at least D+1 ones)

                 */

                // Rule 1 - Re-initializing the information links
                // Check if improvement since last re-init of the links
                initLinkNb = initLinkNb + 1; // Number of iterations since the last check

                if (initLinkNb >= spread) // It's time to check
                {
                    initLinkNb = 0; // Reset to zero the number of iterations since the last check
                    // The swarm size may have been modified, so must be "spread"
                    spread = spreadIter(parameters.spreadProba, R.SW.S, parameters.formula);

                    if (betterThan(error, errorPrev) == 1)	// Improvement
                        initLinks = 0;	 // No need of structural adaptation
                    else			// No improvement
                        initLinks = 1;	// Information links will be	reinitialized
                }
                else initLinks = 0;  // To early, no need to check

                sWorst = worst(R.SW); // Rank of the worst particle, before any adaptation

                // Rule 2 - Adding a particle
                // Check global stagnation (improvement of the global best)
                if (betterThan(R.SW.P[R.SW.best].f, errorPrev) == 1) stagnation = 0;	// Improvement
                else stagnation++; // No improvement during this iteration

                swarmMod = 0; // Information flag

                if (stagnation >= spread)  // Too many iterations without global improvement
                // =>  add a particle
                {
                    if (R.SW.S < Constants.SMax) // if not too many particles
                    {
                        s = R.SW.S;
                        R.SW.X[s] = memPos.InitializeFar(pb); // Init in a non-searched area
                        R.SW.X[s] = Position.Discrete(xvNorm.x, pb); // If discrete search space
                        R.SW.X[s].f = pb.Evaluate(R.SW.X[s]);	 // Evaluation
                        R.SW.V[s] = Velocity.Initialize(R.SW.X[s], pb.SwarmSize); // Init velocity
                        R.SW.P[s] = R.SW.X[s].Clone(); // Previous best = current position
                        R.SW.S = R.SW.S + 1; // Increase the swarm size

                        //fprintf(f_swarm,"%i %i  %f\n",iter, R.SW.S,error.f[0]);
                        // Count the number of added particles (for information)
                        added++;
                        initLinks = 1; // Links will be reinitialised
                        stagnation = 0; // Reset the count for stagnation
                        swarmMod = 1; // A particle has been added
                        //printf("\n iter %i, added %i, S %i, spread %i",iter, added, R.SW.S, spread);
                    }
                }

                // Rule 3 - Removing a particle
                // If enough improvements of some particles, remove the worst
                // (but keep at least D+1 particles)
                // NOTE: this is "the worst" without taking into account the particle
                // that has (possibly) been added
                // NOTE: it is perfectly possible to have a particle added
                // (because of no improvement of the global best)  AND
                // a particle removed (because enough _local_ improvements)

                if (R.SW.S > pb.SwarmSize.D + 1 && improvTot > 0.5 * R.SW.S)
                {
                    if ((swarmMod == 0 && sWorst < R.SW.S - 1) || swarmMod == 1)
                    // if the worst is not the last
                    {
                        R.SW.P[sWorst] = R.SW.P[R.SW.S - 1]; // ... replace it by the last
                        R.SW.V[sWorst] = R.SW.V[R.SW.S - 1];
                        R.SW.X[sWorst] = R.SW.X[R.SW.S - 1];

                        // Compact the matrix of the links
                        for (s1 = 0; s1 < R.SW.S; s1++)  // For each line, compact the columns
                            for (s2 = sWorst; s2 < R.SW.S - 1; s2++) LINKS[s1, s2] = LINKS[s1, s2 + 1];

                        for (s2 = 0; s2 < R.SW.S - 1; s2++)	// For each column, compact the lines
                            for (s1 = sWorst; s1 < R.SW.S - 1; s1++) LINKS[s1, s2] = LINKS[s1 + 1, s2];
                    }
                    R.SW.S = R.SW.S - 1; // Decrease the swarm size
                    if (s < R.SW.best) R.SW.best = R.SW.best - 1; // The rank of the best may
                    // have been modified
                    // Count the number of removed particles (for information)
                    removed++;
                    swarmMod = -1; // A particle has been remowed
                    //printf("\n iter %i, removed %i, S %i, spread %i",iter, removed, R.SW.S, spread);
                }

                // Save on a the result of the iteration on a file
                if (swarmMod != 0)
                {
                    //fprintf(f_swarm,"%i %i  %f\n",iter, R.SW.S,R.SW.P[R.SW.best].f.f[0]);
                }

                // End of the iteration
            end: ;

            } // End of "while (noStop==0)"

            // Convergence rate, just for information
            R.convRate = (errorInit.f[0] - R.SW.P[R.SW.best].f.f[0]) / errorInit.f[0];

            // Information about the evolution of the swarm size
            Console.WriteLine("{0} iterations, +{1} -{2} particles", iter, added, removed);

            // Final number of evaluations
            R.nEval = nEval;
            // Final fitness
            R.error = R.SW.P[R.SW.best].f;
            return R;
        }
Ejemplo n.º 2
0
        static XV move(Result R, int s, int g, IProblem pb,
                    Parameters parameters)
        {
            double c1, c2;
            int d;
            Velocity GX = new Velocity(Constants.DMax);
            Velocity PX = new Velocity(Constants.DMax);

            XV xv = new XV();
            double w;

            xv.x.size = pb.SwarmSize.D;
            xv.v.Size = pb.SwarmSize.D;
            PX.Size = pb.SwarmSize.D;
            GX.Size = pb.SwarmSize.D;

            w = parameters.w;
            c1 = parameters.c1;
            c2 = parameters.c2;

            // Update the velocity (Exploration tendency)
            for (d = 0; d < pb.SwarmSize.D; d++)
            {
                xv.v.V[d] = w * R.SW.V[s].V[d];
            }

            // --------------------------------------------------------NORMAL PSO STRATEGY

            // Prepare Exploitation tendency
            for (d = 0; d < pb.SwarmSize.D; d++) // p-x
            {
                PX.V[d] = R.SW.P[s].x[d] - R.SW.X[s].x[d];
            }

            if (g != s)
            {
                for (d = 0; d < pb.SwarmSize.D; d++) // g-x
                {
                    GX.V[d] = R.SW.P[g].x[d] - R.SW.X[s].x[d];
                }
            }

            // Complete the velocity
            for (d = 0; d < pb.SwarmSize.D; d++)
            {
                xv.v.V[d] = xv.v.V[d] + Alea.NextDouble(0, c1) * PX.V[d];

            }

            if (g != s) // If the best neighbour is not the particle itself
            {
                for (d = 0; d < pb.SwarmSize.D; d++)
                {
                    xv.v.V[d] = xv.v.V[d] + Alea.NextDouble(0, c2) * GX.V[d];
                }
            }

            // Update the position
            for (d = 0; d < pb.SwarmSize.D; d++)
            {
                xv.x.x[d] = R.SW.X[s].x[d] + xv.v.V[d];
            }

            // NOTE that the position is not yet evaluated
            return xv;
        }