Ejemplo n.º 1
0
        /** Assigns crowding distances to all solutions in a <code>SolutionSet</code>.
           * @param solutionSet The <code>SolutionSet</code>.
           * @param nObjs Number of objectives.
           */
        public static void CrowdingDistanceAssignment(SolutionSet solutionSet, int nObjs)
        {
            int size = solutionSet.Size();

            if (size == 0)
            {
                return;
            }

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

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

            //Use a new SolutionSet to evite alter original solutionSet
            SolutionSet front = new SolutionSet(size);
            for (int i = 0; i < size; i++)
            {
                front.Add(solutionSet[i]);
            }

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

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

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

                for (int j = 1; j < size - 1; j++)
                {
                    double distance = front[j + 1].Objective[i] - front[j - 1].Objective[i];
                    distance = distance/(objetiveMaxn - objetiveMinn);
                    distance += front[j].CrowdingDistance;
                    front[j].CrowdingDistance = (distance);
                } // for
            } // for
        }
        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[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[k]);
                }
            }

            return result;
        }
Ejemplo n.º 3
0
        public override SolutionSet Execute()
        {
            int populationSize;
            int maxEvaluations;
            QualityIndicator indicators;

            Operator mutationOperator;
            Operator crossoverOperator;
            Operator selectionOperator;

            object parameter;
            if (InputParameters.TryGetValue("populationSize", out parameter))
            {
                populationSize = (int) parameter;
            }
            else
            {
                throw new Exception("populationSize does not exist");
            }

            if (InputParameters.TryGetValue("maxEvaluations", out parameter))
            {
                maxEvaluations = (int) parameter;
            }
            else
            {
                throw new Exception("maxEvaluations does not exist");
            }

            if (InputParameters.TryGetValue("indicators", out parameter))
            {
                indicators = (QualityIndicator) parameter;
            }
            else
            {
                throw new Exception("maxEvaluations does not exist");
            }

            if (InputParameters.TryGetValue("theta", out parameter))
            {
                Theta = (int) parameter;
            }
            else
            {
                throw new Exception("Theta does not exist");
            }

            if (InputParameters.TryGetValue("H", out parameter))
            {
                K = MetalMath.BinomialCoeff(Problema.NumberOfObjectives - 1,
                                            ((int) parameter) + Problema.NumberOfObjectives - 1);
            }
            else
            {
                throw new Exception("H does not exist");
            }

            // Initializing variables
            var population = new SolutionSet(populationSize);
            var evaluations = 0;

            int requiredEvaluations = 0;

            Operator unknownIOperator;
            //Read the operators
            if (UsedOperators.TryGetValue("mutation", out unknownIOperator))
            {
                mutationOperator = unknownIOperator;
            }
            else
            {
                throw new Exception("mutation does not exist");
            }
            if (UsedOperators.TryGetValue("crossover", out unknownIOperator))
            {
                crossoverOperator = unknownIOperator;
            }
            else
            {
                throw new Exception("crossover does not exist");
            }
            if (UsedOperators.TryGetValue("selection", out unknownIOperator))
            {
                selectionOperator = unknownIOperator;
            }
            else
            {
                throw new Exception("selection does not exist");
            }

            K = populationSize;
            ReferencePoints = new Solution[K];

            //Create references points
            GenerateReferencePoint();

            // Create the initial solutionSet
            for (int i = 0; i < populationSize; i++)
            {
                var newSolution = new Solution(Problema);
                Problema.Evaluate(newSolution);
                Problema.EvaluateConstraints(newSolution);
                evaluations++;
                population.Add(newSolution);
            }

            IdealPoint = new Solution(Problema);
            for (var i = 0; i < Problema.NumberOfObjectives; i++)
            {
                IdealPoint.Objective[i] = double.PositiveInfinity;
            }

            ComputeIdealPoint(population);

            ThetaNonDominatedSort ranking = null;

            // Generations
            SolutionSet union = null;

            while (evaluations < maxEvaluations)
            {
                // Create the offSpring solutionSet
                var 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]);
                        Problema.Evaluate(offSpring[0]);
                        Problema.EvaluateConstraints(offSpring[0]);
                        Problema.Evaluate(offSpring[1]);
                        Problema.EvaluateConstraints(offSpring[1]);
                        offspringPopulation.Add(offSpring[0]);
                        offspringPopulation.Add(offSpring[1]);
                        evaluations += 2;
                    }
                }

                //Update ideal point

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

                //Normalize
                NormalizePopulation(union);

                //Clustering and non dominated sort
                ranking = new ThetaNonDominatedSort(union, Clustering(union), Theta);

                //Generate next pop
                population = new SolutionSet(populationSize);
                int front = 0;
                while (population.Size() + ranking.GetSubfront(front).Size() <= populationSize)
                {
                    var frontResult = ranking.GetSubfront(front);
                    foreach (var r in frontResult.SolutionList)
                    {
                        population.Add(r);
                    }
                    front++;
                }

                //Generate new pop
                var localList =
                    ranking.GetSubfront(front).SolutionList.OrderBy(item => PseudoRandom.Instance().Next()).ToList();
                int localSizePt1 = population.Size();
                for (int i = 0; i < populationSize - localSizePt1; i++)
                {
                    population.Add(localList[i]);
                }

                if ((indicators != null) && (requiredEvaluations == 0))
                {
                    /*
                    double hv = indicators.GetHypervolume(population);
                    if (hv >= (0.98 * indicators.GetTrueParetoFrontHypervolume()))
                    {
                        requiredEvaluations = evaluations;
                    }*/

                    double gd = indicators.GetGd(ranking.GetSubfront(0));
                    Console.WriteLine(gd);
                }
            }

            OutputParameters["evaluations"] = requiredEvaluations;
            ranking.GetSubfront(0).PrintFeasibleFUN("FUN_THETA_NSGAIII");
            Console.WriteLine("before quitting");
            Console.WriteLine(indicators.GetGd(ranking.GetSubfront(0)));
            Console.WriteLine("before quitting");
            return ranking.GetSubfront(0);
        }
Ejemplo n.º 4
0
        // NSGAII
        public override SolutionSet Execute()
        {
            int populationSize;
            int maxEvaluations;
            QualityIndicator indicators;

            Operator mutationOperator;
            Operator crossoverOperator;
            Operator selectionOperator;

            object parameter;
            if (InputParameters.TryGetValue("populationSize", out parameter))
            {
                populationSize = (int) parameter;
            }
            else
            {
                throw new Exception("populationSize does not exist");
            }

            if (InputParameters.TryGetValue("maxEvaluations", out parameter))
            {
                maxEvaluations = (int) parameter;
            }
            else
            {
                throw new Exception("maxEvaluations does not exist");
            }

            if (InputParameters.TryGetValue("indicators", out parameter))
            {
                indicators = (QualityIndicator) parameter;
            }
            else
            {
                throw new Exception("maxEvaluations does not exist");
            }

            // Initializing variables
            var population = new SolutionSet(populationSize);
            var evaluations = 0;

            int requiredEvaluations = 0;

            Operator unknownIOperator;
            //Read the operators
            if (UsedOperators.TryGetValue("mutation", out unknownIOperator))
            {
                mutationOperator = unknownIOperator;
            }
            else
            {
                throw new Exception("mutation does not exist");
            }
            if (UsedOperators.TryGetValue("crossover", out unknownIOperator))
            {
                crossoverOperator = unknownIOperator;
            }
            else
            {
                throw new Exception("crossover does not exist");
            }
            if (UsedOperators.TryGetValue("selection", out unknownIOperator))
            {
                selectionOperator = unknownIOperator;
            }
            else
            {
                throw new Exception("selection does not exist");
            }

            // Create the initial solutionSet
            for (int i = 0; i < populationSize; i++)
            {
                var newSolution = new Solution(Problema);
                Problema.Evaluate(newSolution);
                Problema.EvaluateConstraints(newSolution);
                evaluations++;
                population.Add(newSolution);
            } //for

            // Generations
            while (evaluations < maxEvaluations)
            {
                // Create the offSpring solutionSet
                var 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]);
                        Problema.Evaluate(offSpring[0]);
                        Problema.EvaluateConstraints(offSpring[0]);
                        Problema.Evaluate(offSpring[1]);
                        Problema.EvaluateConstraints(offSpring[1]);
                        offspringPopulation.Add(offSpring[0]);
                        offspringPopulation.Add(offSpring[1]);
                        evaluations += 2;
                    } // if
                } // for

                System.Console.WriteLine("evaluation #: " + evaluations);

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

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

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

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

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

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

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

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

                    remain = 0;
                } // if

                // 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.GetTrueParetoFrontHypervolume()))
                    {
                        requiredEvaluations = evaluations;
                    } // if
                } // if
            } // while

            // Return as output parameter the required evaluations
            OutputParameters["evaluations"] = requiredEvaluations;

            // Return the first non-dominated front
            Ranking ranking = new Ranking(population);
            ranking.GetSubfront(0).PrintFeasibleFUN("FUN_NSGAII");

            return ranking.GetSubfront(0);
        }
Ejemplo n.º 5
0
        /// <summary>
        ///     Joins two solution sets: the current one and the one passed as argument
        /// </summary>
        /// <param name="solutionSet">
        ///     A <see cref="SolutionSet" />
        /// </param>
        /// <returns>
        ///     A new solution set
        ///     A <see cref="SolutionSet" />
        /// </returns>
        public SolutionSet Union(SolutionSet solutionSet)
        {
            if (solutionSet == null)
            {
                throw new ArgumentNullException("solutionSet");
            }
            //Check the correct size
            int newSize = Size() + solutionSet.Size();
            if (newSize < Capacity)
            {
                newSize = Capacity;
            }

            //Create a new population
            var union = new SolutionSet(newSize);
            foreach (Solution solution in SolutionList)
            {
                union.Add(solution);
            }

            for (int i = Size(); i < (Size() + solutionSet.Size()); i++)
            {
                union.Add(solutionSet.SolutionList[i - Size()]);
            }

            return union;
        }