c93eb54c87
and cid#1557535 COPY_INSTEAD_OF_MOVE cid#1557520 COPY_INSTEAD_OF_MOVE cid#1557513 COPY_INSTEAD_OF_MOVE cid#1557503 COPY_INSTEAD_OF_MOVE cid#1557487 COPY_INSTEAD_OF_MOVE cid#1557483 COPY_INSTEAD_OF_MOVE cid#1557479 COPY_INSTEAD_OF_MOVE cid#1557474 COPY_INSTEAD_OF_MOVE cid#1557461 COPY_INSTEAD_OF_MOVE cid#1557446 COPY_INSTEAD_OF_MOVE cid#1557445 COPY_INSTEAD_OF_MOVE cid#1557441 COPY_INSTEAD_OF_MOVE cid#1557435 COPY_INSTEAD_OF_MOVE cid#1557433 COPY_INSTEAD_OF_MOVE cid#1557429 COPY_INSTEAD_OF_MOVE cid#1557375 COPY_INSTEAD_OF_MOVE cid#1557372 COPY_INSTEAD_OF_MOVE cid#1557356 COPY_INSTEAD_OF_MOVE cid#1557350 COPY_INSTEAD_OF_MOVE cid#1557344 COPY_INSTEAD_OF_MOVE cid#1557339 COPY_INSTEAD_OF_MOVE cid#1557332 COPY_INSTEAD_OF_MOVE cid#1557330 COPY_INSTEAD_OF_MOVE cid#1557328 COPY_INSTEAD_OF_MOVE cid#1557323 COPY_INSTEAD_OF_MOVE cid#1557315 COPY_INSTEAD_OF_MOVE cid#1557313 COPY_INSTEAD_OF_MOVE cid#1557304 COPY_INSTEAD_OF_MOVE cid#1557297 COPY_INSTEAD_OF_MOVE cid#1557291 COPY_INSTEAD_OF_MOVE cid#1557290 COPY_INSTEAD_OF_MOVE cid#1557271 COPY_INSTEAD_OF_MOVE cid#1557266 COPY_INSTEAD_OF_MOVE cid#1557262 COPY_INSTEAD_OF_MOVE cid#1557259 COPY_INSTEAD_OF_MOVE cid#1557246 COPY_INSTEAD_OF_MOVE cid#1557242 COPY_INSTEAD_OF_MOVE cid#1557241 COPY_INSTEAD_OF_MOVE cid#1557236 COPY_INSTEAD_OF_MOVE cid#1557228 COPY_INSTEAD_OF_MOVE cid#1557225 COPY_INSTEAD_OF_MOVE cid#1557221 COPY_INSTEAD_OF_MOVE cid#1557217 COPY_INSTEAD_OF_MOVE cid#1557213 COPY_INSTEAD_OF_MOVE cid#1557211 COPY_INSTEAD_OF_MOVE cid#1557209 COPY_INSTEAD_OF_MOVE cid#1557205 COPY_INSTEAD_OF_MOVE cid#1557204 COPY_INSTEAD_OF_MOVE cid#1557193 COPY_INSTEAD_OF_MOVE cid#1556082 COPY_INSTEAD_OF_MOVE Change-Id: I07f195a79a69d4bac0d14317854efc88d6fe94d7 Reviewed-on: https://gerrit.libreoffice.org/c/core/+/171927 Tested-by: Jenkins Reviewed-by: Caolán McNamara <caolan.mcnamara@collabora.com>
162 lines
5.2 KiB
C++
162 lines
5.2 KiB
C++
/* -*- 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: */
|