/usr/include/ql/experimental/math/hybridsimulatedannealing.hpp is in libquantlib0-dev 1.12-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 | /* -*- mode: c++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
/*
Copyright (C) 2015 Andres Hernandez
This file is part of QuantLib, a free-software/open-source library
for financial quantitative analysts and developers - http://quantlib.org/
QuantLib is free software: you can redistribute it and/or modify it
under the terms of the QuantLib license. You should have received a
copy of the license along with this program; if not, please email
<quantlib-dev@lists.sf.net>. The license is also available online at
<http://quantlib.org/license.shtml>.
This program is distributed in the hope that it will be useful, but WITHOUT
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
FOR A PARTICULAR PURPOSE. See the license for more details.
*/
/*! \file hybridsimulatedannealing.hpp
\brief Implementation based on:
Very Fast Simulated Re-Annealing, Lester Ingber,
Mathl. Comput. Modelling, 967-973, 1989
*/
#ifndef quantlib_optimization_hybridsimulatedannealing_hpp
#define quantlib_optimization_hybridsimulatedannealing_hpp
#include <ql/math/optimization/problem.hpp>
#include <ql/math/optimization/constraint.hpp>
#include <ql/experimental/math/hybridsimulatedannealingfunctors.hpp>
#include <ql/math/optimization/levenbergmarquardt.hpp>
namespace QuantLib {
/*! Method is fairly straightforward:
1) Sampler provides a probability density (based on current value) for the parameters. Each
iteration a new draw is made from it to find a new point
2) Probability determines whether the new point, obtained from Sampler, is accepted or not
3) Temperature is a schedule T(k) for the iteration k, which affects the Sampler and Probability
4) Reannealing is a departure from the traditional Boltzmann Annealing method: it rescales
the iteration k independently for each dimension so as to improve convergence
The hybrid in the name is because one can provide it a local optimizer for use whenever any new
best point is found or at every accepted point, in which case is used is chose by the user.
Class Sampler must implement the following interface:
\code
void operator()(Array &newPoint, const Array ¤tPoint, const Array &temp) const;
\endcode
Class Probability must implement the following interface:
\code
bool operator()(Real currentValue, Real newValue, const Array &temp) const;
\endcode
Class Temperature must implement the following interface:
\code
void operator()(Array &newTemp, const Array &currTemp, const Array &steps) const;
\endcode
Class Reannealing must implement the following interface:
\code
void operator()(Array & steps, const Array ¤tPoint,
Real aCurrentValue, const Array & currTemp) const;
\endcode
*/
template <class Sampler, class Probability, class Temperature, class Reannealing = ReannealingTrivial>
class HybridSimulatedAnnealing : public OptimizationMethod {
public:
enum LocalOptimizeScheme {
NoLocalOptimize,
EveryNewPoint,
EveryBestPoint
};
enum ResetScheme {
NoResetScheme,
ResetToBestPoint,
ResetToOrigin
};
HybridSimulatedAnnealing(const Sampler &sampler,
const Probability &probability,
const Temperature &temperature,
const Reannealing &reannealing = ReannealingTrivial(),
Real startTemperature = 200.0,
Real endTemperature = 0.01,
Size reAnnealSteps = 50,
ResetScheme resetScheme = ResetToBestPoint,
Size resetSteps = 150,
boost::shared_ptr<OptimizationMethod> localOptimizer
= boost::shared_ptr<OptimizationMethod>(),
LocalOptimizeScheme optimizeScheme = EveryBestPoint)
: sampler_(sampler), probability_(probability),
temperature_(temperature), reannealing_(reannealing),
startTemperature_(startTemperature), endTemperature_(endTemperature),
reAnnealSteps_(reAnnealSteps == 0 ? QL_MAX_INTEGER : reAnnealSteps), resetScheme_(resetScheme),
resetSteps_(resetSteps == 0 ? QL_MAX_INTEGER : resetSteps), localOptimizer_(localOptimizer),
optimizeScheme_(localOptimizer ? optimizeScheme : NoLocalOptimize) {
if (!localOptimizer)
localOptimizer.reset(new LevenbergMarquardt);
}
EndCriteria::Type minimize(Problem &P, const EndCriteria &endCriteria);
private:
Sampler sampler_;
Probability probability_;
Temperature temperature_;
Reannealing reannealing_;
Real startTemperature_;
Real endTemperature_;
Size reAnnealSteps_;
ResetScheme resetScheme_;
Size resetSteps_;
boost::shared_ptr<OptimizationMethod> localOptimizer_;
LocalOptimizeScheme optimizeScheme_;
};
template <class Sampler, class Probability, class Temperature, class Reannealing>
EndCriteria::Type HybridSimulatedAnnealing<Sampler, Probability, Temperature, Reannealing>::minimize(Problem &P, const EndCriteria &endCriteria) {
EndCriteria::Type ecType = EndCriteria::None;
P.reset();
reannealing_.setProblem(P);
Array x = P.currentValue();
Size n = x.size();
Size k = 1;
Size kStationary = 1;
Size kReAnneal = 1;
Size kReset = 1;
Size maxK = endCriteria.maxIterations();
Size maxKStationary = endCriteria.maxStationaryStateIterations();
bool temperatureBreached = false;
Array currentTemperature(n, startTemperature_);
Array annealStep(n, 1.0);
Array bestPoint(x);
Array currentPoint(x);
Array startingPoint(x);
Array newPoint(x);
Real bestValue = P.value(bestPoint);
Real currentValue = bestValue;
Real startingValue = bestValue; //to reset to starting point if desired
while (k <= maxK && kStationary <= maxKStationary && !temperatureBreached)
{
//Draw a new sample point
sampler_(newPoint, currentPoint, currentTemperature);
try{
//Evaluate new point
Real newValue = P.value(newPoint);
//Determine if new point is accepted
if (probability_(currentValue, newValue, currentTemperature)) {
if (optimizeScheme_ == EveryNewPoint) {
P.setCurrentValue(newPoint);
P.setFunctionValue(newValue);
localOptimizer_->minimize(P, endCriteria);
newPoint = P.currentValue();
newValue = P.functionValue();
}
currentPoint = newPoint;
currentValue = newValue;
}
//Check if we have a new best point
if (newValue < bestValue) {
if (optimizeScheme_ == EveryBestPoint) {
P.setCurrentValue(newPoint);
P.setFunctionValue(newValue);
localOptimizer_->minimize(P, endCriteria);
newPoint = P.currentValue();
newValue = P.functionValue();
}
kStationary = 0;
bestValue = newValue;
bestPoint = newPoint;
}
} catch(...){
//Do nothing, move on to new draw
}
//Increase steps
k++;
kStationary++;
for (Size i = 0; i < annealStep.size(); i++)
annealStep[i]++;
//Reanneal if necessary
if (kReAnneal == reAnnealSteps_) {
kReAnneal = 0;
reannealing_(annealStep, currentPoint, currentValue, currentTemperature);
}
kReAnneal++;
//Reset if necessary
if (kReset == resetSteps_) {
kReset = 0;
switch (resetScheme_) {
case NoResetScheme:
break;
case ResetToOrigin:
currentPoint = startingPoint;
currentValue = startingValue;
break;
case ResetToBestPoint:
currentPoint = bestPoint;
currentValue = bestValue;
break;
}
}
kReset++;
//Update the current temperature according to current step
temperature_(currentTemperature, currentTemperature, annealStep);
//Check if temperature condition is breached
for (Size i = 0; i < n; i++)
temperatureBreached = temperatureBreached && currentTemperature[i] < endTemperature_;
}
//Change end criteria type if appropriate
if (k > maxK)
ecType = EndCriteria::MaxIterations;
else if (kStationary > maxKStationary)
ecType = EndCriteria::StationaryPoint;
//Set result to best point
P.setCurrentValue(bestPoint);
P.setFunctionValue(bestValue);
return ecType;
}
typedef HybridSimulatedAnnealing<SamplerGaussian, ProbabilityBoltzmannDownhill, TemperatureExponential, ReannealingTrivial> GaussianSimulatedAnnealing;
typedef HybridSimulatedAnnealing<SamplerLogNormal, ProbabilityBoltzmannDownhill, TemperatureExponential, ReannealingTrivial> LogNormalSimulatedAnnealing;
typedef HybridSimulatedAnnealing<SamplerMirrorGaussian, ProbabilityBoltzmannDownhill, TemperatureExponential, ReannealingTrivial> MirrorGaussianSimulatedAnnealing;
typedef HybridSimulatedAnnealing<SamplerGaussian, ProbabilityBoltzmannDownhill, TemperatureExponential, ReannealingFiniteDifferences> GaussianSimulatedReAnnealing;
typedef HybridSimulatedAnnealing<SamplerVeryFastAnnealing, ProbabilityBoltzmannDownhill, TemperatureVeryFastAnnealing, ReannealingTrivial> VeryFastSimulatedAnnealing;
typedef HybridSimulatedAnnealing<SamplerVeryFastAnnealing, ProbabilityBoltzmannDownhill, TemperatureVeryFastAnnealing, ReannealingFiniteDifferences> VeryFastSimulatedReAnnealing;
}
#endif
|