45 std::cout <<
"Optimization Problem: Minimize Sphere Function in 2D" << std::endl;
46 std::cout <<
"Search Space: [-10, 10] per dimension" << std::endl;
47 std::cout <<
"Running optimizer..." << std::endl;
52 for (
auto val : coords) {
61 pso.
setMode(opt::OptimizationMode::MINIMIZE);
65 if (iteration == 0 || iteration % 10 == 0) {
66 std::cout <<
"[Sphere Test | Step " << std::setw(3) << iteration <<
"] "
67 <<
"Best Value: " << std::scientific << std::setprecision(5)
68 << current_best.
value << std::defaultfloat << std::endl;
73 auto start = std::chrono::high_resolution_clock::now();
75 auto end = std::chrono::high_resolution_clock::now();
76 std::chrono::duration<double, std::milli> duration = end - start;
79 std::cout <<
"\nOptimization Completed in " << duration.count() <<
" ms." << std::endl;
80 std::cout <<
"Best Value Found: " << std::fixed << std::setprecision(10) << best_sol.
value << std::endl;
81 std::cout <<
"Best Position: [ ";
82 for (
auto val : best_sol.
params) {
83 std::cout << std::fixed << std::setprecision(5) << val <<
" ";
85 std::cout <<
"]" << std::endl;
96 std::cout <<
"\n-------------------------------------------" << std::endl;
97 std::cout <<
"TEST 2: Boundary Constraint Test (Linear Plane)" << std::endl;
98 std::cout <<
"Objective: f(x,y) = x + y (Minimization)" << std::endl;
99 std::cout <<
"Search Space: [-10, 10] per dimension" << std::endl;
100 std::cout <<
"Expected Result: -20.0 at [-10.0, -10.0]" << std::endl;
101 std::cout <<
"Running optimizer..." << std::endl;
106 for (
auto val : coords) {
116 pso.
setMode(opt::OptimizationMode::MINIMIZE);
120 if (iteration % 20 == 0) {
121 std::cout <<
"[Boundary Test | Step " << std::setw(3) << iteration <<
"] "
122 <<
"Val: " << std::fixed << std::setprecision(4) << current_best.
value << std::endl;
127 auto start = std::chrono::high_resolution_clock::now();
129 auto end = std::chrono::high_resolution_clock::now();
130 std::chrono::duration<double, std::milli> duration = end - start;
133 std::cout <<
"Optimization Completed in " << duration.count() <<
" ms." << std::endl;
134 std::cout <<
"Best Value Found: " << std::fixed << std::setprecision(5) << best_sol.
value << std::endl;
135 std::cout <<
"Best Position: [ ";
136 for (
auto val : best_sol.
params) {
137 std::cout << std::fixed << std::setprecision(5) << val <<
" ";
139 std::cout <<
"]" << std::endl;
142 if (std::abs(best_sol.
value - (-20.0)) < 1e-3) {
143 std::cout <<
">> SUCCESS: Boundary minimum found correctly!" << std::endl;
145 std::cout <<
">> WARNING: Did not reach the exact boundary." << std::endl;
161 std::cout <<
"\n-------------------------------------------" << std::endl;
162 std::cout <<
"TEST 3: High-Dimensional Stress Test (Rastrigin Function)" << std::endl;
163 std::cout <<
"Dimension: " <<
dim <<
"D" << std::endl;
164 std::cout <<
"Search Space: [-5.12, 5.12] per dimension" << std::endl;
165 std::cout <<
"Goal: Find global minimum 0.0 (avoiding local traps)" << std::endl;
172 double pi = 3.14159265358979323846;
174 for (
auto x : coords) {
175 sum += (x * x) - (A * std::cos(2.0 * pi * x));
177 return A *
dim + sum;
198 local_pso.
setMode(opt::OptimizationMode::MINIMIZE);
203 std::cout <<
"[Rastrigin " << i <<
"] Best: "
204 << std::scientific << std::setprecision(4) << sol.
value
205 << std::defaultfloat << std::endl;
209 std::cout <<
"Running optimizer (this might take longer)..." << std::endl;
211 auto start = std::chrono::high_resolution_clock::now();
213 auto end = std::chrono::high_resolution_clock::now();
214 std::chrono::duration<double, std::milli> duration = end - start;
216 std::cout <<
"Optimization Completed in " << duration.count() <<
" ms." << std::endl;
217 std::cout <<
"Best Value Found: " << std::fixed << std::setprecision(5) << best_sol.
value << std::endl;
222 if (best_sol.
value < 1e-2) {
223 std::cout <<
">> SUCCESS: Global minimum found!" << std::endl;
224 }
else if (best_sol.
value < 5.0) {
225 std::cout <<
">> ACCEPTABLE: Found a good local minimum, but not global." << std::endl;
227 std::cout <<
">> FAIL: Stuck in a high local minimum." << std::endl;
232 std::cout <<
"===========================================" << std::endl;
233 std::cout <<
" Visual PSO Benchmark (2D Animation)" << std::endl;
234 std::cout <<
"===========================================" << std::endl;
248 double pi = 3.14159265358979323846;
249 for (
double val : x) sum += val*val - A*std::cos(2*pi*val);
254 pso.
setBounds({-5.12, -5.12}, {5.12, 5.12});
255 pso.
setMode(opt::OptimizationMode::MINIMIZE);
258 std::string baseName =
"pso_vis";
259 std::string gridFile =
"pso_grid.dat";
261 std::cout <<
"Generating background grid (heatmap)..." << std::endl;
269 std::cout <<
"Saved frame " << iter <<
"/" << config.
max_iterations <<
"\r" << std::flush;
273 std::cout <<
"Running optimization..." << std::endl;
275 std::cout <<
"\nOptimization finished." << std::endl;
278 std::cout <<
"Launching Gnuplot animation..." << std::endl;
283 std::cout <<
"===========================================" << std::endl;
284 std::cout <<
" Visual PSO Benchmark (3D Animation)" << std::endl;
285 std::cout <<
"===========================================" << std::endl;
297 double pi = 3.14159265358979323846;
298 for (
double val : x) {
299 sum += val * val - A * std::cos(2 * pi * val);
301 return 3.0 * A + sum;
307 double min_b = -5.12;
309 pso.
setBounds({min_b, min_b, min_b}, {max_b, max_b, max_b});
310 pso.
setMode(opt::OptimizationMode::MINIMIZE);
313 std::string baseName =
"pso_vis_3d";
314 std::string slicesFile =
"pso_slices_3d.dat";
317 std::cout <<
"Generating 3D function slices (walls)..." << std::endl;
323 if (iter % 10 == 0) std::cout <<
"Generating Frame " << iter <<
"/" << config.
max_iterations <<
"\r" << std::flush;
327 std::cout <<
"Running 3D optimization..." << std::endl;
329 std::cout <<
"\nOptimization finished." << std::endl;
332 std::cout <<
"Launching Gnuplot 3D animation..." << std::endl;
339 std::cout <<
"=========================================" << std::endl;
340 std::cout <<
" Particle Swarm Optimization (PSO) Benchmark" << std::endl;
341 std::cout <<
"=========================================" << std::endl;
374 }
catch (
const std::exception& e) {
375 std::cerr <<
"Optimization failed: " << e.what() << std::endl;
Benchmarking framework for Monte Carlo integration algorithms.
Particle Swarm Optimization algorithm.
Solution optimize() override
Execute the optimization loop for max_iterations.
const std::vector< Particle > & getParticles() const
Access the current swarm state (particles).
void setBounds(const Coordinates &lower, const Coordinates &upper) override
Set lower/upper bounds of the search hyper-rectangle.
void setCallback(StepCallback cb) override
Register a callback invoked after each step().
void setObjectiveFunction(ObjectiveFunction func) override
Set the objective function to optimize.
void setMode(OptimizationMode mode) override
Set optimization mode (minimize or maximize).
constexpr int dim
Default dimensionality for integration.
std::function< Real(const Coordinates &)> ObjectiveFunction
Objective function signature.
std::vector< Real > Coordinates
A point in the N-dimensional search space.
double Real
Scalar precision used across optimizers.
void createPSOAnimationScript3D(const std::string &scriptName, const std::string &slicesFile, const std::string &swarmBasename, size_t max_iter, const std::string &title, double min_bound, double max_bound)
void saveFunctionGrid(const std::string &filename, const Func &func, double x_min, double x_max, double y_min, double y_max, int resolution=100)
void createPSOAnimationScript(const std::string &scriptName, const std::string &gridFile, const std::string &swarmBasename, size_t max_iter, const std::string &title)
void saveSwarmFrame(const std::string &basename, size_t iteration, const std::vector< ParticleT > &swarm)
void saveFunctionSlices3D(const std::string &filename, const Func &func, double min, double max, int resolution=50)
void runSphereTest(opt::PSO &pso, const opt::Coordinates &lower, const opt::Coordinates &upper)
Test 1: Sphere Function.
void runVisualPSOBenchmark()
void runRastriginTest(opt::PSO &pso, int dim)
Test 3: High-Dimensional Rastrigin Function Objective: Minimize f(x) = 10n + sum(x_i^2 - 10cos(2*pi*x...
void runVisualPSO3DBenchmark()
void runBoundaryTest(opt::PSO &pso, const opt::Coordinates &lower, const opt::Coordinates &upper)
Test 2: Boundary Constraint Test Objective: Minimize f(x, y) = x + y This function is a constant incl...
void runOptimizationBenchmarksPSO()
Configuration parameters for PSO.
size_t population_size
Number of particles in the swarm.
Real social_coeff
Social coefficient (c2): scales attraction to global best.
Real cognitive_coeff
Cognitive coefficient (c1): scales attraction to particle best.
Real inertia_weight
Inertia weight (w): scales previous velocity.
size_t max_iterations
Number of iterations to run the optimizer.
Represents a candidate solution in the search space.
Coordinates params
Parameter vector (coordinates in the search space).
Real value
Evaluated objective value for params.