1 /* Copyright (c) 2007 Massachusetts Institute of Technology
3 * Permission is hereby granted, free of charge, to any person obtaining
4 * a copy of this software and associated documentation files (the
5 * "Software"), to deal in the Software without restriction, including
6 * without limitation the rights to use, copy, modify, merge, publish,
7 * distribute, sublicense, and/or sell copies of the Software, and to
8 * permit persons to whom the Software is furnished to do so, subject to
9 * the following conditions:
11 * The above copyright notice and this permission notice shall be
12 * included in all copies or substantial portions of the Software.
14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18 * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19 * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20 * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
27 #include "nlopt-util.h"
31 /* test integrand from Joe and Kuo paper ... integrates to 1 */
32 static double testfunc(unsigned n, const double *x)
36 for (j = 1; j <= n; ++j) {
37 double cj = pow((double) j, 0.3333333333333333333);
38 f *= (fabs(4*x[j-1] - 2) + cj) / (1 + cj);
43 int main(int argc, char **argv)
45 unsigned n, j, i, sdim;
46 static double x[MAXDIM];
47 double testint_sobol = 0, testint_rand = 0;
50 fprintf(stderr, "Usage: %s <sdim> <ngen>\n", argv[0]);
53 nlopt_init_genrand(time(NULL));
55 s = nlopt_sobol_create(sdim);
57 nlopt_sobol_skip(s, n, x);
58 for (j = 1; j <= n; ++j) {
59 nlopt_sobol_next01(s, x);
60 testint_sobol += testfunc(sdim, x);
62 printf("x[%u]: %g", j, x[0]);
63 for (i = 1; i < sdim; ++i) printf(", %g", x[i]);
66 for (i = 0; i < sdim; ++i) x[i] = nlopt_urand(0.,1.);
67 testint_rand += testfunc(sdim, x);
69 nlopt_sobol_destroy(s);
70 printf("Test integral = %g using Sobol, %g using pseudorandom.\n",
71 testint_sobol / n, testint_rand / n);
72 printf(" error = %g using Sobol, %g using pseudorandom.\n",
73 testint_sobol / n - 1, testint_rand / n - 1);