ODOptions.java

  1. package org.opentrafficsim.road.gtu.generator.od;

  2. import java.util.HashMap;
  3. import java.util.Map;

  4. import org.djunits.value.vdouble.scalar.Frequency;
  5. import org.djunits.value.vdouble.scalar.Length;
  6. import org.opentrafficsim.core.gtu.GTUType;
  7. import org.opentrafficsim.core.gtu.animation.GTUColorer;
  8. import org.opentrafficsim.core.idgenerator.IdGenerator;
  9. import org.opentrafficsim.core.network.LinkType;
  10. import org.opentrafficsim.core.network.Node;
  11. import org.opentrafficsim.road.gtu.animation.DefaultSwitchableGTUColorer;
  12. import org.opentrafficsim.road.gtu.generator.CFBARoomChecker;
  13. import org.opentrafficsim.road.gtu.generator.GeneratorPositions.LaneBias;
  14. import org.opentrafficsim.road.gtu.generator.GeneratorPositions.LaneBiases;
  15. import org.opentrafficsim.road.gtu.generator.LaneBasedGTUGenerator.RoomChecker;
  16. import org.opentrafficsim.road.gtu.generator.MarkovCorrelation;
  17. import org.opentrafficsim.road.gtu.generator.headway.ArrivalsHeadwayGenerator.HeadwayDistribution;
  18. import org.opentrafficsim.road.network.lane.Lane;

  19. import nl.tudelft.simulation.language.Throw;

  20. /**
  21.  * Options for vehicle generation based on an OD matrix.
  22.  * <p>
  23.  * Copyright (c) 2013-2018 Delft University of Technology, PO Box 5, 2600 AA, Delft, the Netherlands. All rights reserved. <br>
  24.  * BSD-style license. See <a href="http://opentrafficsim.org/node/13">OpenTrafficSim License</a>.
  25.  * <p>
  26.  * @version $Revision$, $LastChangedDate$, by $Author$, initial version 6 dec. 2017 <br>
  27.  * @author <a href="http://www.tbm.tudelft.nl/averbraeck">Alexander Verbraeck</a>
  28.  * @author <a href="http://www.tudelft.nl/pknoppers">Peter Knoppers</a>
  29.  * @author <a href="http://www.transport.citg.tudelft.nl">Wouter Schakel</a>
  30.  */
  31. public class ODOptions
  32. {

  33.     /** Headway randomization option. */
  34.     public static final Option<HeadwayDistribution> HEADWAY_DIST =
  35.             new Option<>("headway distribution", HeadwayDistribution.EXPONENTIAL);

  36.     /** ID generator option. */
  37.     public static final Option<IdGenerator> GTU_ID = new Option<>("gtu id", new IdGenerator(""));

  38.     /** GTU colorer option. */
  39.     public static final Option<GTUColorer> GTU_COLORER = new Option<>("gtu colorer", new DefaultSwitchableGTUColorer());

  40.     /** GTU characteristics generator option. */
  41.     public static final Option<GTUCharacteristicsGeneratorOD> GTU_TYPE =
  42.             new Option<>("gtu type", new DefaultGTUCharacteristicsGeneratorOD());

  43.     /** Room checker option. */
  44.     public static final Option<RoomChecker> ROOM_CHECKER = new Option<>("room checker", new CFBARoomChecker());

  45.     /** Markov chain for GTU type option. */
  46.     public static final Option<MarkovCorrelation<GTUType, Frequency>> MARKOV = new Option<>("markov", null);

  47.     /** Lane bias. Default is Truck: truck right (strong right, max 2 lanes), Vehicle (other): weak left. */
  48.     public static final Option<LaneBiases> LANE_BIAS = new Option<>("lane bias",
  49.             new LaneBiases().addBias(GTUType.TRUCK, LaneBias.TRUCK_RIGHT).addBias(GTUType.VEHICLE, LaneBias.WEAK_LEFT));

  50.     /** Initial distance over which lane changes shouldn't be performed option. */
  51.     public static final Option<Length> NO_LC_DIST = new Option<>("no lc distance", null);

  52.     /** Show generator animation option. */
  53.     public static final Option<Boolean> ANIMATION = new Option<>("show generator animation", false);

  54.     /** Options overall. */
  55.     private OptionSet<Void> options = new OptionSet<>();

  56.     /** Options per lane. */
  57.     private OptionSet<Lane> laneOptions = new OptionSet<>();

  58.     /** Options per node. */
  59.     private OptionSet<Node> nodeOptions = new OptionSet<>();

  60.     /** Options per road type. */
  61.     private OptionSet<LinkType> linkTypeOptions = new OptionSet<>();

  62.     /**
  63.      * Set option value.
  64.      * @param option Option&lt;K&gt;; option
  65.      * @param value K; option value
  66.      * @param <K> value type
  67.      * @return this option set
  68.      */
  69.     public final <K> ODOptions set(final Option<K> option, final K value)
  70.     {
  71.         this.options.set(null, option, value);
  72.         return this;
  73.     }

  74.     /**
  75.      * Set option value for lane.
  76.      * @param lane Lane; lane
  77.      * @param option Option&lt;K&gt;; option
  78.      * @param value K; option value
  79.      * @param <K> value type
  80.      * @return this option set
  81.      */
  82.     public final <K> ODOptions set(final Lane lane, final Option<K> option, final K value)
  83.     {
  84.         this.laneOptions.set(lane, option, value);
  85.         return this;
  86.     }

  87.     /**
  88.      * Set option value for node.
  89.      * @param node Node; node
  90.      * @param option Option&lt;K&gt;; option
  91.      * @param value K; option value
  92.      * @param <K> value type
  93.      * @return this option set
  94.      */
  95.     public final <K> ODOptions set(final Node node, final Option<K> option, final K value)
  96.     {
  97.         this.nodeOptions.set(node, option, value);
  98.         return this;
  99.     }

  100.     /**
  101.      * Set option value for link type.
  102.      * @param linkType LinkType; link type
  103.      * @param option Option&lt;K&gt;; option
  104.      * @param value K; option value
  105.      * @param <K> value type
  106.      * @return this option set
  107.      */
  108.     public final <K> ODOptions set(final LinkType linkType, final Option<K> option, final K value)
  109.     {
  110.         this.linkTypeOptions.set(linkType, option, value);
  111.         return this;
  112.     }

  113.     /**
  114.      * Get option value. If a value is specified for a specific category, it is returned. The following order is used:
  115.      * <ul>
  116.      * <li>{@code Lane}</li>
  117.      * <li>{@code Node} (origin)</li>
  118.      * <li>{@code LinkType}</li>
  119.      * <li>None (global option value)</li>
  120.      * <li>Default option value</li>
  121.      * </ul>
  122.      * @param option Option&lt;K&gt;; option
  123.      * @param lane Lane; lane to obtain specific option value, may be null
  124.      * @param node Node; node to obtain specific option value, may be null
  125.      * @param linkType LinkType; link type to obtain specific option value, may be null
  126.      * @param <K> value type
  127.      * @return K; option value
  128.      */
  129.     public final <K> K get(final Option<K> option, final Lane lane, final Node node, final LinkType linkType)
  130.     {
  131.         Throw.whenNull(option, "Option may not be null.");
  132.         K value = this.laneOptions.get(lane, option);
  133.         if (value != null)
  134.         {
  135.             return value;
  136.         }
  137.         value = this.nodeOptions.get(node, option);
  138.         if (value != null)
  139.         {
  140.             return value;
  141.         }
  142.         value = this.linkTypeOptions.get(linkType, option);
  143.         if (value != null)
  144.         {
  145.             return value;
  146.         }
  147.         value = this.options.get(null, option);
  148.         if (value != null)
  149.         {
  150.             return value;
  151.         }
  152.         return option.getDefaultValue();
  153.     }

  154.     /**
  155.      * Utility class to store options.
  156.      * <p>
  157.      * Copyright (c) 2013-2018 Delft University of Technology, PO Box 5, 2600 AA, Delft, the Netherlands. All rights reserved.
  158.      * <br>
  159.      * BSD-style license. See <a href="http://opentrafficsim.org/node/13">OpenTrafficSim License</a>.
  160.      * <p>
  161.      * @version $Revision$, $LastChangedDate$, by $Author$, initial version 6 dec. 2017 <br>
  162.      * @author <a href="http://www.tbm.tudelft.nl/averbraeck">Alexander Verbraeck</a>
  163.      * @author <a href="http://www.tudelft.nl/pknoppers">Peter Knoppers</a>
  164.      * @author <a href="http://www.transport.citg.tudelft.nl">Wouter Schakel</a>
  165.      * @param <K> option value type
  166.      */
  167.     private static final class Option<K>
  168.     {

  169.         /** Id. */
  170.         private final String id;

  171.         /** Default value. */
  172.         private final K defaultValue;

  173.         /**
  174.          * Constructor.
  175.          * @param id String; id
  176.          * @param defaultValue K; default value
  177.          */
  178.         Option(final String id, final K defaultValue)
  179.         {
  180.             this.id = id;
  181.             this.defaultValue = defaultValue;
  182.         }

  183.         /**
  184.          * Returns the default value.
  185.          * @return default value
  186.          */
  187.         public K getDefaultValue()
  188.         {
  189.             return this.defaultValue;
  190.         }

  191.         /** {@inheritDoc} */
  192.         @Override
  193.         public int hashCode()
  194.         {
  195.             final int prime = 31;
  196.             int result = 1;
  197.             result = prime * result + ((this.id == null) ? 0 : this.id.hashCode());
  198.             return result;
  199.         }

  200.         /** {@inheritDoc} */
  201.         @Override
  202.         public boolean equals(final Object obj)
  203.         {
  204.             if (this == obj)
  205.             {
  206.                 return true;
  207.             }
  208.             if (obj == null)
  209.             {
  210.                 return false;
  211.             }
  212.             if (getClass() != obj.getClass())
  213.             {
  214.                 return false;
  215.             }
  216.             Option<?> other = (Option<?>) obj;
  217.             if (this.id == null)
  218.             {
  219.                 if (other.id != null)
  220.                 {
  221.                     return false;
  222.                 }
  223.             }
  224.             else if (!this.id.equals(other.id))
  225.             {
  226.                 return false;
  227.             }
  228.             return true;
  229.         }

  230.         /** {@inheritDoc} */
  231.         @Override
  232.         public String toString()
  233.         {
  234.             return "Option [id=" + this.id + "]";
  235.         }

  236.     }

  237.     /**
  238.      * Single set of options for a certain category, i.e. lane, node, link type or null (i.e. global).
  239.      * <p>
  240.      * Copyright (c) 2013-2018 Delft University of Technology, PO Box 5, 2600 AA, Delft, the Netherlands. All rights reserved.
  241.      * <br>
  242.      * BSD-style license. See <a href="http://opentrafficsim.org/node/13">OpenTrafficSim License</a>.
  243.      * <p>
  244.      * @version $Revision$, $LastChangedDate$, by $Author$, initial version 11 jan. 2018 <br>
  245.      * @author <a href="http://www.tbm.tudelft.nl/averbraeck">Alexander Verbraeck</a>
  246.      * @author <a href="http://www.tudelft.nl/pknoppers">Peter Knoppers</a>
  247.      * @author <a href="http://www.transport.citg.tudelft.nl">Wouter Schakel</a>
  248.      * @param <C> category type, i.e. {@code Lane}, {@code Node}, {@code LinkType} or {@code Void} (i.e. global).
  249.      */
  250.     private class OptionSet<C>
  251.     {

  252.         /** Options. */
  253.         private Map<C, Map<Option<?>, Object>> optionsSet = new HashMap<>();

  254.         /**
  255.          * Constructor.
  256.          */
  257.         OptionSet()
  258.         {
  259.             //
  260.         }

  261.         /**
  262.          * Set value in option set.
  263.          * @param category C; category
  264.          * @param option Option&lt;K&gt;; option
  265.          * @param value K; value
  266.          * @param <K> option value type
  267.          */
  268.         public <K> void set(final C category, final Option<K> option, final K value)
  269.         {
  270.             Map<Option<?>, Object> map = this.optionsSet.get(category);
  271.             if (map == null)
  272.             {
  273.                 map = new HashMap<>();
  274.                 this.optionsSet.put(category, map);
  275.             }
  276.             map.put(option, value);
  277.         }

  278.         /**
  279.          * Returns the option value for the category.
  280.          * @param category C; category
  281.          * @param option Option&lt;K&gt;; option
  282.          * @return option value for the category
  283.          * @param <K> value type
  284.          */
  285.         @SuppressWarnings("unchecked")
  286.         public <K> K get(final C category, final Option<K> option)
  287.         {
  288.             if (!this.optionsSet.containsKey(category))
  289.             {
  290.                 return null;
  291.             }
  292.             return (K) this.optionsSet.get(category).get(option);
  293.         }

  294.     }

  295. }