View Javadoc
1   package org.opentrafficsim.road.gtu.lane.tactical;
2   
3   import java.util.ArrayList;
4   import java.util.List;
5   
6   import nl.tudelft.simulation.language.d3.DirectedPoint;
7   
8   import org.djunits.unit.TimeUnit;
9   import org.djunits.value.vdouble.scalar.Time;
10  import org.opentrafficsim.core.gtu.GTU;
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.LanePerceptionFull;
18  import org.opentrafficsim.road.gtu.lane.tactical.following.AccelerationStep;
19  import org.opentrafficsim.road.gtu.lane.tactical.following.GTUFollowingModelOld;
20  import org.opentrafficsim.road.gtu.lane.tactical.following.HeadwayGTU;
21  
22  /**
23   * Lane-based tactical planner that implements car following behavior. This tactical planner retrieves the car following model
24   * from the strategical planner and will generate an operational plan for the GTU.
25   * <p>
26   * This lane-based tactical planner makes decisions based on headway (GTU following model). It can ask the strategic planner for
27   * assistance on the route to take when the network splits.
28   * <p>
29   * Copyright (c) 2013-2015 Delft University of Technology, PO Box 5, 2600 AA, Delft, the Netherlands. All rights reserved. <br>
30   * BSD-style license. See <a href="http://opentrafficsim.org/docs/license.html">OpenTrafficSim License</a>.
31   * </p>
32   * $LastChangedDate: 2015-07-24 02:58:59 +0200 (Fri, 24 Jul 2015) $, @version $Revision: 1147 $, by $Author: averbraeck $,
33   * initial version Nov 25, 2015 <br>
34   * @author <a href="http://www.tbm.tudelft.nl/averbraeck">Alexander Verbraeck</a>
35   * @author <a href="http://www.tudelft.nl/pknoppers">Peter Knoppers</a>
36   */
37  public class LaneBasedGTUFollowingTacticalPlanner extends AbstractLaneBasedTacticalPlanner
38  {
39      /** */
40      private static final long serialVersionUID = 20151125L;
41  
42      /**
43       * Instantiated a tactical planner with just GTU following behavior and no lane changes.
44       */
45      public LaneBasedGTUFollowingTacticalPlanner()
46      {
47          super();
48      }
49  
50      /** {@inheritDoc} */
51      @Override
52      public OperationalPlan generateOperationalPlan(final GTU gtu, final Time.Abs startTime,
53          final DirectedPoint locationAtStartTime) throws OperationalPlanException, NetworkException, GTUException
54      {
55          // ask Perception for the local situation
56          LaneBasedGTU laneBasedGTU = (LaneBasedGTU) gtu;
57          LanePerceptionFull perception = laneBasedGTU.getPerception();
58  
59          // if the GTU's maximum speed is zero (block), generate a stand still plan for one second
60          if (laneBasedGTU.getMaximumVelocity().si < OperationalPlan.DRIFTING_SPEED_SI)
61          {
62              return new OperationalPlan(gtu, locationAtStartTime, startTime, new Time.Rel(1.0, TimeUnit.SECOND));
63          }
64  
65          // perceive every time step... This is the 'classical' way of tactical planning.
66          perception.perceive();
67  
68          // get some models to help us make a plan
69          GTUFollowingModelOld gtuFollowingModel =
70              laneBasedGTU.getStrategicalPlanner().getDrivingCharacteristics().getGTUFollowingModel();
71  
72          // see how far we can drive
73          LanePathInfo lanePathInfo =
74              buildLanePathInfo(laneBasedGTU, laneBasedGTU.getBehavioralCharacteristics().getForwardHeadwayDistance());
75  
76          // look at the conditions for headway
77          HeadwayGTU headwayGTU = perception.getForwardHeadwayGTU();
78          AccelerationStep accelerationStep = null;
79          if (headwayGTU.getGtuId() == null)
80          {
81              accelerationStep =
82                  gtuFollowingModel.computeAccelerationStepWithNoLeader(laneBasedGTU, lanePathInfo.getPath().getLength(),
83                      perception.getSpeedLimit());
84          }
85          else
86          {
87              accelerationStep =
88                  gtuFollowingModel.computeAccelerationStep(laneBasedGTU, headwayGTU.getGtuSpeed(), headwayGTU.getDistance(),
89                      lanePathInfo.getPath().getLength(), perception.getSpeedLimit());
90          }
91  
92          // see if we have to continue standing still. In that case, generate a stand still plan
93          if (accelerationStep.getAcceleration().si < 1E-6
94              && laneBasedGTU.getVelocity().si < OperationalPlan.DRIFTING_SPEED_SI)
95          {
96              return new OperationalPlan(gtu, locationAtStartTime, startTime, accelerationStep.getDuration());
97          }
98  
99          List<Segment> operationalPlanSegmentList = new ArrayList<>();
100         if (accelerationStep.getAcceleration().si == 0.0)
101         {
102             Segment segment = new OperationalPlan.SpeedSegment(accelerationStep.getDuration());
103             operationalPlanSegmentList.add(segment);
104         }
105         else
106         {
107             Segment segment =
108                 new OperationalPlan.AccelerationSegment(accelerationStep.getDuration(),
109                     accelerationStep.getAcceleration());
110             operationalPlanSegmentList.add(segment);
111         }
112         OperationalPlan op =
113             new OperationalPlan(gtu, lanePathInfo.getPath(), startTime, gtu.getVelocity(), operationalPlanSegmentList);
114         return op;
115     }
116 }