simulatedAnnealing.hpp
Go to the documentation of this file.
1 #ifndef JULIAN_SIMULATEDANNEALING_HPP
2 #define JULIAN_SIMULATEDANNEALING_HPP
3 
4 #include <cmath>
5 #include <vector>
6 #include <utils/smartPointer.hpp>
9 
10 namespace julian {
73  public:
77  step_(rand), rng_(rng) {}
78 
79 
80  void setCoolingSchedule(const std::vector<double>& cooling_schedule);
81  void setLinearCooling(double Tstart, double Tend, double param);
82  void setExponentialCooling(double Tstart, double Tend, double param);
83 
84  template<typename F, typename PA>
85  double calculate(F f,PA prob_accept,double x_initial, int iters_per_t);
86  template<typename F, typename PA>
87  std::vector<double> calculate(F f,PA prob_accept,std::vector<double> x_initial, int iters_per_t);
88 
89  double takeStep(double x);
90  std::vector<double> takeStep(std::vector<double> x);
94  private:
97  std::vector<double> cooling_schedule_;
98  };
99 
108  template<typename F, typename PA>
109  double SimulatedAnnealing::calculate(F f,PA prob_accept, double x_initial, int iters_per_t) {
110  double x = x_initial;
111  double x_best = x;
112  double x_new;
113 
114  double E = f(x_initial);
115  double E_best = E;
116  double E_new;
117 
118  for (auto T : cooling_schedule_) {
119  for (int iter = 0; iter < iters_per_t; iter++) {
120  x_new = takeStep(x);
121  E_new = f(x_new);
122  if (E_new <= E_best) {
123  x_best = x_new;
124  E_best = E_new;
125  }
126 
127  if (E_new < E) {
128  x = x_new;
129  E = E_new;
130  } else if ( rng_->getRandom() < prob_accept(E, E_new, T)) {
131  x = x_new;
132  E = E_new;
133  }
134  }
135  }
136  return x_best;
137  }
138 
147  template<typename F, typename PA>
148  std::vector<double> SimulatedAnnealing::calculate(F f, PA prob_accept, std::vector<double> x_initial, int iters_per_t) {
149  std::vector<double> x = x_initial;
150  std::vector<double> x_best = x;
151  std::vector<double> x_new;
152 
153  double E = f(x_initial);
154  double E_best = E;
155  double E_new;
156 
157  for (auto T : cooling_schedule_) {
158  for (int iter = 0; iter < iters_per_t; iter++) {
159  x_new = takeStep(x);
160  E_new = f(x_new);
161 
162  if (E_new <= E_best) {
163  x_best = x_new;
164  E_best = E_new;
165  }
166 
167  if (E_new < E) {
168  x = x_new;
169  E = E_new;
170  } else if ( rng_->getRandom() < prob_accept(E, E_new, T)) {
171  x = x_new;
172  E = E_new;
173  }
174  }
175  }
176 
177  return x_best;
178  }
179 } // namespace julian
180 
181 #endif /* SIMULATEDANNEALING_HPP */
File contains template of deep-coping smart pointer.
Definition: cadHoliday.cpp:3
SimulatedAnnealing(SmartPointer< UniformRNG > rng, SmartPointer< RandomVariable > rand)
Constructor.
Definition: simulatedAnnealing.hpp:76
void setExponentialCooling(double Tstart, double Tend, double param)
Calculates exponential cooling schedule.
Definition: simulatedAnnealing.cpp:31
Template of deep-coping smart pointer.
Definition: smartPointer.hpp:14
File contains implementation of RNG generating random variables from uniform distribution.
void setLinearCooling(double Tstart, double Tend, double param)
Calculates linear cooling schedule.
Definition: simulatedAnnealing.cpp:18
std::vector< double > cooling_schedule_
Cooling schedule.
Definition: simulatedAnnealing.hpp:97
Class implements Simulated Annealing minimizer.
Definition: simulatedAnnealing.hpp:72
SmartPointer< UniformRNG > rng_
Random number generator used in acceptance of new state.
Definition: simulatedAnnealing.hpp:96
SmartPointer< RandomVariable > step_
Distribution used in picking of neighbour state.
Definition: simulatedAnnealing.hpp:95
void setCoolingSchedule(const std::vector< double > &cooling_schedule)
Set cooling schedule.
Definition: simulatedAnnealing.cpp:8
~SimulatedAnnealing()
Destructor.
Definition: simulatedAnnealing.hpp:93
double takeStep(double x)
Calculate the new state.
Definition: simulatedAnnealing.cpp:40
double calculate(F f, PA prob_accept, double x_initial, int iters_per_t)
Method finds the minimum of provided function.
Definition: simulatedAnnealing.hpp:109
File contains definition of RandomVariable.