|
Java example source code file (ValueServer.java)
The ValueServer.java Java example source code/* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You under the Apache License, Version 2.0 * (the "License"); you may not use this file except in compliance with * the License. You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package org.apache.commons.math3.random; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.net.MalformedURLException; import java.net.URL; import org.apache.commons.math3.exception.MathIllegalArgumentException; import org.apache.commons.math3.exception.MathIllegalStateException; import org.apache.commons.math3.exception.NullArgumentException; import org.apache.commons.math3.exception.ZeroException; import org.apache.commons.math3.exception.util.LocalizedFormats; /** * Generates values for use in simulation applications. * <p> * How values are generated is determined by the <code>mode * property.</p> * <p> * Supported <code>mode values are:
valuesFileURL
* <li> UNIFORM_MODE -- generates uniformly distributed random values with
* mean = <code>mu
* <li> EXPONENTIAL_MODE -- generates exponentially distributed random values
* with mean = <code>mu
* <li> GAUSSIAN_MODE -- generates Gaussian distributed random values with
* mean = <code>mu and
* standard deviation = <code>sigma
* <li> CONSTANT_MODE -- returns mu every time.binCount bins.
* <p>
* <code>valuesFileURL must exist and be readable by this process
* at runtime.</p>
* <p>
* This method must be called before using <code>getNext()
* with <code>mode = DIGEST_MODE
*
* @param binCount the number of bins used in computing the empirical
* distribution
* @throws NullArgumentException if the {@code valuesFileURL} has not been set
* @throws IOException if an error occurs reading the input file
* @throws ZeroException if URL contains no data
*/
public void computeDistribution(int binCount) throws NullArgumentException, IOException, ZeroException {
empiricalDistribution = new EmpiricalDistribution(binCount, randomData.getRandomGenerator());
empiricalDistribution.load(valuesFileURL);
mu = empiricalDistribution.getSampleStats().getMean();
sigma = empiricalDistribution.getSampleStats().getStandardDeviation();
}
/**
* Returns the data generation mode. See {@link ValueServer the class javadoc}
* for description of the valid values of this property.
*
* @return Value of property mode.
*/
public int getMode() {
return mode;
}
/**
* Sets the data generation mode.
*
* @param mode New value of the data generation mode.
*/
public void setMode(int mode) {
this.mode = mode;
}
/**
* Returns the URL for the file used to build the empirical distribution
* when using {@link #DIGEST_MODE}.
*
* @return Values file URL.
*/
public URL getValuesFileURL() {
return valuesFileURL;
}
/**
* Sets the {@link #getValuesFileURL() values file URL} using a string
* URL representation.
*
* @param url String representation for new valuesFileURL.
* @throws MalformedURLException if url is not well formed
*/
public void setValuesFileURL(String url) throws MalformedURLException {
this.valuesFileURL = new URL(url);
}
/**
* Sets the the {@link #getValuesFileURL() values file URL}.
*
* <p>The values file must be an ASCII text file containing one
* valid numeric entry per line.</p>
*
* @param url URL of the values file.
*/
public void setValuesFileURL(URL url) {
this.valuesFileURL = url;
}
/**
* Returns the {@link EmpiricalDistribution} used when operating in {@value #DIGEST_MODE}.
*
* @return EmpircalDistribution built by {@link #computeDistribution()}
*/
public EmpiricalDistribution getEmpiricalDistribution() {
return empiricalDistribution;
}
/**
* Resets REPLAY_MODE file pointer to the beginning of the <code>valuesFileURL.
*
* @throws IOException if an error occurs opening the file
* @throws NullPointerException if the {@code valuesFileURL} has not been set.
*/
public void resetReplayFile() throws IOException {
if (filePointer != null) {
try {
filePointer.close();
filePointer = null;
} catch (IOException ex) { //NOPMD
// ignore
}
}
filePointer = new BufferedReader(new InputStreamReader(valuesFileURL.openStream(), "UTF-8"));
}
/**
* Closes {@code valuesFileURL} 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;
}
}
/**
* Returns the mean used when operating in {@link #GAUSSIAN_MODE}, {@link #EXPONENTIAL_MODE}
* or {@link #UNIFORM_MODE}. When operating in {@link #CONSTANT_MODE}, this is the constant
* value always returned. Calling {@link #computeDistribution()} sets this value to the
* overall mean of the values in the {@link #getValuesFileURL() values file}.
*
* @return Mean used in data generation.
*/
public double getMu() {
return mu;
}
/**
* Sets the {@link #getMu() mean} used in data generation. Note that calling this method
* after {@link #computeDistribution()} has been called will have no effect on data
* generated in {@link #DIGEST_MODE}.
*
* @param mu new Mean value.
*/
public void setMu(double mu) {
this.mu = mu;
}
/**
* Returns the standard deviation used when operating in {@link #GAUSSIAN_MODE}.
* Calling {@link #computeDistribution()} sets this value to the overall standard
* deviation of the values in the {@link #getValuesFileURL() values file}. This
* property has no effect when the data generation mode is not
* {@link #GAUSSIAN_MODE}.
*
* @return Standard deviation used when operating in {@link #GAUSSIAN_MODE}.
*/
public double getSigma() {
return sigma;
}
/**
* Sets the {@link #getSigma() standard deviation} used in {@link #GAUSSIAN_MODE}.
*
* @param sigma New standard deviation.
*/
public void setSigma(double sigma) {
this.sigma = sigma;
}
/**
* Reseeds the random data generator.
*
* @param seed Value with which to reseed the {@link RandomDataImpl}
* used to generate random data.
*/
public void reSeed(long seed) {
randomData.reSeed(seed);
}
//------------- private methods ---------------------------------
/**
* Gets a random value in DIGEST_MODE.
* <p>
* <strong>Preconditions:
computeDistribution()
* must have completed successfully; otherwise an
* <code>IllegalStateException will be thrownOther Java examples (source code examples)Here is a short list of links related to this Java ValueServer.java source code file: |
... this post is sponsored by my books ... | |
#1 New Release! |
FP Best Seller |
Copyright 1998-2021 Alvin Alexander, alvinalexander.com
All Rights Reserved.
A percentage of advertising revenue from
pages under the /java/jwarehouse
URI on this website is
paid back to open source projects.