Esempio n. 1
0
        public double ComputeHypervolume(SolutionSet solutionSet)
        {
            double hv;

            if (solutionSet.Size() == 0)
            {
                hv = 0.0;
            }
            else
            {
                numberOfObjectives = solutionSet.Get(0).NumberOfObjectives;
                referencePoint     = new Solution(numberOfObjectives);
                UpdateReferencePoint(solutionSet);
                if (numberOfObjectives == 2)
                {
                    solutionSet.Sort(new ObjectiveComparator(numberOfObjectives - 1, true));
                    hv = Get2DHV(solutionSet);
                }
                else
                {
                    UpdateReferencePoint(solutionSet);
                    Front front = new Front(solutionSet.Size(), numberOfObjectives, solutionSet);
                    hv = new WFGHV(numberOfObjectives, solutionSet.Size(), referencePoint).GetHV(front);
                }
            }

            return(hv);
        }
Esempio n. 2
0
        public double ComputeHypervolume(SolutionSet solutionSet, Solution referencePoint)
        {
            double hv = 0.0;

            if (solutionSet.Size() == 0)
            {
                hv = 0.0;
            }
            else
            {
                this.numberOfObjectives = solutionSet.Get(0).NumberOfObjectives;
                this.referencePoint     = referencePoint;

                if (this.numberOfObjectives == 2)
                {
                    solutionSet.Sort(new ObjectiveComparator(this.numberOfObjectives - 1, true));

                    hv = Get2DHV(solutionSet);
                }
                else
                {
                    WFGHV wfg   = new WFGHV(this.numberOfObjectives, solutionSet.Size());
                    Front front = new Front(solutionSet.Size(), numberOfObjectives, solutionSet);
                    hv = wfg.GetHV(front, referencePoint);
                }
            }

            return(hv);
        }
Esempio n. 3
0
        /// <summary>
        /// Assigns crowding distances to all solutions in a <code>SolutionSet</code>.
        /// </summary>
        /// <param name="solutionSet">The <code>SolutionSet</code>.</param>
        /// <param name="nObjs">Number of objectives.</param>
        public void CrowdingDistanceAssignment(SolutionSet solutionSet, int nObjs)
        {
            int size = solutionSet.Size();

            if (size == 0)
            {
                return;
            }

            if (size == 1)
            {
                solutionSet.Get(0).CrowdingDistance = double.PositiveInfinity;
                return;
            }

            if (size == 2)
            {
                solutionSet.Get(0).CrowdingDistance = double.PositiveInfinity;
                solutionSet.Get(1).CrowdingDistance = double.PositiveInfinity;
                return;
            }

            //Use a new SolutionSet to evite alter original solutionSet
            SolutionSet front = new SolutionSet(size);

            for (int i = 0; i < size; i++)
            {
                front.Add(solutionSet.Get(i));
            }

            for (int i = 0; i < size; i++)
            {
                front.Get(i).CrowdingDistance = 0.0;
            }

            double objetiveMaxn;
            double objetiveMinn;
            double distance;

            for (int i = 0; i < nObjs; i++)
            {
                // Sort the population by Obj n
                front.Sort(new ObjectiveComparator(i));
                objetiveMinn = front.Get(0).Objective[i];
                objetiveMaxn = front.Get(front.Size() - 1).Objective[i];

                //Set de crowding distance
                front.Get(0).CrowdingDistance        = double.PositiveInfinity;
                front.Get(size - 1).CrowdingDistance = double.PositiveInfinity;

                for (int j = 1; j < size - 1; j++)
                {
                    distance  = front.Get(j + 1).Objective[i] - front.Get(j - 1).Objective[i];
                    distance  = distance / (objetiveMaxn - objetiveMinn);
                    distance += front.Get(j).CrowdingDistance;
                    front.Get(j).CrowdingDistance = distance;
                }
            }
        }
        /// <summary>
        /// Performs the operation
        /// </summary>
        /// <param name="obj">Object representing a SolutionSet.</param>
        /// <returns>An object representing a <code>SolutionSet<code> with the selected parents</returns>
        public override object Execute(object obj)
        {
            SolutionSet population     = (SolutionSet)obj;
            int         populationSize = (int)Parameters["populationSize"];
            SolutionSet result         = new SolutionSet(populationSize);

            //->Ranking the union
            Ranking ranking = new Ranking(population);

            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()))
            {
                //Asign crowding distance to individuals
                distance.CrowdingDistanceAssignment(front, problem.NumberOfObjectives);
                //Add the individuals of this front
                for (int k = 0; k < front.Size(); k++)
                {
                    result.Add(front.Get(k));
                }

                //Decrement remaint
                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 containt individuals to insert
                distance.CrowdingDistanceAssignment(front, problem.NumberOfObjectives);
                front.Sort(crowdingComparator);
                for (int k = 0; k < remain; k++)
                {
                    result.Add(front.Get(k));
                }

                remain = 0;
            }

            return(result);
        }
        /// <summary>
        /// Runs the SMS-EMOA 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;
            double offset = 100.0;

            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;

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

            //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);
                Problem.Evaluate(newSolution);
                Problem.EvaluateConstraints(newSolution);
                evaluations++;
                population.Add(newSolution);
            }

            // Generations ...
            while (evaluations < maxEvaluations)
            {
                // select parents
                offspringPopulation = new SolutionSet(populationSize);
                List <Solution> selectedParents = new List <Solution>();
                Solution[]      parents         = new Solution[0];
                while (selectedParents.Count < 2)
                {
                    object selected = selectionOperator.Execute(population);
                    try
                    {
                        Solution parent = (Solution)selected;
                        selectedParents.Add(parent);
                    }
                    catch (InvalidCastException e)
                    {
                        parents = (Solution[])selected;
                        selectedParents.AddRange(parents);
                    }
                }
                parents = selectedParents.ToArray <Solution>();

                // crossover
                Solution[] offSpring = (Solution[])crossoverOperator.Execute(parents);

                // mutation
                mutationOperator.Execute(offSpring[0]);

                // evaluation
                Problem.Evaluate(offSpring[0]);
                Problem.EvaluateConstraints(offSpring[0]);

                // insert child into the offspring population
                offspringPopulation.Add(offSpring[0]);

                evaluations++;

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

                // Ranking the union (non-dominated sorting)
                Ranking ranking = new Ranking(union);

                // ensure crowding distance values are up to date
                // (may be important for parent selection)
                for (int j = 0; j < population.Size(); j++)
                {
                    population.Get(j).CrowdingDistance = 0.0;
                }

                SolutionSet lastFront = ranking.GetSubfront(ranking.GetNumberOfSubfronts() - 1);
                if (lastFront.Size() > 1)
                {
                    double[][] frontValues        = lastFront.WriteObjectivesToMatrix();
                    int        numberOfObjectives = Problem.NumberOfObjectives;
                    // STEP 1. Obtain the maximum and minimum values of the Pareto front
                    double[] maximumValues = utils.GetMaximumValues(union.WriteObjectivesToMatrix(), numberOfObjectives);
                    double[] minimumValues = utils.GetMinimumValues(union.WriteObjectivesToMatrix(), numberOfObjectives);
                    // STEP 2. Get the normalized front
                    double[][] normalizedFront = utils.GetNormalizedFront(frontValues, maximumValues, minimumValues);
                    // compute offsets for reference point in normalized space
                    double[] offsets = new double[maximumValues.Length];
                    for (int i = 0; i < maximumValues.Length; i++)
                    {
                        offsets[i] = offset / (maximumValues[i] - minimumValues[i]);
                    }
                    // STEP 3. Inverse the pareto front. This is needed because the original
                    //metric by Zitzler is for maximization problems
                    double[][] invertedFront = utils.InvertedFront(normalizedFront);
                    // shift away from origin, so that boundary points also get a contribution > 0
                    for (int j = 0, lj = invertedFront.Length; j < lj; j++)
                    {
                        var point = invertedFront[j];
                        for (int i = 0; i < point.Length; i++)
                        {
                            point[i] += offsets[i];
                        }
                    }

                    // calculate contributions and sort
                    double[] contributions = HvContributions(invertedFront);
                    for (int i = 0; i < contributions.Length; i++)
                    {
                        // contribution values are used analogously to crowding distance
                        lastFront.Get(i).CrowdingDistance = contributions[i];
                    }

                    lastFront.Sort(new CrowdingDistanceComparator());
                }

                // all but the worst are carried over to the survivor population
                SolutionSet front = null;
                population.Clear();
                for (int i = 0; i < ranking.GetNumberOfSubfronts() - 1; i++)
                {
                    front = ranking.GetSubfront(i);
                    for (int j = 0; j < front.Size(); j++)
                    {
                        population.Add(front.Get(j));
                    }
                }
                for (int i = 0; i < lastFront.Size() - 1; i++)
                {
                    population.Add(lastFront.Get(i));
                }

                // This piece of code shows how to use the indicator object into the code
                // of SMS-EMOA. 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 rnk = new Ranking(population);

            Result = rnk.GetSubfront(0);
            return(Result);
        }
        public override SolutionSet Execute()
        {
            double contrDE   = 0;
            double contrSBX  = 0;
            double contrBLXA = 0;
            double contrPol  = 0;

            double[] contrReal = new double[3];
            contrReal[0] = contrReal[1] = contrReal[2] = 0;

            IComparer <Solution> dominance          = new DominanceComparator();
            IComparer <Solution> crowdingComparator = new CrowdingComparator();
            Distance             distance           = new Distance();

            Operator selectionOperator;

            //Read parameter values
            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);

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

            selectionOperator = Operators["selection"];

            Offspring[] getOffspring;
            int         N_O;     // number of offpring objects

            getOffspring = (Offspring[])GetInputParameter("offspringsCreators");
            N_O          = getOffspring.Length;

            contribution        = new double[N_O];
            contributionCounter = new int[N_O];

            contribution[0] = (double)(populationSize / (double)N_O) / (double)populationSize;
            for (int i = 1; i < N_O; i++)
            {
                contribution[i] = (double)(populationSize / (double)N_O) / (double)populationSize + (double)contribution[i - 1];
            }

            // Create the initial solutionSet
            Solution newSolution;

            for (int i = 0; i < populationSize; i++)
            {
                newSolution = new Solution(Problem);
                Problem.Evaluate(newSolution);
                Problem.EvaluateConstraints(newSolution);
                evaluations++;
                newSolution.Location = i;
                population.Add(newSolution);
            }

            while (evaluations < maxEvaluations)
            {
                // Create the offSpring solutionSet
                // Create the offSpring solutionSet
                offspringPopulation = new SolutionSet(2);
                Solution[] parents = new Solution[2];

                int      selectedSolution = JMetalRandom.Next(0, populationSize - 1);
                Solution individual       = new Solution(population.Get(selectedSolution));

                int      selected  = 0;
                bool     found     = false;
                Solution offSpring = null;
                double   rnd       = JMetalRandom.NextDouble();
                for (selected = 0; selected < N_O; selected++)
                {
                    if (!found && (rnd <= contribution[selected]))
                    {
                        if ("DE" == getOffspring[selected].Id)
                        {
                            offSpring = getOffspring[selected].GetOffspring(population, selectedSolution);
                            contrDE++;
                        }
                        else if ("SBXCrossover" == getOffspring[selected].Id)
                        {
                            offSpring = getOffspring[selected].GetOffspring(population);
                            contrSBX++;
                        }
                        else if ("BLXAlphaCrossover" == getOffspring[selected].Id)
                        {
                            offSpring = getOffspring[selected].GetOffspring(population);
                            contrBLXA++;
                        }
                        else if ("PolynomialMutation" == getOffspring[selected].Id)
                        {
                            offSpring = ((PolynomialMutationOffspring)getOffspring[selected]).GetOffspring(individual);
                            contrPol++;
                        }
                        else
                        {
                            Console.Error.WriteLine("Error in NSGAIIARandom. Operator " + offSpring + " does not exist");
                            Logger.Log.Error("NSGAIIARandom. Operator " + offSpring + " does not exist");
                        }

                        offSpring.Fitness = (int)selected;
                        currentCrossover  = selected;
                        found             = true;
                    }
                }

                Problem.Evaluate(offSpring);
                offspringPopulation.Add(offSpring);
                evaluations += 1;

                // 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;
                }
            }

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

            Result = rank.GetSubfront(0);

            return(Result);
        }
Esempio n. 7
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
            IntergenSolution newSolution;

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

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

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

            // Generations
            while (evaluations < maxEvaluations)
            {
                // Create the offSpring solutionSet
                offspringPopulation = new SolutionSet(populationSize);
                IntergenSolution[] parents = new IntergenSolution[2];

                for (int i = 0; i < (populationSize / 2); i++)
                {
                    if (evaluations < maxEvaluations)
                    {
                        //obtain parents
                        parents[0] = (IntergenSolution)selectionOperator.Execute(population);
                        parents[1] = (IntergenSolution)selectionOperator.Execute(population);
                        IntergenSolution[] offSpring = (IntergenSolution[])crossoverOperator.Execute(parents);
                        mutationOperator.Execute(offSpring[0]);
                        mutationOperator.Execute(offSpring[1]);

                        parallelEvaluator.AddTaskForExecution(new object[] { offSpring[0], evaluations + i });
                        parallelEvaluator.AddTaskForExecution(new object[] { offSpring[1], evaluations + i });
                    }
                }

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

                foreach (IntergenSolution solution in solutions)
                {
                    offspringPopulation.Add(solution);
                    evaluations++;
                    //solution.FoundAtEval = 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;
                    }
                }

                //TODO

                /*
                 * Ranking rank2 = new Ranking(population);
                 *
                 * Result = rank2.GetSubfront(0);
                 */

                /*Ranking forGraphicOutput = new Ranking(population);
                 * var currentBestResultSet = forGraphicOutput.GetSubfront(0);
                 *
                 * var firstBestResult = currentBestResultSet.Get(0);
                 *
                 * var myProblem = (IntergenProblem)Problem;
                 * //myProblem.calculated;
                 *
                 * //var variantValuesWithoutInteraction = Matrix.Multiply(myProblem.calculated, firstBestResult.);
                 * //var Model = myProblem.GetModel();
                 * int mycounter = 0;
                 * if (mycounter % 500 == 0)
                 * {
                 *  //RIntegrator.PlotValues(currentBestResultSet, myProblem);
                 * }
                 * mycounter++;
                 * var progress = new UserProgress();
                 * progress.FeatureP = firstBestResult.Objective[0];
                 * if (!myProblem.Model.Setting.NoVariantCalculation) progress.VariantP = firstBestResult.Objective[1];
                 * myProblem.Worker.ReportProgress(evaluations * 100 / maxEvaluations, progress); ;
                 *
                 * //Model.CurrentBestImage = "CurrentBest.png"; */
                front = ranking.GetSubfront(0);

                var minmax = new MinMaxFitness
                {
                    FeatMax  = double.MinValue,
                    FeatMin  = double.MaxValue,
                    VarMax   = double.MinValue,
                    VarMin   = double.MaxValue,
                    InterMax = double.MinValue,
                    InterMin = double.MaxValue
                };
                var prob = (IntergenProblem)Problem;
                var list = ObjectiveMapping.GetList(prob.ProblemType);
                for (var i = 0; i < populationSize; i++)
                {
                    var sol = population.Get(i);



                    var objindex = 0;
                    if (list[0])
                    {
                        if (sol.Objective[objindex] < minmax.FeatMin)
                        {
                            minmax.FeatMin = sol.Objective[objindex];
                        }
                        if (sol.Objective[objindex] > minmax.FeatMax)
                        {
                            minmax.FeatMax = sol.Objective[objindex];
                        }

                        objindex++;
                    }
                    if (list[1])
                    {
                        if (sol.Objective[objindex] < minmax.InterMin)
                        {
                            minmax.InterMin = sol.Objective[objindex];
                        }
                        if (sol.Objective[objindex] > minmax.InterMax)
                        {
                            minmax.InterMax = sol.Objective[objindex];
                        }

                        objindex++;
                    }
                    if (list[2])
                    {
                        if (sol.Objective[objindex] < minmax.VarMin)
                        {
                            minmax.VarMin = sol.Objective[objindex];
                        }
                        if (sol.Objective[objindex] > minmax.VarMax)
                        {
                            minmax.VarMax = sol.Objective[objindex];
                        }
                    }
                }

                var sol0 = front.Best(new CrowdingDistanceComparator());
                var done = FitnessTracker.AddFitn(minmax);
                SolutionPlotter.Plot(sol0);
                ProgressReporter.ReportSolution(evaluations, sol0, _worker);

                if (done)
                {
                    Ranking rank3 = new Ranking(population);

                    Result = rank3.GetSubfront(0);
                    SetOutputParameter("evaluations", evaluations);

                    return(this.Result);
                }
            }
            // Return as output parameter the required evaluations
            SetOutputParameter("evaluations", evaluations);

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

            Result = rank.GetSubfront(0);

            return(this.Result);
        }
Esempio n. 8
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. 9
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()
        {
            // !!!! NEEDED PARAMETES !!! start
            //J* Parameters
            int populationSize = -1; // J* store population size
            int maxEvaluations = -1; // J* store number of max Evaluations
            int evaluations;         // J* number of current evaluations

            // J* Objects needed to illustrate the use of quality indicators inside the algorithms
            QualityIndicator indicators = null; // QualityIndicator object
            int requiredEvaluations;            // Use in the example of use of the
            // indicators object (see below)

            // J* populations needed to implement NSGA-II
            SolutionSet population;          //J* Current population
            SolutionSet offspringPopulation; //J* offspring population
            SolutionSet union;               //J* population resultant from current and offpring population

            //J* Genetic Operators
            Operator mutationOperator;
            Operator crossoverOperator;
            Operator selectionOperator;

            //J* Used to evaluate crowding distance
            Distance distance = new Distance();

            //J* !!!! NEEDED PARAMETES !!! end

            //J* !!! INITIALIZING PARAMETERS - start !!!

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

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

            requiredEvaluations = 0;

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

            //J* !!! INITIALIZING PARAMETERS - end !!!


            //J* !!! Creating first population !!!

            JMetalRandom.SetRandom(comp.MyRand);
            comp.LogAddMessage("Random seed = " + comp.Seed);

            // Create the initial solutionSet
            Solution newSolution;

            for (int i = 0; i < populationSize; i++)
            {
                newSolution = new Solution(Problem);
                Problem.Evaluate(newSolution);
                Problem.EvaluateConstraints(newSolution);
                evaluations++;
                population.Add(newSolution);
            }

            // 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]);
                        Problem.Evaluate(offSpring[0]);
                        Problem.EvaluateConstraints(offSpring[0]);
                        Problem.Evaluate(offSpring[1]);
                        Problem.EvaluateConstraints(offSpring[1]);
                        offspringPopulation.Add(offSpring[0]);
                        offspringPopulation.Add(offSpring[1]);
                        evaluations += 2;
                    }
                }

                // 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);

            comp.LogAddMessage("Evaluations = " + evaluations);
            // Return the first non-dominated front
            Ranking rank = new Ranking(population);

            Result = rank.GetSubfront(0);

            return(Result);
        }
        /// <summary>
        /// Implements the referenceSetUpdate method.
        /// </summary>
        /// <param name="build">build if true, indicates that the reference has to be build for the
        /// first time; if false, indicates that the reference set has to be
        /// updated with new solutions</param>
        public void ReferenceSetUpdate(bool build)
        {
            if (build)
            {             // Build a new reference set
                // STEP 1. Select the p best individuals of P, where p is refSet1Size.
                //         Selection Criterium: Spea2Fitness
                Solution individual;
                (new Spea2Fitness(solutionSet)).FitnessAssign();
                solutionSet.Sort(fitness);

                // STEP 2. Build the RefSet1 with these p individuals
                for (int i = 0; i < refSet1Size; i++)
                {
                    individual = solutionSet.Get(0);
                    solutionSet.Remove(0);
                    individual.UnMarked();
                    refSet1.Add(individual);
                }

                // STEP 3. Compute Euclidean distances in SolutionSet to obtain q
                //         individuals, where q is refSet2Size_
                for (int i = 0; i < solutionSet.Size(); i++)
                {
                    individual = solutionSet.Get(i);
                    individual.DistanceToSolutionSet = distance.DistanceToSolutionSetInSolutionSpace(individual, refSet1);
                }

                int size = refSet2Size;
                if (solutionSet.Size() < refSet2Size)
                {
                    size = solutionSet.Size();
                }

                // STEP 4. Build the RefSet2 with these q individuals
                for (int i = 0; i < size; i++)
                {
                    // Find the maximumMinimunDistanceToPopulation
                    double maxMinimum = 0.0;
                    int    index      = 0;
                    for (int j = 0; j < solutionSet.Size(); j++)
                    {
                        if (solutionSet.Get(j).DistanceToSolutionSet > maxMinimum)
                        {
                            maxMinimum = solutionSet.Get(j).DistanceToSolutionSet;
                            index      = j;
                        }
                    }
                    individual = solutionSet.Get(index);
                    solutionSet.Remove(index);

                    // Update distances to REFSET in population
                    for (int j = 0; j < solutionSet.Size(); j++)
                    {
                        double aux = distance.DistanceBetweenSolutions(solutionSet.Get(j), individual);
                        if (aux < individual.DistanceToSolutionSet)
                        {
                            solutionSet.Get(j).DistanceToSolutionSet = aux;
                        }
                    }

                    // Insert the individual into REFSET2
                    refSet2.Add(individual);

                    // Update distances in REFSET2
                    for (int j = 0; j < refSet2.Size(); j++)
                    {
                        for (int k = 0; k < refSet2.Size(); k++)
                        {
                            if (i != j)
                            {
                                double aux = distance.DistanceBetweenSolutions(refSet2.Get(j), refSet2.Get(k));
                                if (aux < refSet2.Get(j).DistanceToSolutionSet)
                                {
                                    refSet2.Get(j).DistanceToSolutionSet = aux;
                                }
                            }
                        }
                    }
                }
            }
            else
            {             // Update the reference set from the subset generation result
                Solution individual;
                for (int i = 0; i < subSet.Size(); i++)
                {
                    individual   = (Solution)improvementOperator.Execute(subSet.Get(i));
                    evaluations += improvementOperator.GetEvaluations();

                    if (RefSet1Test(individual))
                    {                     //Update distance of RefSet2
                        for (int indSet2 = 0; indSet2 < refSet2.Size(); indSet2++)
                        {
                            double aux = distance.DistanceBetweenSolutions(individual, refSet2.Get(indSet2));
                            if (aux < refSet2.Get(indSet2).DistanceToSolutionSet)
                            {
                                refSet2.Get(indSet2).DistanceToSolutionSet = aux;
                            }
                        }
                    }
                    else
                    {
                        RefSet2Test(individual);
                    }
                }
                subSet.Clear();
            }
        }
Esempio n. 11
0
        public override SolutionSet Execute()
        {
            double contrDE       = 0;
            double contrSBX      = 0;
            double contrPol      = 0;
            double contrTotalDE  = 0;
            double contrTotalSBX = 0;
            double contrTotalPol = 0;

            double[] contrReal = new double[3];
            contrReal[0] = contrReal[1] = contrReal[2] = 0;

            IComparer <Solution> dominance          = new DominanceComparator();
            IComparer <Solution> crowdingComparator = new CrowdingComparator();
            Distance             distance           = new Distance();

            Operator selectionOperator;

            //Read parameter values
            JMetalCSharp.Utils.Utils.GetIntValueFromParameter(this.InputParameters, "maxEvaluations", ref maxEvaluations);
            JMetalCSharp.Utils.Utils.GetIntValueFromParameter(this.InputParameters, "populationSize", ref populationSize);

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

            selectionOperator = Operators["selection"];

            Offspring[] getOffspring;
            int         N_O;     // number of offpring objects

            getOffspring = ((Offspring[])GetInputParameter("offspringsCreators"));
            N_O          = getOffspring.Length;

            contribution        = new double[N_O];
            contributionCounter = new int[N_O];

            contribution[0] = (double)(populationSize / (double)N_O) / (double)populationSize;
            for (int i = 1; i < N_O; i++)
            {
                contribution[i] = (double)(populationSize / (double)N_O) / (double)populationSize + (double)contribution[i - 1];
            }

            for (int i = 0; i < N_O; i++)
            {
                Console.WriteLine(getOffspring[i].Configuration());
                Console.WriteLine("Contribution: " + contribution[i]);
            }

            // Create the initial solutionSet
            Solution newSolution;

            for (int i = 0; i < populationSize; i++)
            {
                newSolution = new Solution(Problem);
                Problem.Evaluate(newSolution);
                Problem.EvaluateConstraints(newSolution);
                evaluations++;
                newSolution.Location = i;
                population.Add(newSolution);
            }

            while (evaluations < maxEvaluations)
            {
                // Create the offSpring solutionSet
                offspringPopulation = new SolutionSet(populationSize);
                Solution[] parents = new Solution[2];
                for (int i = 0; i < (populationSize / 1); i++)
                {
                    if (evaluations < maxEvaluations)
                    {
                        Solution individual = new Solution(population.Get(JMetalRandom.Next(0, populationSize - 1)));

                        int      selected  = 0;
                        bool     found     = false;
                        Solution offSpring = null;
                        double   rnd       = JMetalRandom.NextDouble();
                        for (selected = 0; selected < N_O; selected++)
                        {
                            if (!found && (rnd <= contribution[selected]))
                            {
                                if ("DE" == getOffspring[selected].Id)
                                {
                                    offSpring = getOffspring[selected].GetOffspring(population, i);
                                    contrDE++;
                                }
                                else if ("SBXCrossover" == getOffspring[selected].Id)
                                {
                                    offSpring = getOffspring[selected].GetOffspring(population);
                                    contrSBX++;
                                }
                                else if ("PolynomialMutation" == getOffspring[selected].Id)
                                {
                                    offSpring = ((PolynomialMutationOffspring)getOffspring[selected]).GetOffspring(individual);
                                    contrPol++;
                                }
                                else
                                {
                                    Logger.Log.Error("Error in NSGAIIAdaptive. Operator " + offSpring + " does not exist");
                                    Console.WriteLine("Error in NSGAIIAdaptive. Operator " + offSpring + " does not exist");
                                }

                                offSpring.Fitness = (int)selected;
                                found             = true;
                            }
                        }

                        Problem.Evaluate(offSpring);
                        offspringPopulation.Add(offSpring);
                        evaluations += 1;
                    }
                }

                // 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;
                }

                // CONTRIBUTION CALCULATING PHASE
                // First: reset contribution counter
                for (int i = 0; i < N_O; i++)
                {
                    contributionCounter[i] = 0;
                }

                // Second: determine the contribution of each operator
                for (int i = 0; i < population.Size(); i++)
                {
                    if ((int)population.Get(i).Fitness != -1)
                    {
                        contributionCounter[(int)population.Get(i).Fitness] += 1;
                    }
                    population.Get(i).Fitness = -1;
                }

                contrTotalDE  += contributionCounter[0];
                contrTotalSBX += contributionCounter[1];
                contrTotalPol += contributionCounter[2];

                int minimumContribution      = 2;
                int totalContributionCounter = 0;

                for (int i = 0; i < N_O; i++)
                {
                    if (contributionCounter[i] < minimumContribution)
                    {
                        contributionCounter[i] = minimumContribution;
                    }
                    totalContributionCounter += contributionCounter[i];
                }

                if (totalContributionCounter == 0)
                {
                    for (int i = 0; i < N_O; i++)
                    {
                        contributionCounter[i] = 10;
                    }
                }

                // Third: calculating contribution
                contribution[0] = contributionCounter[0] * 1.0
                                  / (double)totalContributionCounter;
                for (int i = 1; i < N_O; i++)
                {
                    contribution[i] = contribution[i - 1] + 1.0 * contributionCounter[i] / (double)totalContributionCounter;
                }
            }

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

            Result = rank.GetSubfront(0);
            return(Result);
        }
        /// <summary>
        /// Runs the FastSMSEMOA 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;
            double offset = -1;

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

            FastHypervolume fastHypervolume;

            SolutionSet population;
            SolutionSet offspringPopulation;
            SolutionSet union;

            Operator mutationOperator;
            Operator crossoverOperator;
            Operator selectionOperator;

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


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

            requiredEvaluations = 0;

            fastHypervolume = new FastHypervolume(offset);

            //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);
                Problem.Evaluate(newSolution);
                Problem.EvaluateConstraints(newSolution);
                evaluations++;
                population.Add(newSolution);
            }

            // Generations ...
            while (evaluations < maxEvaluations)
            {
                // select parents
                offspringPopulation = new SolutionSet(populationSize);
                List <Solution> selectedParents = new List <Solution>();
                Solution[]      parents         = new Solution[0];
                while (selectedParents.Count < 2)
                {
                    object selected = selectionOperator.Execute(population);

                    try
                    {
                        Solution parent = (Solution)selected;
                        selectedParents.Add(parent);
                    }
                    catch (InvalidCastException e)
                    {
                        parents = (Solution[])selected;
                        selectedParents.AddRange(parents);
                    }
                }
                parents = selectedParents.ToArray <Solution>();

                // crossover
                Solution[] offSpring = (Solution[])crossoverOperator.Execute(parents);

                // mutation
                mutationOperator.Execute(offSpring[0]);

                // evaluation
                Problem.Evaluate(offSpring[0]);
                Problem.EvaluateConstraints(offSpring[0]);

                // insert child into the offspring population
                offspringPopulation.Add(offSpring[0]);

                evaluations++;

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

                // Ranking the union (non-dominated sorting)
                Ranking ranking = new Ranking(union);

                // ensure crowding distance values are up to date
                // (may be important for parent selection)
                for (int j = 0; j < population.Size(); j++)
                {
                    population.Get(j).CrowdingDistance = 0.0;
                }

                SolutionSet lastFront = ranking.GetSubfront(ranking.GetNumberOfSubfronts() - 1);

                fastHypervolume.ComputeHVContributions(lastFront);
                lastFront.Sort(new CrowdingDistanceComparator());

                // all but the worst are carried over to the survivor population
                SolutionSet front = null;
                population.Clear();
                for (int i = 0; i < ranking.GetNumberOfSubfronts() - 1; i++)
                {
                    front = ranking.GetSubfront(i);
                    for (int j = 0; j < front.Size(); j++)
                    {
                        population.Add(front.Get(j));
                    }
                }
                for (int i = 0; i < lastFront.Size() - 1; i++)
                {
                    population.Add(lastFront.Get(i));
                }

                // This piece of code shows how to use the indicator object into the code
                // of SMS-EMOA. 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 rnk = new Ranking(population);

            Result = rnk.GetSubfront(0);
            return(Result);
        }
Esempio n. 13
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 iterationsNumber = -1;
            int evaluations;
            int iteration;

            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.GetIntValueFromParameter(this.InputParameters, "iterationsNumber", ref iterationsNumber);
            JMetalCSharp.Utils.Utils.GetIndicatorsFromParameters(this.InputParameters, "indicators", ref indicators);

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

            requiredEvaluations = 0;

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

            Random random = new Random();

            JMetalRandom.SetRandom(random);

            // Create the initial solutionSet
            Solution newSolution;

            for (int i = 0; i < populationSize; i++)
            {
                newSolution = new Solution(Problem);
                Problem.Evaluate(newSolution);
                Problem.EvaluateConstraints(newSolution);
                evaluations++;
                population.Add(newSolution);
            }

            /*
             * string dir = "Result/MOEAD_ACOR/ZDT4_Real/Record/NSGAII_SBX_nr16";
             * if (Directory.Exists(dir))
             * {
             *  Console.WriteLine("The directory {0} already exists.", dir);
             * }
             * else
             * {
             *  Directory.CreateDirectory(dir);
             *  Console.WriteLine("The directory {0} was created.", dir);
             * }*/

            // Generations
            while (iteration < iterationsNumber)
            {
                // Create the offSpring solutionSet
                offspringPopulation = new SolutionSet(populationSize);
                Solution[] parents = new Solution[2];
                for (int i = 0; i < (populationSize / 2); i++)
                {
                    if (iteration < iterationsNumber)
                    {
                        //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]);
                        Problem.Evaluate(offSpring[0]);
                        Problem.EvaluateConstraints(offSpring[0]);
                        Problem.Evaluate(offSpring[1]);
                        Problem.EvaluateConstraints(offSpring[1]);
                        offspringPopulation.Add(offSpring[0]);
                        offspringPopulation.Add(offSpring[1]);
                        evaluations += 2;
                    }
                }

                // 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;
                }

                /*
                 * string filevar = dir + "/VAR" + iteration;
                 * string filefun = dir + "/FUN" + iteration;
                 * population.PrintVariablesToFile(filevar);
                 * population.PrintObjectivesToFile(filefun);*/

                iteration++;

                // 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;
                    }
                }
            }

            Logger.Log.Info("ITERATION: " + iteration);
            Console.WriteLine("ITERATION: " + iteration);

            // 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(Result);
        }
Esempio n. 14
0
        /// <summary>
        /// Gets 'size' elements from a population of more than 'size' elements
        /// using for this de enviromentalSelection truncation
        /// </summary>
        /// <param name="size">The number of elements to get.</param>
        /// <returns></returns>
        public SolutionSet EnvironmentalSelection(int size)
        {
            if (solutionSet.Size() < size)
            {
                size = solutionSet.Size();
            }

            // Create a new auxiliar population for no alter the original population
            SolutionSet aux = new SolutionSet(solutionSet.Size());

            int i = 0;

            while (i < solutionSet.Size())
            {
                if (solutionSet.Get(i).Fitness < 1.0)
                {
                    aux.Add(solutionSet.Get(i));
                    solutionSet.Remove(i);
                }
                else
                {
                    i++;
                }
            }

            if (aux.Size() < size)
            {
                IComparer <Solution> comparator = new FitnessComparator();
                solutionSet.Sort(comparator);
                int remain = size - aux.Size();
                for (i = 0; i < remain; i++)
                {
                    aux.Add(solutionSet.Get(i));
                }
                return(aux);
            }
            else if (aux.Size() == size)
            {
                return(aux);
            }

            double[][] distanceMatrix = distance.DistanceMatrix(aux);
            List <List <DistanceNode> > distanceList = new List <List <DistanceNode> >();

            for (int pos = 0; pos < aux.Size(); pos++)
            {
                aux.Get(pos).Location = pos;
                List <DistanceNode> distanceNodeList = new List <DistanceNode>();
                for (int refe = 0; refe < aux.Size(); refe++)
                {
                    if (pos != refe)
                    {
                        distanceNodeList.Add(new DistanceNode(distanceMatrix[pos][refe], refe));
                    }
                }
                distanceList.Add(distanceNodeList);
            }


            foreach (List <DistanceNode> aDistanceList in distanceList)
            {
                aDistanceList.Sort(distanceNodeComparator);
            }

            while (aux.Size() > size)
            {
                double minDistance = double.MaxValue;
                int    toRemove    = 0;
                i = 0;
                foreach (List <DistanceNode> dn in distanceList)
                {
                    if (dn[0].Distance < minDistance)
                    {
                        toRemove    = i;
                        minDistance = dn[0].Distance;
                        //i and toRemove have the same distance to the first solution
                    }
                    else if (dn[0].Distance == minDistance)
                    {
                        int k = 0;
                        while ((dn[k].Distance == distanceList[toRemove][k].Distance) &&
                               k < (distanceList[i].Count - 1))
                        {
                            k++;
                        }

                        if (dn[k].Distance < distanceList[toRemove][k].Distance)
                        {
                            toRemove = i;
                        }
                    }
                    i++;
                }

                int tmp = aux.Get(toRemove).Location;
                aux.Remove(toRemove);
                distanceList.RemoveAt(toRemove);

                foreach (List <DistanceNode> aDistanceList in distanceList)
                {
                    List <DistanceNode> interIterator = aDistanceList.ToList();
                    foreach (var element in interIterator)
                    {
                        if (element.Reference == tmp)
                        {
                            aDistanceList.Remove(element);
                        }
                    }
                }
            }
            return(aux);
        }