Revision: 256
Author:
rodrigo....@gmail.com
Date: Tue Oct 21 04:47:30 2014 UTC
Log: Probability distributions implemented in CloudSim replaced by
similar functions from Apache math commons.
https://code.google.com/p/cloudsim/source/detail?r=256
Modified:
/trunk/modules/cloudsim/src/main/java/org/cloudbus/cloudsim/distributions/ExponentialDistr.java
/trunk/modules/cloudsim/src/main/java/org/cloudbus/cloudsim/distributions/GammaDistr.java
/trunk/modules/cloudsim/src/main/java/org/cloudbus/cloudsim/distributions/LognormalDistr.java
/trunk/modules/cloudsim/src/main/java/org/cloudbus/cloudsim/distributions/ParetoDistr.java
/trunk/modules/cloudsim/src/main/java/org/cloudbus/cloudsim/distributions/UniformDistr.java
/trunk/modules/cloudsim/src/main/java/org/cloudbus/cloudsim/distributions/WeibullDistr.java
=======================================
---
/trunk/modules/cloudsim/src/main/java/org/cloudbus/cloudsim/distributions/ExponentialDistr.java
Wed Jan 11 03:28:54 2012 UTC
+++
/trunk/modules/cloudsim/src/main/java/org/cloudbus/cloudsim/distributions/ExponentialDistr.java
Tue Oct 21 04:47:30 2014 UTC
@@ -8,7 +8,7 @@
package org.cloudbus.cloudsim.distributions;
-import java.util.Random;
+import org.apache.commons.math3.distribution.ExponentialDistribution;
/**
* An exponential number generator.
@@ -19,10 +19,8 @@
public class ExponentialDistr implements ContinuousDistribution {
/** The num gen. */
- private final Random numGen;
+ private final ExponentialDistribution numGen;
- /** The mean. */
- private final double mean;
/**
* Creates a new exponential number generator.
@@ -31,11 +29,8 @@
* @param mean the mean for the distribution.
*/
public ExponentialDistr(long seed, double mean) {
- if (mean <= 0.0) {
- throw new IllegalArgumentException("Mean must be greater than 0.0");
- }
- numGen = new Random(seed);
- this.mean = mean;
+ this(mean);
+ numGen.reseedRandomGenerator(seed);
}
/**
@@ -44,11 +39,7 @@
* @param mean the mean for the distribution.
*/
public ExponentialDistr(double mean) {
- if (mean <= 0.0) {
- throw new IllegalArgumentException("Mean must be greated than 0.0");
- }
- numGen = new Random(System.currentTimeMillis());
- this.mean = mean;
+ numGen = new ExponentialDistribution(mean);
}
/**
@@ -58,7 +49,7 @@
*/
@Override
public double sample() {
- return -mean * Math.log(numGen.nextDouble());
+ return numGen.sample();
}
}
=======================================
---
/trunk/modules/cloudsim/src/main/java/org/cloudbus/cloudsim/distributions/GammaDistr.java
Wed Jan 11 03:28:54 2012 UTC
+++
/trunk/modules/cloudsim/src/main/java/org/cloudbus/cloudsim/distributions/GammaDistr.java
Tue Oct 21 04:47:30 2014 UTC
@@ -11,6 +11,8 @@
import java.util.Random;
+import org.apache.commons.math3.distribution.GammaDistribution;
+
/**
* The Class GammaDistr.
*
@@ -20,13 +22,7 @@
public class GammaDistr implements ContinuousDistribution {
/** The num gen. */
- private final Random numGen;
-
- /** The alpha. */
- private final int alpha;
-
- /** The beta. */
- private final double beta;
+ private final GammaDistribution numGen;
/**
* Instantiates a new gamma distr.
@@ -35,14 +31,9 @@
* @param alpha the alpha
* @param beta the beta
*/
- public GammaDistr(Random seed, int alpha, double beta) {
- if (alpha <= 0 || beta <= 0.0) {
- throw new IllegalArgumentException("Alpha and beta must be greater than
0.0");
- }
-
- numGen = seed;
- this.alpha = alpha;
- this.beta = beta;
+ public GammaDistr(Random seed, int shape, double scale) {
+ this(shape, scale);
+ numGen.reseedRandomGenerator(seed.nextLong());
}
/**
@@ -51,14 +42,8 @@
* @param alpha the alpha
* @param beta the beta
*/
- public GammaDistr(int alpha, double beta) {
- if (alpha <= 0 || beta <= 0.0) {
- throw new IllegalArgumentException("Alpha and beta must be greater than
0.0");
- }
-
- numGen = new Random(System.currentTimeMillis());
- this.alpha = alpha;
- this.beta = beta;
+ public GammaDistr(int shape, double scale) {
+ numGen = new GammaDistribution(shape, scale);
}
/*
@@ -67,12 +52,7 @@
*/
@Override
public double sample() {
- double sum = 0.0;
- for (int i = 0; i < alpha; i++) {
- sum += Math.log(numGen.nextDouble());
- }
-
- return -beta * sum;
+ return numGen.sample();
}
}
=======================================
---
/trunk/modules/cloudsim/src/main/java/org/cloudbus/cloudsim/distributions/LognormalDistr.java
Wed Jan 11 03:28:54 2012 UTC
+++
/trunk/modules/cloudsim/src/main/java/org/cloudbus/cloudsim/distributions/LognormalDistr.java
Tue Oct 21 04:47:30 2014 UTC
@@ -10,6 +10,8 @@
import java.util.Random;
+import org.apache.commons.math3.distribution.LogNormalDistribution;
+
/**
* The Class LognormalDistr.
*
@@ -18,14 +20,10 @@
*/
public class LognormalDistr implements ContinuousDistribution {
+
/** 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.
@@ -34,14 +32,9 @@
* @param mean the mean
* @param dev the dev
*/
- public LognormalDistr(Random seed, double mean, double dev) {
- if (mean <= 0.0 || dev <= 0.0) {
- throw new IllegalArgumentException("Mean and deviation must be greater
than 0.0");
- }
-
- numGen = seed;
- this.mean = mean;
-
this.dev = dev;
+ public LognormalDistr(Random seed, double shape, double scale) {
+ this(shape, scale);
+ numGen.reseedRandomGenerator(seed.nextLong());
}
/**
@@ -50,14 +43,8 @@
* @param mean the mean
* @param dev the dev
*/
- public LognormalDistr(double mean, double dev) {
- if (mean <= 0.0 || dev <= 0.0) {
- throw new IllegalArgumentException("Mean and deviation must be greater
than 0.0");
- }
-
- numGen = new Random(System.currentTimeMillis());
- this.mean = mean;
-
this.dev = dev;
+ public LognormalDistr(double shape, double scale) {
+ numGen = new LogNormalDistribution(scale, shape);
}
/*
@@ -66,12 +53,7 @@
*/
@Override
public double sample() {
- // generate a normal variate from a uniform variate
- 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);
+ return numGen.sample();
}
}
=======================================
---
/trunk/modules/cloudsim/src/main/java/org/cloudbus/cloudsim/distributions/ParetoDistr.java
Wed Jan 11 03:28:54 2012 UTC
+++
/trunk/modules/cloudsim/src/main/java/org/cloudbus/cloudsim/distributions/ParetoDistr.java
Tue Oct 21 04:47:30 2014 UTC
@@ -11,6 +11,8 @@
import java.util.Random;
+import org.apache.commons.math3.distribution.ParetoDistribution;
+
/**
* The Class ParetoDistr.
*
@@ -20,13 +22,7 @@
public class ParetoDistr implements ContinuousDistribution {
/** The num gen. */
- private final Random numGen;
-
- /** The shape. */
- private final double shape;
-
- /** The location. */
- private final double location;
+ private final ParetoDistribution numGen;
/**
* Instantiates a new pareto distr.
@@ -36,13 +32,8 @@
* @param location the location
*/
public ParetoDistr(Random seed, double shape, double location) {
- if (shape <= 0.0 || location <= 0.0) {
- throw new IllegalArgumentException("Mean and deviation must be greater
than 0.0");
- }
-
- numGen = seed;
- this.shape = shape;
- this.location = location;
+ this(shape, location);
+ numGen.reseedRandomGenerator(seed.nextLong());
}
/**
@@ -52,13 +43,7 @@
* @param location the location
*/
public ParetoDistr(double shape, double location) {
- if (shape <= 0.0 || location <= 0.0) {
- throw new IllegalArgumentException("Mean and deviation must be greater
than 0.0");
- }
-
- numGen = new Random(System.currentTimeMillis());
- this.shape = shape;
- this.location = location;
+ numGen = new ParetoDistribution(location, shape);
}
/*
@@ -67,7 +52,7 @@
*/
@Override
public double sample() {
- return location / Math.pow(numGen.nextDouble(), 1 / shape);
+ return numGen.sample();
}
}
=======================================
---
/trunk/modules/cloudsim/src/main/java/org/cloudbus/cloudsim/distributions/UniformDistr.java
Wed Jan 11 03:28:54 2012 UTC
+++
/trunk/modules/cloudsim/src/main/java/org/cloudbus/cloudsim/distributions/UniformDistr.java
Tue Oct 21 04:47:30 2014 UTC
@@ -10,6 +10,8 @@
import java.util.Random;
+import org.apache.commons.math3.distribution.UniformRealDistribution;
+
/**
* A random number generator based on the Uniform distribution.
*
@@ -19,10 +21,7 @@
public class UniformDistr implements ContinuousDistribution {
/** The num gen. */
- private final Random numGen;
-
- /** The min. */
- private final double mag, min;
+ private final UniformRealDistribution numGen;
/**
* Creates new uniform distribution.
@@ -31,12 +30,7 @@
* @param max maximum value
*/
public UniformDistr(double min, double max) {
- if (min >= max) {
- throw new IllegalArgumentException("Maximum must be greater than the
minimum.");
- }
- numGen = new Random();
- mag = max - min;
- this.min = min;
+ numGen = new UniformRealDistribution(min, max);
}
/**
@@ -47,13 +41,8 @@
* @param seed simulation seed to be used
*/
public UniformDistr(double min, double max, long seed) {
- if (min >= max) {
- throw new IllegalArgumentException("Maximum must be greater than the
minimum.");
- }
-
- numGen = new Random(seed);
- mag = max - min;
- this.min = min;
+ this(min, max);
+ numGen.reseedRandomGenerator(seed);
}
/**
@@ -63,7 +52,7 @@
*/
@Override
public double sample() {
- return (numGen.nextDouble() * (mag)) + min;
+ return numGen.sample();
}
/**
@@ -89,7 +78,7 @@
* @param seed the new seed for the generator
*/
public void setSeed(long seed) {
- numGen.setSeed(seed);
+ numGen.reseedRandomGenerator(seed);
}
}
=======================================
---
/trunk/modules/cloudsim/src/main/java/org/cloudbus/cloudsim/distributions/WeibullDistr.java
Wed Jan 11 03:28:54 2012 UTC
+++
/trunk/modules/cloudsim/src/main/java/org/cloudbus/cloudsim/distributions/WeibullDistr.java
Tue Oct 21 04:47:30 2014 UTC
@@ -11,6 +11,8 @@
import java.util.Random;
+import org.apache.commons.math3.distribution.WeibullDistribution;
+
/**
* The Class WeibullDistr.
*
@@ -20,13 +22,7 @@
public class WeibullDistr implements ContinuousDistribution {
/** The num gen. */
- private final Random numGen;
-
- /** The alpha. */
- private final double alpha;
-
- /** The beta. */
- private final double beta;
+ private final WeibullDistribution numGen;
/**
* Instantiates a new weibull distr.
@@ -36,13 +32,8 @@
* @param beta the beta
*/
public WeibullDistr(Random seed, double alpha, double beta) {
- if (alpha <= 0.0 || beta <= 0.0) {
- throw new IllegalArgumentException("Alpha and beta must be greater than
0.0");
- }
-
- numGen = seed;
- this.alpha = alpha;
- this.beta = beta;
+ this(alpha, beta);
+ numGen.reseedRandomGenerator(seed.nextLong());
}
/**
@@ -52,13 +43,7 @@
* @param beta the beta
*/
public WeibullDistr(double alpha, double beta) {
- if (alpha <= 0.0 || beta <= 0.0) {
- throw new IllegalArgumentException("Alpha and beta must be greater than
0.0");
- }
-
- numGen = new Random(System.currentTimeMillis());
- this.alpha = alpha;
- this.beta = beta;
+ numGen = new WeibullDistribution(alpha, beta);
}
/*
@@ -67,7 +52,7 @@
*/
@Override
public double sample() {
- return beta * Math.pow(-Math.log(numGen.nextDouble()), 1 / alpha);
+ return numGen.sample();
}
}