/src/jmetal/GDE3.cpp

https://github.com/wkoder/mocde · C++ · 216 lines · 116 code · 44 blank · 56 comment · 18 complexity · a1a0dd76cf7d17115df422a41fee9b63 MD5 · raw file

  1. #include "../config.h"
  2. #ifdef JMETAL
  3. // GDE3.cpp
  4. //
  5. // Author:
  6. // Esteban López <esteban@lcc.uma.es>
  7. //
  8. // Copyright (c) 2011 Antonio J. Nebro, Juan J. Durillo
  9. //
  10. // This program is free software: you can redistribute it and/or modify
  11. // it under the terms of the GNU Lesser General Public License as published by
  12. // the Free Software Foundation, either version 3 of the License, or
  13. // (at your option) any later version.
  14. //
  15. // This program is distributed in the hope that it will be useful,
  16. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. // GNU Lesser General Public License for more details.
  19. //
  20. // You should have received a copy of the GNU Lesser General Public License
  21. // along with this program. If not, see <http://www.gnu.org/licenses/>.
  22. #include "GDE3.h"
  23. /*
  24. * This class implements the GDE3 algorithm.
  25. */
  26. /**
  27. * Constructor
  28. * @param problem Problem to solve
  29. */
  30. GDE3::GDE3(Problem *problem) : Algorithm(problem) {
  31. } // GDE3
  32. /*
  33. * Runs the GDE3 algorithm.
  34. * @return a <code>SolutionSet</code> that is a set of non dominated solutions
  35. * as a result of the algorithm execution
  36. */
  37. SolutionSet * GDE3::execute() {
  38. int populationSize;
  39. int maxIterations;
  40. int evaluations;
  41. int iterations;
  42. SolutionSet * population;
  43. SolutionSet * offspringPopulation;
  44. Distance * distance;
  45. Comparator * dominance;
  46. Operator * crossoverOperator;
  47. Operator * selectionOperator;
  48. distance = new Distance();
  49. dominance = new DominanceComparator();
  50. Solution ** parent;
  51. //Read the parameters
  52. populationSize = *(int *) getInputParameter("populationSize");
  53. maxIterations = *(int *) getInputParameter("maxIterations");
  54. // cout << "GDE3: populationSize = " << populationSize << endl;
  55. // cout << "GDE3: maxIterations = " << maxIterations << endl;
  56. //Initialize the variables
  57. population = new SolutionSet(populationSize);
  58. evaluations = 0;
  59. iterations = 0;
  60. // cout << "GDE3: Poblacion inicializada con maxsize = " << population->getMaxSize() << endl;
  61. // cout << "GDE3: Poblacion inicializada con size = " << population->size() << endl;
  62. //Read the operators
  63. crossoverOperator = operators_["crossover"];
  64. selectionOperator = operators_["selection"];
  65. // cout << "GDE3: Problema: " << problem_->getName() << endl;
  66. // Create the initial solutionSet
  67. Solution * newSolution;
  68. for (int i = 0; i < populationSize; i++) {
  69. newSolution = new Solution(problem_);
  70. problem_->evaluate(newSolution);
  71. problem_->evaluateConstraints(newSolution);
  72. evaluations++;
  73. population->add(newSolution);
  74. } //for
  75. // cout << "GDE3: Comienzan las generaciones." << endl;
  76. // Generations ...
  77. while (iterations < maxIterations) {
  78. // Create the offSpring solutionSet
  79. offspringPopulation = new SolutionSet(populationSize * 2);
  80. for (int i = 0; i < populationSize; i++){
  81. // Obtain parents. Two parameters are required: the population and the
  82. // index of the current individual
  83. void ** object1 = new void*[2];
  84. object1[0] = population;
  85. object1[1] = &i;
  86. parent = (Solution **) (selectionOperator->execute(object1));
  87. delete[] object1;
  88. Solution * child ;
  89. // Crossover. Two parameters are required: the current individual and the
  90. // array of parents
  91. void ** object2 = new void*[2];
  92. object2[0] = population->get(i);
  93. object2[1] = parent;
  94. child = (Solution *) (crossoverOperator->execute(object2));
  95. delete[] object2;
  96. delete[] parent;
  97. problem_->evaluate(child) ;
  98. problem_->evaluateConstraints(child);
  99. evaluations++ ;
  100. // Dominance test
  101. int result ;
  102. result = dominance->compare(population->get(i), child) ;
  103. if (result == -1) { // Solution i dominates child
  104. offspringPopulation->add(new Solution(population->get(i)));
  105. delete child;
  106. } // if
  107. else if (result == 1) { // child dominates
  108. offspringPopulation->add(child) ;
  109. } // else if
  110. else { // the two solutions are non-dominated
  111. offspringPopulation->add(child) ;
  112. offspringPopulation->add(new Solution(population->get(i)));
  113. } // else
  114. } // for
  115. // Ranking the offspring population
  116. Ranking * ranking = new Ranking(offspringPopulation);
  117. int remain = populationSize;
  118. int index = 0;
  119. SolutionSet * front = NULL;
  120. for (int i = 0; i < populationSize; i++) {
  121. delete population->get(i);
  122. }
  123. population->clear();
  124. // Obtain the next front
  125. front = ranking->getSubfront(index);
  126. while ((remain > 0) && (remain >= front->size())){
  127. //Assign crowding distance to individuals
  128. distance->crowdingDistanceAssignment(front,problem_->getNumberOfObjectives());
  129. //Add the individuals of this front
  130. for (int k = 0; k < front->size(); k++ ) {
  131. population->add(new Solution(front->get(k)));
  132. } // for
  133. //Decrement remain
  134. remain = remain - front->size();
  135. //Obtain the next front
  136. index++;
  137. if (remain > 0) {
  138. front = ranking->getSubfront(index);
  139. } // if
  140. } // while
  141. // remain is less than front(index).size, insert only the best one
  142. if (remain > 0) { // front contains individuals to insert
  143. while (front->size() > remain) {
  144. distance->crowdingDistanceAssignment(front,problem_->getNumberOfObjectives());
  145. Comparator * crowdingComparator = new CrowdingComparator();
  146. int indexWorst = front->indexWorst(crowdingComparator);
  147. delete crowdingComparator;
  148. delete front->get(indexWorst);
  149. front->remove(indexWorst);
  150. }
  151. for (int k = 0; k < front->size(); k++) {
  152. population->add(new Solution(front->get(k)));
  153. }
  154. remain = 0;
  155. } // if
  156. delete ranking;
  157. delete offspringPopulation;
  158. iterations ++ ;
  159. } // while
  160. delete dominance;
  161. delete distance;
  162. // Return the first non-dominated front
  163. Ranking * ranking = new Ranking(population);
  164. SolutionSet * result = new SolutionSet(ranking->getSubfront(0)->size());
  165. for (int i=0;i<ranking->getSubfront(0)->size();i++) {
  166. result->add(new Solution(ranking->getSubfront(0)->get(i)));
  167. }
  168. delete ranking;
  169. delete population;
  170. return result;
  171. } // execute
  172. #endif