You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@commons.apache.org by md...@apache.org on 2003/06/07 03:36:58 UTC
cvs commit: jakarta-commons-sandbox/math/src/test/org/apache/commons/math ValueServerTest.java
mdiggory 2003/06/06 18:36:58
Modified: math/src/java/org/apache/commons/math ValueServer.java
math/src/test/org/apache/commons/math ValueServerTest.java
Log:
PR: http://nagoya.apache.org/bugzilla/show_bug.cgi?id=20543
Submitted by: phil@steitz.com
Revision Changes Path
1.2 +137 -73 jakarta-commons-sandbox/math/src/java/org/apache/commons/math/ValueServer.java
Index: ValueServer.java
===================================================================
RCS file: /home/cvs/jakarta-commons-sandbox/math/src/java/org/apache/commons/math/ValueServer.java,v
retrieving revision 1.1
retrieving revision 1.2
diff -u -r1.1 -r1.2
--- ValueServer.java 21 May 2003 14:21:15 -0000 1.1
+++ ValueServer.java 7 Jun 2003 01:36:57 -0000 1.2
@@ -61,9 +61,11 @@
import java.net.MalformedURLException;
/**
- * Generates values for use in simulation applications.<br>
+ * Generates values for use in simulation applications.
+ * <p>
* How values are generated is determined by the <code>mode</code>
- * property. <p>
+ * property.
+ * <p>
* Supported <code>mode</code> values are: <ul>
* <li> DIGEST_MODE -- uses an empirical distribution </li>
* <li> REPLAY_MODE -- replays data from <code>valuesFile</code></li>
@@ -110,13 +112,13 @@
/** Replay data from valuesFilePath */
public static final int REPLAY_MODE = 1;
- /** Uniform random variates with mean = mu */
+ /** Uniform random deviates with mean = mu */
public static final int UNIFORM_MODE = 2;
- /** Exponential random variates with mean = mu */
+ /** Exponential random deviates with mean = mu */
public static final int EXPONENTIAL_MODE = 3;
- /** Gaussian random variates with mean = mu, std dev = sigma */
+ /** Gaussian random deviates with mean = mu, std dev = sigma */
public static final int GAUSSIAN_MODE = 4;
/** Always return mu */
@@ -128,10 +130,10 @@
/**
* Returns the next generated value, generated according
- * to the mode value (see MODE constants)
+ * to the mode value (see MODE constants).
*
* @return generated value
- * @throws IOException in REPLAY_MODE if file I/O error occurs
+ * @throws IOException in REPLAY_MODE if a file I/O error occurs
*/
public double getNext() throws IOException {
switch (mode) {
@@ -146,8 +148,36 @@
}
}
+ /**
+ * Fills the input array with values generated using getNext() repeatedly.
+ *
+ * @param values array to be filled
+ * @throws IOException in REPLAY_MODE if a file I/O error occurs
+ */
+ public void fill(double[] values) throws IOException {
+ for (int i = 0; i < values.length; i++) {
+ values[i] = getNext();
+ }
+ }
+
+ /**
+ * Returns an array of length <code>length</code> with values generated
+ * using getNext() repeatedly.
+ *
+ * @param length length of output array
+ * @return array of generated values
+ * @throws IOException in REPLAY_MODE if a file I/O error occurs
+ */
+ public double[] fill(int length) throws IOException {
+ double[] out = new double[length];
+ for (int i = 0; i < length; i++) {
+ out[i] = getNext();
+ }
+ return out;
+ }
+
/**
- * Computes the empirical distribution using values from file
+ * Computes the empirical distribution using values from the file
* in <code>valuesFilePath</code>, using the default number of bins.
* <p>
* <code>valuesFileURL</code> must exist and be
@@ -173,16 +203,106 @@
* This method must be called before using <code>getNext()</code>
* with <code>mode = DISGEST_MODE</code>
*
+ * @param binCount the number of bins used in computing the empirical
+ * distribution
* @throws IOException if an error occurs reading the input file
*/
public void computeDistribution(int binCount)
- throws IOException{
+ throws IOException {
empiricalDistribution = new EmpiricalDistributionImpl(binCount);
empiricalDistribution.load(valuesFileURL.getFile());
mu = empiricalDistribution.getSampleStats().getMean();
sigma = empiricalDistribution.getSampleStats().getStandardDeviation();
}
+ /** Getter for property mode.
+ * @return Value of property mode.
+ */
+ public int getMode() {
+ return mode;
+ }
+
+ /** Setter for property mode.
+ * @param mode New value of property mode.
+ */
+ public void setMode(int mode) {
+ this.mode = mode;
+ }
+
+ /** Getter for property valuesFilePath.
+ * @return Value of property valuesFilePath.
+ */
+ public String getValuesFileURL() {
+ return valuesFileURL.toString();
+ }
+
+ /** Setter for property valuesFilePath.
+ * @param url New value of property valuesFilePath.
+ * @throws MalformedURLException if url is not well formed
+ */
+ public void setValuesFileURL(String url) throws MalformedURLException {
+ this.valuesFileURL = new URL(url);
+ }
+
+ /** Getter for property empiricalDistribution.
+ * @return Value of property empiricalDistribution.
+ */
+ public EmpiricalDistribution getEmpiricalDistribution() {
+ return empiricalDistribution;
+ }
+
+ /**
+ * Opens <code>valuesFilePath</code> to use in REPLAY_MODE.
+ *
+ * @throws IOException if an error occurs opening the file
+ */
+ public void openReplayFile() throws IOException {
+ filePointer = new BufferedReader(new FileReader
+ (new File(valuesFileURL.getFile())));
+ }
+
+ /**
+ * Closes <code>valuesFilePath</code> after use in REPLAY_MODE.
+ *
+ * @throws IOException if an error occurs closing the file
+ */
+ public void closeReplayFile() throws IOException {
+ if (filePointer != null) {
+ filePointer.close();
+ filePointer = null;
+ }
+ }
+
+ /** Getter for property mu.
+ * @return Value of property mu.
+ */
+ public double getMu() {
+ return mu;
+ }
+
+ /** Setter for property mu.
+ * @param mu New value of property mu.
+ */
+ public void setMu(double mu) {
+ this.mu = mu;
+ }
+
+ /** Getter for property sigma.
+ * @return Value of property sigma.
+ */
+ public double getSigma() {
+ return sigma;
+ }
+
+ /** Setter for property sigma.
+ * @param sigma New value of property sigma.
+ */
+ public void setSigma(double sigma) {
+ this.sigma = sigma;
+ }
+
+ //------------- private methods ---------------------------------
+
/**
* Gets a random value in DIGEST_MODE.
* <p>
@@ -191,7 +311,7 @@
* must have completed successfully; otherwise an
* <code>IllegalStateException</code> will be thrown</li></ul>
*
- * @return next random value form the empirical distribution digest
+ * @return next random value from the empirical distribution digest
*/
private double getNextDigest() {
if ((empiricalDistribution == null) ||
@@ -216,7 +336,7 @@
* @return next value from the replay file
* @throws IOException if there is a problem reading from the file
*/
- private double getNextReplay() throws IOException{
+ private double getNextReplay() throws IOException {
String str = null;
if (filePointer == null) {
throw new IllegalStateException("replay file not open");
@@ -230,16 +350,16 @@
}
/**
- * Gets a uniformly distributed random value with mean = mu
+ * Gets a uniformly distributed random value with mean = mu.
*
* @return random uniform value
*/
private double getNextUniform() {
- return 2.0*mu*Math.random();
+ return randomData.nextUniform(0, 2 * mu);
}
/**
- * Gets an exponentially distributed random value with mean = mu
+ * Gets an exponentially distributed random value with mean = mu.
*
* @return random exponential value
*/
@@ -249,68 +369,12 @@
/**
* Gets a Gaussian distributed random value with mean = mu
- * and standard deviation = sigma
+ * and standard deviation = sigma.
*
* @return random Gaussian value
*/
private double getNextGaussian() {
- return randomData.nextGaussian(mu,sigma);
- }
-
- /** Getter for property mode.
- * @return Value of property mode.
- */
- public int getMode() {
- return mode;
- }
-
- /** Setter for property mode.
- * @param mode New value of property mode.
- */
- public void setMode(int mode) {
- this.mode = mode;
- }
-
- /** Getter for property valuesFilePath.
- * @return Value of property valuesFilePath.
- */
- public String getValuesFileURL() {
- return valuesFileURL.toString();
- }
-
- /** Setter for property valuesFilePath.
- * @param valuesFilePath New value of property valuesFilePath.
- */
- public void setValuesFileURL(String URL) throws MalformedURLException {
- this.valuesFileURL = new URL(URL);
- }
-
- /** Getter for property empiricalDistribution.
- * @return Value of property empiricalDistribution.
- */
- public EmpiricalDistribution getEmpiricalDistribution() {
- return empiricalDistribution;
- }
-
- /**
- * Opens <code>valuesFilePath</code> to use in REPLAY_MODE
- *
- * @throws IOException if an error occurs opening the file
- */
- public void openReplayFile() throws IOException {
- filePointer = new BufferedReader(new FileReader
- (new File(valuesFileURL.getFile())));
- }
-
- /**
- * Closes <code>valuesFilePath</code> after use in REPLAY_MODE
- *
- * @throws IOException if an error occurs closing the file
- */
- public void closeReplayFile() throws IOException {
- if (filePointer != null) {
- filePointer.close();
- }
+ return randomData.nextGaussian(mu, sigma);
}
}
1.4 +82 -2 jakarta-commons-sandbox/math/src/test/org/apache/commons/math/ValueServerTest.java
Index: ValueServerTest.java
===================================================================
RCS file: /home/cvs/jakarta-commons-sandbox/math/src/test/org/apache/commons/math/ValueServerTest.java,v
retrieving revision 1.3
retrieving revision 1.4
diff -u -r1.3 -r1.4
--- ValueServerTest.java 29 May 2003 20:35:45 -0000 1.3
+++ ValueServerTest.java 7 Jun 2003 01:36:57 -0000 1.4
@@ -103,6 +103,8 @@
double next = 0.0;
double tolerance = 0.1;
vs.computeDistribution();
+ assertTrue("empirical distribution property",
+ vs.getEmpiricalDistribution() != null);
Univariate stats = new UnivariateImpl();
for (int i = 1; i < 1000; i++) {
next = vs.getNext();
@@ -110,7 +112,20 @@
}
assertEquals("mean", 5.069831575018909, stats.getMean(), tolerance);
assertEquals
- ("std dev", 1.0173699343977738, stats.getStandardDeviation(), tolerance);
+ ("std dev", 1.0173699343977738, stats.getStandardDeviation(),
+ tolerance);
+
+ vs.computeDistribution(500);
+ stats = new UnivariateImpl();
+ for (int i = 1; i < 1000; i++) {
+ next = vs.getNext();
+ stats.addValue(next);
+ }
+ assertEquals("mean", 5.069831575018909, stats.getMean(), tolerance);
+ assertEquals
+ ("std dev", 1.0173699343977738, stats.getStandardDeviation(),
+ tolerance);
+
}
/**
@@ -159,5 +174,70 @@
assertEquals(compareValue,firstDataValue,tolerance);
compareValue = vs.getNext();
assertEquals(compareValue,secondDataValue,tolerance);
+ vs.closeReplayFile();
+ // make sure no NPE
+ vs.closeReplayFile();
+ }
+
+ /**
+ * Test other ValueServer modes
+ */
+ public void testModes() throws Exception {
+ vs.setMode(ValueServer.CONSTANT_MODE);
+ vs.setMu(0);
+ assertEquals("constant mode test",vs.getMu(),vs.getNext(),Double.MIN_VALUE);
+ vs.setMode(ValueServer.UNIFORM_MODE);
+ vs.setMu(2);
+ double val = vs.getNext();
+ assertTrue(val > 0 && val < 4);
+ vs.setSigma(1);
+ vs.setMode(ValueServer.GAUSSIAN_MODE);
+ val = vs.getNext();
+ assertTrue("gaussian value close enough to mean",
+ val < vs.getMu() + 100*vs.getSigma());
+ vs.setMode(ValueServer.EXPONENTIAL_MODE);
+ val = vs.getNext();
+ assertTrue(val > 0);
+ try {
+ vs.setMode(1000);
+ vs.getNext();
+ fail("bad mode, expecting IllegalStateException");
+ } catch (IllegalStateException ex) {
+ ;
+ }
+ }
+
+ /**
+ * Test fill
+ */
+ public void testFill() throws Exception {
+ vs.setMode(ValueServer.CONSTANT_MODE);
+ vs.setMu(2);
+ double[] val = new double[5];
+ vs.fill(val);
+ for (int i = 0; i < 5; i++) {
+ assertEquals("fill test in place",2,val[i],Double.MIN_VALUE);
+ }
+ double v2[] = vs.fill(3);
+ for (int i = 0; i < 3; i++) {
+ assertEquals("fill test in place",2,v2[i],Double.MIN_VALUE);
+ }
+ }
+
+ /**
+ * Test getters to make Clover happy
+ */
+ public void testProperties() throws Exception {
+ vs.setMode(ValueServer.CONSTANT_MODE);
+ assertEquals("mode test",ValueServer.CONSTANT_MODE,vs.getMode());
+ vs.setValuesFileURL("http://www.apache.org");
+ String s = vs.getValuesFileURL();
+ assertEquals("valuesFileURL test","http://www.apache.org",s);
}
+
+
+
+
+
+
}
---------------------------------------------------------------------
To unsubscribe, e-mail: commons-dev-unsubscribe@jakarta.apache.org
For additional commands, e-mail: commons-dev-help@jakarta.apache.org