SSJ API Documentation
Stochastic Simulation in Java
Loading...
Searching...
No Matches
PaddedPointSet.java
1/*
2 * Class: PaddedPointSet
3 * Description: container class
4 * Environment: Java
5 * Software: SSJ
6 * Copyright (C) 2001 Pierre L'Ecuyer and Universite de Montreal
7 * Organization: DIRO, Universite de Montreal
8 * @author
9 * @since
10 *
11 *
12 * Licensed under the Apache License, Version 2.0 (the "License");
13 * you may not use this file except in compliance with the License.
14 * You may obtain a copy of the License at
15 *
16 * http://www.apache.org/licenses/LICENSE-2.0
17 *
18 * Unless required by applicable law or agreed to in writing, software
19 * distributed under the License is distributed on an "AS IS" BASIS,
20 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
21 * See the License for the specific language governing permissions and
22 * limitations under the License.
23 *
24 */
25package umontreal.ssj.hups;
26
27import umontreal.ssj.util.PrintfFormat;
28import umontreal.ssj.rng.RandomStream;
29
55public class PaddedPointSet extends PointSet {
56 protected int curPointSets = 0; // Current number of padded point sets.
57 protected int maxPointSets; // Max. number of padded point sets.
58 protected PointSet pointSet[]; // List of padded point sets
59 protected int startDim[]; // Starting dim. for padded points sets.
60 protected int permutation[][]; // One permutation for each point set.
61
70 public PaddedPointSet(int maxPointSets) {
71 this.maxPointSets = maxPointSets;
72 pointSet = new PointSet[maxPointSets];
73 startDim = new int[maxPointSets];
74 permutation = new int[maxPointSets][];
75 }
76
83 public void padPointSet(PointSet P) {
84 if (curPointSets == maxPointSets)
85 throw new IllegalArgumentException("Cannot pad more, increase maxPointSets parameter");
86
87 if (dim == Integer.MAX_VALUE)
88 throw new IllegalArgumentException("Cannot pad more, dimension already infinite");
89
90 if (curPointSets > 0 && numPoints != P.getNumPoints())
91 throw new IllegalArgumentException("Padded points must have same number of points");
92
93 if (curPointSets == 0)
94 numPoints = P.getNumPoints();
95
96 if (P.getDimension() == Integer.MAX_VALUE)
97 dim = Integer.MAX_VALUE;
98 else
99 dim += P.getDimension();
100
101 pointSet[curPointSets] = P;
102 startDim[curPointSets] = dim;
103 ++curPointSets;
104 }
105
115 if (curPointSets == 0)
116 numPoints = P.getNumPoints();
117 if (numPoints == Integer.MAX_VALUE)
118 throw new IllegalArgumentException("Cannot generate infinite permutation");
119 permutation[curPointSets] = new int[numPoints];
120 for (int i = 0; i < numPoints; i++)
121 permutation[curPointSets][i] = i;
122 padPointSet(P);
123 }
124
128 public double getCoordinate(int i, int j) {
129 int set = 0; // Will be the padded set to which this coordinate belongs,
130 if (j >= dim)
131 throw new IllegalArgumentException("Not enough dimensions");
132 while (j >= startDim[set])
133 set++;
134 /*
135 * if (permutation[set] == null) pointSet[set].resetPoint(i); else
136 * pointSet[set].resetPoint(permutation[set][i]);
137 *
138 * if (set == 0) pointSet[0].resetCoordinate (j); else
139 * pointSet[set].resetCoordinate (j - startDim[set - 1]);
140 *
141 * return pointSet[set].nextCoordinate();
142 */
143 if (permutation[set] != null)
144 i = permutation[set][i];
145 if (set != 0)
146 j = j - startDim[set - 1];
147 return pointSet[set].getCoordinate(i, j);
148 }
149
154 public void unrandomize() {
155 for (int set = 0; set < curPointSets; set++) {
156 if (permutation[set] != null) {
157 for (int i = 0; i < numPoints; i++)
158 permutation[set][i] = i;
159 }
160 }
161 }
162
167 public void randomize(RandomStream stream) {
168 // Executes the randomizations of the list
169 // super.randomize (stream); // Removed this because PointSet.randomize(stream)
170 // does nothing.
171 /* can also use lazy permutations */
172 for (int set = 0; set < curPointSets; set++)
173 if (permutation[set] != null) {
174 for (int i = 0; i < numPoints - 1; i++) {
175 int u = stream.nextInt(0, numPoints - i - 1);
176 int h = permutation[set][i];
177 permutation[set][i] = permutation[set][i + u];
178 permutation[set][i + u] = h;
179 }
180 }
181 }
182
187 return new PaddedIterator();
188 }
189
190 public String toString() {
191 StringBuffer sb = new StringBuffer("Padded point set" + PrintfFormat.NEWLINE);
192 sb.append("Maximal number of point sets: " + maxPointSets + PrintfFormat.NEWLINE);
193 sb.append("Current number of point sets: " + curPointSets + PrintfFormat.NEWLINE);
194 sb.append("Number of points: " + numPoints + PrintfFormat.NEWLINE);
195 for (int i = 0; i < curPointSets; i++) {
196 if (i != 0)
197 sb.append(PrintfFormat.NEWLINE);
198 if (permutation[i] == null)
199 sb.append("Point set ");
200 else
201 sb.append("Permuted point set ");
202 sb.append(i + " information: {" + PrintfFormat.NEWLINE + pointSet[i].toString() + PrintfFormat.NEWLINE + "}");
203 }
204 return sb.toString();
205 }
206
207 // ************************************************************
208
209 private class PaddedIterator extends DefaultPointSetIterator {
210
211 private PointSetIterator[] pointSetIterators; // One for each padded set.
212 private int currentSet = 0;
213 private double[] temp;
214
215 public PaddedIterator() {
216 pointSetIterators = new PointSetIterator[curPointSets];
217 int maxdim = 0;
218 for (int i = 0; i < curPointSets; i++) {
219 pointSetIterators[i] = pointSet[i].iterator();
220 if (pointSet[i].getDimension() > maxdim)
221 maxdim = pointSet[i].getDimension();
222 if (permutation[i] != null)
223 pointSetIterators[i].setCurPointIndex(permutation[i][0]);
224 }
225 if (maxdim == Integer.MAX_VALUE)
226 temp = new double[16];
227 else
228 temp = new double[maxdim];
229 }
230
231 public void setCurCoordIndex(int j) {
232 int set = 0;
233 if (j >= dim)
234 throw new IllegalArgumentException("Not enough dimensions");
235 while (j >= startDim[set])
236 set++;
237 currentSet = set;
238 pointSetIterators[currentSet].setCurCoordIndex(set == 0 ? j : j - startDim[set - 1]);
239 for (set = currentSet + 1; set < pointSetIterators.length; set++)
240 pointSetIterators[set].resetCurCoordIndex();
241 curCoordIndex = j;
242 }
243
244 public void resetCurCoordIndex() {
245 currentSet = 0;
246 for (int i = 0; i < pointSetIterators.length; i++)
247 pointSetIterators[i].resetCurCoordIndex();
248 curCoordIndex = 0;
249 }
250
251 public double nextCoordinate() {
252 if (curPointIndex >= numPoints || curCoordIndex >= dim)
253 outOfBounds();
254 if (curCoordIndex >= startDim[currentSet])
255 currentSet++;
256 double coord = pointSetIterators[currentSet].nextCoordinate();
257 curCoordIndex++;
258 return coord;
259 }
260
261 public void nextCoordinates(double[] p, int d) {
262 if (curPointIndex >= numPoints || d > dim)
263 outOfBounds();
264 int i = 0;
265 while (i < d) {
266 int dimen = pointSet[currentSet].getDimension();
267 if (dimen == Integer.MAX_VALUE)
268 dimen = d - i;
269 else
270 dimen -= pointSetIterators[currentSet].getCurCoordIndex();
271 pointSetIterators[currentSet].nextCoordinates(temp, dimen);
272 System.arraycopy(temp, 0, p, i, dimen);
273 i += dimen;
274 curCoordIndex += dimen;
275 if (i < d)
276 currentSet++;
277 }
278 }
279
280 public void setCurPointIndex(int i) {
281 for (int it = 0; it < pointSetIterators.length; it++)
282 pointSetIterators[it].setCurPointIndex(permutation[it] == null ? i : permutation[it][i]);
283 curPointIndex = i;
284 curCoordIndex = 0;
285 currentSet = 0;
286 }
287
288 public void resetCurPointIndex() {
289 for (int i = 0; i < pointSetIterators.length; i++) {
290 if (permutation[i] == null)
291 pointSetIterators[i].resetCurPointIndex();
292 else
293 pointSetIterators[i].setCurPointIndex(permutation[i][0]);
294 }
295 curPointIndex = 0;
296 curCoordIndex = 0;
297 currentSet = 0;
298 }
299
300 public int resetToNextPoint() {
301 for (int i = 0; i < pointSetIterators.length; i++) {
302 if (permutation[i] == null)
303 pointSetIterators[i].resetToNextPoint();
304 else
305 pointSetIterators[i].setCurPointIndex(permutation[i][curPointIndex + 1]);
306 }
307 currentSet = 0;
308 curCoordIndex = 0;
309 return ++curPointIndex;
310 }
311
312 public String formatState() {
313 return super.formatState() + PrintfFormat.NEWLINE + "Current padded set: " + currentSet;
314 }
315 }
316}
This abstract class represents a general point set.
Definition PointSet.java:99
int getNumPoints()
Returns the number of points.
int getDimension()
Returns the dimension (number of available coordinates) of the points.
This is the interface for iterators that permit one to go through the points of a PointSet and the su...
This interface defines the basic structures to handle multiple streams of uniform (pseudo)random numb...
int nextInt(int i, int j)
Returns a (pseudo)random number from the discrete uniform distribution over the integers ,...
PointSetIterator iterator()
Returns a PaddedIterator for this padded point set.
void padPointSetPermute(PointSet P)
Pads the point set P, which is assumed to be finite.
void unrandomize()
Erases all the random permutations of the padded point sets that are randomly permuted.
void padPointSet(PointSet P)
Pads the point set P to the present structure.
PaddedPointSet(int maxPointSets)
This container class realizes padded point sets, constructed by taking some coordinates from a point ...
double getCoordinate(int i, int j)
void randomize(RandomStream stream)
Randomly permutes the points for the padded point sets that have been added via padPointSetPermute.