/// <summary>
        /// Adds a solution to be evaluated to a list of tasks
        /// </summary>
        /// <param name="taskParameters"></param>
        public void AddTaskForExecution(object[] taskParameters)
        {
            IntergenSolution solution = (IntergenSolution)taskParameters[0];

            solution.FoundAtEval = counter;
            counter++;
            if (taskList == null)
            {
                taskList = new List <Task <IntergenSolution> >();
            }

            taskList.Add(new Task <IntergenSolution>(() =>
            {
                problem.Evaluate(solution);
                problem.EvaluateConstraints(solution);

                return(solution);
            }));
        }
        /// <summary>
        /// Evaluates a list of solutions
        /// </summary>
        /// <returns>A list with the evaluated solutions</returns>
        public object ParallelExecution()
        {
            try
            {
                foreach (var task in taskList)
                {
                    task.Start();
                }
            }
            catch (Exception ex)
            {
                Logger.Log.Error("Error in MultithreadedEvaluator.ParallelExecution", ex);
                Console.Error.WriteLine(ex.StackTrace);
            }

            Task.WaitAll(taskList.ToArray());

            List <IntergenSolution> solutionList = new List <IntergenSolution>();

            foreach (Task <IntergenSolution> task in taskList)
            {
                IntergenSolution solution = null;
                try
                {
                    solution = task.Result;
                    solutionList.Add(solution);
                }
                catch (Exception ex)
                {
                    Logger.Log.Error("Error in MultithreadedEvaluator.ParallelExecution", ex);
                    Console.Error.WriteLine(ex.StackTrace);
                }
            }
            taskList = null;
            return(solutionList);
        }
示例#3
0
        private IntergenSolution[] DoCrossover(double probability, IntergenSolution parent1, IntergenSolution parent2)
        {
            IntergenSolution[] offSpring = new IntergenSolution[2];

            offSpring[0] = new IntergenSolution(parent1);
            offSpring[1] = new IntergenSolution(parent2);

            int    i;
            double rand;
            double y1, y2, yL, yu;
            double c1, c2;
            double alpha, beta, betaq;
            double valueX1, valueX2;
            XReal  x1    = new XReal(parent1);
            XReal  x2    = new XReal(parent2);
            XReal  offs1 = new XReal(offSpring[0]);
            XReal  offs2 = new XReal(offSpring[1]);

            int numberOfVariables = x1.GetNumberOfDecisionVariables();

            if (JMetalRandom.NextDouble() <= probability)
            {
                for (i = 0; i < numberOfVariables; i++)
                {
                    valueX1 = x1.GetValue(i);
                    valueX2 = x2.GetValue(i);


                    if (JMetalRandom.NextDouble() <= 0.5)
                    {
                        if (Math.Abs(valueX1 - valueX2) > EPS)
                        {
                            if (valueX1 < valueX2)
                            {
                                y1 = valueX1;
                                y2 = valueX2;
                            }
                            else
                            {
                                y1 = valueX2;
                                y2 = valueX1;
                            }

                            yL   = x1.GetLowerBound(i);
                            yu   = x1.GetUpperBound(i);
                            rand = JMetalRandom.NextDouble();


                            beta = 1.0 + (2.0 * (y1 - yL) / (y2 - y1));
                            //Console.WriteLine("Beta1" + beta);
                            alpha = 2.0 - Math.Pow(beta, -(distributionIndex + 1.0));

                            if (rand <= (1.0 / alpha))
                            {
                                betaq = Math.Pow((rand * alpha), (1.0 / (distributionIndex + 1.0)));

                                /* if (double.IsNaN(betaq))
                                 * {
                                 *   Console.WriteLine("NAN BETAq");
                                 * } */
                            }
                            else
                            {
                                betaq = Math.Pow((1.0 / (2.0 - rand * alpha)), (1.0 / (distributionIndex + 1.0)));

                                /* var betax = Math.Pow((1.0 / (2.0 - rand * alpha)), (1.0 / (distributionIndex + 1.0)));
                                 * if (double.IsNaN(betax))
                                 * {
                                 *   Console.WriteLine("NAN BETAx");
                                 * }
                                 * if (double.IsNaN(betaq))
                                 * {
                                 *   Console.WriteLine("NAN BETAq");
                                 * }
                                 */
                            }

                            /*
                             * if (double.IsNaN(betaq))
                             * {
                             *  Console.WriteLine("NAN BETAq");
                             * } */
                            c1    = 0.5 * ((y1 + y2) - betaq * (y2 - y1));
                            beta  = 1.0 + (2.0 * (yu - y2) / (y2 - y1));
                            alpha = 2.0 - Math.Pow(beta, -(distributionIndex + 1.0));

                            if (rand <= (1.0 / alpha))
                            {
                                betaq = Math.Pow((rand * alpha), (1.0 / (distributionIndex + 1.0)));

                                /*if (double.IsNaN(betaq))
                                 * {
                                 *  Console.WriteLine("NAN BETAq");
                                 * } */
                            }
                            else
                            {
                                betaq = Math.Pow((1.0 / (2.0 - rand * alpha)), (1.0 / (distributionIndex + 1.0)));

                                /*if (double.IsNaN(betaq))
                                 * {
                                 *  Console.WriteLine("NAN BETAq");
                                 * }*/
                            }

                            c2 = 0.5 * ((y1 + y2) + betaq * (y2 - y1));

                            if (c1 < yL)
                            {
                                c1 = yL;
                            }

                            if (c2 < yL)
                            {
                                c2 = yL;
                            }

                            if (c1 > yu)
                            {
                                c1 = yu;
                            }

                            if (c2 > yu)
                            {
                                c2 = yu;
                            }

                            /*if (double.IsNaN(c2) || double.IsNaN(c1))
                             * {
                             *  Console.WriteLine("Setting NAN");
                             * } */

                            if (JMetalRandom.NextDouble() <= 0.5)
                            {
                                offs1.SetValue(i, c2);
                                offs2.SetValue(i, c1);
                            }
                            else
                            {
                                offs1.SetValue(i, c1);
                                offs2.SetValue(i, c2);
                            }
                        }
                        else
                        {
                            offs1.SetValue(i, valueX1);
                            offs2.SetValue(i, valueX2);
                        }
                    }
                    else
                    {
                        offs1.SetValue(i, valueX2);
                        offs2.SetValue(i, valueX1);
                    }
                }
            }



            /*
             * var o1 = offSpring[0];
             * XReal values = new XReal(o1);
             * for (int u = 0; u < values.Size(); u++)
             * {
             *
             *  if (double.IsNaN(values.GetValue(u)))
             *  {
             *
             *      Console.WriteLine("crossover NAN1");
             *  }
             * }
             *
             * var s = offSpring[1];
             * XReal values2 = new XReal(s);
             * for (int u = 0; u < values2.Size(); u++)
             * {
             *  if (double.IsNaN(values2.GetValue(u)))
             *  {
             *      Console.WriteLine("crossover NAN2");
             *  }
             * } */

            return(offSpring);
        }
示例#4
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;

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

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

            requiredEvaluations = 0;

            //Read the operators
            mutationOperator  = Operators["mutation"];
            crossoverOperator = Operators["crossover"];
            selectionOperator = Operators["selection"];
            var    plotCounter = 0;
            var    plotModulo  = 4;
            Random random      = new Random(2);

            JMetalRandom.SetRandom(random);

            // Create the initial solutionSet
            IntergenSolution newSolution;

            for (int i = 0; i < populationSize; i++)
            {
                //var test = (IntergenProblem) Problem;
                newSolution = new IntergenSolution((IntergenProblem)Problem);
                Problem.Evaluate(newSolution);
                Problem.EvaluateConstraints(newSolution);
                evaluations++;
                population.Add(newSolution);
            }

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

                var sol0 = front.Best(new CrowdingComparator());
                //if (plotCounter%plotModulo == 0)

                SolutionPlotter.Plot(sol0);
                ProgressReporter.ReportSolution(evaluations, sol0, _worker);
            }

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