jade.h 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  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. namespace jade {
  39. // ********************************************************************** //
  40. // ********************************************************************** //
  41. // ********************************************************************** //
  42. /// @brief Population controlled by single MPI process.
  43. class SubPopulation {
  44. public:
  45. /// @brief Externaly defined fitness function, used by pointer.
  46. double (*FitnessFunction)(const std::vector<double> &x) = nullptr;
  47. /// @brief Class initialization.
  48. int Init(long total_population, long dimension); // NOLINT
  49. /// @brief Vizualize used random distributions (to do manual check).
  50. void SetFeed(std::vector<std::vector<double> > x_feed_vectors);
  51. void CheckRandom();
  52. /// @brief Find optimum value of fitness function.
  53. int RunOptimization();
  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(double p);
  61. int SetAdapitonFrequencyC(double 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(double lbound, double ubound);
  68. void SetAllBoundsVectors
  69. (std::vector<double> lbound, std::vector<double> ubound);
  70. /// @brief Print Optimization parameters.
  71. int PrintParameters(std::string comment);
  72. /// @brief Print final result
  73. int PrintResult(std::string comment);
  74. std::vector<double> GetFinalFitness();
  75. std::vector<double> GetBest(double *best_fitness);
  76. std::vector<double> GetWorst(double *worst_fitness);
  77. int ErrorStatus() {return error_status_;};
  78. void SwitchOffPMCRADE(){isPMCRADE_ = false;};
  79. private:
  80. bool isPMCRADE_ = true;
  81. bool isFeed_ = false;
  82. int CreateInitialPopulation();
  83. int PrintPopulation();
  84. int PrintEvaluated();
  85. int PrintSingleVector(std::vector<double> x);
  86. int SortEvaluatedCurrent();
  87. /// @brief Apply fitness function to current population.
  88. int EvaluateCurrentVectors();
  89. /// @brief Generate crossover and mutation factors for current individual
  90. int SetCRiFi(long i);
  91. /// @name Main algorithm steps.
  92. // @{
  93. int Selection(std::vector<double> crossover_u,
  94. long individual_index);
  95. int ArchiveCleanUp();
  96. int Adaption();
  97. std::vector<double> Mutation(long individual_index);
  98. std::vector<double> Crossover(std::vector<double> mutated_v,
  99. long individual_index);
  100. // @}
  101. /// @name Other algorithm steps.
  102. // @{
  103. std::vector<double> GetXpBestCurrent();
  104. /// @brief Returns random vector from current population and
  105. /// vector`s index.
  106. std::vector<double> GetXRandomCurrent(long *index,
  107. long forbidden_index);
  108. std::vector<double> GetXRandomArchiveAndCurrent(
  109. long forbidden_index1, long forbidden_index2);
  110. // @}
  111. /// @name Population, individuals and algorithm .
  112. // @{
  113. /// @brief Search minimum or maximum of fitness function.
  114. bool is_find_minimum_ = true;
  115. /// @brief Maximum number of generations used for optimization.
  116. long total_generations_max_ = 0; // NOLINT
  117. /// @brief Total number of individuals in all subpopulations.
  118. long total_population_ = 0; // NOLINT
  119. /// @brief Number of individuals in subpopulation
  120. long subpopulation_ = 0; // NOLINT
  121. /* /// @brief All individuals are indexed. First and last index of */
  122. /* /// individuals in subpopulations. */
  123. /* long index_first_ = -1, index_last_ = -1; // NOLINT */
  124. /// @brief Dimension of the optimization task (number of variables
  125. /// to optimize).
  126. long dimension_ = -1; // NOLINT
  127. /// @brief Current generation of evalution process;
  128. long current_generation_ = -1; // NOLINT
  129. /// @brief Several feed vectors.
  130. std::vector<std::vector<double> > x_feed_vectors_;
  131. /// @brief Current state vectors of all individuals in subpopulation.
  132. std::vector<std::vector<double> > x_vectors_current_;
  133. /// @brief State vectors of all individuals in subpopulation in
  134. /// new generation.
  135. std::vector<std::vector<double> > x_vectors_next_generation_;
  136. /// @brief Sometimes sorted list of evaluated fitness function.
  137. std::list<std::pair<double, long> > // NOLINT
  138. evaluated_fitness_for_current_vectors_;
  139. /// @brief Sometimes sorted list of evaluated fitness function for
  140. /// next generation.
  141. std::list<std::pair<double, long> > // NOLINT
  142. evaluated_fitness_for_next_generation_;
  143. /// @brief Archived best solutions (state vactors)
  144. std::list<std::vector<double> > archived_best_A_;
  145. std::list<std::vector<double> > to_be_archived_best_A_;
  146. /// @brief Sometimes sorted list of evaluated fitness function for
  147. /// best vectors.
  148. std::list<std::pair<double, long> > // NOLINT
  149. evaluated_fitness_for_archived_best_;
  150. /// @brief Low and upper bounds for x vectors.
  151. std::vector<double> x_lbound_;
  152. std::vector<double> x_ubound_;
  153. /// @brief JADE+ adaption parameter for mutation factor
  154. double adaptor_mutation_mu_F_ = 0.5;
  155. /// @brief JADE+ adaption parameter for crossover probability
  156. double adaptor_crossover_mu_CR_ = 0.5;
  157. /// @brief Individual mutation and crossover parameters for each individual.
  158. std::vector<double> mutation_F_, crossover_CR_;
  159. std::list<double> successful_mutation_parameters_S_F_;
  160. std::list<double> successful_crossover_parameters_S_CR_;
  161. /// @brief Share of all individuals in current population to be
  162. /// the best, recomended value range 0.05-0.2
  163. //const double best_share_p_ = 0.12;
  164. double best_share_p_ = 0.05;
  165. /* //debug Change it back before production!!!! */
  166. /* const double best_share_p_ = 0.3; */
  167. /// @brief 1/c - number of generations accounted for parameter
  168. /// adaption, recommended value 5 to 20 generation;
  169. //const double adaptation_frequency_c_ = 1.0/20.0;
  170. double adaptation_frequency_c_ = 0.1;
  171. // @}
  172. /// @name Random generation
  173. /// Names are in notation from Jingqiao Zhang and Arthur C. Sanderson book.
  174. // @{
  175. /// @todo Select random generator enginge for best results in DE!
  176. std::mt19937_64 generator_;
  177. //std::ranlux48 generator_;
  178. /// @brief randn(&mu;, &sigma^2; ) denotes a random value from a normal
  179. /// distribution of mean &mu; and variance &sigma^2;
  180. double randn(double mean, double stddev);
  181. /// @brief randc(&mu;, &delta; ) a random value from a Cauchy distribution
  182. /// with location and scale parameters &mu; and &delta;
  183. double randc(double location, double scale);
  184. /// @brief randint(1, D) is an integer randomly chosen from 1 to D
  185. long randint(long lbound, long ubound); // NOLINT
  186. /// @brief rand(a, b) is an uniform random number chosen from a to b
  187. double rand(double lbound, double ubound); // NOLINT
  188. // @}
  189. /// @name MPI section
  190. // @{
  191. int process_rank_;
  192. int number_of_processes_;
  193. int AllGatherVectorDouble(std::vector<double> to_send);
  194. std::vector<double> recieve_double_;
  195. int AllGatherVectorLong(std::vector<long> to_send);
  196. std::vector<long> recieve_long_;
  197. // @}
  198. /// @brief Subpopulation status. If non-zero than some error has appeared.
  199. int error_status_ = 0; // @todo move to exceptions!
  200. int distribution_level_ = 0;
  201. }; // end of class SubPopulation
  202. // ********************************************************************** //
  203. // ********************************************************************** //
  204. // ********************************************************************** //
  205. /// @brief Error codes
  206. ///
  207. /// Error codes used with jade
  208. /// @todo move to exceptions!
  209. enum Errors {
  210. /// no error
  211. kDone = 0,
  212. /// Unspecified (pending to be described).
  213. kError
  214. }; // end of enum Errors
  215. // ********************************************************************** //
  216. // ********************************************************************** //
  217. // ********************************************************************** //
  218. const int kOutput = 0; /// Process rank to do output with printf
  219. template<class T> inline T pow2(const T value) {return value*value;}
  220. } // end of namespace jade
  221. #endif // SRC_JADE_H_