int IComparer.Compare (object x, object y) { if (x == null) return 1; else if (y == null) return -1; int dominate1; // dominate1 indicates if some objective of solution1 // dominates the same objective in solution2. dominate2 int dominate2; // is the complementary of dominate1. dominate1 = 0; dominate2 = 0; Solution solution1 = (Solution)x; Solution solution2 = (Solution)y; int flag; double value1, value2; for (int i = 0; i < solution1.numberOfObjectives_; i++) { IComparer objectiveComparator = new ObjectiveComparator (i); flag = objectiveComparator.Compare (solution1, solution2); value1 = solution1.objective_[i]; value2 = solution2.objective_[i]; if (value1 < value2) { flag = -1; } else if (value1 > value2) { flag = 1; } else { flag = 0; } if (flag == -1) { dominate1 = 1; } if (flag == 1) { dominate2 = 1; } } if (dominate1 == 0 && dominate2 == 0) { return 0; //No one dominate the other } if (dominate1 == 1) { return -1; // solution1 dominate } else if (dominate2 == 1) { return 1; // solution2 dominate } return 2; }
// 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
// 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
int IComparer.Compare(object x, object y) { if (x == null) { return(1); } else if (y == null) { return(-1); } int dominate1; // dominate1 indicates if some objective of solution1 // dominates the same objective in solution2. dominate2 int dominate2; // is the complementary of dominate1. dominate1 = 0; dominate2 = 0; Solution solution1 = (Solution)x; Solution solution2 = (Solution)y; int flag; double value1, value2; for (int i = 0; i < solution1.numberOfObjectives_; i++) { IComparer objectiveComparator = new ObjectiveComparator(i); flag = objectiveComparator.Compare(solution1, solution2); value1 = solution1.objective_[i]; value2 = solution2.objective_[i]; if (value1 < value2) { flag = -1; } else if (value1 > value2) { flag = 1; } else { flag = 0; } if (flag == -1) { dominate1 = 1; } if (flag == 1) { dominate2 = 1; } } if (dominate1 == 0 && dominate2 == 0) { return(0); //No one dominate the other } if (dominate1 == 1) { return(-1); // solution1 dominate } else if (dominate2 == 1) { return(1); // solution2 dominate } return(2); }