This example shows the use of several different optimizers: firefly algorithm, hybrid simulated annealing, particle swarm optimization, simulated annealing, and differential evolution.
#if !defined(BOOST_ALL_NO_LIB) && defined(BOOST_MSVC)
#endif
#include <iomanip>
#include <iostream>
#include <utility>
unsigned long seed = 127;
p1 += i * i;
}
p1 = -0.2*std::sqrt(0.5*p1);
p2 *= 0.5;
return M_E + 20.0 - 20.0*std::exp(p1)-std::exp(p2);
}
p1 = -0.2*std::sqrt(0.5*p1);
y[i] =
M_E + 20.0 - 20.0*std::exp(p1)-std::exp(p2);
}
}
}
}
}
for (
Size i = 0; i < x.
size() - 1; i++) {
Real temp = (x[i + 1] - x[i] * x[i]);
result += (x[i] - 1.0)*(x[i] - 1.0) + 100.0*temp*temp;
}
return result;
}
p1 *= std::cos(i);
}
return -p1*std::exp(-p2);
}
Real p1 = std::cos(x[i]);
y[i] = -p1*std::exp(-p2);
}
}
return -p*std::sin(std::sqrt(std::abs(0.5*x[0] + p))) -
x[0] * std::sin(std::sqrt(std::abs(x[0] - p)));
}
std::cout << " f(" << x[0];
std::cout << ", " << x[i];
}
std::cout << ") = " << val << std::endl;
return val;
}
public:
typedef ext::function<
Real(
const Array&)> RealFunc;
typedef ext::function<
Array(
const Array&)> ArrayFunc;
explicit TestFunction(RealFunc
f, ArrayFunc fs = ArrayFunc())
~TestFunction() override = default;
Real value(
const Array& x)
const override {
return f_(x); }
throw std::runtime_error("Invalid function");
}
private:
};
std::cout << "Starting point: ";
if (!constraint.empty())
c = constraint;
printFunction(p, start);
std::cout << "End point: ";
if(!optimum.empty())
{
std::cout << "Global optimum: ";
Real optimVal = printFunction(p, optimum);
if(std::abs(optimVal) < 1e-13)
return static_cast<int>(std::abs(val - optimVal) < 1e-6);
else
return static_cast<int>(std::abs((val - optimVal) / optimVal) < 1e-6);
}
return 1;
}
void testFirefly() {
optimum[0] = 512.0;
optimum[1] = 404.2319;
auto intensity = ext::make_shared<ExponentialIntensity>(10.0, 1e-8, intense);
auto randomWalk = ext::make_shared<LevyFlightWalk>(vola, 0.5, 1.0, seed);
std::cout << "Function eggholder, Agents: " << agents
<< ", Vola: " << vola << ", Intensity: " << intense << std::endl;
TestFunction
f(eggholder);
test(fa,
f, ec, x, constraint, optimum);
std::cout << "================================================================" << std::endl;
}
void testSimulatedAnnealing(
Size dimension,
Size maxSteps,
Size staticSteps){
TestFunction
f(ackley, ackleyValues);
Array optimum(dimension, 0.0);
Array lower(dimension, -5.0);
Array upper(dimension, 5.0);
std::cout << "Function ackley, Lambda: " << lambda
<< ", Temperature: " << temperature
<< ", Epsilon: " << epsilon
<< ", Iterations: " << ms
<< std::endl;
EndCriteria ec(maxSteps, staticSteps, 1.0e-8, 1.0e-8, 1.0e-8);
test(sa,
f, ec, x, constraint, optimum);
std::cout << "================================================================" << std::endl;
}
void testGaussianSA(
Size dimension,
GaussianSimulatedAnnealing::ResetToBestPoint,
GaussianSimulatedAnnealing::EveryBestPoint,
const ext::shared_ptr<OptimizationMethod>& localOptimizer =
ext::make_shared<LevenbergMarquardt>()) {
TestFunction
f(ackley, ackleyValues);
std::cout << "Function: ackley, Dimensions: " << dimension
<< ", Initial temp:" << initialTemp
<< ", Final temp:" << finalTemp
<< ", Reset scheme:" << resetScheme
<< ", Reset steps:" << resetSteps
<< std::endl;
Array optimum(dimension, 0.0);
Array lower(dimension, -5.0);
Array upper(dimension, 5.0);
initialTemp, finalTemp, 50, resetScheme,
resetSteps, localOptimizer,
optimizeScheme);
EndCriteria ec(maxSteps, staticSteps, 1.0e-8, 1.0e-8, 1.0e-8);
test(sa,
f, ec, x, constraint, optimum);
std::cout << "================================================================" << std::endl;
}
std::cout <<
"Function: rosenbrock, Dimensions: " <<
n
<< ", Agents: " << agents << ", K-neighbors: " << kneighbor
<< ", Threshold: " << threshold << std::endl;
auto topology = ext::make_shared<KNeighbors>(kneighbor);
auto inertia = ext::make_shared<LevyFlightInertia>(1.5, threshold, seed);
TestFunction
f(rosenbrock);
test(pso,
f, ec, x, constraint, optimum);
std::cout << "================================================================" << std::endl;
}
void testDifferentialEvolution(
Size n,
Size agents){
TestFunction
f(rosenbrock);
Real stepsizeWeight = 0.6;
std::cout <<
"Function: rosenbrock, Dimensions: " <<
n <<
", Agents: " << agents
<< ", Probability: " << probability
<< ", StepsizeWeight: " << stepsizeWeight
<< ", Strategy: BestMemberWithJitter" << std::endl;
test(de,
f, ec, x, constraint, optimum);
std::cout << "================================================================" << std::endl;
}
int main(int, char* []) {
try {
std::cout << std::endl;
std::cout << "++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++" << std::endl;
std::cout << "Firefly Algorithm Test" << std::endl;
std::cout << "----------------------------------------------------------------" << std::endl;
testFirefly();
std::cout << "++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++" << std::endl;
std::cout << "Hybrid Simulated Annealing Test" << std::endl;
std::cout << "----------------------------------------------------------------" << std::endl;
testGaussianSA(3, 500, 200, 100.0, 0.1, GaussianSimulatedAnnealing::ResetToBestPoint, 150, GaussianSimulatedAnnealing::EveryNewPoint);
testGaussianSA(10, 500, 200, 100.0, 0.1, GaussianSimulatedAnnealing::ResetToBestPoint, 150, GaussianSimulatedAnnealing::EveryNewPoint);
testGaussianSA(30, 500, 200, 100.0, 0.1, GaussianSimulatedAnnealing::ResetToBestPoint, 150, GaussianSimulatedAnnealing::EveryNewPoint);
std::cout << "++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++" << std::endl;
std::cout << "Particle Swarm Optimization Test" << std::endl;
std::cout << "----------------------------------------------------------------" << std::endl;
testPSO(3);
testPSO(10);
testPSO(30);
std::cout << "++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++" << std::endl;
std::cout << "Simulated Annealing Test" << std::endl;
std::cout << "----------------------------------------------------------------" << std::endl;
testSimulatedAnnealing(3, 10000, 4000);
testSimulatedAnnealing(10, 10000, 4000);
testSimulatedAnnealing(30, 10000, 4000);
std::cout << "++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++" << std::endl;
std::cout << "Differential Evolution Test" << std::endl;
std::cout << "----------------------------------------------------------------" << std::endl;
testDifferentialEvolution(3, 50);
testDifferentialEvolution(10, 150);
testDifferentialEvolution(30, 450);
return 0;
} catch (std::exception& e) {
std::cerr << e.what() << std::endl;
return 1;
} catch (...) {
std::cerr << "unknown error" << std::endl;
return 1;
}
}
1-D array used in linear algebra.
bool empty() const
whether the array is empty
Size size() const
dimension of the array
Cost function abstract class for optimization problem.
Configuration & withBounds(bool b=true)
Configuration & withSeed(unsigned long s)
Configuration & withStepsizeWeight(Real w)
Configuration & withStrategy(Strategy s)
Configuration & withCrossoverProbability(Real p)
Configuration & withPopulationMembers(Size n)
Differential Evolution configuration object.
Criteria to end optimization process:
Constraint imposing i-th argument to be in [low_i,high_i] for all i
Abstract class for constrained optimization method.
virtual EndCriteria::Type minimize(Problem &P, const EndCriteria &endCriteria)=0
minimize the optimization problem P
Boltzmann Downhill Probability.
Constrained optimization problem.
const Array & currentValue() const
current value of the local minimum
Real value(const Array &x)
call cost function computation and increment evaluation counter
ext::function< Real(Real)> f_
Differential Evolution optimization method.
#define QL_REQUIRE(condition, message)
throw an error if the given pre-condition is not verified
Implementation based on: Yang, Xin-She (2009) Firefly Algorithm, Levy Flights and Global Optimization...
Maps function, bind and cref to either the boost or std implementation.
std::size_t Size
size of a container
Implementation based on: Very Fast Simulated Re-Annealing, Lester Ingber, Mathl. Comput....
Real DotProduct(const Array &v1, const Array &v2)
Implementation based on: Clerc, M., Kennedy, J. (2002) The particle swarm-explosion,...
Global definitions and compiler switches.
Numerical Recipes in C (second edition), Chapter 10.9, with the original exit criterion in f(x) repla...
Maps tuple to either the boost or std implementation.