SSJ API Documentation
Stochastic Simulation in Java
Loading...
Searching...
No Matches
CycleBasedPointSet.java
1/*
2 * Class: CycleBasedPointSet
3 * Description: provides the basic structures for storing and manipulating
4 a highly uniform point set defined by a set of cycles
5 defined by a set of cycles.
6 * Environment: Java
7 * Software: SSJ
8 * Copyright (C) 2001 Pierre L'Ecuyer and Universite de Montreal
9 * Organization: DIRO, Universite de Montreal
10 * @author
11 * @since
12 *
13 *
14 * Licensed under the Apache License, Version 2.0 (the "License");
15 * you may not use this file except in compliance with the License.
16 * You may obtain a copy of the License at
17 *
18 * http://www.apache.org/licenses/LICENSE-2.0
19 *
20 * Unless required by applicable law or agreed to in writing, software
21 * distributed under the License is distributed on an "AS IS" BASIS,
22 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
23 * See the License for the specific language governing permissions and
24 * limitations under the License.
25 *
26 */
27package umontreal.ssj.hups;
28
29import umontreal.ssj.util.*;
30import umontreal.ssj.rng.RandomStream;
31import cern.colt.list.*;
32
51public abstract class CycleBasedPointSet extends PointSet {
52
53 protected int numCycles = 0; // Total number of cycles.
54 // dim = Integer.MAX_VALUE; // Dimension is infinite.
55 private double[] shift; // Random shift, initially null.
56 // Entry j is for dimension j.
57 protected ObjectArrayList cycles = new ObjectArrayList(); // List of cycles.
58
59 public double getCoordinate(int i, int j) {
60 // Find cycle that contains point i, then index in cycle.
61 int l = 0; // Length of next cycle.
62 int n = 0; // Total length of cycles added so far.
63 int k;
64 for (k = 0; n <= i; k++)
65 n += l = ((AbstractList) cycles.get(k)).size();
66 AbstractList curCycle = (AbstractList) cycles.get(k - 1);
67 int coordinate = (i - n + l + j) % curCycle.size();
68// double[] curCycleD = ((DoubleArrayList) curCycle).elements();
69// return curCycleD[coordinate];
70 double x = ((DoubleArrayList) curCycle).get(coordinate);
71 return x;
72 }
73
79 public void addRandomShift(int d1, int d2, RandomStream stream) {
80 if (null == stream)
81 throw new IllegalArgumentException(PrintfFormat.NEWLINE + " Calling addRandomShift with null stream");
82 if (0 == d2)
83 d2 = Math.max(dim, 1);
84 if (shift == null) {
85 shift = new double[d2];
86 capacityShift = d2;
87 } else if (d2 > capacityShift) {
88 int d3 = Math.max(4, capacityShift);
89 while (d2 > d3)
90 d3 *= 2;
91 double[] temp = new double[d3];
92 capacityShift = d3;
93 for (int i = 0; i < d1; i++)
94 temp[i] = shift[i];
95 shift = temp;
96 }
97 dimShift = d2;
98 for (int i = d1; i < d2; i++)
99 shift[i] = stream.nextDouble();
100 shiftStream = stream;
101 }
102
106 public void clearRandomShift() {
107 super.clearRandomShift();
108 shift = null;
109 }
110
115 protected void addCycle(AbstractList c) {
116 // Adds the cycle `c` to the list of all cycles.
117 // Used by subclass constructors to fill up the list of cycles.
118 cycles.add(c);
119 numCycles++;
120 numPoints += c.size();
121 }
122
123 public int getDimension() {
124 return Integer.MAX_VALUE;
125 }
126
128 return new CycleBasedPointSetIterator();
129 }
130
131 public String toString() {
132 String s = super.toString();
133 return s + PrintfFormat.NEWLINE + "Number of cycles: " + numCycles;
134 }
135
136 public String formatPoints() {
137 StringBuffer sb = new StringBuffer(toString());
138 for (int c = 0; c < numCycles; c++) {
139 AbstractList curCycle = (AbstractList) cycles.get(c);
140 double[] cycle = ((DoubleArrayList) curCycle).elements();
141 sb.append(PrintfFormat.NEWLINE + "Cycle " + c + ": (");
142 boolean first = true;
143 for (int e = 0; e < curCycle.size(); e++) {
144 if (first)
145 first = false;
146 else
147 sb.append(", ");
148 sb.append(cycle[e]);
149 }
150 sb.append(")");
151 }
152 return sb.toString();
153 }
154
155 // %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
156
157 public class CycleBasedPointSetIterator extends DefaultPointSetIterator {
158
159 protected int startPointInCycle = 0; // Index where the current point
160 // starts in the current cycle.
161 protected int curCoordInCycle = 0; // Index of the current coordinate
162 // in the current cycle.
163 protected int curCycleIndex = 0; // Index of the current cycle.
164 protected AbstractList curCycle; // The current cycle.
165 protected double[] curCycleD; // The array for current cycle
166
167 public CycleBasedPointSetIterator() {
168 init();
169 }
170
171 protected void init() {
172 resetCurCycle(0);
173 }
174
175 public void resetCurCycle(int index) {
176 curCycleIndex = index;
177 curCycle = (AbstractList) cycles.get(index);
178 curCycleD = ((DoubleArrayList) curCycle).elements();
179 }
180
181 public void setCurCoordIndex(int i) {
182 curCoordIndex = i;
183 curCoordInCycle = (i + startPointInCycle) % curCycle.size();
184 }
185
186 public void resetCurCoordIndex() {
187 curCoordIndex = 0;
188 curCoordInCycle = startPointInCycle;
189 }
190
191 public boolean hasNextCoordinate() {
192 return true;
193 }
194
195 // We want to avoid generating 0 or 1
196 public double nextDouble() {
197 return nextCoordinate() + EpsilonHalf;
198 }
199
200 public double nextCoordinate() {
201 // First, verify if there are still points....
203 outOfBounds();
204 double x = curCycleD[curCoordInCycle];
205 if (shift != null) {
206 if (curCoordIndex >= dimShift) // Extend the shift.
208 x += shift[curCoordIndex];
209 if (x >= 1.0)
210 x -= 1.0;
211 if (x <= 0.0)
212 x = EpsilonHalf; // avoid x = 0
213 }
215 curCoordInCycle++;
216 if (curCoordInCycle >= curCycle.size())
217 curCoordInCycle = 0;
218 return x;
219 }
220
221 public void nextCoordinates(double p[], int dim) {
222 // First, verify if there are still points....
224 outOfBounds();
225 if (curCoordIndex + dim >= dimShift)
227 // int j = curCoordInCycle;
228 int maxj = curCycle.size();
229 double x;
230 for (int i = 0; i < dim; i++) {
231 x = curCycleD[curCoordInCycle++];
232 if (curCoordInCycle >= maxj)
233 curCoordInCycle = 0;
234 if (shift != null) {
235 x += shift[curCoordIndex + i];
236 if (x >= 1.0)
237 x -= 1.0;
238 if (x <= 0.0)
239 x = EpsilonHalf; // avoid x = 0
240 }
241 p[i] = x;
242 }
244 }
245
246 public void setCurPointIndex(int i) {
247 int l = 0;
248 int n = 0;
249 int j;
250 for (j = 0; n <= i; j++)
251 n += l = ((AbstractList) cycles.get(j)).size();
252 resetCurCycle(j - 1);
253 startPointInCycle = i - n + l;
254 curPointIndex = i;
255 curCoordIndex = 0;
256 curCoordInCycle = startPointInCycle;
257 }
258
259 public void resetCurPointIndex() {
260 resetCurCycle(0);
261 startPointInCycle = 0;
262 curPointIndex = 0;
263 curCoordIndex = 0;
264 curCoordInCycle = 0;
265 }
266
267 public int resetToNextPoint() {
269 startPointInCycle++;
270 if (startPointInCycle >= curCycle.size()) {
271 startPointInCycle = 0;
272 if (curCycleIndex < (numCycles - 1))
273 resetCurCycle(curCycleIndex + 1);
274 }
275 curCoordIndex = 0;
276 curCoordInCycle = startPointInCycle;
277 return curPointIndex;
278 }
279
280 public int nextPoint(double p[], int dim) {
281 // First, verify if there are still points....
283 outOfBounds();
284 int j = startPointInCycle;
285 int maxj = curCycle.size() - 1;
286 for (int i = 0; i < dim; i++) {
287 p[i] = curCycleD[j];
288 if (j < maxj)
289 j++;
290 else
291 j = 0;
292 }
294 return curPointIndex;
295 }
296
297 public String formatState() {
298 return super.formatState() + PrintfFormat.NEWLINE + "Current cycle: " + curCycleIndex;
299 }
300 }
301
302}
int nextPoint(double p[], int dim)
Same as nextPoint(p, 0, d).
void resetCurPointIndex()
Resets both the current point index and the current coordinate to 0.
void setCurPointIndex(int i)
Resets the current point index to i and current coordinate to 0.
int resetToNextPoint()
Resets the current point index to the next one and current coordinate to 0, and returns the new curre...
This abstract class provides the basic structures for storing and manipulating a point set defined by...
void addCycle(AbstractList c)
Adds the cycle c to the list of all cycles.
PointSetIterator iterator()
Constructs and returns a point set iterator.
int getDimension()
Returns the dimension (number of available coordinates) of the points.
String formatPoints()
Same as invoking formatPoints(n, d) with and equal to the number of points and the dimension of thi...
void addRandomShift(int d1, int d2, RandomStream stream)
Same as the same method in PointSet.
double getCoordinate(int i, int j)
Returns , the coordinate of the point .
void clearRandomShift()
Erases the current random shift, if any.
String toString()
Formats a string that contains information about the point set.
This class implements a default point set iterator.
int curPointIndex
Index of the current point.
int curCoordIndex
Index of the current coordinate.
double EpsilonHalf
Default constant epsilon/2 added to the points after a random shift.
void outOfBounds()
Error message for index out of bounds.
This abstract class represents a general point set.
Definition PointSet.java:99
int dimShift
Current dimension of the shift.
void addRandomShift()
Same as addRandomShift(0, dim), where dim is the dimension of the point set.
int numPoints
Number of points.
int getNumPoints()
Returns the number of points.
int capacityShift
Number of array elements in the shift vector, always >= dimShift.
int dim
Dimension of the points.
RandomStream shiftStream
Stream used to generate the random shifts.
This class acts like a StringBuffer which defines new types of append methods.
static final String NEWLINE
End-of-line symbol or line separator.
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...
double nextDouble()
Returns a (pseudo)random number from the uniform distribution over the interval , using this stream,...