Optimization Example 1: Demonstrates the use of the Particle Swarm method.
#endif
std::srand(std::time(nullptr));
cout << "\n" << "---------------------------------------------------------------------------------------------------\n";
cout << std::scientific; cout.precision(5);
cout << "EXAMPLE 1: use the Particle Swarm algorithm to minimize the six-hump camel function\n"
<< " f(x,y) = (4-2.1 *x^2 + x^4 / 3) * x^2 + x * y + (-4 + 4 * y^2) * y^2\n"
<< " domain is x in (-3, +3), y in (-2, +2)\n"
<< " using 50 particles and 200 iterations\n"
<< " See the comments in example_optimization_01.cpp\n\n";
cout << "the problem has two solutions at (-8.98420e-02, 7.12656e-01) and (8.98420e-02, -7.12656e-01)\n"
<< "the objective at the solutions is -1.03163.\n\n";
int num_dimensions = 2;
int num_particles = 50;
state.initializeParticlesInsideBox({-3.0, -2.0}, {3.0, 2.0});
num_dimensions,
[](const std::vector<double> &x)->double {
return (4.0 - 2.1 * x[0]*x[0] + x[0]*x[0]*x[0]*x[0] / 3.0) * x[0]*x[0] +
x[0] * x[1] +
(-4.0 + 4.0 * x[1]*x[1]) * x[1]*x[1];
});
int num_iterations = 200;
double inertia_weight = 0.5, cognitive_coeff = 2.0, social_coeff = 2.0;
inertia_weight, cognitive_coeff, social_coeff,
num_iterations, state);
std::vector<double> best_position = state.getBestPosition();
std::vector<double> best_objective(1);
objective(best_position, best_objective);
cout << "Using the objective function\n"
<< "found best position after 200 iteration:\n"
<< "best value for x = " << best_position[0] << "\n"
<< "best value for y = " << best_position[1] << "\n"
<< "value of the objective = " << best_objective[0] << "\n\n";
grid.setDomainTransform({-3.0, -2.0}, {3.0, 2.0});
std::vector<double> points = grid.getNeededPoints();
std::vector<double> values(grid.getNumNeeded());
objective(points, values);
grid.loadNeededValues(values);
state.initializeParticlesInsideBox({-3.0, -2.0}, {3.0, 2.0});
[&](std::vector<double> const &x, std::vector<double> &y)->void{
grid.evaluateBatch(x, y);
},
inertia_weight, cognitive_coeff, social_coeff,
num_iterations, state);
best_position = state.getBestPosition();
objective(best_position, best_objective);
cout << "Using the surrogate to the objective function\n"
<< "found best position after 200 iteration:\n"
<< "best value for x = " << best_position[0] << "\n"
<< "best value for y = " << best_position[1] << "\n"
<< "value of the objective = " << best_objective[0] << "\n\n";
cout << "Note: the method will find only one of the minimums\n"
<< " the random seed will determine which one.\n";
cout << "\n" << "---------------------------------------------------------------------------------------------------\n";
#ifndef __TASMANIAN_DOXYGEN_SKIP