33#define FUNCTION_FILE "../function.txt"
47 const std::string& filename,
48 std::vector<std::array<double, dim>>& normals,
49 std::vector<double>& offsets);
54int main(
int argc,
char* argv[]) {
56 std::uint32_t seed = 12345;
61 seed =
static_cast<std::uint32_t
>(std::stoul(argv[1]));
62 std::cout <<
"Using custom seed: " << seed << std::endl;
63 }
catch (
const std::exception& e) {
64 std::cerr <<
"Invalid seed argument. Using default seed: " << seed << std::endl;
67 std::cout <<
"Using default seed: " << seed << std::endl;
68 std::cout <<
"(To use a different seed, run: ./program <seed>)" << std::endl;
73 std::cout << std::endl;
78 std::cout <<
"===========================================" << std::endl;
79 std::cout <<
" Monte Carlo Integration Benchmarks" << std::endl;
80 std::cout <<
"===========================================" << std::endl;
93 std::cout <<
"Select mode:" << std::endl;
94 std::cout << Parser <<
". Use function from file (function.txt) - Uses Parser (Slower)" << std::endl;
95 std::cout << UnifHard <<
". Use hardcoded function with Uniform distribution - Uses C++ Lambda (Faster)" << std::endl;
96 std::cout << MeHaHard <<
". Use hardcoded function with Metropolis-Hastings distribution - Uses C++ Lambda (Faster)" << std::endl;
97 std::cout << Polytope <<
". Do you want to use a polytope con covex hull (IT: Inviluppo Convesso)" << std::endl;
98 std::cout << PSO <<
". Run Optimizer Benchmarks (PSO)" << std::endl;
99 std::cout << GA <<
". Run Optimizer Benchmarks (GA)" << std::endl;
100 std::cout <<
"Choice: ";
103 if (!(std::cin >> choice)) {
104 std::cerr <<
"Invalid input." << std::endl;
108 std::cin.ignore(std::numeric_limits<std::streamsize>::max(),
'\n');
110 bool useGnuplot =
false;
111 if (choice >= 1 && choice <= 2) {
112 std::cout <<
"Enable Gnuplot visualization for results? (y/n): ";
114 std::cin >> gpChoice;
115 std::cin.ignore(std::numeric_limits<std::streamsize>::max(),
'\n');
116 useGnuplot = (gpChoice ==
'y' || gpChoice ==
'Y');
119 if (choice == Parser) {
122 std::cout <<
"\nLoaded expression: " << expression << std::endl;
123 std::cout <<
"Starting PARSER benchmarks..." << std::endl;
125 }
catch (
const std::exception& e) {
126 std::cerr <<
"Error: " << e.what() << std::endl;
129 }
else if (choice == UnifHard) {
130 std::cout <<
"\nStarting HARDCODED benchmarks..." << std::endl;
133 }
else if (choice == MeHaHard) {
134 std::cout <<
"\nStarting HARDCODED benchmarks with Metropolis-Hastings distribution..." << std::endl;
137 }
else if (choice == Polytope) {
138 std::cout <<
"\nReading Points, Normals and Offsets..." << std::endl;
140 std::vector<mc::geom::Point<dim>> points = read_points_from_file<dim>(
"../points.txt");
143 std::vector<std::array<double, dim>> normals;
144 std::vector<double> offsets;
146 read_normals_and_offsets_from_qhull_n<dim>(
"../hull.txt", normals, offsets);
186 double I_const = integrator.
OLDintegrate(f_const, 1000000);
190 std::cout <<
"Integral f=1 ≈ " << I_const <<
" (exact: " << 3*std::sqrt(3)/2 <<
")\n";
191 std::cout <<
"Integral f=x ≈ " << I_x <<
" (exact: 0)\n";
192 std::cout <<
"Integral f=y ≈ " << I_y <<
" (exact: 0)\n";
194 }
else if (choice == PSO) {
196 }
else if (choice == GA) {
199 std::cerr <<
"Invalid choice selected." << std::endl;
203 std::cout <<
"\nAll benchmarks completed." << std::endl;
205 std::cout <<
"Check opened Gnuplot windows. Press Enter in console or close windows to finish fully if needed." << std::endl;
214 std::ifstream file(filename);
216 if (!file.is_open()) {
217 throw std::runtime_error(
"Could not open function file at: " + filename +
218 "\nMake sure the file exists in the repository root.");
221 std::string expression;
222 if (!std::getline(file, expression)) {
223 throw std::runtime_error(
"File is empty: " + filename);
226 if (!expression.empty() && expression.back() ==
'\r') {
227 expression.pop_back();
232 if (expression.empty()) {
233 throw std::runtime_error(
"Expression in file is empty: " + filename);
242 std::ifstream in(filename);
244 throw std::runtime_error(
"Cannot open file: " + filename);
247 std::size_t num_points = 0;
248 std::size_t file_dim = 0;
251 in >> num_points >> file_dim;
254 throw std::runtime_error(
"Error reading header from file: " + filename);
257 if (file_dim !=
static_cast<std::size_t
>(
dim)) {
258 throw std::runtime_error(
259 "Dimension mismatch: file has dim = " + std::to_string(file_dim) +
260 " but template expects dim = " + std::to_string(
dim));
263 std::vector<mc::geom::Point<dim>> points;
264 points.reserve(num_points);
266 for (std::size_t i = 0; i < num_points; ++i) {
268 for (
int k = 0; k <
dim; ++k) {
270 throw std::runtime_error(
271 "Error reading coordinate " + std::to_string(k) +
272 " of point " + std::to_string(i) +
273 " from file: " + filename);
284 const std::string& filename,
285 std::vector<std::array<double, dim>>& normals,
286 std::vector<double>& offsets)
288 std::ifstream in(filename);
290 throw std::runtime_error(
"Cannot open normals file: " + filename);
293 std::size_t file_dim = 0;
294 std::size_t num_facets = 0;
297 in >> file_dim >> num_facets;
299 throw std::runtime_error(
"Error reading header (dim, num_facets) from: " + filename);
302 if (file_dim !=
dim + 1) {
303 throw std::runtime_error(
304 "Dimension mismatch in normals file: file has dim = " +
305 std::to_string(file_dim) +
" but template expects dim = " +
306 std::to_string(
dim));
311 normals.reserve(num_facets);
312 offsets.reserve(num_facets);
314 for (std::size_t f = 0; f < num_facets; ++f) {
315 std::array<double, dim> n{};
319 for (std::size_t k = 0; k <
dim; ++k) {
321 throw std::runtime_error(
322 "Error reading normal component " + std::to_string(k) +
323 " for facet " + std::to_string(f) +
324 " from: " + filename);
330 throw std::runtime_error(
331 "Error reading offset d for facet " + std::to_string(f) +
332 " from: " + filename);
337 normals.push_back(n);
338 offsets.push_back(b);
Classic Monte Carlo integration engine.
Benchmarking framework for Monte Carlo integration algorithms.
void runBenchmarks(bool useGnuplot)
Run integration benchmarks with hardcoded integrands.
void runOptimizationBenchmarksGA()
void runBenchmarksMH()
Run Metropolis-Hastings MCMC integration benchmarks.
void runOptimizationBenchmarksPSO()
Convex polytope (convex polyhedron) integration domain.
N-dimensional point representation.
Uniform-sampling Monte Carlo integrator for N-dimensional domains.
double OLDintegrate(const std::function< double(const mc::geom::Point< dim > &)> &f, int n_samples)
Legacy integration routine (deprecated).
Core geometric types for Monte Carlo integration.
constexpr int dim
Default dimensionality for integration.
std::vector< mc::geom::Point< dim > > read_points_from_file(const std::string &filename)
void read_normals_and_offsets_from_qhull_n(const std::string &filename, std::vector< std::array< double, dim > > &normals, std::vector< double > &offsets)
#define FUNCTION_FILE
Path to user-defined integrand function.
std::string readFunctionFromFile(const std::string &filename)
bool set_global_seed(std::uint32_t s)
Set the global seed used by all library RNG components.
void closeGnuplotWindows()
Utility to close all currently open Gnuplot windows.
N-dimensional polytope (convex polyhedron) domain for Monte Carlo integration.