jade.h 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  1. #ifndef SRC_JADE_H_
  2. #define SRC_JADE_H_
  3. ///
  4. /// @file jade.h
  5. /// @author Ladutenko Konstantin <kostyfisik at gmail (.) com>
  6. /// @date Thu Aug 15 19:21:57 2013
  7. /// @copyright 2013 Ladutenko Konstantin
  8. /// @section LICENSE
  9. /// This file is part of JADE++.
  10. ///
  11. /// JADE++ is free software: you can redistribute it and/or modify
  12. /// it under the terms of the GNU General Public License as published by
  13. /// the Free Software Foundation, either version 3 of the License, or
  14. /// (at your option) any later version.
  15. ///
  16. /// JADE++ is distributed in the hope that it will be useful,
  17. /// but WITHOUT ANY WARRANTY; without even the implied warranty of
  18. /// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  19. /// GNU General Public License for more details.
  20. ///
  21. /// You should have received a copy of the GNU General Public License
  22. /// along with JADE++. If not, see <http://www.gnu.org/licenses/>.
  23. /// @brief JADE++ is a free (GPLv3+) high performance implementation of
  24. /// adaptive differential evolution optimization algorithm from
  25. /// Jingqiao Zhang and Arthur C. Sanderson book 'Adaptive Differential
  26. /// Evolution. A Robust Approach to Multimodal Problem Optimization'
  27. /// Springer, 2009. Crossover rate was patched according to PMCRADE
  28. /// approach supposed by Jie Li, Wujie Zhu, Mengjun Zhou, and Hua Wang
  29. /// in 'Power Mean Based Crossover Rate Adaptive Differential
  30. /// Evolution' in H. Deng et al. (Eds.): AICI 2011, Part II, LNAI
  31. /// 7003, pp. 34–41, 2011
  32. #include <random>
  33. #include <utility>
  34. #include <list>
  35. #include <stdexcept>
  36. #include <string>
  37. #include <vector>
  38. #include "sph_bessel.h"
  39. namespace jade {
  40. /// @brief Population controlled by single MPI process.
  41. class SubPopulation {
  42. public:
  43. /// @brief Externaly defined fitness function, used by pointer.
  44. Real (*FitnessFunction)(std::vector<Real> x) = nullptr;
  45. Real (*FitnessFunction_p)(std::vector<Real>&, void*) = nullptr;
  46. /// @brief Class initialization.
  47. int Init(long total_population, long dimension); // NOLINT
  48. /// @brief Vizualize used random distributions (to do manual check).
  49. void SetFeed(std::vector<std::vector<Real> > x_feed_vectors);
  50. void CheckRandom();
  51. /// @brief Find optimum value of fitness function.
  52. int RunOptimization();
  53. int RunOptimization(std::ostream&, void* param);
  54. /// @brief Set maximum number of generations used for optimization.
  55. void SetTotalGenerationsMax(long gen) {total_generations_max_ = gen;} // NOLINT
  56. /// @brief Select if to find global minimum or maximum of fitness function.
  57. void SetTargetToMinimum() {is_find_minimum_ = true;}
  58. void SetTargetToMaximum() {is_find_minimum_ = false;}
  59. /// @brief Set adaption parameters.
  60. int SetBestShareP(Real p);
  61. int SetAdapitonFrequencyC(Real c);
  62. /// @brief Set level of algorithm distribution.
  63. /// 0 - no distribution, each MPI process acts independantly.
  64. int SetDistributionLevel(int level);
  65. /// @brief Set same search bounds for all components of fitness
  66. /// function input vector.
  67. int SetAllBounds(Real lbound, Real ubound);
  68. void SetAllBoundsVectors(std::vector<Real> lbound, std::vector<Real> ubound);
  69. /// @brief Print Optimization parameters.
  70. int PrintParameters(std::string comment);
  71. /// @brief Print final result
  72. int PrintResult(std::string comment);
  73. std::vector<Real> GetFinalFitness();
  74. std::vector<Real> GetBest(Real *best_fitness);
  75. std::vector<Real> GetWorst(Real *worst_fitness);
  76. int ErrorStatus() {return error_status_;};
  77. void SwitchOffPMCRADE(){isPMCRADE_ = false;};
  78. private:
  79. bool isPMCRADE_ = true;
  80. bool isFeed_ = false;
  81. int CreateInitialPopulation();
  82. int PrintPopulation();
  83. int PrintPopulation(long, std::ostream&);
  84. int PrintEvaluated();
  85. int PrintSingleVector(std::vector<Real> x);
  86. int SortEvaluatedCurrent();
  87. /// @brief Apply fitness function to current population.
  88. int EvaluateCurrentVectors();
  89. int EvaluateCurrentVectors(void* param);
  90. /// @brief Generate crossover and mutation factors for current individual
  91. int SetCRiFi(long i);
  92. /// @name Main algorithm steps.
  93. // @{
  94. int Selection(std::vector<Real> crossover_u, long individual_index);
  95. int Selection(std::vector<Real> crossover_u, void* param, long individual_index);
  96. int ArchiveCleanUp();
  97. int Adaption();
  98. std::vector<Real> Mutation(long individual_index);
  99. std::vector<Real> Crossover(std::vector<Real> mutated_v,
  100. long individual_index);
  101. // @}
  102. /// @name Other algorithm steps.
  103. // @{
  104. std::vector<Real> GetXpBestCurrent();
  105. /// @brief Returns random vector from current population and
  106. /// vector`s index.
  107. std::vector<Real> GetXRandomCurrent(long *index,
  108. long forbidden_index);
  109. std::vector<Real> GetXRandomArchiveAndCurrent(
  110. unsigned long forbidden_index1, unsigned long forbidden_index2);
  111. // @}
  112. /// @name Population, individuals and algorithm .
  113. // @{
  114. /// @brief Search minimum or maximum of fitness function.
  115. bool is_find_minimum_ = true;
  116. /// @brief Maximum number of generations used for optimization.
  117. long total_generations_max_ = 0; // NOLINT
  118. /// @brief Total number of individuals in all subpopulations.
  119. long total_population_ = 0; // NOLINT
  120. /// @brief Number of individuals in subpopulation
  121. unsigned long subpopulation_ = 0; // NOLINT
  122. /* /// @brief All individuals are indexed. First and last index of */
  123. /* /// individuals in subpopulations. */
  124. /* long index_first_ = -1, index_last_ = -1; // NOLINT */
  125. /// @brief Dimension of the optimization task (number of variables
  126. /// to optimize).
  127. unsigned long dimension_ = 0; // NOLINT
  128. /// @brief Current generation of evalution process;
  129. long current_generation_ = -1; // NOLINT
  130. /// @brief Several feed vectors.
  131. std::vector<std::vector<Real> > x_feed_vectors_;
  132. /// @brief Current state vectors of all individuals in subpopulation.
  133. std::vector<std::vector<Real> > x_vectors_current_;
  134. /// @brief State vectors of all individuals in subpopulation in
  135. /// new generation.
  136. std::vector<std::vector<Real> > x_vectors_next_generation_;
  137. /// @brief Sometimes sorted list of evaluated fitness function.
  138. std::list<std::pair<Real, long> > // NOLINT
  139. evaluated_fitness_for_current_vectors_;
  140. /// @brief Sometimes sorted list of evaluated fitness function for
  141. /// next generation.
  142. std::list<std::pair<Real, long> > // NOLINT
  143. evaluated_fitness_for_next_generation_;
  144. /// @brief Archived best solutions (state vactors)
  145. std::list<std::vector<Real> > archived_best_A_;
  146. std::list<std::vector<Real> > to_be_archived_best_A_;
  147. /// @brief Sometimes sorted list of evaluated fitness function for
  148. /// best vectors.
  149. std::list<std::pair<Real, long> > // NOLINT
  150. evaluated_fitness_for_archived_best_;
  151. /// @brief Low and upper bounds for x vectors.
  152. std::vector<Real> x_lbound_;
  153. std::vector<Real> x_ubound_;
  154. /// @brief JADE+ adaption parameter for mutation factor
  155. Real adaptor_mutation_mu_F_ = 0.5;
  156. /// @brief JADE+ adaption parameter for crossover probability
  157. Real adaptor_crossover_mu_CR_ = 0.5;
  158. /// @brief Individual mutation and crossover parameters for each individual.
  159. std::vector<Real> mutation_F_, crossover_CR_;
  160. std::list<Real> successful_mutation_parameters_S_F_;
  161. std::list<Real> successful_crossover_parameters_S_CR_;
  162. /// @brief Share of all individuals in current population to be
  163. /// the best, recomended value range 0.05-0.2
  164. //const Real best_share_p_ = 0.12;
  165. Real best_share_p_ = 0.05;
  166. /* //debug Change it back before production!!!! */
  167. /* const Real best_share_p_ = 0.3; */
  168. /// @brief 1/c - number of generations accounted for parameter
  169. /// adaption, recommended value 5 to 20 generation;
  170. //const Real adaptation_frequency_c_ = 1.0/20.0;
  171. Real adaptation_frequency_c_ = 0.1;
  172. // @}
  173. /// @name Random generation
  174. /// Names are in notation from Jingqiao Zhang and Arthur C. Sanderson book.
  175. // @{
  176. /// @todo Select random generator enginge for best results in DE!
  177. //std::mt19937_64 generator_;
  178. std::ranlux48 generator_;
  179. /// @brief randn(&mu;, &sigma^2; ) denotes a random value from a normal
  180. /// distribution of mean &mu; and variance &sigma^2;
  181. Real randn(Real mean, Real stddev);
  182. /// @brief randc(&mu;, &delta; ) a random value from a Cauchy distribution
  183. /// with location and scale parameters &mu; and &delta;
  184. Real randc(Real location, Real scale);
  185. /// @brief randint(1, D) is an integer randomly chosen from 1 to D
  186. long randint(long lbound, long ubound); // NOLINT
  187. /// @brief rand(a, b) is an uniform random number chosen from a to b
  188. Real rand(Real lbound, Real ubound); // NOLINT
  189. // @}
  190. /// @name MPI section
  191. // @{
  192. int process_rank_;
  193. int number_of_processes_;
  194. int AllGatherVectorDouble(std::vector<Real> to_send);
  195. std::vector<Real> recieve_Real_;
  196. int AllGatherVectorLong(std::vector<long> to_send);
  197. std::vector<long> recieve_long_;
  198. // @}
  199. /// @brief Subpopulation status. If non-zero than some error has appeared.
  200. int error_status_ = 0; // @todo move to exceptions!
  201. int distribution_level_ = 0;
  202. }; // end of class SubPopulation
  203. // ********************************************************************** //
  204. // ********************************************************************** //
  205. // ********************************************************************** //
  206. /// @brief Error codes
  207. ///
  208. /// Error codes used with jade
  209. /// @todo move to exceptions!
  210. enum Errors {
  211. /// no error
  212. kDone = 0,
  213. /// Unspecified (pending to be described).
  214. kError
  215. }; // end of enum Errors
  216. // ********************************************************************** //
  217. // ********************************************************************** //
  218. // ********************************************************************** //
  219. const int kOutput = 0; /// Process rank to do output with printf
  220. template<class T> inline T pow2(const T value) {return value*value;}
  221. } // end of namespace jade
  222. #endif // SRC_JADE_H_