QuantLib: a free/open-source library for quantitative finance
Fully annotated sources - version 1.32
Loading...
Searching...
No Matches
fireflyalgorithm.hpp
1/* -*- mode: c++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2
3/*
4Copyright (C) 2015 Andres Hernandez
5
6This file is part of QuantLib, a free-software/open-source library
7for financial quantitative analysts and developers - http://quantlib.org/
8
9QuantLib is free software: you can redistribute it and/or modify it
10under the terms of the QuantLib license. You should have received a
11copy of the license along with this program; if not, please email
12<quantlib-dev@lists.sf.net>. The license is also available online at
13<http://quantlib.org/license.shtml>.
14
15This program is distributed in the hope that it will be useful, but WITHOUT
16ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
17FOR A PARTICULAR PURPOSE. See the license for more details.
18*/
19
27#ifndef quantlib_optimization_fireflyalgorithm_hpp
28#define quantlib_optimization_fireflyalgorithm_hpp
29
30#include <ql/math/optimization/problem.hpp>
31#include <ql/math/optimization/constraint.hpp>
32#include <ql/experimental/math/isotropicrandomwalk.hpp>
33#include <ql/experimental/math/levyflightdistribution.hpp>
34#include <ql/math/randomnumbers/mt19937uniformrng.hpp>
35#include <ql/math/randomnumbers/seedgenerator.hpp>
36
37#include <cmath>
38#include <random>
39
40namespace QuantLib {
41
81 public:
82 class RandomWalk;
83 class Intensity;
85 ext::shared_ptr<Intensity> intensity,
86 ext::shared_ptr<RandomWalk> randomWalk,
87 Size Mde = 0,
88 Real mutationFactor = 1.0,
89 Real crossoverFactor = 0.5,
90 unsigned long seed = SeedGenerator::instance().get());
91 void startState(Problem &P, const EndCriteria &endCriteria);
92 EndCriteria::Type minimize(Problem& P, const EndCriteria& endCriteria) override;
93
94 protected:
95 std::vector<Array> x_, xI_, xRW_;
96 std::vector<std::pair<Real, Size> > values_;
100 ext::shared_ptr<Intensity> intensity_;
101 ext::shared_ptr<RandomWalk> randomWalk_;
102 std::mt19937 generator_;
103 std::uniform_int_distribution<QuantLib::Size> distribution_;
105 };
106
108
111 friend class FireflyAlgorithm;
112 public:
113 virtual ~Intensity() = default;
115 void findBrightest();
116 protected:
118 const std::vector<Array> *x_;
119 const std::vector<std::pair<Real, Size> > *values_;
120 std::vector<Array> *xI_;
121
122 virtual Real intensityImpl(Real valueX, Real valueY, Real distance) = 0;
123 inline Real distance(const Array& x, const Array& y) const {
124 Real d = 0.0;
125 for (Size i = 0; i < N_; i++) {
126 Real diff = x[i] - y[i];
127 d += diff*diff;
128 }
129 return d;
130 }
131
132 private:
134 x_ = &fa->x_;
135 xI_ = &fa->xI_;
136 values_ = &fa->values_;
137 Mfa_ = fa->Mfa_;
138 N_ = fa->N_;
139 }
140 };
141
143 /* Exponentially decreasing intensity
144 */
146 public:
147 ExponentialIntensity(Real beta0, Real betaMin, Real gamma)
148 : beta0_(beta0), betaMin_(betaMin), gamma_(gamma) {}
149 protected:
150 Real intensityImpl(Real valueX, Real valueY, Real d) override {
151 return (beta0_ - betaMin_) * std::exp(-gamma_ * d) + betaMin_;
152 }
154 };
155
157 /* Inverse law square
158 */
160 public:
162 : beta0_(beta0), betaMin_(betaMin) {}
163 protected:
164 Real intensityImpl(Real valueX, Real valueY, Real d) override {
165 return (beta0_ - betaMin_) / (d + QL_EPSILON) + betaMin_;
166 }
168 };
169
171
174 friend class FireflyAlgorithm;
175 public:
176 virtual ~RandomWalk() = default;
178 void walk() {
179 for (Size i = 0; i < Mfa_; i++) {
180 walkImpl((*xRW_)[(*values_)[i].second]);
181 }
182 }
183 protected:
185 const std::vector<Array> *x_;
186 const std::vector<std::pair<Real, Size> > *values_;
187 std::vector<Array> *xRW_;
189
190 virtual void walkImpl(Array & xRW) = 0;
191 virtual void init(FireflyAlgorithm *fa) {
192 x_ = &fa->x_;
193 xRW_ = &fa->xRW_;
194 values_ = &fa->values_;
195 Mfa_ = fa->Mfa_;
196 N_ = fa->N_;
197 lX_ = &fa->lX_;
198 uX_ = &fa->uX_;
199 }
200 };
201
203 /* Random walk given by distribution template parameter. The
204 distribution must be compatible with std::mt19937.
205 */
206 template <class Distribution>
208 public:
210 Real delta = 0.9,
211 unsigned long seed = SeedGenerator::instance().get())
212 : walkRandom_(std::mt19937(seed), std::move(dist), 1, Array(1, 1.0), seed),
213 delta_(delta) {}
214 protected:
215 void walkImpl(Array& xRW) override {
216 walkRandom_.nextReal(&xRW[0]);
217 xRW *= delta_;
218 }
219 void init(FireflyAlgorithm* fa) override {
221 walkRandom_.setDimension(N_, *lX_, *uX_);
222 }
225 };
226
228 /* Gaussian random walk
229 */
230 class GaussianWalk : public DistributionRandomWalk<std::normal_distribution<QuantLib::Real>> {
231 public:
232 explicit GaussianWalk(Real sigma,
233 Real delta = 0.9,
234 unsigned long seed = SeedGenerator::instance().get())
235 : DistributionRandomWalk<std::normal_distribution<QuantLib::Real>>(
236 std::normal_distribution<QuantLib::Real>(0.0, sigma), delta, seed){}
237 };
238
240 /* Levy flight random walk
241 */
242 class LevyFlightWalk : public DistributionRandomWalk<LevyFlightDistribution> {
243 public:
244 explicit LevyFlightWalk(Real alpha,
245 Real xm = 0.5,
246 Real delta = 0.9,
247 unsigned long seed = SeedGenerator::instance().get())
249 LevyFlightDistribution(xm, alpha), delta, seed) {}
250 };
251
253 /* Gaussian random walk, but size of step decreases with each iteration step
254 */
256 public:
258 Real sigma,
259 Real delta = 0.9,
260 unsigned long seed = SeedGenerator::instance().get())
261 : GaussianWalk(sigma, delta, seed), delta0_(delta) {}
262 protected:
263 void walkImpl(Array& xRW) override {
264 iteration_++;
265 if (iteration_ > Mfa_) {
266 //Every time all the fireflies have been processed
267 //multiply delta by itself
268 iteration_ = 0;
269 delta_ *= delta_;
270 }
272 }
273 void init(FireflyAlgorithm* fa) override {
275 iteration_ = 0;
276 delta_ = delta0_;
277 }
278
279 private:
282 };
283}
284
285#endif
1-D array used in linear algebra.
Definition: array.hpp:52
DecreasingGaussianWalk(Real sigma, Real delta=0.9, unsigned long seed=SeedGenerator::instance().get())
void walkImpl(Array &xRW) override
void init(FireflyAlgorithm *fa) override
DistributionRandomWalk(Distribution dist, Real delta=0.9, unsigned long seed=SeedGenerator::instance().get())
void walkImpl(Array &xRW) override
void init(FireflyAlgorithm *fa) override
IsotropicRandomWalk< Distribution, std::mt19937 > walkRandom_
Criteria to end optimization process:
Definition: endcriteria.hpp:40
Real intensityImpl(Real valueX, Real valueY, Real d) override
ExponentialIntensity(Real beta0, Real betaMin, Real gamma)
const std::vector< std::pair< Real, Size > > * values_
virtual Real intensityImpl(Real valueX, Real valueY, Real distance)=0
Real distance(const Array &x, const Array &y) const
void findBrightest()
find brightest firefly for each firefly
const std::vector< std::pair< Real, Size > > * values_
virtual void init(FireflyAlgorithm *fa)
virtual void walkImpl(Array &xRW)=0
std::vector< std::pair< Real, Size > > values_
std::uniform_int_distribution< QuantLib::Size > distribution_
ext::shared_ptr< Intensity > intensity_
void startState(Problem &P, const EndCriteria &endCriteria)
MersenneTwisterUniformRng rng_
std::vector< Array > xRW_
std::vector< Array > xI_
EndCriteria::Type minimize(Problem &P, const EndCriteria &endCriteria) override
minimize the optimization problem P
ext::shared_ptr< RandomWalk > randomWalk_
GaussianWalk(Real sigma, Real delta=0.9, unsigned long seed=SeedGenerator::instance().get())
Real intensityImpl(Real valueX, Real valueY, Real d) override
InverseLawSquareIntensity(Real beta0, Real betaMin)
Levy Flight Random Walk.
LevyFlightWalk(Real alpha, Real xm=0.5, Real delta=0.9, unsigned long seed=SeedGenerator::instance().get())
Uniform random number generator.
Abstract class for constrained optimization method.
Definition: method.hpp:36
Constrained optimization problem.
Definition: problem.hpp:42
static SeedGenerator & instance()
access to the unique instance
Definition: singleton.hpp:104
#define QL_EPSILON
Definition: qldefines.hpp:178
QL_REAL Real
real number
Definition: types.hpp:50
std::size_t Size
size of a container
Definition: types.hpp:58
Definition: any.hpp:35
STL namespace.