Exemplo n.º 1
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="cid">the id of current subproblem</param>
        /// <param name="type">1 - whole population probability; otherwise - whole neighborhood probability</param>

        /*public int ACOrSelection4(int cid, int type)
         * {
         *  int ss;
         *  ss = neighborhood[cid].Length;
         *  double r;
         *  int b = neighborhood[cid][0];
         *  Solution[] parents = new Solution[ss];
         *  double[] fitness = new double[ss];
         *  Solution[] parent = new Solution[populationSize];
         *  double[] fit = new double[populationSize];
         *  double sum = 0;
         *  double[] pro = new double[ss];
         *  double[] p = new double[populationSize];
         *  double a1 = 0;
         *  double a2 = 0;
         *
         *  if (type == 1)
         *  {
         *      for (int i = 0; i < ss; i++)
         *      {
         *          parents[i] = population.Get(neighborhood[cid][i]);
         *          fitness[i] = 1 / FitnessFunction(parents[i], lambda[cid]);
         *          sum = sum + fitness[i];
         *      }
         *      for (int j = 0; j < ss; j++)
         *      {
         *          pro[j] = fitness[j] / sum;
         *      }
         *      r = JMetalRandom.NextDouble();
         *      for (int k = 0; k < pro.Length; k++)
         *      {
         *          a2 = a2 + pro[k];
         *          if (r < a2 && r >= a1)
         *          {
         *              b = neighborhood[cid][k];
         *              break;
         *          }
         *          a1 = a1 + pro[k];
         *      }
         *  }
         *  else
         *  {
         *      for (int i = 0; i < populationSize; i++)
         *      {
         *          parent[i] = population.Get(i);
         *          fit[i] = 1 / FitnessFunction(parent[i], lambda[cid]);
         *          sum = sum + fit[i];
         *      }
         *      for (int j = 0; j < populationSize; j++)
         *      {
         *          p[j] = fit[j] / sum;
         *      }
         *      r = JMetalRandom.NextDouble();
         *      for (int k = 0; k < p.Length; k++)
         *      {
         *          a2 = a2 + p[k];
         *          if (r < a2 && r >= a1)
         *          {
         *              b = k;
         *              break;
         *          }
         *          a1 = a1 + p[k];
         *      }
         *  }
         *
         *  return b;
         * }*/

        /// <summary>
        ///
        /// </summary>
        /// <param name="indiv">child solution</param>
        /// <param name="id">the id of current subproblem</param>
        /// <param name="type">update solutions in - neighborhood (1) or whole population (otherwise)</param>
        private void UpdateProblem(Solution indiv, int id, int type)
        {
            int size;
            int time;

            time = 0;

            if (type == 1)
            {
                size = neighborhood[id].Length;
            }
            else
            {
                size = population.Size();
            }
            int[] perm = new int[size];

            Utils.RandomPermutation(perm, size);

            for (int i = 0; i < size; i++)
            {
                int k;
                if (type == 1)
                {
                    k = neighborhood[id][perm[i]];
                }
                else
                {
                    k = perm[i];                          // calculate the values of objective function regarding the current subproblem
                }
                double f1, f2;

                f1 = FitnessFunction(population.Get(k), lambda[k]);
                f2 = FitnessFunction(indiv, lambda[k]);

                if (f2 < f1)
                {
                    population.Replace(k, new Solution(indiv));
                    time++;
                }
                // the maximal number of solutions updated is not allowed to exceed 'limit'
                if (time >= nr)
                {
                    return;
                }
            }
        }
        /// <summary>
        /// Runs of the aMOCell2 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()
        {
            //Init the param
            int populationSize = -1,
                archiveSize    = -1,
                maxEvaluations = -1,
                evaluations    = -1;

            Operator        mutationOperator, crossoverOperator, selectionOperator;
            SolutionSet     currentSolutionSet;
            CrowdingArchive archive;

            SolutionSet[]        neighbors;
            Neighborhood         neighborhood;
            IComparer <Solution> dominance = new DominanceComparator(),
                                 crowding  = new CrowdingComparator();
            Distance distance = new Distance();

            //Read the params
            JMetalCSharp.Utils.Utils.GetIntValueFromParameter(this.InputParameters, "populationSize", ref populationSize);
            JMetalCSharp.Utils.Utils.GetIntValueFromParameter(this.InputParameters, "archiveSize", ref archiveSize);
            JMetalCSharp.Utils.Utils.GetIntValueFromParameter(this.InputParameters, "maxEvaluations", ref maxEvaluations);

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

            //Initialize the variables
            currentSolutionSet = new SolutionSet(populationSize);
            archive            = new CrowdingArchive(archiveSize, this.Problem.NumberOfObjectives);
            evaluations        = 0;
            neighborhood       = new Neighborhood(populationSize);
            neighbors          = new SolutionSet[populationSize];


            //Create the initial population
            for (int i = 0; i < populationSize; i++)
            {
                Solution solution = new Solution(this.Problem);
                this.Problem.Evaluate(solution);
                this.Problem.EvaluateConstraints(solution);
                currentSolutionSet.Add(solution);
                solution.Location = i;
                evaluations++;
            }


            while (evaluations < maxEvaluations)
            {
                for (int ind = 0; ind < currentSolutionSet.Size(); ind++)
                {
                    Solution individual = new Solution(currentSolutionSet.Get(ind));

                    Solution[] parents = new Solution[2];
                    Solution[] offSpring;

                    neighbors[ind] = neighborhood.GetEightNeighbors(currentSolutionSet, ind);
                    neighbors[ind].Add(individual);

                    //parents
                    parents[0] = (Solution)selectionOperator.Execute(neighbors[ind]);
                    if (archive.Size() > 0)
                    {
                        parents[1] = (Solution)selectionOperator.Execute(archive);
                    }
                    else
                    {
                        parents[1] = (Solution)selectionOperator.Execute(neighbors[ind]);
                    }

                    //Create a new solution, using genetic operators mutation and crossover
                    offSpring = (Solution[])crossoverOperator.Execute(parents);
                    mutationOperator.Execute(offSpring[0]);

                    //->Evaluate solution and constraints
                    this.Problem.Evaluate(offSpring[0]);
                    this.Problem.EvaluateConstraints(offSpring[0]);
                    evaluations++;

                    int flag = dominance.Compare(individual, offSpring[0]);

                    if (flag == 1)
                    {                     // OffSpring[0] dominates
                        offSpring[0].Location = individual.Location;
                        currentSolutionSet.Replace(offSpring[0].Location, offSpring[0]);
                        archive.Add(new Solution(offSpring[0]));
                    }
                    else if (flag == 0)
                    {                     //Both two are non-dominated
                        neighbors[ind].Add(offSpring[0]);
                        Ranking rank = new Ranking(neighbors[ind]);
                        for (int j = 0; j < rank.GetNumberOfSubfronts(); j++)
                        {
                            distance.CrowdingDistanceAssignment(rank.GetSubfront(j), this.Problem.NumberOfObjectives);
                        }

                        bool deleteMutant = true;

                        int compareResult = crowding.Compare(individual, offSpring[0]);
                        if (compareResult == 1)
                        {                         //The offSpring[0] is better
                            deleteMutant = false;
                        }

                        if (!deleteMutant)
                        {
                            offSpring[0].Location = individual.Location;
                            currentSolutionSet.Replace(offSpring[0].Location, offSpring[0]);
                            archive.Add(new Solution(offSpring[0]));
                        }
                        else
                        {
                            archive.Add(new Solution(offSpring[0]));
                        }
                    }
                }
            }
            Result = archive;
            return(archive);
        }
Exemplo n.º 3
0
        /// <summary>
        /// Execute the algorithm
        /// </summary>
        /// <returns></returns>
        public override SolutionSet Execute()
        {
            int populationSize = -1,
                archiveSize    = -1,
                maxEvaluations = -1,
                evaluations    = -1,
                feedBack       = -1;

            Operator        mutationOperator, crossoverOperator, selectionOperator;
            SolutionSet     currentPopulation;
            CrowdingArchive archive;

            SolutionSet[]        neighbors;
            Neighborhood         neighborhood;
            IComparer <Solution> dominance          = new DominanceComparator();
            IComparer <Solution> crowdingComparator = new CrowdingComparator();
            Distance             distance           = new Distance();

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


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

            //Initialize the variables
            //Initialize the population and the archive
            currentPopulation = new SolutionSet(populationSize);
            archive           = new CrowdingArchive(archiveSize, this.Problem.NumberOfObjectives);
            evaluations       = 0;
            neighborhood      = new Neighborhood(populationSize);
            neighbors         = new SolutionSet[populationSize];

            //Create the comparator for check dominance
            dominance = new DominanceComparator();

            //Create the initial population
            for (int i = 0; i < populationSize; i++)
            {
                Solution individual = new Solution(this.Problem);
                this.Problem.Evaluate(individual);
                this.Problem.EvaluateConstraints(individual);
                currentPopulation.Add(individual);
                individual.Location = i;
                evaluations++;
            }

            while (evaluations < maxEvaluations)
            {
                for (int ind = 0; ind < currentPopulation.Size(); ind++)
                {
                    Solution individual = new Solution(currentPopulation.Get(ind));

                    Solution[] parents = new Solution[2];
                    Solution[] offSpring;

                    neighbors[ind] = neighborhood.GetEightNeighbors(currentPopulation, ind);
                    neighbors[ind].Add(individual);

                    //parents
                    parents[0] = (Solution)selectionOperator.Execute(neighbors[ind]);
                    parents[1] = (Solution)selectionOperator.Execute(neighbors[ind]);

                    //Create a new individual, using genetic operators mutation and crossover
                    offSpring = (Solution[])crossoverOperator.Execute(parents);
                    mutationOperator.Execute(offSpring[0]);

                    //->Evaluate individual an his constraints
                    this.Problem.Evaluate(offSpring[0]);
                    this.Problem.EvaluateConstraints(offSpring[0]);
                    evaluations++;
                    //<-Individual evaluated

                    int flag = dominance.Compare(individual, offSpring[0]);

                    if (flag == 1)
                    {                     //The new individuals dominate
                        offSpring[0].Location = individual.Location;
                        currentPopulation.Replace(offSpring[0].Location, offSpring[0]);
                        archive.Add(new Solution(offSpring[0]));
                    }
                    else if (flag == 0)
                    {                    //The individuals are non-dominates
                        neighbors[ind].Add(offSpring[0]);
                        offSpring[0].Location = -1;
                        Ranking rank = new Ranking(neighbors[ind]);
                        for (int j = 0; j < rank.GetNumberOfSubfronts(); j++)
                        {
                            distance.CrowdingDistanceAssignment(rank.GetSubfront(j), this.Problem.NumberOfObjectives);
                        }
                        Solution worst = neighbors[ind].Worst(crowdingComparator);

                        if (worst.Location == -1)
                        {                        //The worst is the offspring
                            archive.Add(new Solution(offSpring[0]));
                        }
                        else
                        {
                            offSpring[0].Location = worst.Location;
                            currentPopulation.Replace(offSpring[0].Location, offSpring[0]);
                            archive.Add(new Solution(offSpring[0]));
                        }
                    }
                }

                //Store a portion of the archive into the population
                distance.CrowdingDistanceAssignment(archive, this.Problem.NumberOfObjectives);
                for (int j = 0; j < feedBack; j++)
                {
                    if (archive.Size() > j)
                    {
                        int r = JMetalRandom.Next(0, currentPopulation.Size() - 1);
                        if (r < currentPopulation.Size())
                        {
                            Solution individual = archive.Get(j);
                            individual.Location = r;
                            currentPopulation.Replace(r, new Solution(individual));
                        }
                    }
                }
            }
            Result = archive;
            return(archive);
        }