123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231 |
- #ifndef SRC_JADE_H_
- #define SRC_JADE_H_
- ///
- /// @file jade.h
- /// @author Ladutenko Konstantin <kostyfisik at gmail (.) com>
- /// @date Thu Aug 15 19:21:57 2013
- /// @copyright 2013 Ladutenko Konstantin
- /// @section LICENSE
- /// This file is part of JADE++.
- ///
- /// JADE++ is free software: you can redistribute it and/or modify
- /// it under the terms of the GNU General Public License as published by
- /// the Free Software Foundation, either version 3 of the License, or
- /// (at your option) any later version.
- ///
- /// JADE++ is distributed in the hope that it will be useful,
- /// but WITHOUT ANY WARRANTY; without even the implied warranty of
- /// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- /// GNU General Public License for more details.
- ///
- /// You should have received a copy of the GNU General Public License
- /// along with JADE++. If not, see <http://www.gnu.org/licenses/>.
- /// @brief JADE++ is a free (GPLv3+) high performance implementation of
- /// adaptive differential evolution optimization algorithm from
- /// Jingqiao Zhang and Arthur C. Sanderson book 'Adaptive Differential
- /// Evolution. A Robust Approach to Multimodal Problem Optimization'
- /// Springer, 2009. Crossover rate was patched according to PMCRADE
- /// approach supposed by Jie Li, Wujie Zhu, Mengjun Zhou, and Hua Wang
- /// in 'Power Mean Based Crossover Rate Adaptive Differential
- /// Evolution' in H. Deng et al. (Eds.): AICI 2011, Part II, LNAI
- /// 7003, pp. 34–41, 2011
- #include <random>
- #include <utility>
- #include <list>
- #include <stdexcept>
- #include <string>
- #include <vector>
- #include "sph_bessel.h"
- namespace jade {
- /// @brief Population controlled by single MPI process.
- class SubPopulation {
- public:
- /// @brief Externaly defined fitness function, used by pointer.
- Real (*FitnessFunction)(std::vector<Real> x) = nullptr;
- Real (*FitnessFunction_p)(std::vector<Real>&, void*) = nullptr;
- /// @brief Class initialization.
- int Init(long total_population, long dimension); // NOLINT
- /// @brief Vizualize used random distributions (to do manual check).
- void SetFeed(std::vector<std::vector<Real> > x_feed_vectors);
- void CheckRandom();
- /// @brief Find optimum value of fitness function.
- int RunOptimization();
- int RunOptimization(std::ostream&, void* param);
- /// @brief Set maximum number of generations used for optimization.
- void SetTotalGenerationsMax(long gen) {total_generations_max_ = gen;} // NOLINT
- /// @brief Select if to find global minimum or maximum of fitness function.
- void SetTargetToMinimum() {is_find_minimum_ = true;}
- void SetTargetToMaximum() {is_find_minimum_ = false;}
- /// @brief Set adaption parameters.
- int SetBestShareP(Real p);
- int SetAdapitonFrequencyC(Real c);
- /// @brief Set level of algorithm distribution.
- /// 0 - no distribution, each MPI process acts independantly.
- int SetDistributionLevel(int level);
- /// @brief Set same search bounds for all components of fitness
- /// function input vector.
- int SetAllBounds(Real lbound, Real ubound);
- void SetAllBoundsVectors(std::vector<Real> lbound, std::vector<Real> ubound);
- /// @brief Print Optimization parameters.
- int PrintParameters(std::string comment);
- /// @brief Print final result
- int PrintResult(std::string comment);
- std::vector<Real> GetFinalFitness();
- std::vector<Real> GetBest(Real *best_fitness);
- std::vector<Real> GetWorst(Real *worst_fitness);
- int ErrorStatus() {return error_status_;};
- void SwitchOffPMCRADE(){isPMCRADE_ = false;};
-
- private:
- bool isPMCRADE_ = true;
- bool isFeed_ = false;
- int CreateInitialPopulation();
- int PrintPopulation();
- int PrintPopulation(long, std::ostream&);
- int PrintEvaluated();
- int PrintSingleVector(std::vector<Real> x);
- int SortEvaluatedCurrent();
- /// @brief Apply fitness function to current population.
- int EvaluateCurrentVectors();
- int EvaluateCurrentVectors(void* param);
- /// @brief Generate crossover and mutation factors for current individual
- int SetCRiFi(long i);
- /// @name Main algorithm steps.
- // @{
- int Selection(std::vector<Real> crossover_u, long individual_index);
- int Selection(std::vector<Real> crossover_u, void* param, long individual_index);
- int ArchiveCleanUp();
- int Adaption();
- std::vector<Real> Mutation(long individual_index);
- std::vector<Real> Crossover(std::vector<Real> mutated_v,
- long individual_index);
- // @}
- /// @name Other algorithm steps.
- // @{
- std::vector<Real> GetXpBestCurrent();
- /// @brief Returns random vector from current population and
- /// vector`s index.
- std::vector<Real> GetXRandomCurrent(long *index,
- long forbidden_index);
- std::vector<Real> GetXRandomArchiveAndCurrent(
- unsigned long forbidden_index1, unsigned long forbidden_index2);
- // @}
- /// @name Population, individuals and algorithm .
- // @{
- /// @brief Search minimum or maximum of fitness function.
- bool is_find_minimum_ = true;
- /// @brief Maximum number of generations used for optimization.
- long total_generations_max_ = 0; // NOLINT
- /// @brief Total number of individuals in all subpopulations.
- long total_population_ = 0; // NOLINT
- /// @brief Number of individuals in subpopulation
- unsigned long subpopulation_ = 0; // NOLINT
- /* /// @brief All individuals are indexed. First and last index of */
- /* /// individuals in subpopulations. */
- /* long index_first_ = -1, index_last_ = -1; // NOLINT */
- /// @brief Dimension of the optimization task (number of variables
- /// to optimize).
- unsigned long dimension_ = 0; // NOLINT
- /// @brief Current generation of evalution process;
- long current_generation_ = -1; // NOLINT
- /// @brief Several feed vectors.
- std::vector<std::vector<Real> > x_feed_vectors_;
- /// @brief Current state vectors of all individuals in subpopulation.
- std::vector<std::vector<Real> > x_vectors_current_;
- /// @brief State vectors of all individuals in subpopulation in
- /// new generation.
- std::vector<std::vector<Real> > x_vectors_next_generation_;
- /// @brief Sometimes sorted list of evaluated fitness function.
- std::list<std::pair<Real, long> > // NOLINT
- evaluated_fitness_for_current_vectors_;
- /// @brief Sometimes sorted list of evaluated fitness function for
- /// next generation.
- std::list<std::pair<Real, long> > // NOLINT
- evaluated_fitness_for_next_generation_;
- /// @brief Archived best solutions (state vactors)
- std::list<std::vector<Real> > archived_best_A_;
- std::list<std::vector<Real> > to_be_archived_best_A_;
- /// @brief Sometimes sorted list of evaluated fitness function for
- /// best vectors.
- std::list<std::pair<Real, long> > // NOLINT
- evaluated_fitness_for_archived_best_;
- /// @brief Low and upper bounds for x vectors.
- std::vector<Real> x_lbound_;
- std::vector<Real> x_ubound_;
- /// @brief JADE+ adaption parameter for mutation factor
- Real adaptor_mutation_mu_F_ = 0.5;
- /// @brief JADE+ adaption parameter for crossover probability
- Real adaptor_crossover_mu_CR_ = 0.5;
- /// @brief Individual mutation and crossover parameters for each individual.
- std::vector<Real> mutation_F_, crossover_CR_;
- std::list<Real> successful_mutation_parameters_S_F_;
- std::list<Real> successful_crossover_parameters_S_CR_;
- /// @brief Share of all individuals in current population to be
- /// the best, recomended value range 0.05-0.2
- //const Real best_share_p_ = 0.12;
- Real best_share_p_ = 0.05;
- /* //debug Change it back before production!!!! */
- /* const Real best_share_p_ = 0.3; */
- /// @brief 1/c - number of generations accounted for parameter
- /// adaption, recommended value 5 to 20 generation;
- //const Real adaptation_frequency_c_ = 1.0/20.0;
- Real adaptation_frequency_c_ = 0.1;
- // @}
- /// @name Random generation
- /// Names are in notation from Jingqiao Zhang and Arthur C. Sanderson book.
- // @{
- /// @todo Select random generator enginge for best results in DE!
- //std::mt19937_64 generator_;
- std::ranlux48 generator_;
- /// @brief randn(μ, &sigma^2; ) denotes a random value from a normal
- /// distribution of mean μ and variance &sigma^2;
- Real randn(Real mean, Real stddev);
- /// @brief randc(μ, δ ) a random value from a Cauchy distribution
- /// with location and scale parameters μ and δ
- Real randc(Real location, Real scale);
- /// @brief randint(1, D) is an integer randomly chosen from 1 to D
- long randint(long lbound, long ubound); // NOLINT
- /// @brief rand(a, b) is an uniform random number chosen from a to b
- Real rand(Real lbound, Real ubound); // NOLINT
- // @}
- /// @name MPI section
- // @{
- int process_rank_;
- int number_of_processes_;
- int AllGatherVectorDouble(std::vector<Real> to_send);
- std::vector<Real> recieve_Real_;
- int AllGatherVectorLong(std::vector<long> to_send);
- std::vector<long> recieve_long_;
-
- // @}
- /// @brief Subpopulation status. If non-zero than some error has appeared.
- int error_status_ = 0; // @todo move to exceptions!
- int distribution_level_ = 0;
- }; // end of class SubPopulation
- // ********************************************************************** //
- // ********************************************************************** //
- // ********************************************************************** //
- /// @brief Error codes
- ///
- /// Error codes used with jade
- /// @todo move to exceptions!
- enum Errors {
- /// no error
- kDone = 0,
- /// Unspecified (pending to be described).
- kError
- }; // end of enum Errors
- // ********************************************************************** //
- // ********************************************************************** //
- // ********************************************************************** //
- const int kOutput = 0; /// Process rank to do output with printf
- template<class T> inline T pow2(const T value) {return value*value;}
- } // end of namespace jade
- #endif // SRC_JADE_H_
|