View Javadoc
1   package org.opentrafficsim.road.gtu.lane.tactical.lmrs;
2   
3   import java.io.Serializable;
4   import java.util.LinkedHashSet;
5   import java.util.Set;
6   
7   import org.opentrafficsim.base.parameters.ParameterException;
8   import org.opentrafficsim.base.parameters.ParameterSet;
9   import org.opentrafficsim.base.parameters.ParameterTypes;
10  import org.opentrafficsim.base.parameters.Parameters;
11  import org.opentrafficsim.core.gtu.GtuException;
12  import org.opentrafficsim.road.gtu.lane.LaneBasedGtu;
13  import org.opentrafficsim.road.gtu.lane.perception.PerceptionFactory;
14  import org.opentrafficsim.road.gtu.lane.tactical.AbstractLaneBasedTacticalPlannerFactory;
15  import org.opentrafficsim.road.gtu.lane.tactical.following.CarFollowingModel;
16  import org.opentrafficsim.road.gtu.lane.tactical.following.CarFollowingModelFactory;
17  import org.opentrafficsim.road.gtu.lane.tactical.util.ConflictUtil;
18  import org.opentrafficsim.road.gtu.lane.tactical.util.TrafficLightUtil;
19  import org.opentrafficsim.road.gtu.lane.tactical.util.lmrs.Cooperation;
20  import org.opentrafficsim.road.gtu.lane.tactical.util.lmrs.GapAcceptance;
21  import org.opentrafficsim.road.gtu.lane.tactical.util.lmrs.LmrsParameters;
22  import org.opentrafficsim.road.gtu.lane.tactical.util.lmrs.LmrsUtil;
23  import org.opentrafficsim.road.gtu.lane.tactical.util.lmrs.MandatoryIncentive;
24  import org.opentrafficsim.road.gtu.lane.tactical.util.lmrs.Synchronization;
25  import org.opentrafficsim.road.gtu.lane.tactical.util.lmrs.Tailgating;
26  import org.opentrafficsim.road.gtu.lane.tactical.util.lmrs.VoluntaryIncentive;
27  
28  /**
29   * Factory for a tactical planner using LMRS with any car-following model.
30   * <p>
31   * Copyright (c) 2013-2024 Delft University of Technology, PO Box 5, 2600 AA, Delft, the Netherlands. All rights reserved. <br>
32   * BSD-style license. See <a href="https://opentrafficsim.org/docs/license.html">OpenTrafficSim License</a>.
33   * </p>
34   * @author <a href="https://github.com/averbraeck">Alexander Verbraeck</a>
35   * @author <a href="https://tudelft.nl/staff/p.knoppers-1">Peter Knoppers</a>
36   * @author <a href="https://github.com/wjschakel">Wouter Schakel</a>
37   */
38  public class LmrsFactory extends AbstractLaneBasedTacticalPlannerFactory<Lmrs> implements Serializable
39  {
40  
41      /** */
42      private static final long serialVersionUID = 20160811L;
43  
44      /** Type of synchronization. */
45      private final Synchronization synchronization;
46  
47      /** Type of cooperation. */
48      private final Cooperation cooperation;
49  
50      /** Type of gap-acceptance. */
51      private final GapAcceptance gapAcceptance;
52  
53      /** Type of tail gating. */
54      private final Tailgating tailgating;
55  
56      /** Mandatory incentives. */
57      private final Set<MandatoryIncentive> mandatoryIncentives = new LinkedHashSet<>();
58  
59      /** Mandatory incentives. */
60      private final Set<VoluntaryIncentive> voluntaryIncentives = new LinkedHashSet<>();
61  
62      /** Mandatory incentives. */
63      private final Set<AccelerationIncentive> accelerationIncentives = new LinkedHashSet<>();
64  
65      /**
66       * Constructor using default incentives and passive synchronization.
67       * @param carFollowingModelFactory CarFollowingModelFactory&lt;? extends CarFollowingModel&gt;; factory of the car-following
68       *            model
69       * @param perceptionFactory PerceptionFactory; perception factory
70       */
71      public LmrsFactory(final CarFollowingModelFactory<? extends CarFollowingModel> carFollowingModelFactory,
72              final PerceptionFactory perceptionFactory)
73      {
74          super(carFollowingModelFactory, perceptionFactory);
75          this.synchronization = Synchronization.PASSIVE;
76          this.cooperation = Cooperation.PASSIVE;
77          this.gapAcceptance = GapAcceptance.INFORMED;
78          this.tailgating = Tailgating.NONE;
79      }
80  
81      /**
82       * Constructor with full control over incentives and type of synchronization.
83       * @param carFollowingModelFactory CarFollowingModelFactory&lt;? extends CarFollowingModel&gt;; factory of the car-following
84       *            model
85       * @param perceptionFactory PerceptionFactory; perception factory
86       * @param synchronization Synchronization; type of synchronization
87       * @param cooperation Cooperation; type of cooperation
88       * @param gapAcceptance GapAcceptance; gap-acceptance
89       * @param tailgating Tailgating; tail gating
90       * @param mandatoryIncentives mandatory incentives; note that order may matter
91       * @param voluntaryIncentives voluntary incentives; note that order may matter
92       * @param accelerationIncentives Set&lt;AccelerationIncentive&gt;; acceleration incentives
93       */
94      public LmrsFactory(final CarFollowingModelFactory<? extends CarFollowingModel> carFollowingModelFactory,
95              final PerceptionFactory perceptionFactory, final Synchronization synchronization, final Cooperation cooperation,
96              final GapAcceptance gapAcceptance, final Tailgating tailgating, final Set<MandatoryIncentive> mandatoryIncentives,
97              final Set<VoluntaryIncentive> voluntaryIncentives, final Set<AccelerationIncentive> accelerationIncentives)
98      {
99          super(carFollowingModelFactory, perceptionFactory);
100         this.synchronization = synchronization;
101         this.cooperation = cooperation;
102         this.gapAcceptance = gapAcceptance;
103         this.tailgating = tailgating;
104         this.mandatoryIncentives.addAll(mandatoryIncentives);
105         this.voluntaryIncentives.addAll(voluntaryIncentives);
106         this.accelerationIncentives.addAll(accelerationIncentives);
107     }
108 
109     // TODO: use factory instead of constructors
110 
111     /** {@inheritDoc} */
112     @Override
113     public final Parameters getParameters() throws ParameterException
114     {
115         ParameterSet parameters = new ParameterSet();
116         parameters.setDefaultParameters(LmrsUtil.class);
117         parameters.setDefaultParameters(LmrsParameters.class);
118         parameters.setDefaultParameters(ConflictUtil.class);
119         parameters.setDefaultParameters(TrafficLightUtil.class);
120         getCarFollowingParameters().setAllIn(parameters);
121         getPerceptionFactory().getParameters().setAllIn(parameters);
122         parameters.setDefaultParameter(ParameterTypes.VCONG);
123         parameters.setDefaultParameter(ParameterTypes.T0);
124         parameters.setDefaultParameter(ParameterTypes.LCDUR);
125         return parameters;
126     }
127 
128     /** {@inheritDoc} */
129     @Override
130     public final Lmrs create(final LaneBasedGtu gtu) throws GtuException
131     {
132         Lmrs lmrs = new Lmrs(nextCarFollowingModel(gtu), gtu, getPerceptionFactory().generatePerception(gtu),
133                 this.synchronization, this.cooperation, this.gapAcceptance, this.tailgating);
134         if (this.mandatoryIncentives.isEmpty())
135         {
136             lmrs.setDefaultIncentives();
137         }
138         else
139         {
140             this.mandatoryIncentives.forEach(incentive -> lmrs.addMandatoryIncentive(incentive));
141             this.voluntaryIncentives.forEach(incentive -> lmrs.addVoluntaryIncentive(incentive));
142             this.accelerationIncentives.forEach(incentive -> lmrs.addAccelerationIncentive(incentive));
143         }
144         return lmrs;
145     }
146 
147     /** {@inheritDoc} */
148     @Override
149     public final String toString()
150     {
151         return "LMRSFactory [car-following=" + getCarFollowingModelFactoryString() + "]";
152     }
153 
154 }