public void Merge(Neighborhood other)
        {
            // Add all the fusions from the other neighborhood into this
            // neighborhood
            foreach (Fusion fusion in other)
            {
                Add(fusion);
            }

            // Make sure to also update the best configuration if the best configuration
            // from the other neighborhood is better than this one
            if (other.ConfigurationBest != null &&
                (d_best == null || other.ConfigurationBest.Fitness > d_best.Fitness))
            {
                d_best = (Particle)other.ConfigurationBest.Clone();
            }
        }
Esempio n. 2
0
        public override void InitializePopulation()
        {
            base.InitializePopulation();

            d_mutationProbability.Parse(Configuration.MutationProbability);
            d_socialMutationProbability.Parse(Configuration.SocialMutationProbability);
            d_cognitiveMutationProbability.Parse(Configuration.CognitiveMutationProbability);

            if (Configuration.MinNeighborhoods <= 0)
            {
                Configuration.MinNeighborhoods = 1;
            }

            d_subswarmBests = new Dictionary <uint, Particle>();
            int idx = 0;

            if (Configuration.FusedParticles <= 0)
            {
                throw new Exception("Number of fused particles must be larger than 0");
            }

            // Do the particle fusion
            Neighborhood neighborhood = new Neighborhood();

            /* Calculate the number of fusion sets in each neighborhood to fullfil
             * the initial number of neighborhoods constraint:
             *
             * n = Ceil(PopulationSize / FusionSize) / InitialNeighborhoods
             *  */
            double fusionPerNeighborhood;

            if (Configuration.InitialNeighborhoods <= 0)
            {
                // Maximum number of neighborhoods
                fusionPerNeighborhood = 2;
            }
            else
            {
                fusionPerNeighborhood = System.Math.Ceiling(Population.Count / (double)Configuration.FusedParticles) / Configuration.InitialNeighborhoods;
            }

            if (fusionPerNeighborhood < 1)
            {
                fusionPerNeighborhood = 1;
            }

            double nextFusion = fusionPerNeighborhood;

            while (idx < Population.Count)
            {
                List <Solution> solutions = Population.GetRange(idx, System.Math.Min(Configuration.FusedParticles, Population.Count - idx));
                List <Particle> particles = new List <Particle>();

                idx += Configuration.FusedParticles;

                foreach (Solution solution in solutions)
                {
                    particles.Add((Particle)solution);
                }

                // Create new fusion set for the particles
                Fusion fus = new Fusion(particles, State);

                // Initialize will intialize the subswarm configuration of the fusion set
                fus.Initialize();

                // Add the fusion set to the neighborhood
                neighborhood.Add(fus);

                if (neighborhood.Count >= nextFusion)
                {
                    nextFusion += fusionPerNeighborhood - neighborhood.Count;

                    d_neighborhoods.Add(neighborhood);
                    neighborhood = new Neighborhood();
                }
            }

            if (neighborhood.Count != 0)
            {
                d_neighborhoods.Add(neighborhood);
            }

            if (Configuration.MergeInterval == 0)
            {
                /* Calculate the merge interval based on the number of neighborhoods,
                 * number of particles and number of iterations.
                 *
                 * Number of merges 'nm':
                 * ------------------------------------------------------
                 * 2^n = numNeighborhoods
                 * n = log(numNeighborhoods - minNeighborhoods) / log(2)
                 * interval = maxIterations / (n + 1)
                 */
                double num = System.Math.Ceiling(System.Math.Log(d_neighborhoods.Count / Configuration.MinNeighborhoods) / System.Math.Log(2));
                Configuration.MergeInterval = Configuration.MaxIterations / (double)(num + 1);
            }

            d_nextMerge = Configuration.MergeInterval;
        }