View Javadoc
1   package org.opentrafficsim.road.gtu.lane.tactical.lmrs;
2   
3   import java.io.Serializable;
4   import java.util.Set;
5   import java.util.function.Supplier;
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://github.com/peter-knoppers">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 Supplier<Set<MandatoryIncentive>> mandatoryIncentives;
58  
59      /** Mandatory incentives. */
60      private final Supplier<Set<VoluntaryIncentive>> voluntaryIncentives;
61  
62      /** Mandatory incentives. */
63      private final Supplier<Set<AccelerationIncentive>> accelerationIncentives;
64  
65      /**
66       * Constructor using default incentives and passive synchronization.
67       * @param carFollowingModelFactory factory of the car-following model
68       * @param perceptionFactory perception factory
69       */
70      public LmrsFactory(final CarFollowingModelFactory<? extends CarFollowingModel> carFollowingModelFactory,
71              final PerceptionFactory perceptionFactory)
72      {
73          super(carFollowingModelFactory, perceptionFactory);
74          this.synchronization = Synchronization.PASSIVE;
75          this.cooperation = Cooperation.PASSIVE;
76          this.gapAcceptance = GapAcceptance.INFORMED;
77          this.tailgating = Tailgating.NONE;
78          this.mandatoryIncentives = null;
79          this.voluntaryIncentives = null;
80          this.accelerationIncentives = null;
81      }
82  
83      /**
84       * Constructor with full control over incentives and type of synchronization.
85       * @param carFollowingModelFactory factory of the car-following model
86       * @param perceptionFactory perception factory
87       * @param synchronization type of synchronization
88       * @param cooperation type of cooperation
89       * @param gapAcceptance gap-acceptance
90       * @param tailgating tail gating
91       * @param mandatoryIncentives note that order may matter
92       * @param voluntaryIncentives note that order may matter
93       * @param accelerationIncentives acceleration incentives
94       */
95      public LmrsFactory(final CarFollowingModelFactory<? extends CarFollowingModel> carFollowingModelFactory,
96              final PerceptionFactory perceptionFactory, final Synchronization synchronization, final Cooperation cooperation,
97              final GapAcceptance gapAcceptance, final Tailgating tailgating,
98              final Supplier<Set<MandatoryIncentive>> mandatoryIncentives,
99              final Supplier<Set<VoluntaryIncentive>> voluntaryIncentives,
100             final Supplier<Set<AccelerationIncentive>> accelerationIncentives)
101     {
102         super(carFollowingModelFactory, perceptionFactory);
103         this.synchronization = synchronization;
104         this.cooperation = cooperation;
105         this.gapAcceptance = gapAcceptance;
106         this.tailgating = tailgating;
107         this.mandatoryIncentives = mandatoryIncentives;
108         this.voluntaryIncentives = voluntaryIncentives;
109         this.accelerationIncentives = accelerationIncentives;
110     }
111 
112     // TODO: use factory instead of constructors
113 
114     @Override
115     public final Parameters getParameters() throws ParameterException
116     {
117         ParameterSet parameters = new ParameterSet();
118         parameters.setDefaultParameters(LmrsUtil.class);
119         parameters.setDefaultParameters(LmrsParameters.class);
120         parameters.setDefaultParameters(ConflictUtil.class);
121         parameters.setDefaultParameters(TrafficLightUtil.class);
122         getCarFollowingParameters().setAllIn(parameters);
123         getPerceptionFactory().getParameters().setAllIn(parameters);
124         parameters.setDefaultParameter(ParameterTypes.VCONG);
125         parameters.setDefaultParameter(ParameterTypes.T0);
126         parameters.setDefaultParameter(ParameterTypes.LCDUR);
127         return parameters;
128     }
129 
130     @Override
131     public final Lmrs create(final LaneBasedGtu gtu) throws GtuException
132     {
133         Lmrs lmrs = new Lmrs(nextCarFollowingModel(gtu), gtu, getPerceptionFactory().generatePerception(gtu),
134                 this.synchronization, this.cooperation, this.gapAcceptance, this.tailgating);
135         if (this.mandatoryIncentives == null)
136         {
137             lmrs.setDefaultIncentives();
138         }
139         else
140         {
141             this.mandatoryIncentives.get().forEach(incentive -> lmrs.addMandatoryIncentive(incentive));
142             this.voluntaryIncentives.get().forEach(incentive -> lmrs.addVoluntaryIncentive(incentive));
143             this.accelerationIncentives.get().forEach(incentive -> lmrs.addAccelerationIncentive(incentive));
144         }
145         return lmrs;
146     }
147 
148     @Override
149     public final String toString()
150     {
151         return "LMRSFactory [car-following=" + getCarFollowingModelFactoryString() + "]";
152     }
153 
154 }