40 double a = 1.0, b = 100.0;
41 return std::pow(a - x[0], 2) + b * std::pow(x[1] - x[0]*x[0], 2);
105 std::vector<double>
results(n_samples);
107 #pragma omp parallel for
108 for (
int i = 0; i < n_samples; ++i) {
111 std::uniform_real_distribution<double> dist(0.0, 1.0);
119 std::cout <<
"=== RNG Reproducibility Test ===\n\n";
121 bool all_passed =
true;
124 std::cout <<
"Init: set global seed once...\n";
128 std::cout <<
" set_global_seed returned: " << (seed_ok ?
"true" :
"false") <<
"\n";
129 std::cout <<
" get_global_seed: " << seed_now <<
"\n\n";
131 if (!seed_ok || seed_now != 12345u) {
132 std::cout <<
" FAILED: Global seed initialization failed\n\n";
139 std::cout <<
"Test 1: PSO determinism...\n";
143 std::cout <<
" Run 1: " << std::fixed << std::setprecision(10) << pso_run1 <<
"\n";
144 std::cout <<
" Run 2: " << std::fixed << std::setprecision(10) << pso_run2 <<
"\n";
146 if (std::abs(pso_run1 - pso_run2) < 1e-12) {
147 std::cout <<
" PASSED: Results are identical\n\n";
149 std::cout <<
" FAILED: Results differ!\n\n";
156 std::cout <<
"Test 2: GA determinism...\n";
160 std::cout <<
" Run 1: " << std::fixed << std::setprecision(10) << ga_run1 <<
"\n";
161 std::cout <<
" Run 2: " << std::fixed << std::setprecision(10) << ga_run2 <<
"\n";
163 if (std::abs(ga_run1 - ga_run2) < 1e-12) {
164 std::cout <<
" PASSED: Results are identical\n\n";
166 std::cout <<
" FAILED: Results differ!\n\n";
173 std::cout <<
"Test 3: Parallel sample generation reproducibility...\n";
175 int n_samples = 1000;
180 for (
int i = 0; i < n_samples; ++i) {
181 if (std::abs(samples1[i] - samples2[i]) > 1e-15) {
186 std::cout <<
" Generated " << n_samples <<
" samples in parallel\n";
187 std::cout <<
" Mismatches between runs: " << mismatches <<
"\n";
189 if (mismatches == 0) {
190 std::cout <<
" PASSED: All samples are identical\n\n";
192 std::cout <<
" FAILED: Some samples differ!\n\n";
199 std::cout <<
"=================================\n";
201 std::cout <<
"ALL TESTS PASSED!\n";
204 std::cout <<
"SOME TESTS FAILED!\n";
Genetic Algorithm (GA) interface and data structures.
Particle Swarm Optimization (PSO) interface and data structures.
Genetic Algorithm optimizer.
void setMode(OptimizationMode mode) override
Set optimization mode (minimize or maximize).
void setBounds(const Coordinates &lower, const Coordinates &upper) override
Set lower/upper bounds of the search hyper-rectangle.
Solution optimize() override
Run GA for max_generations.
void setObjectiveFunction(ObjectiveFunction func) override
Set the objective function to optimize.
Particle Swarm Optimization algorithm.
Solution optimize() override
Execute the optimization loop for max_iterations.
void setBounds(const Coordinates &lower, const Coordinates &upper) override
Set lower/upper bounds of the search hyper-rectangle.
void setObjectiveFunction(ObjectiveFunction func) override
Set the objective function to optimize.
void setMode(OptimizationMode mode) override
Set optimization mode (minimize or maximize).
bool set_global_seed(std::uint32_t s)
Set the global seed used by all library RNG components.
std::mt19937 make_engine(std::uint64_t stream_id)
Create a deterministic RNG engine for a specific stream.
std::uint32_t get_global_seed()
Get the current global seed.
Factory for creating deterministic, independent RNG engines.
Configuration parameters for GA.
size_t elitism_count
Number of top individuals copied unchanged to next generation.
size_t tournament_k
Tournament size for selection (k >= 2).
Real mutation_rate
Per-gene mutation probability.
size_t population_size
Size of the population.
Real crossover_rate
Probability of performing crossover in reproduction.
Real mutation_sigma
Mutation magnitude (scaled by coordinate span).
size_t max_generations
Number of generations to evolve.
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.
Real value
Evaluated objective value for params.
Stores benchmark result for a single sample count.
std::vector< double > generate_samples(int n_samples, std::uint64_t stream_id)
double rosenbrock(const std::vector< double > &x)
Rosenbrock function benchmark.
double run_ga_test()
Run GA on Rosenbrock function and return best value found.
double run_pso_test()
Run PSO on Rosenbrock function and return best value found.