View Javadoc
1   package org.opentrafficsim.road.gtu.lane.tactical.lmrs;
2   
3   import org.djunits.value.vdouble.scalar.Acceleration;
4   import org.djunits.value.vdouble.scalar.Length;
5   import org.djutils.exceptions.Try;
6   import org.opentrafficsim.base.parameters.ParameterException;
7   import org.opentrafficsim.base.parameters.ParameterTypes;
8   import org.opentrafficsim.base.parameters.Parameters;
9   import org.opentrafficsim.core.gtu.perception.EgoPerception;
10  import org.opentrafficsim.core.gtu.plan.operational.OperationalPlanException;
11  import org.opentrafficsim.road.gtu.lane.LaneBasedGtu;
12  import org.opentrafficsim.road.gtu.lane.perception.LanePerception;
13  import org.opentrafficsim.road.gtu.lane.perception.PerceptionCollectable;
14  import org.opentrafficsim.road.gtu.lane.perception.RelativeLane;
15  import org.opentrafficsim.road.gtu.lane.perception.categories.InfrastructurePerception;
16  import org.opentrafficsim.road.gtu.lane.perception.categories.IntersectionPerception;
17  import org.opentrafficsim.road.gtu.lane.perception.categories.neighbors.NeighborsPerception;
18  import org.opentrafficsim.road.gtu.lane.perception.headway.HeadwayConflict;
19  import org.opentrafficsim.road.gtu.lane.perception.headway.HeadwayGtu;
20  import org.opentrafficsim.road.gtu.lane.perception.headway.HeadwayTrafficLight;
21  import org.opentrafficsim.road.gtu.lane.tactical.following.CarFollowingModel;
22  import org.opentrafficsim.road.gtu.lane.tactical.util.CarFollowingUtil;
23  import org.opentrafficsim.road.gtu.lane.tactical.util.lmrs.Desire;
24  import org.opentrafficsim.road.gtu.lane.tactical.util.lmrs.VoluntaryIncentive;
25  import org.opentrafficsim.road.network.lane.conflict.Conflict;
26  import org.opentrafficsim.road.network.lane.object.trafficlight.TrafficLight;
27  import org.opentrafficsim.road.network.speed.SpeedLimitInfo;
28  
29  /**
30   * Incentive to join the shortest queue near intersection.
31   * <p>
32   * Copyright (c) 2013-2024 Delft University of Technology, PO Box 5, 2600 AA, Delft, the Netherlands. All rights reserved. <br>
33   * BSD-style license. See <a href="https://opentrafficsim.org/docs/license.html">OpenTrafficSim License</a>.
34   * </p>
35   * @author <a href="https://github.com/averbraeck">Alexander Verbraeck</a>
36   * @author <a href="https://tudelft.nl/staff/p.knoppers-1">Peter Knoppers</a>
37   * @author <a href="https://github.com/wjschakel">Wouter Schakel</a>
38   */
39  public class IncentiveQueue implements VoluntaryIncentive
40  {
41  
42      /** {@inheritDoc} */
43      @Override
44      public Desire determineDesire(final Parameters parameters, final LanePerception perception,
45              final CarFollowingModel carFollowingModel, final Desire mandatoryDesire, final Desire voluntaryDesire)
46              throws ParameterException, OperationalPlanException
47      {
48          if (!perception.contains(IntersectionPerception.class))
49          {
50              return Desire.ZERO;
51          }
52          EgoPerception<?, ?> ego = perception.getPerceptionCategory(EgoPerception.class);
53          double aCur = Try.assign(() -> perception.getGtu().getCarFollowingAcceleration().si, OperationalPlanException.class,
54                  "Could not obtain the car-following acceleration.");
55          if (aCur <= 0.0 && ego.getSpeed().eq0())
56          {
57              return Desire.ZERO;
58          }
59          IntersectionPerception inter = perception.getPerceptionCategoryOrNull(IntersectionPerception.class);
60          PerceptionCollectable<HeadwayConflict, Conflict> conflicts = inter.getConflicts(RelativeLane.CURRENT);
61          PerceptionCollectable<HeadwayTrafficLight, TrafficLight> lights = inter.getTrafficLights(RelativeLane.CURRENT);
62          // TODO: a ramp-metering traffic light triggers this incentive with possible cooperation from the main line
63          if (conflicts.isEmpty() && lights.isEmpty())
64          {
65              return Desire.ZERO;
66          }
67          Acceleration a = parameters.getParameter(ParameterTypes.A);
68          NeighborsPerception neigbors = perception.getPerceptionCategory(NeighborsPerception.class);
69          InfrastructurePerception infra = perception.getPerceptionCategory(InfrastructurePerception.class);
70  
71          SpeedLimitInfo sli = infra.getSpeedLimitProspect(RelativeLane.CURRENT).getSpeedLimitInfo(Length.ZERO);
72  
73          double dLeft = 0.0;
74          if (infra.getCrossSection().contains(RelativeLane.LEFT))
75          {
76              PerceptionCollectable<HeadwayGtu, LaneBasedGtu> leaders = neigbors.getLeaders(RelativeLane.LEFT);
77              if (!leaders.isEmpty())
78              {
79                  Acceleration acc = CarFollowingUtil.followSingleLeader(carFollowingModel, parameters, ego.getSpeed(), sli,
80                          leaders.first());
81                  dLeft = (acc.si - aCur) / a.si;
82              }
83          }
84          double dRight = 0.0;
85          if (infra.getCrossSection().contains(RelativeLane.RIGHT))
86          {
87              PerceptionCollectable<HeadwayGtu, LaneBasedGtu> leaders = neigbors.getLeaders(RelativeLane.RIGHT);
88              if (!leaders.isEmpty())
89              {
90                  Acceleration acc = CarFollowingUtil.followSingleLeader(carFollowingModel, parameters, ego.getSpeed(), sli,
91                          leaders.first());
92                  dRight = (acc.si - aCur) / a.si;
93              }
94          }
95          return new Desire(dLeft, dRight);
96      }
97  
98      /** {@inheritDoc} */
99      @Override
100     public final String toString()
101     {
102         return "IncentiveQueue";
103     }
104 
105 }