29 ext::shared_ptr<Topology> topology,
30 ext::shared_ptr<Inertia> inertia,
34 : M_(M), rng_(seed), topology_(
std::move(topology)), inertia_(
std::move(inertia)) {
36 QL_ENSURE(phi*phi - 4 * phi != 0.0,
"Invalid phi");
37 c0_ = 2.0 / std::abs(2.0 - phi - sqrt(phi*phi - 4 * phi));
43 ext::shared_ptr<Topology> topology,
44 ext::shared_ptr<Inertia> inertia,
49 : M_(M), c0_(omega), c1_(c1), c2_(c2), rng_(seed), topology_(
std::move(topology)),
50 inertia_(
std::move(inertia)) {}
72 for (
Size i = 0; i <
M_; i++) {
74 X_.emplace_back(
N_, 0.0);
76 V_.emplace_back(
N_, 0.0);
78 gBX_.emplace_back(
N_, 0.0);
79 for (
Size j = 0; j <
N_; j++) {
81 x[j] =
lX_[j] + bounds[j] * sample[2 * j];
83 v[j] = bounds[j] * (2.0*sample[2 * j + 1] - 1.0);
101 Size iterationStat = 0;
105 Size bestPosition = 0;
109 for (
Size i = 0; i <
M_; i++) {
110 if (
pBF_[i] < bestValue) {
121 if (iteration > maxIteration || iterationStat > maxIStationary)
131 for (
Size i = 0; i <
M_; i++) {
138 for (
Size j = 0; j <
N_; j++) {
148 else if (x[j] >
uX_[j]) {
168 if (iteration > maxIteration)
180 Real currBest = (*pBF_)[0];
181 for (
Size i = 1; i <
M_; i++) {
182 if (currBest >(*
pBF_)[i]) currBest = (*pBF_)[i];
185 if (currBest <
best_) {
203 for (
Size i = 0; i <
M_; i++) {
209 for (
Size i = 0; i <
M_; i++) {
210 Real bestF = (*pBF_)[i];
215 Size lower = std::max(i,
K_ + 1) -
K_ - 1;
216 for (
Size j = lower; j < upper; j++) {
217 if ((*
pBF_)[j] < bestF) {
223 for (
Size j = 0; j < i +
K_ -
M_; j++) {
224 if ((*
pBF_)[j] < bestF) {
231 for (
Size j =
M_ - (
K_ - i) - 1; j <
M_; j++) {
232 if ((*
pBF_)[j] < bestF) {
238 (*gBX_)[i] = (*pBX_)[bestX];
249 : totalClubs_(totalClubs), maxClubs_(maxClubs), minClubs_(minClubs),
250 defaultClubs_(defaultClubs), resetIteration_(resetIteration), bestByClub_(totalClubs, 0),
251 worstByClub_(totalClubs, 0), generator_(seed), distribution_(1, totalClubs_) {
253 "Total number of clubs must be larger or equal than default clubs");
255 "Number of default clubs must be larger or equal than minimum clubs");
257 "Number of maximum clubs must be larger or equal than default clubs");
259 "Total number of clubs must be larger or equal than maximum clubs");
269 for (
Size i = 0; i <
M_; i++) {
274 clubSet[index] =
true;
302 for (
Size j = 0; j <
M_; j++) {
303 if (particlesSet[j]) {
304 if (bestByClub >(*
pBF_)[j]) {
305 bestByClub = (*pBF_)[j];
308 else if (worstByClub < (*
pBF_)[j]) {
309 worstByClub = (*pBF_)[j];
319 for (
Size i = 0; i <
M_; i++) {
323 Size currentClubs = 0;
357 Size bestNeighborX = 0;
364 (*gBX_)[i] = (*pBX_)[bestNeighborX];
365 (*gBF_)[i] = bestNeighborF;
375 if (index == randIndex) {
392 if (index == randIndex) {
void setValues() override
produce changes to PSO state for current iteration
1-D array used in linear algebra.
Size size() const
dimension of the array
std::uniform_int_distribution< QuantLib::Size > distribution_
std::vector< std::vector< bool > > particles4clubs_
std::vector< std::vector< bool > > clubs4particles_
std::vector< Size > worstByClub_
ClubsTopology(Size defaultClubs, Size totalClubs, Size maxClubs, Size minClubs, Size resetIteration, unsigned long seed=SeedGenerator::instance().get())
void setSize(Size M) override
initialize state for current problem
decltype(distribution_)::param_type param_type
void findSocialBest() override
produce changes to PSO state for current iteration
std::vector< Size > bestByClub_
void joinRandomClub(Size particle, Size currentClubs)
void leaveRandomClub(Size particle, Size currentClubs)
Array lowerBound(const Array ¶ms) const
Array upperBound(const Array ¶ms) const
Criteria to end optimization process:
Size maxIterations() const
Size maxStationaryStateIterations() const
void findSocialBest() override
produce changes to PSO state for current iteration
ParticleSwarmOptimization(Size M, ext::shared_ptr< Topology > topology, ext::shared_ptr< Inertia > inertia, Real c1=2.05, Real c2=2.05, unsigned long seed=SeedGenerator::instance().get())
std::vector< Array > gBX_
void startState(Problem &P, const EndCriteria &endCriteria)
MersenneTwisterUniformRng rng_
ext::shared_ptr< Topology > topology_
ext::shared_ptr< Inertia > inertia_
std::vector< Array > pBX_
EndCriteria::Type minimize(Problem &P, const EndCriteria &endCriteria) override
minimize the optimization problem P
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)
Sobol low-discrepancy sequence generator.
const SobolRsg::sample_type & nextSequence() const
#define QL_ENSURE(condition, message)
throw an error if the given post-condition is not verified
#define QL_REQUIRE(condition, message)
throw an error if the given pre-condition is not verified
std::size_t Size
size of a container
Implementation based on: Clerc, M., Kennedy, J. (2002) The particle swarm-explosion,...
ext::shared_ptr< BlackVolTermStructure > v
Sobol low-discrepancy sequence generator.
std::vector< Real > value_type