Esempio n. 1
0
        /// <summary>
        /// Runs the NSGA-II algorithm.
        /// </summary>
        /// <returns>a <code>SolutionSet</code> that is a set of non dominated solutions as a result of the algorithm execution</returns>
        public override SolutionSet Execute()
        {
            int populationSize = -1;
            int maxEvaluations = -1;
            int evaluations;

            JMetalCSharp.QualityIndicator.QualityIndicator indicators = null; // QualityIndicator object
            int requiredEvaluations;                                          // Use in the example of use of the
            // indicators object (see below)

            SolutionSet population;
            SolutionSet offspringPopulation;
            SolutionSet union;

            Operator mutationOperator;
            Operator crossoverOperator;
            Operator selectionOperator;

            Distance distance = new Distance();

            //Read the parameters
            JMetalCSharp.Utils.Utils.GetIntValueFromParameter(this.InputParameters, "maxEvaluations", ref maxEvaluations);
            JMetalCSharp.Utils.Utils.GetIntValueFromParameter(this.InputParameters, "populationSize", ref populationSize);
            JMetalCSharp.Utils.Utils.GetIndicatorsFromParameters(this.InputParameters, "indicators", ref indicators);

            parallelEvaluator.StartParallelRunner(Problem);;

            //Initialize the variables
            population  = new SolutionSet(populationSize);
            evaluations = 0;

            requiredEvaluations = 0;

            //Read the operators
            mutationOperator  = Operators["mutation"];
            crossoverOperator = Operators["crossover"];
            selectionOperator = Operators["selection"];

            // Create the initial solutionSet
            Solution newSolution;

            for (int i = 0; i < populationSize; i++)
            {
                newSolution = new Solution(Problem);
                parallelEvaluator.AddTaskForExecution(new object[] { newSolution });;
            }

            List <Solution> solutionList = (List <Solution>)parallelEvaluator.ParallelExecution();

            foreach (Solution solution in solutionList)
            {
                population.Add(solution);
                evaluations++;
            }

            // Generations
            while (evaluations < maxEvaluations)
            {
                // Create the offSpring solutionSet
                offspringPopulation = new SolutionSet(populationSize);
                Solution[] parents = new Solution[2];
                for (int i = 0; i < (populationSize / 2); i++)
                {
                    if (evaluations < maxEvaluations)
                    {
                        //obtain parents
                        parents[0] = (Solution)selectionOperator.Execute(population);
                        parents[1] = (Solution)selectionOperator.Execute(population);
                        Solution[] offSpring = (Solution[])crossoverOperator.Execute(parents);
                        mutationOperator.Execute(offSpring[0]);
                        mutationOperator.Execute(offSpring[1]);
                        parallelEvaluator.AddTaskForExecution(new object[] { offSpring[0] });;
                        parallelEvaluator.AddTaskForExecution(new object[] { offSpring[1] });;
                    }
                }

                List <Solution> solutions = (List <Solution>)parallelEvaluator.ParallelExecution();

                foreach (Solution solution in solutions)
                {
                    offspringPopulation.Add(solution);
                    evaluations++;
                }

                // Create the solutionSet union of solutionSet and offSpring
                union = ((SolutionSet)population).Union(offspringPopulation);

                // Ranking the union
                Ranking ranking = new Ranking(union);

                int         remain = populationSize;
                int         index  = 0;
                SolutionSet front  = null;
                population.Clear();

                // Obtain the next front
                front = ranking.GetSubfront(index);

                while ((remain > 0) && (remain >= front.Size()))
                {
                    //Assign crowding distance to individuals
                    distance.CrowdingDistanceAssignment(front, Problem.NumberOfObjectives);
                    //Add the individuals of this front
                    for (int k = 0; k < front.Size(); k++)
                    {
                        population.Add(front.Get(k));
                    }

                    //Decrement remain
                    remain = remain - front.Size();

                    //Obtain the next front
                    index++;
                    if (remain > 0)
                    {
                        front = ranking.GetSubfront(index);
                    }
                }

                // Remain is less than front(index).size, insert only the best one
                if (remain > 0)
                {                  // front contains individuals to insert
                    distance.CrowdingDistanceAssignment(front, Problem.NumberOfObjectives);
                    front.Sort(new CrowdingComparator());
                    for (int k = 0; k < remain; k++)
                    {
                        population.Add(front.Get(k));
                    }

                    remain = 0;
                }

                // This piece of code shows how to use the indicator object into the code
                // of NSGA-II. In particular, it finds the number of evaluations required
                // by the algorithm to obtain a Pareto front with a hypervolume higher
                // than the hypervolume of the true Pareto front.
                if ((indicators != null) &&
                    (requiredEvaluations == 0))
                {
                    double HV = indicators.GetHypervolume(population);
                    if (HV >= (0.98 * indicators.TrueParetoFrontHypervolume))
                    {
                        requiredEvaluations = evaluations;
                    }
                }
            }

            // Return as output parameter the required evaluations
            SetOutputParameter("evaluations", requiredEvaluations);

            // Return the first non-dominated front
            Ranking rank = new Ranking(population);

            Result = rank.GetSubfront(0);

            return(this.Result);
        }
Esempio n. 2
0
        /// <summary>
        /// Runs of the SMPSO algorithm.
        /// </summary>
        /// <returns>a <code>SolutionSet</code> that is a set of non dominated solutions
        /// as a result of the algorithm execution  </returns>
        public override SolutionSet Execute()
        {
            InitParams();
            //->Step 1 (and 3) Create the initial population and evaluate
            for (int i = 0; i < swarmSize; i++)
            {
                Solution particle = new Solution(this.Problem);
                particles.Add(particle);
                parallelEvaluator.AddTaskForExecution(new object[] { particle });;
            }

            parallelEvaluator.ParallelExecution();

            //-> Step2. Initialize the speed_ of each particle to 0
            for (int i = 0; i < swarmSize; i++)
            {
                for (int j = 0; j < this.Problem.NumberOfVariables; j++)
                {
                    speed[i][j] = 0.0;
                }
            }

            // Step4 and 5
            for (int i = 0; i < particles.Size(); i++)
            {
                Solution particle = new Solution(particles.Get(i));
                Leaders.Add(particle);
            }

            //-> Step 6. Initialize the memory of each particle
            for (int i = 0; i < particles.Size(); i++)
            {
                Solution particle = new Solution(particles.Get(i));
                best[i] = particle;
            }

            //Crowding the leaders_
            distance.CrowdingDistanceAssignment(Leaders, this.Problem.NumberOfObjectives);

            //-> Step 7. Iterations ..
            while (iteration < maxIterations)
            {
                try
                {
                    //Compute the speed_
                    ComputeSpeed(iteration, maxIterations);
                }
                catch (IOException ex)
                {
                    Logger.Log.Error(this.GetType().FullName + ".Execute()", ex);
                }

                //Compute the new positions for the particles
                ComputeNewPositions();

                //Mutate the particles
                MopsoMutation(iteration, maxIterations);

                for (int i = 0; i < particles.Size(); i++)
                {
                    Solution particle = particles.Get(i);
                    parallelEvaluator.AddTaskForExecution(new object[] { particle });;
                }

                parallelEvaluator.ParallelExecution();

                //Actualize the archive
                for (int i = 0; i < particles.Size(); i++)
                {
                    Solution particle = new Solution(particles.Get(i));
                    Leaders.Add(particle);
                }

                //Actualize the memory of this particle
                for (int i = 0; i < particles.Size(); i++)
                {
                    int flag = dominance.Compare(particles.Get(i), best[i]);
                    if (flag != 1)
                    {                     // the new particle is best than the older remeber
                        Solution particle = new Solution(particles.Get(i));
                        best[i] = particle;
                    }
                }

                //Assign crowding distance to the leaders_
                distance.CrowdingDistanceAssignment(Leaders, this.Problem.NumberOfObjectives);
                iteration++;
            }

            Result = this.Leaders;
            return(this.Leaders);
        }