/* Point_monte.cpp - Do a brain-dead Monte-Carlo simulation James S. Plank CS494/594 Lecture notes. Mon May 18 12:10:21 EDT 2020 */ #include "points.hpp" #include "MOA.hpp" #include #include #include #include #include #include #include using namespace std; using neuro::MOA; typedef std::runtime_error SRE; int main(int argc, char **argv) { Points points; vector x, best; double d; double max; long long seed; long long iterations; size_t i; long long j; MOA rng; if (argc != 3) { fprintf(stderr, "usage: points_monte iterations seed - points on stdin\n"); exit(1); } if (sscanf(argv[1], "%lld", &iterations) != 1) { fprintf(stderr, "Bad iterations\n"); exit(0); } if (sscanf(argv[2], "%lld", &seed) != 1) { fprintf(stderr, "Bad seed\n"); exit(0); } rng.Seed(seed); /* Read the points from stdin. */ try { points.Read(cin); } catch (const SRE &e) { fprintf(stderr, "Error reading points on stdin:\n%s\n", e.what()); exit(1); } max = 0; x.resize(points.p[0].size()); /* Choose random points */ for (j = 0; j < iterations; j++) { for (i = 0; i < x.size(); i++) x[i] = rng.Random_Double()*8.0+1.0; d = points.Min_Distance(x); if (d > max) { max = d; best = x; } } printf("Best x:"); for (i = 0; i < best.size(); i++) printf(" %lg", best[i]); printf("\n"); printf("Max: %lg\n", max); return 0; }