SSJ API Documentation
Stochastic Simulation in Java
Loading...
Searching...
No Matches
SearcherKorobov.java
1/*
2 * Class: SearcherKorobov
3 * Description: searches the best Korobov lattices with respect to a
4 given discrepancy
5 * Environment: Java
6 * Software: SSJ
7 * Copyright (C) 2001 Pierre L'Ecuyer and Universite de Montreal
8 * Organization: DIRO, Universite de Montreal
9 * @author Richard Simard
10 * @since January 2009
11
12 * SSJ is free software: you can redistribute it and/or modify it under
13 * the terms of the GNU General Public License (GPL) as published by the
14 * Free Software Foundation, either version 3 of the License, or
15 * any later version.
16
17 * SSJ is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU General Public License for more details.
21
22 * A copy of the GNU General Public License is available at
23 <a href="http://www.gnu.org/licenses">GPL licence site</a>.
24 */
25package umontreal.ssj.discrepancy;
26
27import umontreal.ssj.util.Num;
28import umontreal.ssj.hups.KorobovLattice;
29
45public class SearcherKorobov extends Searcher {
46 protected int bestA; // best generator a for Korobov lattices
47
48 protected void print(int y) {
49 // For debugging
50 System.out.printf(" a = %d%n", y);
51 }
52
53 private void calcAs(int n, int s, int a) {
54 long b = a;
55 bestAs[0] = 1;
56 for (int j = 1; j < s; ++j)
57 bestAs[j] = (int) ((b * bestAs[j - 1]) % n);
58 }
59
60 private double exhaust(int s, boolean relPrime) {
61 int n = disc.getNumPoints();
62 double err;
63 gamma = disc.getGamma();
64 bestVal = Double.MAX_VALUE;
65 bestA = -1;
66
67 for (int i = 2; i < n; i++) {
68 if (relPrime) {
69 // Consider only values i relatively prime to n
70 if (power2F) {
71 if ((i & 1) == 0)
72 continue;
73 } else {
74 if (Num.gcd(n, i) != 1)
75 continue;
76 }
77 }
78 // print (i);
79 lat = new KorobovLattice(n, i, s);
80 err = disc.compute(lat, gamma);
81 if (err < bestVal) {
82 bestVal = err;
83 bestA = i;
84 }
85 }
86
87 calcAs(n, s, bestA);
88 return bestVal;
89 }
90
91 private double random(int s, int k, boolean relPrime) {
92 int n = disc.getNumPoints();
93 if (k >= n)
94 return exhaust(s, relPrime);
95
96 gamma = disc.getGamma();
97 final int nm1 = n - 1;
98 double err;
99 bestVal = Double.MAX_VALUE;
100 bestA = -1;
101 int a;
102 int i = 0;
103
104 while (i < k) {
105 if (power2F) {
106 a = gen.nextInt(2, nm1);
107 if (relPrime)
108 a |= 1;
109 } else {
110 do {
111 a = gen.nextInt(2, nm1);
112 } while (relPrime && (Num.gcd(n, a) != 1));
113 }
114 // print (a);
115 lat = new KorobovLattice(n, a, s);
116 i++;
117 err = disc.compute(lat, gamma);
118 if (err < bestVal) {
119 bestVal = err;
120 bestA = a;
121 }
122 }
123
124 calcAs(n, s, bestA);
125 return bestVal;
126 }
127
135 public SearcherKorobov(Discrepancy disc, boolean primeN) {
136 super(disc, primeN);
137 }
138
147 public double exhaust(int s) {
148 return exhaust(s, false);
149 }
150
155 public double exhaustPrime(int s) {
156 if (primeN)
157 return exhaust(s, false); // all a are rel. prime to n
158 else
159 return exhaust(s, true);
160 }
161
169 public double random(int s, int k) {
170 return random(s, k, false);
171 }
172
177 public double randomPrime(int s, int k) {
178 if (primeN)
179 return random(s, k, false);
180 else
181 return random(s, k, true);
182 }
183
188 public int getBestA() {
189 return bestA;
190 }
191
192}
This abstract class is the base class of all discrepancy classes.
double exhaustPrime(int s)
Similar to exhaust(s), except that only values of relatively prime to are considered.
double exhaust(int s)
Exhaustive search to find the best Korobov lattice (i.e.
double randomPrime(int s, int k)
Similar to random(s, k), except that only values of relatively prime to are considered.
SearcherKorobov(Discrepancy disc, boolean primeN)
The number of points , the dimension , and possibly the weight factors must be given in disc.
double random(int s, int k)
Random search to find the best Korobov lattice (with the smallest discrepancy) in dimension .
int getBestA()
Returns the generator of the lattice which gave the best value of the discrepancy in the last search...
Searcher(Discrepancy disc, boolean primeN)
The number of points , the dimension , and possibly the weight factors must be given in disc.
This class implements a Korobov lattice, which represents the same point set as in class LCGPointSet,...
This class provides various constants and methods to compute numerical quantities such as factorials,...
Definition Num.java:35
static int gcd(int x, int y)
Returns the greatest common divisor (gcd) of and .
Definition Num.java:200