Pallas Solver  0.1
C++ Global Optimization Algorithms
metropolis.h
1 
10 // Pallas Solver
11 // Copyright 2015. All rights reserved.
12 //
13 // Redistribution and use in source and binary forms, with or without
14 // modification, are permitted provided that the following conditions are met:
15 //
16 // * Redistributions of source code must retain the above copyright notice,
17 // this list of conditions and the following disclaimer.
18 // * Redistributions in binary form must reproduce the above copyright notice,
19 // this list of conditions and the following disclaimer in the documentation
20 // and/or other materials provided with the distribution.
21 //
22 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
23 // AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24 // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25 // ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
26 // LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
27 // CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
28 // SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
29 // INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
30 // CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
31 // ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
32 // POSSIBILITY OF SUCH DAMAGE.
33 //
34 // Author: ryan.latture@gmail.com (Ryan Latture)
35 
36 #ifndef PALLAS_METROPOLIS_H
37 #define PALLAS_METROPOLIS_H
38 
39 #include <math.h>
40 
42 
43 namespace pallas {
44  namespace internal {
48  class Metropolis {
49  double beta;
52  public:
53 
54  Metropolis() {
55  beta = 1.0;
56  random_num = new RandomNumberGenerator<double>();
57  };
58 
62  Metropolis(double T) {
63  beta = 1.0 / T;
64  random_num = new RandomNumberGenerator<double>();
65  };
66 
69  delete random_num;
70  };
71 
87  bool accept_reject(double cost_new, double cost_old) {
88  double w = std::min(1.0, exp(-1.0 * (cost_new - cost_old) * beta));
89  double r = (*random_num)();
90  return w >= r;
91  };
92 
105  inline bool operator()(double cost_new, double cost_old) {
106  return accept_reject(cost_new, cost_old);
107  };
108  };
109  } // namespace internal
110 } // namespace pallas
111 
112 #endif //METROPOLIS_H
Metropolis(double T)
Definition: metropolis.h:62
Definition: basinhopping.h:51
~Metropolis()
Definition: metropolis.h:68
Implements a probabilistic acceptance criterion for candidate solutions.
Definition: metropolis.h:48
bool operator()(double cost_new, double cost_old)
Calls the accept_reject function Accepts the candidate solution based on the function: /code double w...
Definition: metropolis.h:105
bool accept_reject(double cost_new, double cost_old)
Returns a bool whether to accept the candidate solution.
Definition: metropolis.h:87