ParseDistribution.java

  1. package org.opentrafficsim.road.network.factory.xml.utils;

  2. import org.djunits.unit.Unit;
  3. import org.djunits.value.vdouble.scalar.base.DoubleScalarRel;
  4. import org.djutils.eval.Eval;
  5. import org.opentrafficsim.core.units.distributions.ContinuousDistDoubleScalar;
  6. import org.opentrafficsim.road.network.factory.xml.XmlParserException;
  7. import org.opentrafficsim.xml.generated.ConstantDistType;
  8. import org.opentrafficsim.xml.generated.DiscreteDistType;

  9. import nl.tudelft.simulation.dsol.experiment.StreamInformation;
  10. import nl.tudelft.simulation.jstats.distributions.DistBernoulli;
  11. import nl.tudelft.simulation.jstats.distributions.DistBeta;
  12. import nl.tudelft.simulation.jstats.distributions.DistBinomial;
  13. import nl.tudelft.simulation.jstats.distributions.DistConstant;
  14. import nl.tudelft.simulation.jstats.distributions.DistContinuous;
  15. import nl.tudelft.simulation.jstats.distributions.DistDiscrete;
  16. import nl.tudelft.simulation.jstats.distributions.DistDiscreteConstant;
  17. import nl.tudelft.simulation.jstats.distributions.DistDiscreteUniform;
  18. import nl.tudelft.simulation.jstats.distributions.DistErlang;
  19. import nl.tudelft.simulation.jstats.distributions.DistExponential;
  20. import nl.tudelft.simulation.jstats.distributions.DistGamma;
  21. import nl.tudelft.simulation.jstats.distributions.DistGeometric;
  22. import nl.tudelft.simulation.jstats.distributions.DistLogNormal;
  23. import nl.tudelft.simulation.jstats.distributions.DistLogNormalTrunc;
  24. import nl.tudelft.simulation.jstats.distributions.DistNegBinomial;
  25. import nl.tudelft.simulation.jstats.distributions.DistNormal;
  26. import nl.tudelft.simulation.jstats.distributions.DistNormalTrunc;
  27. import nl.tudelft.simulation.jstats.distributions.DistPearson5;
  28. import nl.tudelft.simulation.jstats.distributions.DistPearson6;
  29. import nl.tudelft.simulation.jstats.distributions.DistPoisson;
  30. import nl.tudelft.simulation.jstats.distributions.DistTriangular;
  31. import nl.tudelft.simulation.jstats.distributions.DistUniform;
  32. import nl.tudelft.simulation.jstats.distributions.DistWeibull;
  33. import nl.tudelft.simulation.jstats.streams.StreamInterface;

  34. /**
  35.  * Parse a distribution from text to a distribution.
  36.  * <p>
  37.  * Copyright (c) 2013-2024 Delft University of Technology, PO Box 5, 2600 AA, Delft, the Netherlands. All rights reserved. <br>
  38.  * BSD-style license. See <a href="https://opentrafficsim.org/docs/license.html">OpenTrafficSim License</a>.
  39.  * </p>
  40.  * @author <a href="https://github.com/averbraeck">Alexander Verbraeck</a>
  41.  * @author <a href="https://github.com/wjschakel">Wouter Schakel</a>
  42.  */
  43. public final class ParseDistribution
  44. {
  45.     /** Utility class. */
  46.     private ParseDistribution()
  47.     {
  48.         // do not instantiate
  49.     }

  50.     /**
  51.      * Parse a relative unit distribution, e.g. <code>UNIFORM(1, 3) m</code>.
  52.      * @param streamMap StreamInformation; the map with streams from the RUN tag
  53.      * @param distribution ConstantDistType; the tag to parse, a sub type of ConstantDistType
  54.      * @param unit U; unit
  55.      * @param eval Eval; expression evaluator.
  56.      * @return a typed continuous random distribution.
  57.      * @throws XmlParserException in case of a parse error.
  58.      */
  59.     public static <T extends DoubleScalarRel<U, T>, U extends Unit<U>> ContinuousDistDoubleScalar.Rel<T, U> parseContinuousDist(
  60.             final StreamInformation streamMap, final ConstantDistType distribution, final U unit, final Eval eval)
  61.             throws XmlParserException
  62.     {
  63.         return new ContinuousDistDoubleScalar.Rel<T, U>(makeDistContinuous(streamMap, distribution, eval), unit);
  64.     }

  65.     /**
  66.      * Parse a discrete distribution.
  67.      * @param streamMap StreamInformation; map with stream information
  68.      * @param distType DiscreteDistType; the distribution to parse
  69.      * @param eval Eval; expression evaluator.
  70.      * @return the generated distribution.
  71.      * @throws XmlParserException in case distribution unknown or parameter number does not match.
  72.      */
  73.     public static DistDiscrete makeDistDiscrete(final StreamInformation streamMap, final DiscreteDistType distType,
  74.             final Eval eval) throws XmlParserException
  75.     {
  76.         StreamInterface stream = ParseUtil.findStream(streamMap, distType.getRandomStream(), eval);
  77.         if (distType.getBernoulliI() != null)
  78.         {
  79.             return new DistBernoulli(stream, distType.getBernoulliI().getP().get(eval));
  80.         }
  81.         else if (distType.getBinomial() != null)
  82.         {
  83.             return new DistBinomial(stream, distType.getBinomial().getN().get(eval), distType.getBinomial().getP().get(eval));
  84.         }
  85.         else if (distType.getConstant() != null)
  86.         {
  87.             return new DistDiscreteConstant(stream, distType.getConstant().getC().get(eval));
  88.         }
  89.         else if (distType.getGeometric() != null)
  90.         {
  91.             return new DistGeometric(stream, distType.getGeometric().getP().get(eval));
  92.         }
  93.         else if (distType.getNegBinomial() != null)
  94.         {
  95.             return new DistNegBinomial(stream, (int) distType.getNegBinomial().getN().get(eval),
  96.                     distType.getGeometric().getP().get(eval));
  97.         }
  98.         else if (distType.getPoisson() != null)
  99.         {
  100.             return new DistPoisson(stream, distType.getPoisson().getLambda().get(eval));
  101.         }
  102.         else if (distType.getUniform() != null)
  103.         {
  104.             return new DistDiscreteUniform(stream, distType.getUniform().getMin().get(eval),
  105.                     distType.getUniform().getMax().get(eval));
  106.         }
  107.         throw new XmlParserException("makeDistDiscrete - unknown distribution function " + distType);
  108.     }

  109.     /**
  110.      * Parse a continuous distribution.
  111.      * @param streamMap StreamInformation; map with stream information
  112.      * @param distType ConstantDistType; the distribution to parse
  113.      * @param eval Eval; expression evaluator.
  114.      * @return the generated distribution.
  115.      * @throws XmlParserException in case distribution unknown or parameter number does not match.
  116.      */
  117.     public static DistContinuous makeDistContinuous(final StreamInformation streamMap, final ConstantDistType distType,
  118.             final Eval eval) throws XmlParserException
  119.     {
  120.         StreamInterface stream = ParseUtil.findStream(streamMap, distType.getRandomStream(), eval);
  121.         if (distType.getConstant() != null)
  122.         {
  123.             return new DistConstant(stream, distType.getConstant().getC().get(eval));
  124.         }
  125.         else if (distType.getExponential() != null)
  126.         {
  127.             return new DistExponential(stream, distType.getExponential().getLambda().get(eval));
  128.         }
  129.         else if (distType.getTriangular() != null)
  130.         {
  131.             return new DistTriangular(stream, distType.getTriangular().getMin().get(eval),
  132.                     distType.getTriangular().getMode().get(eval), distType.getTriangular().getMax().get(eval));
  133.         }
  134.         else if (distType.getNormal() != null)
  135.         {
  136.             return new DistNormal(stream, distType.getNormal().getMu().get(eval), distType.getNormal().getSigma().get(eval));
  137.         }
  138.         else if (distType.getNormal() != null)
  139.         {
  140.             return new DistNormalTrunc(stream, distType.getNormalTrunc().getMu().get(eval),
  141.                     distType.getNormalTrunc().getSigma().get(eval), distType.getNormalTrunc().getMin().get(eval),
  142.                     distType.getNormalTrunc().getMax().get(eval));
  143.         }
  144.         else if (distType.getBeta() != null)
  145.         {
  146.             return new DistBeta(stream, distType.getBeta().getAlpha1().get(eval), distType.getBeta().getAlpha2().get(eval));
  147.         }
  148.         else if (distType.getErlang() != null)
  149.         {
  150.             return new DistErlang(stream, distType.getErlang().getMean().get(eval), distType.getErlang().getK().get(eval));
  151.         }
  152.         else if (distType.getGamma() != null)
  153.         {
  154.             return new DistGamma(stream, distType.getGamma().getAlpha().get(eval), distType.getGamma().getBeta().get(eval));
  155.         }
  156.         else if (distType.getLogNormal() != null)
  157.         {
  158.             return new DistLogNormal(stream, distType.getLogNormal().getMu().get(eval),
  159.                     distType.getLogNormal().getSigma().get(eval));
  160.         }
  161.         else if (distType.getLogNormalTrunc() != null)
  162.         {
  163.             return new DistLogNormalTrunc(stream, distType.getLogNormalTrunc().getMu().get(eval),
  164.                     distType.getLogNormalTrunc().getSigma().get(eval), distType.getLogNormalTrunc().getMin().get(eval),
  165.                     distType.getLogNormalTrunc().getMax().get(eval));
  166.         }
  167.         else if (distType.getPearson5() != null)
  168.         {
  169.             return new DistPearson5(stream, distType.getPearson5().getAlpha().get(eval),
  170.                     distType.getPearson5().getBeta().get(eval));
  171.         }
  172.         else if (distType.getPearson6() != null)
  173.         {
  174.             return new DistPearson6(stream, distType.getPearson6().getAlpha1().get(eval),
  175.                     distType.getPearson6().getAlpha2().get(eval), distType.getPearson6().getBeta().get(eval));
  176.         }
  177.         else if (distType.getUniform() != null)
  178.         {
  179.             return new DistUniform(stream, distType.getUniform().getMin().get(eval), distType.getUniform().getMax().get(eval));
  180.         }
  181.         else if (distType.getWeibull() != null)
  182.         {
  183.             return new DistWeibull(stream, distType.getWeibull().getAlpha().get(eval),
  184.                     distType.getWeibull().getBeta().get(eval));
  185.         }
  186.         throw new XmlParserException("makeDistContinuous - unknown distribution function " + distType);
  187.     }

  188. }