View Javadoc
1   package org.opentrafficsim.road.gtu.lane.tactical.util.lmrs;
2   
3   import org.djunits.unit.AccelerationUnit;
4   import org.djunits.value.vdouble.scalar.Acceleration;
5   import org.djunits.value.vdouble.scalar.Speed;
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.core.network.LateralDirectionality;
12  import org.opentrafficsim.road.gtu.lane.perception.LanePerception;
13  import org.opentrafficsim.road.gtu.lane.perception.RelativeLane;
14  import org.opentrafficsim.road.gtu.lane.perception.categories.NeighborsPerception;
15  import org.opentrafficsim.road.gtu.lane.perception.headway.HeadwayGTU;
16  import org.opentrafficsim.road.gtu.lane.tactical.following.CarFollowingModel;
17  import org.opentrafficsim.road.network.speed.SpeedLimitInfo;
18  
19  /**
20   * Different forms of cooperation.
21   * <p>
22   * Copyright (c) 2013-2018 Delft University of Technology, PO Box 5, 2600 AA, Delft, the Netherlands. All rights reserved. <br>
23   * BSD-style license. See <a href="http://opentrafficsim.org/node/13">OpenTrafficSim License</a>.
24   * <p>
25   * @version $Revision$, $LastChangedDate$, by $Author$, initial version 2 mrt. 2018 <br>
26   * @author <a href="http://www.tbm.tudelft.nl/averbraeck">Alexander Verbraeck</a>
27   * @author <a href="http://www.tudelft.nl/pknoppers">Peter Knoppers</a>
28   * @author <a href="http://www.transport.citg.tudelft.nl">Wouter Schakel</a>
29   */
30  public interface Cooperation extends LmrsParameters
31  {
32  
33      /** Simple passive cooperation. */
34      Cooperation PASSIVE = new Cooperation()
35      {
36          @Override
37          public Acceleration cooperate(final LanePerception perception, final Parameters params, final SpeedLimitInfo sli,
38                  final CarFollowingModel cfm, final LateralDirectionality lat, final Desire ownDesire)
39                  throws ParameterException, OperationalPlanException
40          {
41              if ((lat.isLeft() && !perception.getLaneStructure().getExtendedCrossSection().contains(RelativeLane.LEFT))
42                      || (lat.isRight() && !perception.getLaneStructure().getExtendedCrossSection().contains(RelativeLane.RIGHT)))
43              {
44                  return new Acceleration(Double.MAX_VALUE, AccelerationUnit.SI);
45              }
46              Acceleration b = params.getParameter(ParameterTypes.B);
47              Acceleration a = new Acceleration(Double.MAX_VALUE, AccelerationUnit.SI);
48              double dCoop = params.getParameter(DCOOP);
49              Speed ownSpeed = perception.getPerceptionCategory(EgoPerception.class).getSpeed();
50              RelativeLane relativeLane = new RelativeLane(lat, 1);
51              for (HeadwayGTU leader : Synchronization.removeAllUpstreamOfConflicts(Synchronization.removeAllUpstreamOfConflicts(
52                      perception.getPerceptionCategory(NeighborsPerception.class).getLeaders(relativeLane), perception,
53                      relativeLane), perception, RelativeLane.CURRENT))
54              {
55                  Parameters params2 = leader.getParameters();
56                  double desire = lat.equals(LateralDirectionality.LEFT) ? params2.getParameter(DRIGHT)
57                          : lat.equals(LateralDirectionality.RIGHT) ? params2.getParameter(DLEFT) : 0;
58                  if (desire >= dCoop && (leader.getSpeed().gt0() || leader.getDistance().gt0()))
59                  {
60                      Acceleration aSingle = LmrsUtil.singleAcceleration(leader.getDistance(), ownSpeed, leader.getSpeed(),
61                              desire, params, sli, cfm);
62                      a = Acceleration.min(a, aSingle);
63                  }
64              }
65              return Acceleration.max(a, b.neg());
66          }
67      };
68  
69      /** Simple passive cooperation. */
70      Cooperation PASSIVE_MOVING = new Cooperation()
71      {
72          @Override
73          public Acceleration cooperate(final LanePerception perception, final Parameters params, final SpeedLimitInfo sli,
74                  final CarFollowingModel cfm, final LateralDirectionality lat, final Desire ownDesire)
75                  throws ParameterException, OperationalPlanException
76          {
77              if ((lat.isLeft() && !perception.getLaneStructure().getExtendedCrossSection().contains(RelativeLane.LEFT))
78                      || (lat.isRight() && !perception.getLaneStructure().getExtendedCrossSection().contains(RelativeLane.RIGHT)))
79              {
80                  return new Acceleration(Double.MAX_VALUE, AccelerationUnit.SI);
81              }
82              Acceleration b = params.getParameter(ParameterTypes.B);
83              Acceleration a = new Acceleration(Double.MAX_VALUE, AccelerationUnit.SI);
84              double dCoop = params.getParameter(DCOOP);
85              Speed ownSpeed = perception.getPerceptionCategory(EgoPerception.class).getSpeed();
86              RelativeLane relativeLane = new RelativeLane(lat, 1);
87              for (HeadwayGTU leader : Synchronization.removeAllUpstreamOfConflicts(Synchronization.removeAllUpstreamOfConflicts(
88                      perception.getPerceptionCategory(NeighborsPerception.class).getLeaders(relativeLane), perception,
89                      relativeLane), perception, RelativeLane.CURRENT))
90              {
91                  Parameters params2 = leader.getParameters();
92                  double desire = lat.equals(LateralDirectionality.LEFT) ? params2.getParameter(DRIGHT)
93                          : lat.equals(LateralDirectionality.RIGHT) ? params2.getParameter(DLEFT) : 0;
94                  if (desire >= dCoop && (leader.getSpeed().gt0() || leader.getDistance().gt0())
95                          && leader.getAcceleration().gt(params.getParameter(ParameterTypes.BCRIT).neg()))
96                  {
97                      Acceleration aSingle = LmrsUtil.singleAcceleration(leader.getDistance(), ownSpeed, leader.getSpeed(),
98                              desire, params, sli, cfm);
99                      a = Acceleration.min(a, aSingle);
100                 }
101             }
102             return Acceleration.max(a, b.neg());
103         }
104     };
105 
106     /** Cooperation similar to the default, with nuanced differences of when to ignore. */
107     Cooperation ACTIVE = new Cooperation()
108     {
109         @Override
110         public Acceleration cooperate(final LanePerception perception, final Parameters params, final SpeedLimitInfo sli,
111                 final CarFollowingModel cfm, final LateralDirectionality lat, final Desire ownDesire)
112                 throws ParameterException, OperationalPlanException
113         {
114 
115             if ((lat.isLeft() && !perception.getLaneStructure().getExtendedCrossSection().contains(RelativeLane.LEFT))
116                     || (lat.isRight() && !perception.getLaneStructure().getExtendedCrossSection().contains(RelativeLane.RIGHT)))
117             {
118                 return new Acceleration(Double.MAX_VALUE, AccelerationUnit.SI);
119             }
120             Acceleration a = new Acceleration(Double.MAX_VALUE, AccelerationUnit.SI);
121             double dCoop = params.getParameter(DCOOP);
122             Speed ownSpeed = perception.getPerceptionCategory(EgoPerception.class).getSpeed();
123             RelativeLane relativeLane = new RelativeLane(lat, 1);
124             for (HeadwayGTU leader : Synchronization.removeAllUpstreamOfConflicts(Synchronization.removeAllUpstreamOfConflicts(
125                     perception.getPerceptionCategory(NeighborsPerception.class).getLeaders(relativeLane), perception,
126                     relativeLane), perception, RelativeLane.CURRENT))
127             {
128                 Parameters params2 = leader.getParameters();
129                 double desire = lat.equals(LateralDirectionality.LEFT) ? params2.getParameter(DRIGHT)
130                         : lat.equals(LateralDirectionality.RIGHT) ? params2.getParameter(DLEFT) : 0;
131                 if (desire >= dCoop && leader.getDistance().gt0()
132                         && leader.getAcceleration().gt(params.getParameter(ParameterTypes.BCRIT).neg()))
133                 {
134                     Acceleration aSingle = LmrsUtil.singleAcceleration(leader.getDistance(), ownSpeed, leader.getSpeed(),
135                             desire, params, sli, cfm);
136                     a = Acceleration.min(a, Synchronization.gentleUrgency(aSingle, desire, params));
137                 }
138             }
139             return a;
140         }
141     };
142 
143     /**
144      * Determine acceleration for cooperation.
145      * @param perception perception
146      * @param params parameters
147      * @param sli speed limit info
148      * @param cfm car-following model
149      * @param lat lateral direction for cooperation
150      * @param ownDesire own lane change desire
151      * @return acceleration for synchronization
152      * @throws ParameterException if a parameter is not defined
153      * @throws OperationalPlanException perception exception
154      */
155     Acceleration cooperate(LanePerception perception, Parameters params, SpeedLimitInfo sli, CarFollowingModel cfm,
156             LateralDirectionality lat, Desire ownDesire) throws ParameterException, OperationalPlanException;
157 }