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.neighbors.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-2019 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      /** Same as passive cooperation, except that cooperation is fully ignored if the potential lane changer brakes heavily. */
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                  {
96                      Acceleration aSingle = LmrsUtil.singleAcceleration(leader.getDistance(), ownSpeed, leader.getSpeed(),
97                              desire, params, sli, cfm);
98                      a = Acceleration.min(a, aSingle);
99                      a = Acceleration.max(a, params.getParameter(ParameterTypes.B).neg());
100                     // if (aSingle.gt(leader.getAcceleration()) || aSingle.gt(params.getParameter(ParameterTypes.B).neg()))
101                     // {
102                     // a = Acceleration.min(a, aSingle);
103                     // a = Synchronization.gentleUrgency(a, desire, params);
104                     // }
105                 }
106             }
107             return a;
108             // return Acceleration.max(a, b.neg());
109         }
110     };
111 
112     /** Cooperation similar to the default, with nuanced differences of when to ignore. */
113     Cooperation ACTIVE = new Cooperation()
114     {
115         @Override
116         public Acceleration cooperate(final LanePerception perception, final Parameters params, final SpeedLimitInfo sli,
117                 final CarFollowingModel cfm, final LateralDirectionality lat, final Desire ownDesire)
118                 throws ParameterException, OperationalPlanException
119         {
120 
121             if ((lat.isLeft() && !perception.getLaneStructure().getExtendedCrossSection().contains(RelativeLane.LEFT))
122                     || (lat.isRight() && !perception.getLaneStructure().getExtendedCrossSection().contains(RelativeLane.RIGHT)))
123             {
124                 return new Acceleration(Double.MAX_VALUE, AccelerationUnit.SI);
125             }
126             Acceleration a = new Acceleration(Double.MAX_VALUE, AccelerationUnit.SI);
127             double dCoop = params.getParameter(DCOOP);
128             Speed ownSpeed = perception.getPerceptionCategory(EgoPerception.class).getSpeed();
129             RelativeLane relativeLane = new RelativeLane(lat, 1);
130             for (HeadwayGTU leader : Synchronization.removeAllUpstreamOfConflicts(Synchronization.removeAllUpstreamOfConflicts(
131                     perception.getPerceptionCategory(NeighborsPerception.class).getLeaders(relativeLane), perception,
132                     relativeLane), perception, RelativeLane.CURRENT))
133             {
134                 Parameters params2 = leader.getParameters();
135                 double desire = lat.equals(LateralDirectionality.LEFT) ? params2.getParameter(DRIGHT)
136                         : lat.equals(LateralDirectionality.RIGHT) ? params2.getParameter(DLEFT) : 0;
137                 if (desire >= dCoop && leader.getDistance().gt0()
138                         && leader.getAcceleration().gt(params.getParameter(ParameterTypes.BCRIT).neg()))
139                 {
140                     Acceleration aSingle = LmrsUtil.singleAcceleration(leader.getDistance(), ownSpeed, leader.getSpeed(),
141                             desire, params, sli, cfm);
142                     a = Acceleration.min(a, Synchronization.gentleUrgency(aSingle, desire, params));
143                 }
144             }
145             return a;
146         }
147     };
148 
149     /**
150      * Determine acceleration for cooperation.
151      * @param perception LanePerception; perception
152      * @param params Parameters; parameters
153      * @param sli SpeedLimitInfo; speed limit info
154      * @param cfm CarFollowingModel; car-following model
155      * @param lat LateralDirectionality; lateral direction for cooperation
156      * @param ownDesire Desire; own lane change desire
157      * @return acceleration for synchronization
158      * @throws ParameterException if a parameter is not defined
159      * @throws OperationalPlanException perception exception
160      */
161     Acceleration cooperate(LanePerception perception, Parameters params, SpeedLimitInfo sli, CarFollowingModel cfm,
162             LateralDirectionality lat, Desire ownDesire) throws ParameterException, OperationalPlanException;
163 }