Probability distributions implemented in CloudSim replaced by similar functions…

Probability distributions implemented in CloudSim replaced by similar functions from Apache math commons.
parent 29c3edf5
...@@ -8,7 +8,7 @@ ...@@ -8,7 +8,7 @@
package org.cloudbus.cloudsim.distributions; package org.cloudbus.cloudsim.distributions;
import java.util.Random; import org.apache.commons.math3.distribution.ExponentialDistribution;
/** /**
* An exponential number generator. * An exponential number generator.
...@@ -19,10 +19,8 @@ import java.util.Random; ...@@ -19,10 +19,8 @@ import java.util.Random;
public class ExponentialDistr implements ContinuousDistribution { public class ExponentialDistr implements ContinuousDistribution {
/** The num gen. */ /** The num gen. */
private final Random numGen; private final ExponentialDistribution numGen;
/** The mean. */
private final double mean;
/** /**
* Creates a new exponential number generator. * Creates a new exponential number generator.
...@@ -31,11 +29,8 @@ public class ExponentialDistr implements ContinuousDistribution { ...@@ -31,11 +29,8 @@ public class ExponentialDistr implements ContinuousDistribution {
* @param mean the mean for the distribution. * @param mean the mean for the distribution.
*/ */
public ExponentialDistr(long seed, double mean) { public ExponentialDistr(long seed, double mean) {
if (mean <= 0.0) { this(mean);
throw new IllegalArgumentException("Mean must be greater than 0.0"); numGen.reseedRandomGenerator(seed);
}
numGen = new Random(seed);
this.mean = mean;
} }
/** /**
...@@ -44,11 +39,7 @@ public class ExponentialDistr implements ContinuousDistribution { ...@@ -44,11 +39,7 @@ public class ExponentialDistr implements ContinuousDistribution {
* @param mean the mean for the distribution. * @param mean the mean for the distribution.
*/ */
public ExponentialDistr(double mean) { public ExponentialDistr(double mean) {
if (mean <= 0.0) { numGen = new ExponentialDistribution(mean);
throw new IllegalArgumentException("Mean must be greated than 0.0");
}
numGen = new Random(System.currentTimeMillis());
this.mean = mean;
} }
/** /**
...@@ -58,7 +49,7 @@ public class ExponentialDistr implements ContinuousDistribution { ...@@ -58,7 +49,7 @@ public class ExponentialDistr implements ContinuousDistribution {
*/ */
@Override @Override
public double sample() { public double sample() {
return -mean * Math.log(numGen.nextDouble()); return numGen.sample();
} }
} }
...@@ -11,6 +11,8 @@ package org.cloudbus.cloudsim.distributions; ...@@ -11,6 +11,8 @@ package org.cloudbus.cloudsim.distributions;
import java.util.Random; import java.util.Random;
import org.apache.commons.math3.distribution.GammaDistribution;
/** /**
* The Class GammaDistr. * The Class GammaDistr.
* *
...@@ -20,13 +22,7 @@ import java.util.Random; ...@@ -20,13 +22,7 @@ import java.util.Random;
public class GammaDistr implements ContinuousDistribution { public class GammaDistr implements ContinuousDistribution {
/** The num gen. */ /** The num gen. */
private final Random numGen; private final GammaDistribution numGen;
/** The alpha. */
private final int alpha;
/** The beta. */
private final double beta;
/** /**
* Instantiates a new gamma distr. * Instantiates a new gamma distr.
...@@ -35,14 +31,9 @@ public class GammaDistr implements ContinuousDistribution { ...@@ -35,14 +31,9 @@ public class GammaDistr implements ContinuousDistribution {
* @param alpha the alpha * @param alpha the alpha
* @param beta the beta * @param beta the beta
*/ */
public GammaDistr(Random seed, int alpha, double beta) { public GammaDistr(Random seed, int shape, double scale) {
if (alpha <= 0 || beta <= 0.0) { this(shape, scale);
throw new IllegalArgumentException("Alpha and beta must be greater than 0.0"); numGen.reseedRandomGenerator(seed.nextLong());
}
numGen = seed;
this.alpha = alpha;
this.beta = beta;
} }
/** /**
...@@ -51,14 +42,8 @@ public class GammaDistr implements ContinuousDistribution { ...@@ -51,14 +42,8 @@ public class GammaDistr implements ContinuousDistribution {
* @param alpha the alpha * @param alpha the alpha
* @param beta the beta * @param beta the beta
*/ */
public GammaDistr(int alpha, double beta) { public GammaDistr(int shape, double scale) {
if (alpha <= 0 || beta <= 0.0) { numGen = new GammaDistribution(shape, scale);
throw new IllegalArgumentException("Alpha and beta must be greater than 0.0");
}
numGen = new Random(System.currentTimeMillis());
this.alpha = alpha;
this.beta = beta;
} }
/* /*
...@@ -67,12 +52,7 @@ public class GammaDistr implements ContinuousDistribution { ...@@ -67,12 +52,7 @@ public class GammaDistr implements ContinuousDistribution {
*/ */
@Override @Override
public double sample() { public double sample() {
double sum = 0.0; return numGen.sample();
for (int i = 0; i < alpha; i++) {
sum += Math.log(numGen.nextDouble());
}
return -beta * sum;
} }
} }
...@@ -10,6 +10,8 @@ package org.cloudbus.cloudsim.distributions; ...@@ -10,6 +10,8 @@ package org.cloudbus.cloudsim.distributions;
import java.util.Random; import java.util.Random;
import org.apache.commons.math3.distribution.LogNormalDistribution;
/** /**
* The Class LognormalDistr. * The Class LognormalDistr.
* *
...@@ -18,14 +20,10 @@ import java.util.Random; ...@@ -18,14 +20,10 @@ import java.util.Random;
*/ */
public class LognormalDistr implements ContinuousDistribution { public class LognormalDistr implements ContinuousDistribution {
/** The num gen. */ /** The num gen. */
private final Random numGen; private final LogNormalDistribution numGen;
/** The mean. */
private final double mean;
/** The dev. */
private final double dev;
/** /**
* Instantiates a new lognormal distr. * Instantiates a new lognormal distr.
...@@ -34,14 +32,9 @@ public class LognormalDistr implements ContinuousDistribution { ...@@ -34,14 +32,9 @@ public class LognormalDistr implements ContinuousDistribution {
* @param mean the mean * @param mean the mean
* @param dev the dev * @param dev the dev
*/ */
public LognormalDistr(Random seed, double mean, double dev) { public LognormalDistr(Random seed, double shape, double scale) {
if (mean <= 0.0 || dev <= 0.0) { this(shape, scale);
throw new IllegalArgumentException("Mean and deviation must be greater than 0.0"); numGen.reseedRandomGenerator(seed.nextLong());
}
numGen = seed;
this.mean = mean;
this.dev = dev;
} }
/** /**
...@@ -50,14 +43,8 @@ public class LognormalDistr implements ContinuousDistribution { ...@@ -50,14 +43,8 @@ public class LognormalDistr implements ContinuousDistribution {
* @param mean the mean * @param mean the mean
* @param dev the dev * @param dev the dev
*/ */
public LognormalDistr(double mean, double dev) { public LognormalDistr(double shape, double scale) {
if (mean <= 0.0 || dev <= 0.0) { numGen = new LogNormalDistribution(scale, shape);
throw new IllegalArgumentException("Mean and deviation must be greater than 0.0");
}
numGen = new Random(System.currentTimeMillis());
this.mean = mean;
this.dev = dev;
} }
/* /*
...@@ -66,12 +53,7 @@ public class LognormalDistr implements ContinuousDistribution { ...@@ -66,12 +53,7 @@ public class LognormalDistr implements ContinuousDistribution {
*/ */
@Override @Override
public double sample() { public double sample() {
// generate a normal variate from a uniform variate return numGen.sample();
double n = Math.sqrt(-2 * Math.log(numGen.nextDouble()))
* Math.sin(2 * Math.PI * numGen.nextDouble());
// use it to generate the lognormal variate
return Math.pow(Math.E, mean + dev * n);
} }
} }
...@@ -11,6 +11,8 @@ package org.cloudbus.cloudsim.distributions; ...@@ -11,6 +11,8 @@ package org.cloudbus.cloudsim.distributions;
import java.util.Random; import java.util.Random;
import org.apache.commons.math3.distribution.ParetoDistribution;
/** /**
* The Class ParetoDistr. * The Class ParetoDistr.
* *
...@@ -20,13 +22,7 @@ import java.util.Random; ...@@ -20,13 +22,7 @@ import java.util.Random;
public class ParetoDistr implements ContinuousDistribution { public class ParetoDistr implements ContinuousDistribution {
/** The num gen. */ /** The num gen. */
private final Random numGen; private final ParetoDistribution numGen;
/** The shape. */
private final double shape;
/** The location. */
private final double location;
/** /**
* Instantiates a new pareto distr. * Instantiates a new pareto distr.
...@@ -36,13 +32,8 @@ public class ParetoDistr implements ContinuousDistribution { ...@@ -36,13 +32,8 @@ public class ParetoDistr implements ContinuousDistribution {
* @param location the location * @param location the location
*/ */
public ParetoDistr(Random seed, double shape, double location) { public ParetoDistr(Random seed, double shape, double location) {
if (shape <= 0.0 || location <= 0.0) { this(shape, location);
throw new IllegalArgumentException("Mean and deviation must be greater than 0.0"); numGen.reseedRandomGenerator(seed.nextLong());
}
numGen = seed;
this.shape = shape;
this.location = location;
} }
/** /**
...@@ -52,13 +43,7 @@ public class ParetoDistr implements ContinuousDistribution { ...@@ -52,13 +43,7 @@ public class ParetoDistr implements ContinuousDistribution {
* @param location the location * @param location the location
*/ */
public ParetoDistr(double shape, double location) { public ParetoDistr(double shape, double location) {
if (shape <= 0.0 || location <= 0.0) { numGen = new ParetoDistribution(location, shape);
throw new IllegalArgumentException("Mean and deviation must be greater than 0.0");
}
numGen = new Random(System.currentTimeMillis());
this.shape = shape;
this.location = location;
} }
/* /*
...@@ -67,7 +52,7 @@ public class ParetoDistr implements ContinuousDistribution { ...@@ -67,7 +52,7 @@ public class ParetoDistr implements ContinuousDistribution {
*/ */
@Override @Override
public double sample() { public double sample() {
return location / Math.pow(numGen.nextDouble(), 1 / shape); return numGen.sample();
} }
} }
...@@ -10,6 +10,8 @@ package org.cloudbus.cloudsim.distributions; ...@@ -10,6 +10,8 @@ package org.cloudbus.cloudsim.distributions;
import java.util.Random; import java.util.Random;
import org.apache.commons.math3.distribution.UniformRealDistribution;
/** /**
* A random number generator based on the Uniform distribution. * A random number generator based on the Uniform distribution.
* *
...@@ -19,10 +21,7 @@ import java.util.Random; ...@@ -19,10 +21,7 @@ import java.util.Random;
public class UniformDistr implements ContinuousDistribution { public class UniformDistr implements ContinuousDistribution {
/** The num gen. */ /** The num gen. */
private final Random numGen; private final UniformRealDistribution numGen;
/** The min. */
private final double mag, min;
/** /**
* Creates new uniform distribution. * Creates new uniform distribution.
...@@ -31,12 +30,7 @@ public class UniformDistr implements ContinuousDistribution { ...@@ -31,12 +30,7 @@ public class UniformDistr implements ContinuousDistribution {
* @param max maximum value * @param max maximum value
*/ */
public UniformDistr(double min, double max) { public UniformDistr(double min, double max) {
if (min >= max) { numGen = new UniformRealDistribution(min, max);
throw new IllegalArgumentException("Maximum must be greater than the minimum.");
}
numGen = new Random();
mag = max - min;
this.min = min;
} }
/** /**
...@@ -47,13 +41,8 @@ public class UniformDistr implements ContinuousDistribution { ...@@ -47,13 +41,8 @@ public class UniformDistr implements ContinuousDistribution {
* @param seed simulation seed to be used * @param seed simulation seed to be used
*/ */
public UniformDistr(double min, double max, long seed) { public UniformDistr(double min, double max, long seed) {
if (min >= max) { this(min, max);
throw new IllegalArgumentException("Maximum must be greater than the minimum."); numGen.reseedRandomGenerator(seed);
}
numGen = new Random(seed);
mag = max - min;
this.min = min;
} }
/** /**
...@@ -63,7 +52,7 @@ public class UniformDistr implements ContinuousDistribution { ...@@ -63,7 +52,7 @@ public class UniformDistr implements ContinuousDistribution {
*/ */
@Override @Override
public double sample() { public double sample() {
return (numGen.nextDouble() * (mag)) + min; return numGen.sample();
} }
/** /**
...@@ -89,7 +78,7 @@ public class UniformDistr implements ContinuousDistribution { ...@@ -89,7 +78,7 @@ public class UniformDistr implements ContinuousDistribution {
* @param seed the new seed for the generator * @param seed the new seed for the generator
*/ */
public void setSeed(long seed) { public void setSeed(long seed) {
numGen.setSeed(seed); numGen.reseedRandomGenerator(seed);
} }
} }
\ No newline at end of file
...@@ -11,6 +11,8 @@ package org.cloudbus.cloudsim.distributions; ...@@ -11,6 +11,8 @@ package org.cloudbus.cloudsim.distributions;
import java.util.Random; import java.util.Random;
import org.apache.commons.math3.distribution.WeibullDistribution;
/** /**
* The Class WeibullDistr. * The Class WeibullDistr.
* *
...@@ -20,13 +22,7 @@ import java.util.Random; ...@@ -20,13 +22,7 @@ import java.util.Random;
public class WeibullDistr implements ContinuousDistribution { public class WeibullDistr implements ContinuousDistribution {
/** The num gen. */ /** The num gen. */
private final Random numGen; private final WeibullDistribution numGen;
/** The alpha. */
private final double alpha;
/** The beta. */
private final double beta;
/** /**
* Instantiates a new weibull distr. * Instantiates a new weibull distr.
...@@ -36,13 +32,8 @@ public class WeibullDistr implements ContinuousDistribution { ...@@ -36,13 +32,8 @@ public class WeibullDistr implements ContinuousDistribution {
* @param beta the beta * @param beta the beta
*/ */
public WeibullDistr(Random seed, double alpha, double beta) { public WeibullDistr(Random seed, double alpha, double beta) {
if (alpha <= 0.0 || beta <= 0.0) { this(alpha, beta);
throw new IllegalArgumentException("Alpha and beta must be greater than 0.0"); numGen.reseedRandomGenerator(seed.nextLong());
}
numGen = seed;
this.alpha = alpha;
this.beta = beta;
} }
/** /**
...@@ -52,13 +43,7 @@ public class WeibullDistr implements ContinuousDistribution { ...@@ -52,13 +43,7 @@ public class WeibullDistr implements ContinuousDistribution {
* @param beta the beta * @param beta the beta
*/ */
public WeibullDistr(double alpha, double beta) { public WeibullDistr(double alpha, double beta) {
if (alpha <= 0.0 || beta <= 0.0) { numGen = new WeibullDistribution(alpha, beta);
throw new IllegalArgumentException("Alpha and beta must be greater than 0.0");
}
numGen = new Random(System.currentTimeMillis());
this.alpha = alpha;
this.beta = beta;
} }
/* /*
...@@ -67,7 +52,7 @@ public class WeibullDistr implements ContinuousDistribution { ...@@ -67,7 +52,7 @@ public class WeibullDistr implements ContinuousDistribution {
*/ */
@Override @Override
public double sample() { public double sample() {
return beta * Math.pow(-Math.log(numGen.nextDouble()), 1 / alpha); return numGen.sample();
} }
} }
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment