26#ifndef quantlib_optimization_simulatedannealing_hpp
27#define quantlib_optimization_simulatedannealing_hpp
45 template <
class RNG = MersenneTwisterUniformRng>
58 const RNG &rng = RNG())
98 Array center(vertices_.front().size(), 0);
99 for (
auto& vertice : vertices_)
101 center *= 1 /
Real(vertices_.size());
103 for (
auto& vertice : vertices_) {
104 Array temp = vertice - center;
105 result +=
Norm2(temp);
107 return result /
Real(vertices_.size());
112 fac1_ = (1.0 - fac) / ((
Real)n_);
114 for (j_ = 0; j_ < n_; j_++) {
115 ptry_[j_] = sum_[j_] * fac1_ - vertices_[ihi_][j_] * fac2_;
120 ytry_ = P.
value(ptry_);
121 if (std::isnan(ytry_)) {
128 yflu_ = ytry_ - tt_ * std::log(rng_.next().value);
130 values_[ihi_] = ytry_;
132 for (j_ = 0; j_ < n_; j_++) {
133 sum_[j_] += ptry_[j_] - vertices_[ihi_][j_];
134 vertices_[ihi_][j_] = ptry_[j_];
144 Size stationaryStateIterations_ = 0;
150 ptry_ =
Array(n_, 0.0);
154 vertices_ = std::vector<Array>(n_ + 1, x);
155 for (i_ = 0; i_ < n_; i_++) {
156 Array direction(n_, 0.0);
160 values_ =
Array(n_ + 1, 0.0);
161 for (i_ = 0; i_ <= n_; i_++) {
165 values_[i_] = P.
value(vertices_[i_]);
166 if (std::isnan(ytry_)) {
175 pb_ =
Array(n_, 0.0);
177 iterationT_ = iteration_;
179 sum_ =
Array(n_, 0.0);
180 for (i_ = 0; i_ <= n_; i_++)
181 sum_ += vertices_[i_];
185 ynhi_ = values_[0] + tt_ * std::log(rng_.next().value);
187 yhi_ = values_[1] + tt_ * std::log(rng_.next().value);
195 for (i_ = 2; i_ < n_ + 1; i_++) {
196 yt_ = values_[i_] + tt_ * std::log(rng_.next().value);
218 stationaryStateIterations_,
232 if (ytry_ >= ynhi_) {
235 if (ytry_ >= ysave_) {
236 for (i_ = 0; i_ < n_ + 1; i_++) {
238 for (j_ = 0; j_ < n_; j_++) {
239 sum_[j_] = 0.5 * (vertices_[i_][j_] +
240 vertices_[ilo_][j_]);
241 vertices_[i_][j_] = sum_[j_];
243 values_[i_] = P.
value(sum_);
247 for (i_ = 0; i_ < n_; i_++)
249 for (i_ = 0; i_ <= n_; i_++)
250 sum_ += vertices_[i_];
256 }
while (iteration_ <
257 iterationT_ + (
scheme_ == ConstantFactor ?
m_ : 1));
261 T_ *= (1.0 - epsilon_);
264 if (iteration_ <= K_)
266 std::pow(1.0 - (
Real)iteration_ / (
Real)K_, alpha_);
1-D array used in linear algebra.
Size size() const
dimension of the array
bool test(const Array &p) const
Real update(Array &p, const Array &direction, Real beta) const
Criteria to end optimization process:
bool checkStationaryPoint(Real xOld, Real xNew, Size &statStateIterations, EndCriteria::Type &ecType) const
bool checkMaxIterations(Size iteration, EndCriteria::Type &ecType) const
Abstract class for constrained optimization method.
Constrained optimization problem.
const Array & currentValue() const
current value of the local minimum
Constraint & constraint() const
Constraint.
Real value(const Array &x)
call cost function computation and increment evaluation counter
void setFunctionValue(Real functionValue)
void setCurrentValue(const Array ¤tValue)
EndCriteria::Type minimize(Problem &P, const EndCriteria &ec) override
minimize the optimization problem P
SimulatedAnnealing(const Real lambda, const Real T0, const Real epsilon, const Size m, const RNG &rng=RNG())
void amotsa(Problem &, Real)
SimulatedAnnealing(const Real lambda, const Real T0, const Size K, const Real alpha, const RNG &rng=RNG())
std::vector< Array > vertices_
Abstract constraint class.
QL_INTEGER Integer
integer number
std::size_t Size
size of a container
const std::unique_ptr< T > scheme_
Real Norm2(const Array &v)
Abstract optimization problem class.