OdOptions.java

  1. package org.opentrafficsim.road.od;

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

  4. import org.djunits.value.vdouble.scalar.Frequency;
  5. import org.djunits.value.vdouble.scalar.Length;
  6. import org.djutils.exceptions.Throw;
  7. import org.opentrafficsim.core.gtu.GtuErrorHandler;
  8. import org.opentrafficsim.core.gtu.GtuType;
  9. import org.opentrafficsim.core.idgenerator.IdGenerator;
  10. import org.opentrafficsim.core.network.LinkType;
  11. import org.opentrafficsim.core.network.Node;
  12. import org.opentrafficsim.road.gtu.generator.CfBaRoomChecker;
  13. import org.opentrafficsim.road.gtu.generator.GeneratorPositions.LaneBiases;
  14. import org.opentrafficsim.road.gtu.generator.LaneBasedGtuGenerator.RoomChecker;
  15. import org.opentrafficsim.road.gtu.generator.MarkovCorrelation;
  16. import org.opentrafficsim.road.gtu.generator.characteristics.LaneBasedGtuCharacteristicsGeneratorOd;
  17. import org.opentrafficsim.road.gtu.generator.headway.ArrivalsHeadwayGenerator.HeadwayDistribution;
  18. import org.opentrafficsim.road.network.lane.Lane;

  19. /**
  20.  * Options for vehicle generation based on an OD matrix.
  21.  * <p>
  22.  * Copyright (c) 2013-2024 Delft University of Technology, PO Box 5, 2600 AA, Delft, the Netherlands. All rights reserved. <br>
  23.  * BSD-style license. See <a href="https://opentrafficsim.org/docs/license.html">OpenTrafficSim License</a>.
  24.  * </p>
  25.  * @author <a href="https://github.com/averbraeck">Alexander Verbraeck</a>
  26.  * @author <a href="https://tudelft.nl/staff/p.knoppers-1">Peter Knoppers</a>
  27.  * @author <a href="https://github.com/wjschakel">Wouter Schakel</a>
  28.  */
  29. public class OdOptions
  30. {
  31.     /** Headway randomization option. */
  32.     public static final Option<HeadwayDistribution> HEADWAY_DIST =
  33.             new Option<>("headway distribution", HeadwayDistribution.EXPONENTIAL);

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

  36.     /** GTU characteristics generator option. */
  37.     public static final Option<LaneBasedGtuCharacteristicsGeneratorOd> GTU_TYPE = new Option<>("gtu type", null);

  38.     /** Room checker option. */
  39.     public static final Option<RoomChecker> ROOM_CHECKER = new Option<>("room checker", new CfBaRoomChecker());

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

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

  44.     /** Whether to perform instantaneous lane changes. */
  45.     public static final Option<Boolean> INSTANT_LC = new Option<>("instant lc", false);

  46.     /** Error handler when GTU exceptions occur. */
  47.     public static final Option<GtuErrorHandler> ERROR_HANDLER = new Option<>("error handler", GtuErrorHandler.THROW);

  48.     /** Lane bias. Default is none, i.e. uniform distribution over lanes for all GTU types. */
  49.     public static final Option<LaneBiases> LANE_BIAS = new Option<>("lane bias", new LaneBiases());

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

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

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

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

  58.     /**
  59.      * Set option value.
  60.      * @param option Option&lt;K&gt;; option
  61.      * @param value K; option value
  62.      * @param <K> value type
  63.      * @return this option set
  64.      */
  65.     public final <K> OdOptions set(final Option<K> option, final K value)
  66.     {
  67.         this.options.set(null, option, value);
  68.         return this;
  69.     }

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

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

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

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

  150.     /**
  151.      * Utility class to store options.
  152.      * <p>
  153.      * Copyright (c) 2013-2024 Delft University of Technology, PO Box 5, 2600 AA, Delft, the Netherlands. All rights reserved.
  154.      * <br>
  155.      * BSD-style license. See <a href="https://opentrafficsim.org/docs/license.html">OpenTrafficSim License</a>.
  156.      * </p>
  157.      * @author <a href="https://github.com/averbraeck">Alexander Verbraeck</a>
  158.      * @author <a href="https://tudelft.nl/staff/p.knoppers-1">Peter Knoppers</a>
  159.      * @author <a href="https://github.com/wjschakel">Wouter Schakel</a>
  160.      * @param <K> option value type
  161.      */
  162.     public static final class Option<K>
  163.     {

  164.         /** Id. */
  165.         private final String id;

  166.         /** Default value. */
  167.         private final K defaultValue;

  168.         /**
  169.          * Constructor.
  170.          * @param id String; id
  171.          * @param defaultValue K; default value
  172.          */
  173.         Option(final String id, final K defaultValue)
  174.         {
  175.             this.id = id;
  176.             this.defaultValue = defaultValue;
  177.         }

  178.         /**
  179.          * Returns the default value.
  180.          * @return default value
  181.          */
  182.         public K getDefaultValue()
  183.         {
  184.             return this.defaultValue;
  185.         }

  186.         /** {@inheritDoc} */
  187.         @Override
  188.         public int hashCode()
  189.         {
  190.             final int prime = 31;
  191.             int result = 1;
  192.             result = prime * result + ((this.id == null) ? 0 : this.id.hashCode());
  193.             return result;
  194.         }

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

  225.         /** {@inheritDoc} */
  226.         @Override
  227.         public String toString()
  228.         {
  229.             return "Option [id=" + this.id + "]";
  230.         }

  231.     }

  232.     /**
  233.      * Single set of options for a certain category, i.e. lane, node, link type or null (i.e. global).
  234.      * <p>
  235.      * Copyright (c) 2013-2024 Delft University of Technology, PO Box 5, 2600 AA, Delft, the Netherlands. All rights reserved.
  236.      * <br>
  237.      * BSD-style license. See <a href="https://opentrafficsim.org/docs/license.html">OpenTrafficSim License</a>.
  238.      * </p>
  239.      * @author <a href="https://github.com/averbraeck">Alexander Verbraeck</a>
  240.      * @author <a href="https://tudelft.nl/staff/p.knoppers-1">Peter Knoppers</a>
  241.      * @author <a href="https://github.com/wjschakel">Wouter Schakel</a>
  242.      * @param <C> category type, i.e. {@code Lane}, {@code Node}, {@code LinkType} or {@code Void} (i.e. global).
  243.      */
  244.     private class OptionSet<C>
  245.     {

  246.         /** Options. */
  247.         private Map<C, Map<Option<?>, Object>> optionsSet = new LinkedHashMap<>();

  248.         /**
  249.          * Constructor.
  250.          */
  251.         OptionSet()
  252.         {
  253.             //
  254.         }

  255.         /**
  256.          * Set value in option set.
  257.          * @param category C; category
  258.          * @param option Option&lt;K&gt;; option
  259.          * @param value K; value
  260.          * @param <K> option value type
  261.          */
  262.         public <K> void set(final C category, final Option<K> option, final K value)
  263.         {
  264.             Map<Option<?>, Object> map = this.optionsSet.get(category);
  265.             if (map == null)
  266.             {
  267.                 map = new LinkedHashMap<>();
  268.                 this.optionsSet.put(category, map);
  269.             }
  270.             map.put(option, value);
  271.         }

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

  288.     }

  289. }