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
|
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
/*
* This file is part of the LibreOffice project.
*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
*
*/
#pragma once
#include <vector>
#include <random>
#include <limits>
struct Individual
{
std::vector<double> mVariables;
};
template <typename DataProvider> class DifferentialEvolutionAlgorithm
{
static constexpr double mnDifferentialWeight = 0.5; // [0, 2]
static constexpr double mnCrossoverProbability = 0.9; // [0, 1]
static constexpr double constAcceptedPrecision = 0.000000001;
DataProvider& mrDataProvider;
size_t mnPopulationSize;
std::vector<Individual> maPopulation;
std::random_device maRandomDevice;
std::mt19937 maGenerator;
size_t mnDimensionality;
std::uniform_int_distribution<> maRandomPopulation;
std::uniform_int_distribution<> maRandomDimensionality;
std::uniform_real_distribution<> maRandom01;
Individual maBestCandidate;
double mfBestFitness;
int mnGeneration;
int mnLastChange;
public:
DifferentialEvolutionAlgorithm(DataProvider& rDataProvider, size_t nPopulationSize)
: mrDataProvider(rDataProvider)
, mnPopulationSize(nPopulationSize)
, maGenerator(maRandomDevice())
, mnDimensionality(mrDataProvider.getDimensionality())
, maRandomPopulation(0, mnPopulationSize - 1)
, maRandomDimensionality(0, mnDimensionality - 1)
, maRandom01(0.0, 1.0)
, mfBestFitness(std::numeric_limits<double>::lowest())
, mnGeneration(0)
, mnLastChange(0)
{
}
std::vector<double> const& getResult() { return maBestCandidate.mVariables; }
int getGeneration() { return mnGeneration; }
int getLastChange() { return mnLastChange; }
void initialize()
{
mnGeneration = 0;
mnLastChange = 0;
maPopulation.clear();
maBestCandidate.mVariables.clear();
// Initialize population with individuals that have been initialized with uniform random
// noise
// uniform noise means random value inside your search space
maPopulation.reserve(mnPopulationSize);
for (size_t i = 0; i < mnPopulationSize; ++i)
{
maPopulation.emplace_back();
Individual& rIndividual = maPopulation.back();
mrDataProvider.initializeVariables(rIndividual.mVariables, maGenerator);
}
}
// Calculate one generation
bool next()
{
bool bBestChanged = false;
for (size_t agentIndex = 0; agentIndex < mnPopulationSize; ++agentIndex)
{
// calculate new candidate solution
// pick random point from population
size_t x = agentIndex; // randomPopulation(generator);
size_t a, b, c;
// create a copy of chosen random agent in population
Individual& rOriginal = maPopulation[x];
Individual aCandidate(rOriginal);
// pick three different random points from population
do
{
a = maRandomPopulation(maGenerator);
} while (a == x);
do
{
b = maRandomPopulation(maGenerator);
} while (b == x || b == a);
do
{
c = maRandomPopulation(maGenerator);
} while (c == x || c == a || c == b);
size_t randomIndex = maRandomDimensionality(maGenerator);
for (size_t index = 0; index < mnDimensionality; ++index)
{
double randomCrossoverProbability = maRandom01(maGenerator);
if (index == randomIndex || randomCrossoverProbability < mnCrossoverProbability)
{
double fVarA = maPopulation[a].mVariables[index];
double fVarB = maPopulation[b].mVariables[index];
double fVarC = maPopulation[c].mVariables[index];
double fNewValue = fVarA + mnDifferentialWeight * (fVarB - fVarC);
fNewValue = mrDataProvider.boundVariable(index, fNewValue);
aCandidate.mVariables[index] = fNewValue;
}
}
double fCandidateFitness = mrDataProvider.calculateFitness(aCandidate.mVariables);
// see if is better than original, if so replace
if (fCandidateFitness > mrDataProvider.calculateFitness(rOriginal.mVariables))
{
maPopulation[x] = std::move(aCandidate);
if (fCandidateFitness > mfBestFitness)
{
if (std::abs(fCandidateFitness - mfBestFitness) > constAcceptedPrecision)
{
bBestChanged = true;
mnLastChange = mnGeneration;
}
mfBestFitness = fCandidateFitness;
maBestCandidate = maPopulation[x];
}
}
}
mnGeneration++;
return bBestChanged;
}
};
/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|