libROM  v1.0
Data-driven physical simulation library
DifferentialEvolution.h
1 
12 // Description: Implementation for the differential evolution algorithm.
13 // The implemented differential evolution algorithm is derived from
14 // Storn, R., Price, K. Differential Evolution – A Simple and
15 // Efficient Heuristic for global Optimization over Continuous
16 // Spaces. Journal of Global Optimization 11, 341–359 (1997).
17 // https://doi.org/10.1023/A:1008202821328
18 
19 #ifndef included_DifferentialEvolution_h
20 #define included_DifferentialEvolution_h
21 
22 #include <vector>
23 #include <functional>
24 #include <random>
25 #include <utility>
26 #include <memory>
27 #include <limits>
28 
29 namespace CAROM
30 {
35 {
36 public:
40  struct Constraints
41  {
49  Constraints(double lower = 0.0, double upper = 1.0,
50  bool isConstrained = false) :
51  lower(lower),
52  upper(upper),
54  {
55 
56  }
57 
61  bool Check(double candidate)
62  {
63  if (isConstrained)
64  {
65  if (candidate <= upper && candidate >= lower)
66  {
67  return true;
68  }
69  else
70  {
71  return false;
72  }
73  }
74  else
75  {
76  return true;
77  }
78  }
79 
83  double lower;
84 
88  double upper;
89 
94  };
95 
99  virtual double EvaluateCost(std::vector<double> inputs) const = 0;
100 
104  virtual unsigned int NumberOfParameters() const = 0;
105 
109  virtual std::vector<Constraints> GetConstraints() const = 0;
110 
114  virtual ~IOptimizable() {}
115 };
116 
121 {
122 public:
141  DifferentialEvolution(const IOptimizable& costFunction,
142  unsigned int populationSize,
143  double F = 0.8,
144  double CR = 0.9,
145  int randomSeed = 1,
146  bool shouldCheckConstraints = true,
147  std::function<void(const DifferentialEvolution&)> callback = nullptr,
148  std::function<bool(const DifferentialEvolution&)> terminationCondition =
149  nullptr);
150 
160  std::vector<double> Optimize(int min_iterations, int max_iterations,
161  double cost_tolerance,
162  bool verbose = true);
163 
164 private:
165 
169  bool CheckConstraints(std::vector<double> agent);
170 
174  void InitPopulation();
175 
179  void SelectionAndCrossing();
180 
184  std::vector<double> GetBestAgent() const;
185 
189  double GetBestCost() const;
190 
194  std::vector<std::pair<std::vector<double>, double>> GetPopulationWithCosts()
195  const;
196 
200  void PrintPopulation() const;
201 
205  const IOptimizable& m_cost;
206 
210  unsigned int m_populationSize;
211 
215  double m_F;
216 
220  double m_CR;
221 
225  unsigned int m_numberOfParameters;
226 
230  bool m_shouldCheckConstraints;
231 
235  std::function<void(const DifferentialEvolution&)> m_callback;
236 
240  std::function<bool(const DifferentialEvolution&)> m_terminationCondition;
241 
245  std::default_random_engine m_generator;
246 
250  std::vector<std::vector<double>> m_population;
251 
255  std::vector<double> m_minCostPerAgent;
256 
260  std::vector<IOptimizable::Constraints> m_constraints;
261 
265  int m_bestAgentIndex;
266 
270  double m_minCost;
271 
275  static constexpr double g_defaultLowerConstraint =
276  -std::numeric_limits<double>::infinity();
277  static constexpr double g_defaultUpperConstraint =
278  std::numeric_limits<double>::infinity();
279 
283  int d_rank;
284 };
285 }
286 
287 #endif
DifferentialEvolution(const IOptimizable &costFunction, unsigned int populationSize, double F=0.8, double CR=0.9, int randomSeed=1, bool shouldCheckConstraints=true, std::function< void(const DifferentialEvolution &)> callback=nullptr, std::function< bool(const DifferentialEvolution &)> terminationCondition=nullptr)
Constructor.
std::vector< double > Optimize(int min_iterations, int max_iterations, double cost_tolerance, bool verbose=true)
Constructor.
virtual ~IOptimizable()
Destructor.
virtual double EvaluateCost(std::vector< double > inputs) const =0
Evaluate the cost function with the current set of inputs.
virtual unsigned int NumberOfParameters() const =0
Return the number of parameters.
virtual std::vector< Constraints > GetConstraints() const =0
Return the list of constraints.
Constraints to be fulfilled by the variable candidates.
double upper
Upper bound for constraint.
double lower
Lower bound for constraint.
Constraints(double lower=0.0, double upper=1.0, bool isConstrained=false)
Constructor.
bool Check(double candidate)
Check whether the current variable candidate fulfills the constraints.
bool isConstrained
Whether to check the constraints.