コード例 #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 プロジェクト: xiebinhqy/Optimo
        //changes the population to list of list of doubles
        private static List<List<double>> PopToSolutionList(SolutionSet population)
        {
            int numVar;
            int numObj;
            if (population.size() > 0)
            {
                numVar = population[0].numberOfVariables_;
                numObj = population[0].numberOfObjectives_;
            }
            else
            {
                throw new Exception("Population siza is 0 and it cannot be changed to list of list of doubles");
            }

            List<List<double>> SolutionsList = new List<List<double>>();
            for (int i = 0; i < numVar; i++)
            {
                List<double> SolutionList = new List<double>();
                for (int j = 0; j < population.size(); j++)
                {
                    SolutionList.Add(population[j].variable_[i].value_);
                }
                SolutionsList.Add(SolutionList);
            }
            for (int i = 0; i < numObj; i++)
            {
                List<double> SolutionList = new List<double>();
                for (int j = 0; j < population.size(); j++)
                {
                    SolutionList.Add(population[j].objective_[i]);
                }
                SolutionsList.Add(SolutionList);
            }
            return SolutionsList;
        }
コード例 #3
0
        public override object execute(object obj)
        {
            // <pex>
            if (obj == (object)null)
            {
                throw new ArgumentNullException("obj");
            }
            if (obj != (object)null && !(obj is SolutionSet))
            {
                throw new ArgumentException("complex reason", "obj");
            }
            // </pex>
            //System.Console.WriteLine ("Creado un operador de seleccion del peor individuo \n");

            SolutionSet solutionSet = (SolutionSet)obj;

            if (solutionSet.size() == 0)
            {
                return(null);
            }

            int worstSolution;

            worstSolution = 0;

            for (int i = 1; i < solutionSet.size(); i++)
            {
                if (comparator_.Compare(solutionSet[i], solutionSet[worstSolution]) > 0)
                {
                    worstSolution = i;
                }
            }
            // for
            return(worstSolution);
        }
コード例 #4
0
        public override object execute(object obj)
        {
            // <pex>
            if (obj == (object)null)
            {
                throw new ArgumentNullException("obj");
            }
            if (obj != (object)null && !(obj is SolutionSet))
            {
                throw new ArgumentException("complex reason", "obj");
            }
            // </pex>

            SolutionSet solutionSet = (SolutionSet)obj;
            Solution    solution1;
            Solution    solution2;


            ///// OJO, FALTA CONTROLAR SI EL SOLUTION ESTA VACIO O TIENE UN SOLO ELEMENTO
            int sol1 = PseudoRandom.Instance().Next(0, solutionSet.size() - 1);
            int sol2 = PseudoRandom.Instance().Next(0, solutionSet.size() - 1);
            int sol3 = PseudoRandom.Instance().Next(0, solutionSet.size() - 1);

            solution1 = solutionSet[sol1];
            solution2 = solutionSet[sol2];

            while (sol1 == sol2)
            {
                sol2      = PseudoRandom.Instance().Next(0, solutionSet.size() - 1);
                solution2 = solutionSet[sol2];
            }

            int result = comparator_.Compare(solution1, solution2);

            if (result == -1)
            {
                return(solution1);
            }
            else if (result == 1)
            {
                return(solution2);
            }
            else
            {
                if (PseudoRandom.Instance().NextDouble() < 0.5)
                {
                    return(solution1);
                }
                else
                {
                    return(solution2);
                }
            }
        }
コード例 #5
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);
        }
コード例 #6
0
ファイル: Optimo.cs プロジェクト: redinkinc/Optimo
        //changes the population to list of list of doubles
        private static List <List <double> > PopToSolutionList(SolutionSet population)
        {
            int numVar;
            int numObj;

            if (population.size() > 0)
            {
                numVar = population[0].numberOfVariables_;
                numObj = population[0].numberOfObjectives_;
            }
            else
            {
                throw new Exception("Population siza is 0 and it cannot be changed to list of list of doubles");
            }

            List <List <double> > SolutionsList = new List <List <double> >();

            for (int i = 0; i < numVar; i++)
            {
                List <double> SolutionList = new List <double>();
                for (int j = 0; j < population.size(); j++)
                {
                    SolutionList.Add(population[j].variable_[i].value_);
                }
                SolutionsList.Add(SolutionList);
            }
            for (int i = 0; i < numObj; i++)
            {
                List <double> SolutionList = new List <double>();
                for (int j = 0; j < population.size(); j++)
                {
                    SolutionList.Add(population[j].objective_[i]);
                }
                SolutionsList.Add(SolutionList);
            }
            return(SolutionsList);
        }
コード例 #7
0
ファイル: Ranking.cs プロジェクト: DailyActie/MATH_OPT-Optimo
        // getSubFront


        /// <summary>
        /// Prints the ranking into a string
        /// </summary>
        /// <returns>
        /// A <see cref="System.String"/>
        /// </returns>
        public override string ToString()
        {
            string str = "POPULATION TO RANK (" + solutionSet_.size() + ")\n";

            for (int i = 0; i < solutionSet_.size(); i++)
            {
                str += "" + i + ": " + solutionSet_[i] + "\n";
            }

            int l = ranking_.GetLength(0);

            str += "Number of ranks: " + l + "\n";

            for (int rank = 0; rank < l; rank++)
            {
                str += "-- Rank: " + rank + "\n";
                for (int sol = 0; sol < ranking_[rank].size(); sol++)
                {
                    str += ranking_[rank][sol] + "\n";
                }
            }

            return(str);
        }
コード例 #8
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));
        }
コード例 #9
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
コード例 #10
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;
        }
コード例 #11
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;
 }
コード例 #12
0
ファイル: Ranking.cs プロジェクト: xiebinhqy/Optimo
 public Ranking (SolutionSet solutionSet)
 {
   // <pex>
   if (solutionSet == (SolutionSet)null)
     throw new ArgumentNullException("solutionSet");
   // </pex>
   solutionSet_ = solutionSet;
   
   // dominateMe[i] contains the number of solutions dominating i
   int[] dominateMe = new int[solutionSet_.size ()];
   
   // iDominate[k] contains the list of solutions dominated by k
   List<int>[] iDominate = new List<int>[solutionSet_.size ()];
   
   // front[i] contains the list of individuals belonging to the front i
   //List<int>[] front = new List<int>[solutionSet_.size () + 1];
   
   List<List<int>> front2 = new List<List<int>> ();
   
   // flagDominate is an auxiliar variable
   int flagDominate;
   
   // Initialize the fronts
   //for (int i = 0; i < front.Length; i++)
   //front[i] = new List<int> ();
   
   front2.Add (new List<int> ());
   
   // Fast non dominated sorting algorithm
   for (int p = 0; p < solutionSet_.size (); p++) {
     // Initialice the list of individuals that i dominate and the number
     // of individuals that dominate me
     iDominate[p] = new List<int> ();
     dominateMe[p] = 0;
     // For all q individuals , calculate if p dominates q or vice versa
     for (int q = 0; q < solutionSet_.size (); q++) {
       //flagDominate =constraint_.compare(solutionSet.get(p),solutionSet.get(q));
       //if (flagDominate == 0) {
       flagDominate = dominance_.Compare (solutionSet[p], solutionSet[q]);
       //}
       
       if (flagDominate == -1) {
         iDominate[p].Add (q);
       } else if (flagDominate == 1) {
         dominateMe[p]++;
       }
     }
     
     // If nobody dominates p, p belongs to the first front
     if (dominateMe[p] == 0) {
       //front[0].Add (p);
       front2[0].Add (p);
       solutionSet[p].rank_ = 0;
     }
   }
   
   //Obtain the rest of fronts
   int currentRank = 0;
   //Iterator<Integer> it1, it2 ; // Iterators
   //while (front[currentRank].Capacity != 0) {
   
   while (front2[currentRank].Count != 0) {
     currentRank++;
     front2.Add (new List<int> ());
     //front[currentRank].Clear ();
     //foreach (int p in front[currentRank - 1]) {
     foreach (int p in front2[currentRank - 1]) {
       foreach (int q in iDominate[p]) {
         dominateMe[q]--;
         if (dominateMe[q] == 0) {
           solutionSet_[q].rank_ = currentRank;
           //  front[currentRank].Add (q);
           front2[currentRank].Add (q);
         }
       }
     }
   }
   
   ranking_ = new SolutionSet[currentRank];
   for (int i = 0; i < currentRank; i++) {
     //ranking_[i] = new SolutionSet (front[i].Capacity);
     ranking_[i] = new SolutionSet (front2[i].Count);
     //foreach (int elem in front[i])
     foreach (int elem in front2[i]) {
       ranking_[i].add (solutionSet_[elem]);
     }
     // foreach
   }
   // for
   
 }
コード例 #13
0
ファイル: Optimo.cs プロジェクト: redinkinc/Optimo
        /// <summary>
        /// Sorts the combination of parents and child population set based on Pareto Fron Ranking.
        /// </summary>
        /// <param name="population">Parent solution set.</param>
        /// <param name="offspringPop">Offspring solution set.</param>
        /// <param name="lowerLimits">List of lower limits.</param>
        /// <param name="upperLimits">List of upper limits.</param>
        /// <returns></returns>
        public static List <List <double> > Sorting(List <List <double> > populationList, List <List <double> > offspringList, List <int> lowerLimits, List <int> upperLimits)
        {
            int numVar = lowerLimits.Count;
            int numObj = populationList.Count - numVar;

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

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

            // Creating the solutionSet union of solutionSet and offSpring
            SolutionSet union = ((SolutionSet)population).union(offspringPop);

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

            int         remain = population.size();
            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, algorithm.problem_.numberOfObjectives_);
                //Add the individuals of this front
                for (int k = 0; k < front.size(); k++)
                {
                    population.@add(front[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, algorithm.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;
            }

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

            return(PopToSolutionList(population));
        }
コード例 #14
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
コード例 #15
0
ファイル: Ranking.cs プロジェクト: DailyActie/MATH_OPT-Optimo
        public Ranking(SolutionSet solutionSet)
        {
            // <pex>
            if (solutionSet == (SolutionSet)null)
            {
                throw new ArgumentNullException("solutionSet");
            }
            // </pex>
            solutionSet_ = solutionSet;

            // dominateMe[i] contains the number of solutions dominating i
            int[] dominateMe = new int[solutionSet_.size()];

            // iDominate[k] contains the list of solutions dominated by k
            List <int>[] iDominate = new List <int> [solutionSet_.size()];

            // front[i] contains the list of individuals belonging to the front i
            //List<int>[] front = new List<int>[solutionSet_.size () + 1];

            List <List <int> > front2 = new List <List <int> > ();

            // flagDominate is an auxiliar variable
            int flagDominate;

            // Initialize the fronts
            //for (int i = 0; i < front.Length; i++)
            //front[i] = new List<int> ();

            front2.Add(new List <int> ());

            // Fast non dominated sorting algorithm
            for (int p = 0; p < solutionSet_.size(); p++)
            {
                // Initialice the list of individuals that i dominate and the number
                // of individuals that dominate me
                iDominate[p]  = new List <int> ();
                dominateMe[p] = 0;
                // For all q individuals , calculate if p dominates q or vice versa
                for (int q = 0; q < solutionSet_.size(); q++)
                {
                    //flagDominate =constraint_.compare(solutionSet.get(p),solutionSet.get(q));
                    //if (flagDominate == 0) {
                    flagDominate = dominance_.Compare(solutionSet[p], solutionSet[q]);
                    //}

                    if (flagDominate == -1)
                    {
                        iDominate[p].Add(q);
                    }
                    else if (flagDominate == 1)
                    {
                        dominateMe[p]++;
                    }
                }

                // If nobody dominates p, p belongs to the first front
                if (dominateMe[p] == 0)
                {
                    //front[0].Add (p);
                    front2[0].Add(p);
                    solutionSet[p].rank_ = 0;
                }
            }

            //Obtain the rest of fronts
            int currentRank = 0;

            //Iterator<Integer> it1, it2 ; // Iterators
            //while (front[currentRank].Capacity != 0) {

            while (front2[currentRank].Count != 0)
            {
                currentRank++;
                front2.Add(new List <int> ());
                //front[currentRank].Clear ();
                //foreach (int p in front[currentRank - 1]) {
                foreach (int p in front2[currentRank - 1])
                {
                    foreach (int q in iDominate[p])
                    {
                        dominateMe[q]--;
                        if (dominateMe[q] == 0)
                        {
                            solutionSet_[q].rank_ = currentRank;
                            //  front[currentRank].Add (q);
                            front2[currentRank].Add(q);
                        }
                    }
                }
            }

            ranking_ = new SolutionSet[currentRank];
            for (int i = 0; i < currentRank; i++)
            {
                //ranking_[i] = new SolutionSet (front[i].Capacity);
                ranking_[i] = new SolutionSet(front2[i].Count);
                //foreach (int elem in front[i])
                foreach (int elem in front2[i])
                {
                    ranking_[i].add(solutionSet_[elem]);
                }
                // foreach
            }
            // for
        }
コード例 #16
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));
        }