DefaultLaneBasedGtuCharacteristicsGeneratorOd.java

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

  2. import java.util.LinkedHashMap;
  3. import java.util.LinkedHashSet;
  4. import java.util.Map;
  5. import java.util.Set;

  6. import org.djutils.exceptions.Throw;
  7. import org.djutils.exceptions.Try;
  8. import org.opentrafficsim.core.definitions.DefaultsNl;
  9. import org.opentrafficsim.core.distributions.Generator;
  10. import org.opentrafficsim.core.gtu.GtuCharacteristics;
  11. import org.opentrafficsim.core.gtu.GtuException;
  12. import org.opentrafficsim.core.gtu.GtuTemplate;
  13. import org.opentrafficsim.core.gtu.GtuType;
  14. import org.opentrafficsim.core.network.Node;
  15. import org.opentrafficsim.core.network.route.Route;
  16. import org.opentrafficsim.road.gtu.lane.VehicleModel;
  17. import org.opentrafficsim.road.gtu.lane.VehicleModelFactory;
  18. import org.opentrafficsim.road.gtu.lane.tactical.following.IdmPlusFactory;
  19. import org.opentrafficsim.road.gtu.lane.tactical.lmrs.DefaultLmrsPerceptionFactory;
  20. import org.opentrafficsim.road.gtu.lane.tactical.lmrs.LmrsFactory;
  21. import org.opentrafficsim.road.gtu.strategical.LaneBasedStrategicalPlannerFactory;
  22. import org.opentrafficsim.road.gtu.strategical.LaneBasedStrategicalRoutePlannerFactory;
  23. import org.opentrafficsim.road.od.Categorization;
  24. import org.opentrafficsim.road.od.Category;

  25. import nl.tudelft.simulation.jstats.streams.StreamInterface;

  26. /**
  27.  * Default generator for {@code LaneBasedGtuCharacteristics} in a context with OD information.
  28.  * <p>
  29.  * Copyright (c) 2013-2024 Delft University of Technology, PO Box 5, 2600 AA, Delft, the Netherlands. All rights reserved. <br>
  30.  * BSD-style license. See <a href="https://opentrafficsim.org/docs/license.html">OpenTrafficSim License</a>.
  31.  * </p>
  32.  * @author <a href="https://github.com/averbraeck">Alexander Verbraeck</a>
  33.  * @author <a href="https://github.com/peter-knoppers">Peter Knoppers</a>
  34.  * @author <a href="https://github.com/wjschakel">Wouter Schakel</a>
  35.  */
  36. public final class DefaultLaneBasedGtuCharacteristicsGeneratorOd implements LaneBasedGtuCharacteristicsGeneratorOd
  37. {
  38.     /** GTU type generator. */
  39.     private Generator<GtuType> gtuTypeGenerator = null;

  40.     /** Templates. */
  41.     private final Map<GtuType, GtuTemplate> templates = new LinkedHashMap<>();

  42.     /** Supplies a strategical factory. */
  43.     private final LaneBasedStrategicalPlannerFactory<?> factory;

  44.     /** Vehicle factory. */
  45.     private VehicleModelFactory vehicleModelFactory = VehicleModelFactory.MINMAX;

  46.     /**
  47.      * Constructor using route supplier, provided GTU templates and provided strategical planner factory supplier.
  48.      * @param gtuTypeGenerator GTU type generator
  49.      * @param templates templates
  50.      * @param factory strategical factory supplier
  51.      * @param vehicleModelFactory vehicle model factory
  52.      */
  53.     private DefaultLaneBasedGtuCharacteristicsGeneratorOd(final Generator<GtuType> gtuTypeGenerator,
  54.             final Set<GtuTemplate> templates, final LaneBasedStrategicalPlannerFactory<?> factory,
  55.             final VehicleModelFactory vehicleModelFactory)
  56.     {
  57.         Throw.whenNull(factory, "Strategical planner factory may not be null.");
  58.         this.gtuTypeGenerator = gtuTypeGenerator;
  59.         if (templates != null)
  60.         {
  61.             for (GtuTemplate template : templates)
  62.             {
  63.                 this.templates.put(template.getGtuType(), template);
  64.             }
  65.         }
  66.         this.factory = factory;
  67.         if (vehicleModelFactory == null)
  68.         {
  69.             this.vehicleModelFactory = VehicleModelFactory.MINMAX;
  70.         }
  71.         else
  72.         {
  73.             this.vehicleModelFactory = vehicleModelFactory;
  74.         }
  75.     }

  76.     @Override
  77.     public LaneBasedGtuCharacteristics draw(final Node origin, final Node destination, final Category category,
  78.             final StreamInterface randomStream) throws GtuException
  79.     {
  80.         Categorization categorization = category.getCategorization();
  81.         // GTU characteristics
  82.         GtuType gtuType;
  83.         if (categorization.entails(GtuType.class))
  84.         {
  85.             gtuType = category.get(GtuType.class);
  86.         }
  87.         else if (this.gtuTypeGenerator != null)
  88.         {
  89.             gtuType = Try.assign(() -> this.gtuTypeGenerator.draw(), GtuException.class, "Parameter while drawing GTU type.");
  90.         }
  91.         else
  92.         {
  93.             gtuType = DefaultsNl.CAR;
  94.         }
  95.         GtuCharacteristics gtuCharacteristics;
  96.         if (this.templates.containsKey(gtuType))
  97.         {
  98.             gtuCharacteristics =
  99.                     Try.assign(() -> this.templates.get(gtuType).draw(), "Exception while drawing GTU characteristics.");
  100.         }
  101.         else
  102.         {
  103.             gtuCharacteristics = Try.assign(() -> GtuType.defaultCharacteristics(gtuType, origin.getNetwork(), randomStream),
  104.                     "Exception while applying default GTU characteristics.");
  105.         }
  106.         Route route = categorization.entails(Route.class) ? category.get(Route.class) : null;
  107.         VehicleModel vehicleModel = this.vehicleModelFactory.create(gtuType);

  108.         return new LaneBasedGtuCharacteristics(gtuCharacteristics, this.factory, route, origin, destination, vehicleModel);
  109.     }

  110.     /**
  111.      * Returns a strategical model factory for a standard LMRS model, to be used in {@code Factory}.
  112.      * @param stream random number stream.
  113.      * @return factory for a standard LMRS model.
  114.      */
  115.     public static LaneBasedStrategicalRoutePlannerFactory defaultLmrs(final StreamInterface stream)
  116.     {
  117.         return new LaneBasedStrategicalRoutePlannerFactory(
  118.                 new LmrsFactory(new IdmPlusFactory(stream), new DefaultLmrsPerceptionFactory()));
  119.     }

  120.     /**
  121.      * Factory for {@code DefaultGtuCharacteristicsGeneratorOD}.
  122.      * <p>
  123.      * Copyright (c) 2013-2024 Delft University of Technology, PO Box 5, 2600 AA, Delft, the Netherlands. All rights reserved.
  124.      * <br>
  125.      * BSD-style license. See <a href="https://opentrafficsim.org/docs/license.html">OpenTrafficSim License</a>.
  126.      * </p>
  127.      * @author <a href="https://github.com/averbraeck">Alexander Verbraeck</a>
  128.      * @author <a href="https://github.com/peter-knoppers">Peter Knoppers</a>
  129.      * @author <a href="https://github.com/wjschakel">Wouter Schakel</a>
  130.      */
  131.     @SuppressWarnings("hiddenfield")
  132.     public static class Factory
  133.     {
  134.         /** GTU type. */
  135.         private Generator<GtuType> gtuTypeGenerator = null;

  136.         /** Templates. */
  137.         private Set<GtuTemplate> templates = new LinkedHashSet<>();

  138.         /** Supplies a strategical factory. */
  139.         private final LaneBasedStrategicalPlannerFactory<?> factory;

  140.         /** Vehicle factory. */
  141.         private VehicleModelFactory vehicleModelFactory = VehicleModelFactory.MINMAX;

  142.         /**
  143.          * Constructor.
  144.          * @param factory set factorySupplier.
  145.          */
  146.         public Factory(final LaneBasedStrategicalPlannerFactory<?> factory)
  147.         {
  148.             this.factory = factory;
  149.         }

  150.         /**
  151.          * @param gtuTypeGenerator set gtuTypeGenerator.
  152.          * @return this factory for method chaining
  153.          */
  154.         public Factory setGtuTypeGenerator(final Generator<GtuType> gtuTypeGenerator)
  155.         {
  156.             this.gtuTypeGenerator = gtuTypeGenerator;
  157.             return this;
  158.         }

  159.         /**
  160.          * @param templates set templates.
  161.          * @return this factory for method chaining
  162.          */
  163.         public Factory setTemplates(final Set<GtuTemplate> templates)
  164.         {
  165.             this.templates = templates;
  166.             return this;
  167.         }

  168.         /**
  169.          * @param vehicleModelFactory set vehicleModelFactory.
  170.          * @return this factory for method chaining
  171.          */
  172.         public Factory setVehicleModelGenerator(final VehicleModelFactory vehicleModelFactory)
  173.         {
  174.             this.vehicleModelFactory = vehicleModelFactory;
  175.             return this;
  176.         }

  177.         /**
  178.          * Creates the default GTU characteristics generator based on OD information.
  179.          * @return default GTU characteristics generator based on OD information
  180.          */
  181.         @SuppressWarnings("synthetic-access")
  182.         public DefaultLaneBasedGtuCharacteristicsGeneratorOd create()
  183.         {
  184.             return new DefaultLaneBasedGtuCharacteristicsGeneratorOd(this.gtuTypeGenerator, this.templates, this.factory,
  185.                     this.vehicleModelFactory);
  186.         }
  187.     }

  188. }