View Javadoc
1   package org.opentrafficsim.road.gtu.lane.tactical;
2   
3   import java.util.ArrayList;
4   import java.util.List;
5   
6   import org.djunits.unit.DurationUnit;
7   import org.djunits.value.vdouble.scalar.Duration;
8   import org.djunits.value.vdouble.scalar.Length;
9   import org.djunits.value.vdouble.scalar.Time;
10  import org.opentrafficsim.base.parameters.ParameterException;
11  import org.opentrafficsim.core.gtu.GTUException;
12  import org.opentrafficsim.core.gtu.plan.operational.OperationalPlan;
13  import org.opentrafficsim.core.gtu.plan.operational.OperationalPlan.Segment;
14  import org.opentrafficsim.core.gtu.plan.operational.OperationalPlanException;
15  import org.opentrafficsim.core.network.NetworkException;
16  import org.opentrafficsim.road.gtu.lane.LaneBasedGTU;
17  import org.opentrafficsim.road.gtu.lane.perception.CategoricalLanePerception;
18  import org.opentrafficsim.road.gtu.lane.perception.LanePerception;
19  import org.opentrafficsim.road.gtu.lane.perception.categories.DefaultSimplePerception;
20  import org.opentrafficsim.road.gtu.lane.perception.categories.DirectDefaultSimplePerception;
21  import org.opentrafficsim.road.gtu.lane.perception.headway.Headway;
22  import org.opentrafficsim.road.gtu.lane.tactical.following.AccelerationStep;
23  import org.opentrafficsim.road.gtu.lane.tactical.following.GTUFollowingModelOld;
24  
25  import nl.tudelft.simulation.language.d3.DirectedPoint;
26  
27  /**
28   * Lane-based tactical planner that implements car following behavior. This tactical planner retrieves the car following model
29   * from the strategical planner and will generate an operational plan for the GTU.
30   * <p>
31   * This lane-based tactical planner makes decisions based on headway (GTU following model). It can ask the strategic planner for
32   * assistance on the route to take when the network splits.
33   * <p>
34   * Copyright (c) 2013-2018 Delft University of Technology, PO Box 5, 2600 AA, Delft, the Netherlands. All rights reserved. <br>
35   * BSD-style license. See <a href="http://opentrafficsim.org/docs/license.html">OpenTrafficSim License</a>.
36   * </p>
37   * $LastChangedDate: 2015-07-24 02:58:59 +0200 (Fri, 24 Jul 2015) $, @version $Revision: 1147 $, by $Author: averbraeck $,
38   * initial version Nov 25, 2015 <br>
39   * @author <a href="http://www.tbm.tudelft.nl/averbraeck">Alexander Verbraeck</a>
40   * @author <a href="http://www.tudelft.nl/pknoppers">Peter Knoppers</a>
41   */
42  public class LaneBasedGTUFollowingTacticalPlanner extends AbstractLaneBasedTacticalPlanner
43  {
44      /** */
45      private static final long serialVersionUID = 20151125L;
46  
47      /**
48       * Instantiate a tactical planner with just GTU following behavior and no lane changes.
49       * @param carFollowingModel Car-following model.
50       * @param gtu GTU
51       */
52      public LaneBasedGTUFollowingTacticalPlanner(final GTUFollowingModelOld carFollowingModel, final LaneBasedGTU gtu)
53      {
54          super(carFollowingModel, gtu, new CategoricalLanePerception(gtu));
55          getPerception().addPerceptionCategory(new DirectDefaultSimplePerception(getPerception()));
56      }
57  
58      /** {@inheritDoc} */
59      @Override
60      public final OperationalPlan generateOperationalPlan(final Time startTime, final DirectedPoint locationAtStartTime)
61              throws OperationalPlanException, NetworkException, GTUException, ParameterException
62      {
63          // ask Perception for the local situation
64          LaneBasedGTU laneBasedGTU = getGtu();
65          LanePerception perception = getPerception();
66  
67          // if the GTU's maximum speed is zero (block), generate a stand still plan for one second
68          if (laneBasedGTU.getMaximumSpeed().si < OperationalPlan.DRIFTING_SPEED_SI)
69          {
70              return new OperationalPlan(getGtu(), locationAtStartTime, startTime, new Duration(1.0, DurationUnit.SECOND));
71          }
72  
73          // perceive every time step... This is the 'classical' way of tactical planning.
74          perception.perceive();
75  
76          // see how far we can drive
77          Length maxDistance = laneBasedGTU.getParameters().getParameter(LOOKAHEAD);
78          LanePathInfo lanePathInfo = buildLanePathInfo(laneBasedGTU, maxDistance);
79  
80          // look at the conditions for headway from a GTU in front
81          DefaultSimplePerception simplePerception = perception.getPerceptionCategory(DefaultSimplePerception.class);
82          Headway headwayGTU = simplePerception.getForwardHeadwayGTU();
83          AccelerationStep accelerationStepGTU = null;
84          if (headwayGTU.getDistance().ge(maxDistance))
85          {
86              // TODO I really don't like this -- if there is a lane drop at 20 m, the GTU should stop...
87              accelerationStepGTU = ((GTUFollowingModelOld) getCarFollowingModel()).computeAccelerationStepWithNoLeader(
88                      laneBasedGTU, lanePathInfo.getPath().getLength(), simplePerception.getSpeedLimit());
89          }
90          else
91          {
92              accelerationStepGTU =
93                      ((GTUFollowingModelOld) getCarFollowingModel()).computeAccelerationStep(laneBasedGTU, headwayGTU.getSpeed(),
94                              headwayGTU.getDistance(), lanePathInfo.getPath().getLength(), simplePerception.getSpeedLimit());
95          }
96  
97          // look at the conditions for headway from an object in front
98          Headway headwayObject = simplePerception.getForwardHeadwayObject();
99          AccelerationStep accelerationStepObject = null;
100         if (headwayObject.getDistance().ge(maxDistance))
101         {
102             accelerationStepObject = ((GTUFollowingModelOld) getCarFollowingModel()).computeAccelerationStepWithNoLeader(
103                     laneBasedGTU, lanePathInfo.getPath().getLength(), simplePerception.getSpeedLimit());
104         }
105         else
106         {
107             accelerationStepObject = ((GTUFollowingModelOld) getCarFollowingModel()).computeAccelerationStep(laneBasedGTU,
108                     headwayObject.getSpeed(), headwayObject.getDistance(), lanePathInfo.getPath().getLength(),
109                     simplePerception.getSpeedLimit());
110         }
111 
112         // see which one is most limiting
113         AccelerationStep accelerationStep = accelerationStepGTU.getAcceleration().lt(accelerationStepObject.getAcceleration())
114                 ? accelerationStepGTU : accelerationStepObject;
115 
116         // see if we have to continue standing still. In that case, generate a stand still plan
117         if (accelerationStep.getAcceleration().si < 1E-6 && laneBasedGTU.getSpeed().si < OperationalPlan.DRIFTING_SPEED_SI)
118         {
119             return new OperationalPlan(getGtu(), locationAtStartTime, startTime, accelerationStep.getDuration());
120         }
121 
122         List<Segment> operationalPlanSegmentList = new ArrayList<>();
123         if (accelerationStep.getAcceleration().si == 0.0)
124         {
125             Segment segment = new OperationalPlan.SpeedSegment(accelerationStep.getDuration());
126             operationalPlanSegmentList.add(segment);
127         }
128         else
129         {
130             Segment segment =
131                     new OperationalPlan.AccelerationSegment(accelerationStep.getDuration(), accelerationStep.getAcceleration());
132             operationalPlanSegmentList.add(segment);
133         }
134         OperationalPlan op = new OperationalPlan(getGtu(), lanePathInfo.getPath(), startTime, getGtu().getSpeed(),
135                 operationalPlanSegmentList);
136         return op;
137     }
138 
139     /** {@inheritDoc} */
140     @Override
141     public final String toString()
142     {
143         return "LaneBasedGTUFollowingTacticalPlanner [carFollowingModel=" + getCarFollowingModel() + "]";
144     }
145 }