QuantLib: a free/open-source library for quantitative finance
Fully annotated sources - version 1.32
Loading...
Searching...
No Matches
differentialevolution.hpp
1/* -*- mode: c++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2
3/*
4 Copyright (C) 2012 Ralph Schreyer
5 Copyright (C) 2012 Mateusz Kapturski
6
7 This file is part of QuantLib, a free-software/open-source library
8 for financial quantitative analysts and developers - http://quantlib.org/
9
10 QuantLib is free software: you can redistribute it and/or modify it
11 under the terms of the QuantLib license. You should have received a
12 copy of the license along with this program; if not, please email
13 <quantlib-dev@lists.sf.net>. The license is also available online at
14 <http://quantlib.org/license.shtml>.
15
16 This program is distributed in the hope that it will be useful, but WITHOUT
17 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
18 FOR A PARTICULAR PURPOSE. See the license for more details.
19*/
20
25#ifndef quantlib_optimization_differential_evolution_hpp
26#define quantlib_optimization_differential_evolution_hpp
27
28#include <ql/math/optimization/constraint.hpp>
29#include <ql/math/optimization/problem.hpp>
30#include <ql/math/randomnumbers/mt19937uniformrng.hpp>
31
32namespace QuantLib {
33
35
58
60 public:
61 enum Strategy {
69 };
74 };
75
76 struct Candidate {
78 Real cost = 0.0;
79 Candidate(Size size = 0) : values(size, 0.0) {}
80 };
81
83 public:
88 unsigned long seed = 0;
89 bool applyBounds = true, crossoverIsAdaptive = false;
90 std::vector<Array> initialPopulation;
92
93 // Clang seems to have problems if we use '= default' here.
94 // NOLINTNEXTLINE(modernize-use-equals-default)
96
97 Configuration& withBounds(bool b = true) {
98 applyBounds = b;
99 return *this;
100 }
101
103 QL_REQUIRE(p>=0.0 && p<=1.0,
104 "Crossover probability (" << p
105 << ") must be in [0,1] range");
107 return *this;
108 }
109
111 QL_REQUIRE(n>0, "Positive number of population members required");
113 initialPopulation.clear();
114 return *this;
115 }
116
117 Configuration& withInitialPopulation(const std::vector<Array>& c) {
119 populationMembers = c.size();
120 return *this;
121 }
122
124 upperBound = u;
125 return *this;
126 }
127
129 lowerBound = l;
130 return *this;
131 }
132
133 Configuration& withSeed(unsigned long s) {
134 seed = s;
135 return *this;
136 }
137
140 return *this;
141 }
142
144 QL_ENSURE(w>=0 && w<=2.0,
145 "Step size weight ("<< w
146 << ") must be in [0,2] range");
147 stepsizeWeight = w;
148 return *this;
149 }
150
152 crossoverType = t;
153 return *this;
154 }
155
157 strategy = s;
158 return *this;
159 }
160 };
161
162
165
166 EndCriteria::Type minimize(Problem& p, const EndCriteria& endCriteria) override;
167
169 return configuration_;
170 }
171
172 private:
178
179 void fillInitialPopulation(std::vector<Candidate>& population,
180 const Problem& p) const;
181
182 void getCrossoverMask(std::vector<Array>& crossoverMask,
183 std::vector<Array>& invCrossoverMask,
184 const Array& mutationProbabilities) const;
185
187 const std::vector<Candidate>& population) const;
188
189 void adaptSizeWeights() const;
190
191 void adaptCrossover() const;
192
193 void calculateNextGeneration(std::vector<Candidate>& population,
194 Problem& costFunction) const;
195
196 Array rotateArray(Array inputArray) const;
197
198 void crossover(const std::vector<Candidate>& oldPopulation,
199 std::vector<Candidate> & population,
200 const std::vector<Candidate>& mutantPopulation,
201 const std::vector<Candidate>& mirrorPopulation,
202 Problem& costFunction) const;
203 };
204
205}
206
207#endif
1-D array used in linear algebra.
Definition: array.hpp:52
Configuration & withInitialPopulation(const std::vector< Array > &c)
Differential Evolution configuration object.
void fillInitialPopulation(std::vector< Candidate > &population, const Problem &p) const
const Configuration & configuration() const
DifferentialEvolution(const Configuration &configuration=Configuration())
void getCrossoverMask(std::vector< Array > &crossoverMask, std::vector< Array > &invCrossoverMask, const Array &mutationProbabilities) const
void calculateNextGeneration(std::vector< Candidate > &population, Problem &costFunction) const
void crossover(const std::vector< Candidate > &oldPopulation, std::vector< Candidate > &population, const std::vector< Candidate > &mutantPopulation, const std::vector< Candidate > &mirrorPopulation, Problem &costFunction) const
Array getMutationProbabilities(const std::vector< Candidate > &population) const
EndCriteria::Type minimize(Problem &p, const EndCriteria &endCriteria) override
minimize the optimization problem P
Array rotateArray(Array inputArray) const
Criteria to end optimization process:
Definition: endcriteria.hpp:40
Uniform random number generator.
Abstract class for constrained optimization method.
Definition: method.hpp:36
Constrained optimization problem.
Definition: problem.hpp:42
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