コード例 #1
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)
        {
            // <pex>
            if (solutionSet == (SolutionSet)null)
            {
                throw new ArgumentNullException("solutionSet");
            }
            // </pex>
            //Check the correct size
            int newSize = this.size() + solutionSet.size();

            if (newSize < capacity_)
            {
                //////////////////
                newSize = capacity_;
            }

            //Create a new population
            SolutionSet union = new SolutionSet(newSize);

            for (int i = 0; i < this.size(); i++)
            {
                union.add(this.solutionList_[i]);
            }
            // for
            for (int i = this.size(); i < (this.size() + solutionSet.size()); i++)
            {
                union.add(solutionSet.solutionList_[i - this.size()]);
            }
            // for
            return(union);
        }
コード例 #2
0
ファイル: Optimo.cs プロジェクト: redinkinc/Optimo
        //changes solution list to solution set
        private static SolutionSet SolutionListToPop(List <List <double> > populationList, Algorithm algorithm, int numVar)
        {
            int popSize = populationList[0].Count;
            int numObj  = populationList.Count - numVar;

            SolutionSet population = new SolutionSet(popSize);
            Solution    newSolution;

            for (int j = 0; j < popSize; j++)
            {
                newSolution = new Solution(algorithm.problem_);
                for (int i = 0; i < numVar; i++)
                {
                    newSolution.variable_[i].value_ = populationList[i][j];
                }
                population.add(newSolution);
            }
            for (int j = 0; j < population.size(); j++)
            {
                for (int i = 0; i < numObj; i++)
                {
                    population[j].objective_[i] = populationList[i + numVar][j];
                }
            }
            return(population);
        }
コード例 #3
0
ファイル: Optimo.cs プロジェクト: redinkinc/Optimo
        /// <summary>
        /// Generated new the children population list based on the parents population list.
        /// This node needs to be used in a loop along with Assign Fitness Function node and
        /// Sorting node.
        /// </summary>
        /// <param name="populationList">Parents population list.</param>
        /// <param name="lowerLimits">List of lower limits for new generation.</param>
        /// <param name="upperLimits">List of upper limits for new generation.</param>
        /// <returns>Generated population list.</returns>
        public static List <List <double> > GenerationAlgorithm(List <List <double> > populationList, List <int> lowerLimits, List <int> upperLimits)
        {
            int numVar = lowerLimits.Count;
            int numObj = populationList.Count - numVar;

            //create the NSGA-II algorithm
            Algorithm algorithm = CreateAlgorithm(numObj, lowerLimits, upperLimits);

            //change solutions list to solutionSet
            SolutionSet population = SolutionListToPop(populationList, algorithm, numVar);


            Operator mutationOperator  = algorithm.operators["mutation"];
            Operator crossoverOperator = algorithm.operators["crossover"];
            Operator selectionOperator = algorithm.operators["selection"];

            int populationSize = population.size();

            SolutionSet offspringPopulation = new SolutionSet(populationSize);

            Solution[] parents = new Solution[2];

            for (int i = 0; i < (populationSize / 2); i++)
            {
                // selection
                parents[0] = (Solution)selectionOperator.execute(population);
                parents[1] = (Solution)selectionOperator.execute(population);

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

                // mutation
                mutationOperator.execute(offSpring[0]);
                mutationOperator.execute(offSpring[1]);

                offspringPopulation.@add(offSpring[0]);
                offspringPopulation.add(offSpring[1]);
            }

            return(PopToSolutionList(offspringPopulation));
        }
コード例 #4
0
ファイル: Optimo.cs プロジェクト: redinkinc/Optimo
        /// <summary>
        /// Creates the initial solution lists of variables and objectives.
        /// The objectives will be assigned after evaluation using
        /// NSGA_II Assigne Fitness Function Results node.
        /// </summary>
        /// <param name="populationSize">Size that will be used to generate population.</param>
        /// <param name="numObjectives">Number of objectives that you are trying to optimize. If your problem is single objective optimization, set this number to one.</param>
        /// <param name="lowerLimits">List of lower limits for all variable. The number of lower limits should be the same as the number of variables.</param>
        /// <param name="upperLimits">List of upper limits for all variable. The number of upper limits should be the same as the number of variables.</param>
        /// <returns>An initial solution list.</returns>
        public static List <List <double> > InitialSolutionList(double populationSize, double numObjectives, List <int> lowerLimits, List <int> upperLimits)
        {
            //number of varables plus number of objectives
            int numObj = Convert.ToInt32(numObjectives);
            int numVar = lowerLimits.Count;

            Algorithm algorithm = CreateAlgorithm(numObjectives, lowerLimits, upperLimits);

            //Initializing variables
            SolutionSet population = new SolutionSet(Convert.ToInt32(populationSize));

            // Creating the initial solutionSet
            Solution newSolution;

            for (int i = 0; i < populationSize; i++)
            {
                newSolution = new Solution(algorithm.problem_);
                population.add(newSolution);
            }

            List <List <double> > test = PopToSolutionList(population);

            return(PopToSolutionList(population));
        }
コード例 #5
0
        // The solution set to be ranked

/*
 * public double [][] distanceMatrix(SolutionSet solutionSet) {
 *  Solution solutionI, solutionJ;
 *
 *  //The matrix of distances
 *  double [][] distance = new double [solutionSet.size()][solutionSet.size()];
 *  //-> Calculate the distances
 *  for (int i = 0; i < solutionSet.size(); i++){
 *    distance[i][i] = 0.0;
 *    solutionI = solutionSet.get(i);
 *    for (int j = i + 1; j < solutionSet.size(); j++){
 *      solutionJ = solutionSet.get(j);
 *      distance[i][j] = this.distanceBetweenObjectives(solutionI,solutionJ);
 *      distance[j][i] = distance[i][j];
 *    } // for
 *  } // for
 *
 *  //->Return the matrix of distances
 *  return distance;
 * } // distanceMatrix
 *
 * public double distanceToSolutionSetInObjectiveSpace(Solution    solution,
 *                                    SolutionSet solutionSet) throws JMException{
 *  //At start point the distance is the max
 *  double distance = Double.MAX_VALUE;
 *
 *  // found the min distance respect to population
 *  for (int i = 0; i < solutionSet.size();i++){
 *    double aux = this.distanceBetweenObjectives(solution,solutionSet.get(i));
 *    if (aux < distance)
 *      distance = aux;
 *  } // for
 *
 *  //->Return the best distance
 *  return distance;
 * } // distanceToSolutionSetinObjectiveSpace
 *
 * public double distanceToSolutionSetInSolutionSpace(Solution    solution,
 *                                    SolutionSet solutionSet) throws JMException{
 *   //At start point the distance is the max
 *   double distance = Double.MAX_VALUE;
 *
 *   // found the min distance respect to population
 *   for (int i = 0; i < solutionSet.size();i++){
 *     double aux = this.distanceBetweenSolutions(solution,solutionSet.get(i));
 *     if (aux < distance)
 *       distance = aux;
 *   } // for
 *
 *   //->Return the best distance
 *   return distance;
 * } // distanceToSolutionSetInSolutionSpace
 *
 *
 * public double distanceBetweenSolutions(Solution solutionI, Solution solutionJ)
 * throws JMException{
 *  //->Obtain his decision variables
 *  Variable[] decisionVariableI = solutionI.getDecisionVariables();
 *  Variable[] decisionVariableJ = solutionJ.getDecisionVariables();
 *
 *  double diff;    //Auxiliar var
 *  double distance = 0.0;
 *  //-> Calculate the Euclidean distance
 *  for (int i = 0; i < decisionVariableI.length; i++){
 *    diff = decisionVariableI[i].getValue() -
 *           decisionVariableJ[i].getValue();
 *    distance += Math.pow(diff,2.0);
 *  } // for
 *
 *  //-> Return the euclidean distance
 *  return Math.sqrt(distance);
 * } // distanceBetweenSolutions
 *
 *
 * public double distanceBetweenObjectives(Solution solutionI, Solution solutionJ){
 *  double diff;    //Auxiliar var
 *  double distance = 0.0;
 *  //-> Calculate the euclidean distance
 *  for (int nObj = 0; nObj < solutionI.numberOfObjectives();nObj++){
 *    diff = solutionI.getObjective(nObj) - solutionJ.getObjective(nObj);
 *    distance += Math.pow(diff,2.0);
 *  } // for
 *
 *  //Return the euclidean distance
 *  return Math.sqrt(distance);
 * } // distanceBetweenObjectives.
 *
 */
        internal static void crowdingDistanceAssignment(SolutionSet solutionSet, int nObjs)
        {
            // <pex>
            if (solutionSet == (SolutionSet)null)
            {
                throw new ArgumentNullException("solutionSet");
            }
            if (solutionSet[0] == (Solution)null)
            {
                throw new ArgumentNullException("solutionSet");
            }
            // </pex>
            int size = solutionSet.size();

            if (size == 0)
            {
                return;
            }

            if (size == 1)
            {
                solutionSet[0].crowdingDistance_ = Double.MaxValue;
                //solutionSet.get(0).setCrowdingDistance(Double.POSITIVE_INFINITY);
                return;
            } // if

            if (size == 2)
            {
                solutionSet[0].crowdingDistance_ = Double.MaxValue;
                solutionSet[1].crowdingDistance_ = Double.MaxValue;
                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;
            }

            double objetiveMaxn;
            double objetiveMinn;
            double distance;

            for (int i = 0; i < nObjs; i++)
            {
                // Sort the population by Obj n
                IComparer comp = new ObjectiveComparator(i);
                front.solutionList_.Sort(comp.Compare);
                //front.sort(new ObjectiveComparator(i));

                objetiveMinn = front[0].objective_[i];
                objetiveMaxn = front[front.size() - 1].objective_[i];

                //Set de crowding distance
                front[0].crowdingDistance_        = Double.MaxValue;
                front[size - 1].crowdingDistance_ = Double.MaxValue;

                for (int j = 1; j < size - 1; j++)
                {
                    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
        }         // crowdingDistanceAssing
コード例 #6
0
ファイル: Optimo.cs プロジェクト: xiebinhqy/Optimo
        /// <summary>
        /// Generated new the children population list based on the parents population list.
        /// This node needs to be used in a loop along with Assign Fitness Function node and
        /// Sorting node.
        /// </summary>
        /// <param name="populationList">Parents population list.</param>
        /// <param name="lowerLimits">List of lower limits for new generation.</param>
        /// <param name="upperLimits">List of upper limits for new generation.</param>
        /// <returns>Generated population list.</returns>
        public static List<List<double>> GenerationAlgorithm(List<List<double>> populationList, List<double> lowerLimits, List<double> upperLimits)
        {
            int numVar = lowerLimits.Count;
            int numObj = populationList.Count - numVar;

            //create the NSGA-II algorithm
            Algorithm algorithm = CreateAlgorithm(numObj, lowerLimits, upperLimits);

            //change solutions list to solutionSet
            SolutionSet population = SolutionListToPop(populationList, algorithm, numVar);
            

            Operator mutationOperator = algorithm.operators["mutation"];
            Operator crossoverOperator = algorithm.operators["crossover"];
            Operator selectionOperator = algorithm.operators["selection"];

            int populationSize = population.size();

            SolutionSet offspringPopulation = new SolutionSet(populationSize);
            Solution[] parents = new Solution[2];

            for (int i = 0; i < (populationSize / 2); i++)
            {
                // selection
                parents[0] = (Solution)selectionOperator.execute(population);
                parents[1] = (Solution)selectionOperator.execute(population);

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

                // mutation
                mutationOperator.execute(offSpring[0]);
                mutationOperator.execute(offSpring[1]);

                offspringPopulation.@add(offSpring[0]);
                offspringPopulation.add(offSpring[1]);
            }

            return PopToSolutionList(offspringPopulation);
        }
コード例 #7
0
ファイル: Optimo.cs プロジェクト: xiebinhqy/Optimo
        /// <summary>
        /// Creates the initial solution lists of variables and objectives.
        /// The objectives will be assigned after evaluation using
        /// NSGA_II Assigne Fitness Function Results node.
        /// </summary>
        /// <param name="populationSize">Size that will be used to generate population.</param>
        /// <param name="numObjectives">Number of objectives that you are trying to optimize. If your problem is single objective optimization, set this number to one.</param>
        /// <param name="lowerLimits">List of lower limits for all variable. The number of lower limits should be the same as the number of variables.</param>
        /// <param name="upperLimits">List of upper limits for all variable. The number of upper limits should be the same as the number of variables.</param>
        /// <returns>An initial solution list.</returns>
        public static List<List<double>> InitialSolutionList(double populationSize, double numObjectives, List<double> lowerLimits, List<double> upperLimits)
        {
            //number of varables plus number of objectives
            int numObj = Convert.ToInt32(numObjectives);
            int numVar = lowerLimits.Count;

            Algorithm algorithm = CreateAlgorithm(numObjectives, lowerLimits, upperLimits);

            //Initializing variables
            SolutionSet population = new SolutionSet(Convert.ToInt32(populationSize));

            // Creating the initial solutionSet
            Solution newSolution;
            for (int i = 0; i < populationSize; i++)
            {
                newSolution = new Solution(algorithm.problem_);
                population.add(newSolution);
            }

            List<List<double>> test = PopToSolutionList(population);

            return PopToSolutionList(population);
        }
コード例 #8
0
ファイル: Optimo.cs プロジェクト: xiebinhqy/Optimo
        //changes solution list to solution set
        private static SolutionSet SolutionListToPop(List<List<double>> populationList, Algorithm algorithm, int numVar)
        {
            int popSize = populationList[0].Count;
            int numObj = populationList.Count - numVar;

            SolutionSet population = new SolutionSet(popSize);
            Solution newSolution;

            for (int j = 0; j < popSize; j++)
            {
                newSolution = new Solution(algorithm.problem_);
                for (int i = 0; i < numVar; i++)
                {
                    newSolution.variable_[i].value_ = populationList[i][j];
                }
                population.add(newSolution);
            }
            for (int j = 0; j < population.size(); j++)
            {
                for (int i = 0; i < numObj; i++)
                {
                    population[j].objective_[i] = populationList[i + numVar][j];
                }
            }
            return population;
        }
コード例 #9
0
ファイル: SolutionSet.cs プロジェクト: xiebinhqy/Optimo
 /// <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)
 {
   // <pex>
   if (solutionSet == (SolutionSet)null)
     throw new ArgumentNullException("solutionSet");
   // </pex>
   //Check the correct size
   int newSize = this.size () + solutionSet.size ();
   if (newSize < capacity_)
     //////////////////
     newSize = capacity_;
   
   //Create a new population
   SolutionSet union = new SolutionSet (newSize);
   for (int i = 0; i < this.size (); i++) {
     union.add (this.solutionList_[i]);
   }
   // for
   for (int i = this.size (); i < (this.size () + solutionSet.size ()); i++) {
     union.add (solutionSet.solutionList_[i - this.size ()]);
   }
   // for
   return union;
 }
コード例 #10
0
ファイル: NSGAII.cs プロジェクト: xiebinhqy/Optimo
        // NSGAII
        public override SolutionSet execute()
        {
            int populationSize;
            int maxEvaluations;
            int evaluations;

            SolutionSet population;
            SolutionSet offspringPopulation;
            SolutionSet union;
            SolutionSet allTest = new SolutionSet();

            Operator mutationOperator;
            Operator crossoverOperator;
            Operator selectionOperator;

            populationSize = (int)inputParameters_["populationSize"];
            maxEvaluations = (int)inputParameters_["maxEvaluations"];

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

            //System.Console.WriteLine("Solves Name:" + problem_.problemName_);
            //System.Console.WriteLine("Pop size : " + populationSize);
            //System.Console.WriteLine("Max Evals: " + maxEvaluations);

            // Reading operators
            mutationOperator = operators["mutation"];
            crossoverOperator = operators["crossover"];
            selectionOperator = operators["selection"];

            //System.Console.WriteLine("Crossover parameters: " + crossoverOperator);
            //System.Console.WriteLine("Mutation parameters: " + mutationOperator);

            // Creating the initial solutionSet
            Solution newSolution;
            for (int i = 0; i < populationSize; i++)
            {
                newSolution = new Solution(problem_);
                //newSolution.variable_[0] = 1;
                problem_.evaluate(newSolution);
                //newSolution.objective_[0] = 10;
                //Mohammad
                //slnLst.Add(newSolution);
                //System.Console.WriteLine ("" + i + ": " + newSolution);
                //problem_.evaluateConstraints(newSolution);
                evaluations++;
                population.add(newSolution);
            }
            //for
            // Main loop
            while (evaluations < maxEvaluations)
            {
                // Creating the offSpring solutionSet

                offspringPopulation = new SolutionSet(populationSize);
                Solution[] parents = new Solution[2];

                for (int i = 0; i < (populationSize / 2); i++)
                {
                    if (evaluations < maxEvaluations)
                    {
                        //if ((evaluations % 1000) == 0)
                        //    Console.WriteLine("Evals: " + evaluations);
                        // selection
                        parents[0] = (Solution)selectionOperator.execute(population);
                        parents[1] = (Solution)selectionOperator.execute(population);

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

                        // mutation
                        mutationOperator.execute(offSpring[0]);
                        mutationOperator.execute(offSpring[1]);

                        //Environment.Exit(0);
                        // evaluation
                        problem_.evaluate(offSpring[0]);
                        problem_.evaluate(offSpring[1]);

                        offspringPopulation.@add(offSpring[0]);
                        offspringPopulation.add(offSpring[1]);

                        evaluations += 2;
                    }
                    // if
                }
                // for
                // Creating the solutionSet union of solutionSet and offSpring
                union = ((SolutionSet)population).union(offspringPopulation);

                //Mohammad
                allTest = ((SolutionSet)allTest).union(union);
                //System.Console.WriteLine ("Union size:" + union.size ());

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

                int remain = populationSize;
                int index = 0;
                SolutionSet front = null;

                //Distance distance = new Distance ();

                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[k]);
                    }
                    // for
                    //Decrement remain
                    remain = remain - front.size();

                    //Obtain the next front
                    index++;
                    if (remain > 0)
                    {
                        front = ranking.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, problem_.numberOfObjectives_);
                    IComparer comp = new CrowdingDistanceComparator();
                    front.solutionList_.Sort(comp.Compare);
                    for (int k = 0; k < remain; k++)
                    {
                        population.@add(front[k]);
                    }
                    // for
                    remain = 0;
                }
                // if
            }
            // while
            // Return as output parameter the required evaluations
            outputParameters_["evaluations"] = evaluations;

            // Return the first non-dominated front
            Ranking ranking2 = new Ranking(population);
            //Console.WriteLine(slnLst[0].objective_.GetValue(0));

            //File.WriteAllLines("C:/text.txt",slnLst.ConvertAll(Convert.ToString));
            return ranking2.getSubfront(0);
        }
コード例 #11
0
ファイル: Distance.cs プロジェクト: xiebinhqy/Optimo
    // The solution set to be ranked

/*
  public double [][] distanceMatrix(SolutionSet solutionSet) {
    Solution solutionI, solutionJ;

    //The matrix of distances
    double [][] distance = new double [solutionSet.size()][solutionSet.size()];        
    //-> Calculate the distances
    for (int i = 0; i < solutionSet.size(); i++){
      distance[i][i] = 0.0;
      solutionI = solutionSet.get(i);
      for (int j = i + 1; j < solutionSet.size(); j++){
        solutionJ = solutionSet.get(j);
        distance[i][j] = this.distanceBetweenObjectives(solutionI,solutionJ);                
        distance[j][i] = distance[i][j];            
      } // for
    } // for        
    
    //->Return the matrix of distances
    return distance;
  } // distanceMatrix
    
  public double distanceToSolutionSetInObjectiveSpace(Solution    solution,
                                      SolutionSet solutionSet) throws JMException{
    //At start point the distance is the max
    double distance = Double.MAX_VALUE;    
        
    // found the min distance respect to population
    for (int i = 0; i < solutionSet.size();i++){            
      double aux = this.distanceBetweenObjectives(solution,solutionSet.get(i));
      if (aux < distance)
        distance = aux;
    } // for
    
    //->Return the best distance
    return distance;
  } // distanceToSolutionSetinObjectiveSpace
    
   public double distanceToSolutionSetInSolutionSpace(Solution    solution,
                                      SolutionSet solutionSet) throws JMException{
     //At start point the distance is the max
     double distance = Double.MAX_VALUE;    
         
     // found the min distance respect to population
     for (int i = 0; i < solutionSet.size();i++){            
       double aux = this.distanceBetweenSolutions(solution,solutionSet.get(i));
       if (aux < distance)
         distance = aux;
     } // for
     
     //->Return the best distance
     return distance;
   } // distanceToSolutionSetInSolutionSpace
    

  public double distanceBetweenSolutions(Solution solutionI, Solution solutionJ) 
  throws JMException{                
    //->Obtain his decision variables
    Variable[] decisionVariableI = solutionI.getDecisionVariables();
    Variable[] decisionVariableJ = solutionJ.getDecisionVariables();    
    
    double diff;    //Auxiliar var
    double distance = 0.0;
    //-> Calculate the Euclidean distance
    for (int i = 0; i < decisionVariableI.length; i++){
      diff = decisionVariableI[i].getValue() -
             decisionVariableJ[i].getValue();
      distance += Math.pow(diff,2.0);
    } // for    
        
    //-> Return the euclidean distance
    return Math.sqrt(distance);
  } // distanceBetweenSolutions
    

  public double distanceBetweenObjectives(Solution solutionI, Solution solutionJ){                
    double diff;    //Auxiliar var
    double distance = 0.0;
    //-> Calculate the euclidean distance
    for (int nObj = 0; nObj < solutionI.numberOfObjectives();nObj++){
      diff = solutionI.getObjective(nObj) - solutionJ.getObjective(nObj);
      distance += Math.pow(diff,2.0);           
    } // for   
        
    //Return the euclidean distance
    return Math.sqrt(distance);
  } // distanceBetweenObjectives.
           
     */
  internal static void crowdingDistanceAssignment (SolutionSet solutionSet, int nObjs)
  {
    // <pex>
    if (solutionSet == (SolutionSet)null)
      throw new ArgumentNullException("solutionSet");
    if (solutionSet[0] == (Solution)null)
      throw new ArgumentNullException("solutionSet");
    // </pex>
    int size = solutionSet.size ();
    
    if (size == 0)
      return;
    
    if (size == 1) {
      solutionSet[0].crowdingDistance_ = Double.MaxValue ;
      //solutionSet.get(0).setCrowdingDistance(Double.POSITIVE_INFINITY);
      return;
    } // if
        
    if (size == 2) {
      solutionSet[0].crowdingDistance_ = Double.MaxValue;
      solutionSet[1].crowdingDistance_ = Double.MaxValue;
      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 ;
        
    double objetiveMaxn;
    double objetiveMinn;
    double distance;
                
    for (int i = 0; i<nObjs; i++) {          
      // Sort the population by Obj n
      IComparer comp = new ObjectiveComparator(i) ;
      front.solutionList_.Sort(comp.Compare) ;
      //front.sort(new ObjectiveComparator(i));

        objetiveMinn = front[0].objective_[i] ;
      objetiveMaxn = front[front.size()-1].objective_[i] ;
      
      //Set de crowding distance            
      front[0].crowdingDistance_ = Double.MaxValue ;
      front[size -1].crowdingDistance_ = Double.MaxValue ;
      
      for (int j = 1; j < size-1; j++) {
        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        
  } // crowdingDistanceAssing
コード例 #12
0
        // NSGAII
        public override SolutionSet execute()
        {
            int populationSize;
            int maxEvaluations;
            int evaluations;

            SolutionSet population;
            SolutionSet offspringPopulation;
            SolutionSet union;
            SolutionSet allTest = new SolutionSet();

            Operator mutationOperator;
            Operator crossoverOperator;
            Operator selectionOperator;

            populationSize = (int)inputParameters_["populationSize"];
            maxEvaluations = (int)inputParameters_["maxEvaluations"];

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

            //System.Console.WriteLine("Solves Name:" + problem_.problemName_);
            //System.Console.WriteLine("Pop size : " + populationSize);
            //System.Console.WriteLine("Max Evals: " + maxEvaluations);

            // Reading operators
            mutationOperator  = operators["mutation"];
            crossoverOperator = operators["crossover"];
            selectionOperator = operators["selection"];

            //System.Console.WriteLine("Crossover parameters: " + crossoverOperator);
            //System.Console.WriteLine("Mutation parameters: " + mutationOperator);

            // Creating the initial solutionSet
            Solution newSolution;

            for (int i = 0; i < populationSize; i++)
            {
                newSolution = new Solution(problem_);
                //newSolution.variable_[0] = 1;
                problem_.evaluate(newSolution);
                //newSolution.objective_[0] = 10;
                //Mohammad
                //slnLst.Add(newSolution);
                //System.Console.WriteLine ("" + i + ": " + newSolution);
                //problem_.evaluateConstraints(newSolution);
                evaluations++;
                population.add(newSolution);
            }
            //for
            // Main loop
            while (evaluations < maxEvaluations)
            {
                // Creating the offSpring solutionSet

                offspringPopulation = new SolutionSet(populationSize);
                Solution[] parents = new Solution[2];

                for (int i = 0; i < (populationSize / 2); i++)
                {
                    if (evaluations < maxEvaluations)
                    {
                        //if ((evaluations % 1000) == 0)
                        //    Console.WriteLine("Evals: " + evaluations);
                        // selection
                        parents[0] = (Solution)selectionOperator.execute(population);
                        parents[1] = (Solution)selectionOperator.execute(population);

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

                        // mutation
                        mutationOperator.execute(offSpring[0]);
                        mutationOperator.execute(offSpring[1]);

                        //Environment.Exit(0);
                        // evaluation
                        problem_.evaluate(offSpring[0]);
                        problem_.evaluate(offSpring[1]);

                        offspringPopulation.@add(offSpring[0]);
                        offspringPopulation.add(offSpring[1]);

                        evaluations += 2;
                    }
                    // if
                }
                // for
                // Creating the solutionSet union of solutionSet and offSpring
                union = ((SolutionSet)population).union(offspringPopulation);

                //Mohammad
                allTest = ((SolutionSet)allTest).union(union);
                //System.Console.WriteLine ("Union size:" + union.size ());

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

                int         remain = populationSize;
                int         index  = 0;
                SolutionSet front  = null;

                //Distance distance = new Distance ();

                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[k]);
                    }
                    // for
                    //Decrement remain
                    remain = remain - front.size();

                    //Obtain the next front
                    index++;
                    if (remain > 0)
                    {
                        front = ranking.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, problem_.numberOfObjectives_);
                    IComparer comp = new CrowdingDistanceComparator();
                    front.solutionList_.Sort(comp.Compare);
                    for (int k = 0; k < remain; k++)
                    {
                        population.@add(front[k]);
                    }
                    // for
                    remain = 0;
                }
                // if
            }
            // while
            // Return as output parameter the required evaluations
            outputParameters_["evaluations"] = evaluations;

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

            //Console.WriteLine(slnLst[0].objective_.GetValue(0));

            //File.WriteAllLines("C:/text.txt",slnLst.ConvertAll(Convert.ToString));
            return(ranking2.getSubfront(0));
        }