Pallas Solver  0.1
C++ Global Optimization Algorithms
simulated_annealing.h
1 // Pallas Solver
2 // Copyright 2015. All rights reserved.
3 //
4 // Redistribution and use in source and binary forms, with or without
5 // modification, are permitted provided that the following conditions are met:
6 //
7 // * Redistributions of source code must retain the above copyright notice,
8 // this list of conditions and the following disclaimer.
9 // * Redistributions in binary form must reproduce the above copyright notice,
10 // this list of conditions and the following disclaimer in the documentation
11 // and/or other materials provided with the distribution.
12 //
13 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
14 // AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15 // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
16 // ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
17 // LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
18 // CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
19 // SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
20 // INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
21 // CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
22 // ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
23 // POSSIBILITY OF SUCH DAMAGE.
24 //
25 // Author: ryan.latture@gmail.com (Ryan Latture)
26 
27 #ifndef PALLAS_SIMULATED_ANNEALING_H
28 #define PALLAS_SIMULATED_ANNEALING_H
29 
30 #include <cfloat>
31 
33 #include "pallas/history_concept.h"
34 #include "pallas/step_function.h"
35 #include "pallas/types.h"
36 #include "pallas/internal/state.h"
37 #include "pallas/internal/metropolis.h"
38 #include "pallas/scoped_ptr.h"
39 
40 
41 namespace pallas {
143  public:
147  struct Options {
148  Options () {
150  local_minimizer_options = GradientLocalMinimizer::Options();
151  scoped_ptr<StepFunction> default_step(new DefaultStepFunction(1.0));
152  step_function.swap(default_step);
153  max_iterations = 1000;
155  dwell_iterations = 20;
156  minimum_cost = -DBL_MAX;
157  is_silent = true;
158  polish_output = false;
160  };
161 
170  void set_step_function(scoped_ptr<StepFunction>& user_step_function) {
171  swap(user_step_function, step_function);
172  };
173 
178 
183  GradientLocalMinimizer::Options local_minimizer_options;
184 
189 
193  unsigned int max_iterations;
194 
199 
203  unsigned int dwell_iterations;
204 
208  double minimum_cost;
209 
214 
218  bool is_silent;
219 
227  };
228 
233  struct Summary {
237  Summary();
238 
239  std::string BriefReport() const;
241  std::string FullReport() const;
243  TerminationType termination_type;
248  CoolingScheduleType cooling_schedule;
249 
250  std::string message;
252  double initial_cost;
254  double final_cost;
256  GradientLocalMinimizer::Summary local_minimization_summary;
258  unsigned int num_parameters;
260  unsigned int num_iterations;
272  HistorySeries history;
273  };
274 
278  struct HistoryOutput {
279 
290  HistoryOutput(unsigned int iteration_number,
291  unsigned int stagnant_iterations,
292  double temperature,
293  const Vector &current_solution,
294  double best_cost,
295  const Vector &best_solution)
296  : iteration_number(iteration_number),
297  stagnant_iterations(stagnant_iterations),
298  temperature(temperature),
299  current_solution(current_solution),
300  best_cost(best_cost),
301  best_solution(best_solution) {}
302  unsigned int iteration_number;
303  unsigned int stagnant_iterations;
304  double temperature;
306  double best_cost;
308  };
309 
314 
326  void Solve(const SimulatedAnnealing::Options& options,
327  const GradientProblem& problem,
328  double* parameters,
329  SimulatedAnnealing::Summary* global_summary);
330 
331  private:
340  bool check_for_termination_(const SimulatedAnnealing::Options& options,
341  std::string *message,
342  TerminationType * termination_type);
343 
347  void prepare_final_summary_(SimulatedAnnealing::Summary *global_summary,
348  const GradientLocalMinimizer::Summary &local_summary);
349 
350  scoped_ptr<CoolingSchedule> cooling_schedule_;
352  internal::Metropolis metropolis_;
353  internal::State current_state_;
354  internal::State candidate_state_;
355  internal::State global_minimum_state_;
357  unsigned int num_iterations_;
358  unsigned int num_stagnant_iterations_;
359  };
360 
372  void Solve(const SimulatedAnnealing::Options& options,
373  const GradientProblem& problem,
374  double* parameters,
375  SimulatedAnnealing::Summary* summary);
376 
383  void dump(const SimulatedAnnealing::HistoryOutput &h, HistoryWriter& writer);
384 
385 } // namespace pallas
386 
387 #endif // PALLAS_SIMULATED_ANNEALING_H
double best_cost
Definition: simulated_annealing.h:306
unsigned int max_iterations
Definition: simulated_annealing.h:193
Stores information about the state of the system for at a given iteration number. ...
Definition: simulated_annealing.h:278
SimulatedAnnealing()
Default constructor.
Definition: simulated_annealing.h:313
unsigned int history_save_frequency
Definition: simulated_annealing.h:226
unsigned int dwell_iterations
Definition: simulated_annealing.h:203
double cost_evaluation_time_in_seconds
Definition: simulated_annealing.h:268
double local_minimization_time_in_seconds
Definition: simulated_annealing.h:264
double initial_cost
Definition: simulated_annealing.h:252
Definition: basinhopping.h:51
Vector current_solution
Definition: simulated_annealing.h:305
Minimizes a function using simulated annealing.
Definition: simulated_annealing.h:142
GradientLocalMinimizer::Summary local_minimization_summary
Definition: simulated_annealing.h:256
unsigned int max_stagnant_iterations
Definition: simulated_annealing.h:198
bool polish_output
Definition: simulated_annealing.h:213
double step_time_in_seconds
Definition: simulated_annealing.h:266
Vector best_solution
Definition: simulated_annealing.h:307
bool is_silent
Definition: simulated_annealing.h:218
unsigned int num_parameters
Definition: simulated_annealing.h:258
GradientLocalMinimizer::Options local_minimizer_options
Definition: simulated_annealing.h:183
Definition: scoped_ptr.h:45
Definition: state.h:34
Implements a probabilistic acceptance criterion for candidate solutions.
Definition: metropolis.h:48
void set_step_function(scoped_ptr< StepFunction > &user_step_function)
Convenience function for changing the default step function.
Definition: simulated_annealing.h:170
HistorySeries history
Definition: simulated_annealing.h:272
Contains a summary of the optimization.
Definition: simulated_annealing.h:233
double final_cost
Definition: simulated_annealing.h:254
TerminationType termination_type
Definition: simulated_annealing.h:243
unsigned int num_iterations
Definition: simulated_annealing.h:260
double total_time_in_seconds
Definition: simulated_annealing.h:262
CoolingSchedule::Options cooling_schedule_options
Definition: simulated_annealing.h:172
HistoryOutput(unsigned int iteration_number, unsigned int stagnant_iterations, double temperature, const Vector &current_solution, double best_cost, const Vector &best_solution)
Constructor.
Definition: simulated_annealing.h:290
Simple candidate generator that modifies the input by a random amount between +/- step_size...
Definition: step_function.h:64
void Solve(const SimulatedAnnealing::Options &options, const GradientProblem &problem, double *parameters, SimulatedAnnealing::Summary *global_summary)
Minimizes the specified gradient problem.
Definition: simulated_annealing.cc:131
Definition: simulated_annealing.h:147
bool was_polished
Definition: simulated_annealing.h:270
double minimum_cost
Definition: simulated_annealing.h:208
unsigned int iteration_number
Definition: simulated_annealing.h:302
scoped_ptr< StepFunction > step_function
Definition: simulated_annealing.h:188
CoolingScheduleType cooling_schedule
Definition: simulated_annealing.h:248
double temperature
Definition: simulated_annealing.h:304
void dump(const Basinhopping::HistoryOutput &h, HistoryWriter &writer)
Dumps the system state contained in the history output into the stream contained by the writer...
Definition: basinhopping.cc:323
unsigned int stagnant_iterations
Definition: simulated_annealing.h:303
Definition: cooling_schedule.h:55
std::string message
Definition: simulated_annealing.h:250