SSJ API Documentation
Stochastic Simulation in Java
Loading...
Searching...
No Matches
Searcher.java
1/*
2 * Class: Searcher
3 * Description: searches the best lattices of rank 1 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.rng.*;
28import umontreal.ssj.hups.*;
29import umontreal.ssj.util.Num;
30
51public class Searcher {
52
53 protected static RandomStream gen = new LFSR113(); // RNG used in random searches
54 protected Discrepancy disc;
55 protected PointSet lat;
56 protected double[] gamma;
57 protected double bestVal; // best value of discr in dim s
58 protected int[] bestAs; // best generator vector for lattices
59 protected boolean primeN = false; // true if n is prime
60 protected boolean power2F = false; // true if n is a power of 2
61
62 protected void print(int[] y, int s) {
63 // For debugging
64 System.out.printf(" a = [ ");
65 for (int j = 0; j < s; ++j) {
66 System.out.printf("%d ", y[j]);
67 }
68 System.out.printf("]%n");
69 }
70
71 private void incr(int[] y, int n, int s) {
72 // increment y[i]; if y[i] == n, set y[i] = 1 and increment y[i-1]; recursively.
73 // Thus we consider all y[i], each component taking all values in [1, 2, ...,
74 // n-1]
75 // If y[1] >= n, stop. The exhaustive search of all y[j] is done.
76
77 for (int i = s - 1; i >= 1; --i) {
78 ++y[i];
79 if (y[i] < n)
80 return;
81 if (i > 1)
82 y[i] = 1;
83 }
84 }
85
86 private void incrPrime(int[] y, int n, int s) {
87 // increment y[i] as in incr; but keeps only y[i] relatively prime to n.
88
89 for (int i = s - 1; i >= 1; --i) {
90 ++y[i];
91 if (power2F) {
92 ++y[i];
93 } else {
94 while (Num.gcd(n, y[i]) != 1)
95 ++y[i];
96 }
97 if (y[i] < n)
98 return;
99 if (i > 1)
100 y[i] = 1;
101 }
102 }
103
104 private double exhaust(int s, boolean relPrime) {
105 // If relPrime is true, only the multipliers y_j relatively prime to n
106 // are considered; otherwise, all multipliers are.
107
108 int n = disc.getNumPoints();
109 gamma = disc.getGamma();
110 double err;
111 int j;
112 int[] y = new int[s];
113 for (j = 0; j < s; j++)
114 y[j] = 1;
115 bestVal = Double.MAX_VALUE;
116 bestAs[0] = 1;
117
118 while (y[1] < n) {
119 // print (y, s);
120 lat = new Rank1Lattice(n, y, s);
121 err = disc.compute(lat, gamma);
122 // System.out.printf (" disc = %8.4f%n", err);
123 if (err < bestVal) {
124 bestVal = err;
125 // keep the best y in bestAs
126 for (j = 1; j < s; j++)
127 bestAs[j] = y[j];
128 }
129 if (relPrime)
130 incrPrime(y, n, s);
131 else
132 incr(y, n, s);
133 }
134
135 return bestVal;
136 }
137
138 private double random(int s, int k, boolean relPrime) {
139 // If relPrime is true, only the multipliers y_j relatively prime to n
140 // are considered; otherwise, all multipliers are.
141
142 int n = disc.getNumPoints();
143 gamma = disc.getGamma();
144 final int nm1 = n - 1;
145 double err;
146 int j;
147 bestVal = Double.MAX_VALUE;
148 int[] y = new int[s];
149 y[0] = bestAs[0] = 1;
150
151 for (int i = 0; i < k; i++) {
152 for (j = 1; j < s; j++) {
153 if (power2F) {
154 y[j] = gen.nextInt(1, nm1);
155 if (relPrime)
156 y[j] |= 1;
157 } else {
158 do {
159 y[j] = gen.nextInt(1, nm1);
160 } while (relPrime && (Num.gcd(n, y[j]) != 1));
161 }
162 }
163 // print (y, s);
164 lat = new Rank1Lattice(n, y, s);
165 err = disc.compute(lat, gamma);
166 if (err < bestVal) {
167 bestVal = err;
168 for (j = 1; j < s; j++)
169 bestAs[j] = y[j];
170 }
171 }
172
173 return bestVal;
174 }
175
183 public Searcher(Discrepancy disc, boolean primeN) {
184 this.disc = disc;
185 int s = disc.getDimension();
186 int n = disc.getNumPoints();
187 bestAs = new int[s];
188 this.primeN = primeN;
189 if ((n & (n - 1)) == 0)
190 power2F = true;
191 else
192 power2F = false;
193 }
194
203 public double exhaust(int s) {
204 return exhaust(s, false);
205 }
206
211 public double exhaustPrime(int s) {
212 if (primeN)
213 return exhaust(s, false); // all a_j are rel. prime to n
214 else
215 return exhaust(s, true);
216 }
217
226 public double random(int s, int k) {
227 return random(s, k, false);
228 }
229
234 public double randomPrime(int s, int k) {
235 if (primeN)
236 return random(s, k, false);
237 else
238 return random(s, k, true);
239 }
240
244 public double getBestVal() {
245 return bestVal;
246 }
247
253 public int[] getBestAs() {
254 return bestAs;
255 }
256
262 public void initGen(int seed) {
263 if (seed == 0 || seed == 1)
264 seed = 7654321;
265 final int s = 12345;
266 int[] state = { seed, s, s, s };
267 LFSR113.setPackageSeed(state);
268 gen = new LFSR113();
269 }
270
271}
This abstract class is the base class of all discrepancy classes.
void initGen(int seed)
Initializes the random number generator used in random searches with the starting seed seed.
double random(int s, int k)
Random search to find the lattice with the best (the smallest) discrepancy in dimension .
double getBestVal()
Returns the best value of the discrepancy found in the last search.
double randomPrime(int s, int k)
Similar to random(s, k), except that only values of relatively prime to are considered.
Searcher(Discrepancy disc, boolean primeN)
The number of points , the dimension , and possibly the weight factors must be given in disc.
double exhaust(int s)
Exhaustive search to find the lattice with the best (the smallest) discrepancy in dimension .
double exhaustPrime(int s)
Similar to exhaust(s), except that only values of relatively prime to are considered.
int[] getBestAs()
Returns the generator of the lattice which gave the best value of the discrepancy in the last search.
This abstract class represents a general point set.
Definition PointSet.java:99
int getNumPoints()
Returns the number of points.
This class implements point sets specified by integration lattices of rank 1.
Extends RandomStreamBase using a composite linear feedback shift register (LFSR) (or Tausworthe) RNG ...
Definition LFSR113.java:47
static void setPackageSeed(int[] seed)
Sets the initial seed for the class LFSR113 to the four integers of the vector seed[0....
Definition LFSR113.java:138
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
This interface defines the basic structures to handle multiple streams of uniform (pseudo)random numb...