All Downloads are FREE. Search and download functionalities are using the official Maven repository.

jasima.core.random.continuous.DblTriangular Maven / Gradle / Ivy

/*******************************************************************************
 * This file is part of jasima, v1.3, the Java simulator for manufacturing and 
 * logistics.
 *  
 * Copyright (c) 2015 		jasima solutions UG
 * Copyright (c) 2010-2015 Torsten Hildebrandt and jasima contributors
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Affero General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with this program.  If not, see .
 *******************************************************************************/
package jasima.core.random.continuous;

import jasima.core.util.Pair;

/**
 * Returns a random real number following a triangular
 * distribution as defined by the three parameters min, mode, and max.
 * 
 * @author Torsten Hildebrandt
 * @version 
 *          "$Id: DblTriangular.java 753 2015-07-27 15:29:49Z [email protected] $"
 */
public class DblTriangular extends DblStream {

	private static final long serialVersionUID = -8098960209631757070L;

	// parameters with some arbitrary default values
	private double min = 0.0;
	private double mode = 5.0;
	private double max = 10.0;

	// constants derived from parameters
	private double critValue, range, minDist, maxDist;

	public DblTriangular(double min, double mode, double max) {
		super();
		setMin(min);
		setMode(mode);
		setMax(max);
	}

	private void calcInternalValues() {
		range = (max - min);
		minDist = (mode - min);
		maxDist = (max - mode);
		critValue = minDist / range;
	}

	@Override
	public double nextDbl() {
		double rnd = rndGen.nextDouble();

		// inverse of CDF
		double v;
		if (rnd <= critValue) {
			v = getMin() + Math.sqrt(range * minDist * rnd);
		} else {
			v = getMax() - Math.sqrt(range * maxDist * (1.0 - rnd));
		}

		return v;
	}

	@Override
	public double getNumericalMean() {
		return (getMin() + getMode() + getMax()) / 3.0;
	}

	@Override
	public String toString() {
		return "DblTriangular(min=" + min + ";mode=" + mode + ";max=" + max
				+ ")";
	}

	@Override
	public Pair getValueRange() {
		return new Pair<>(getMin(), getMax());
	}

	public double getMin() {
		return min;
	}

	public void setMin(double min) {
		this.min = min;
		calcInternalValues();
	}

	public double getMode() {
		return mode;
	}

	public void setMode(double mode) {
		this.mode = mode;
		calcInternalValues();
	}

	public double getMax() {
		return max;
	}

	public void setMax(double max) {
		this.max = max;
		calcInternalValues();
	}

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy