LaneBasedGTUFollowingTacticalPlanner.java

  1. package org.opentrafficsim.road.gtu.lane.tactical;

  2. import java.util.ArrayList;
  3. import java.util.List;

  4. import org.djunits.unit.DurationUnit;
  5. import org.djunits.value.vdouble.scalar.Duration;
  6. import org.djunits.value.vdouble.scalar.Length;
  7. import org.djunits.value.vdouble.scalar.Time;
  8. import org.opentrafficsim.base.parameters.ParameterException;
  9. import org.opentrafficsim.core.gtu.GTUException;
  10. import org.opentrafficsim.core.gtu.plan.operational.OperationalPlan;
  11. import org.opentrafficsim.core.gtu.plan.operational.OperationalPlan.Segment;
  12. import org.opentrafficsim.core.gtu.plan.operational.OperationalPlanException;
  13. import org.opentrafficsim.core.network.NetworkException;
  14. import org.opentrafficsim.road.gtu.lane.LaneBasedGTU;
  15. import org.opentrafficsim.road.gtu.lane.perception.CategoricalLanePerception;
  16. import org.opentrafficsim.road.gtu.lane.perception.LanePerception;
  17. import org.opentrafficsim.road.gtu.lane.perception.categories.DefaultSimplePerception;
  18. import org.opentrafficsim.road.gtu.lane.perception.categories.DirectDefaultSimplePerception;
  19. import org.opentrafficsim.road.gtu.lane.perception.headway.Headway;
  20. import org.opentrafficsim.road.gtu.lane.tactical.following.AccelerationStep;
  21. import org.opentrafficsim.road.gtu.lane.tactical.following.GTUFollowingModelOld;

  22. import nl.tudelft.simulation.language.d3.DirectedPoint;

  23. /**
  24.  * Lane-based tactical planner that implements car following behavior. This tactical planner retrieves the car following model
  25.  * from the strategical planner and will generate an operational plan for the GTU.
  26.  * <p>
  27.  * This lane-based tactical planner makes decisions based on headway (GTU following model). It can ask the strategic planner for
  28.  * assistance on the route to take when the network splits.
  29.  * <p>
  30.  * Copyright (c) 2013-2020 Delft University of Technology, PO Box 5, 2600 AA, Delft, the Netherlands. All rights reserved. <br>
  31.  * BSD-style license. See <a href="http://opentrafficsim.org/docs/license.html">OpenTrafficSim License</a>.
  32.  * </p>
  33.  * $LastChangedDate: 2015-07-24 02:58:59 +0200 (Fri, 24 Jul 2015) $, @version $Revision: 1147 $, by $Author: averbraeck $,
  34.  * initial version Nov 25, 2015 <br>
  35.  * @author <a href="http://www.tbm.tudelft.nl/averbraeck">Alexander Verbraeck</a>
  36.  * @author <a href="http://www.tudelft.nl/pknoppers">Peter Knoppers</a>
  37.  */
  38. public class LaneBasedGTUFollowingTacticalPlanner extends AbstractLaneBasedTacticalPlanner
  39. {
  40.     /** */
  41.     private static final long serialVersionUID = 20151125L;

  42.     /**
  43.      * Instantiate a tactical planner with just GTU following behavior and no lane changes.
  44.      * @param carFollowingModel GTUFollowingModelOld; Car-following model.
  45.      * @param gtu LaneBasedGTU; GTU
  46.      */
  47.     public LaneBasedGTUFollowingTacticalPlanner(final GTUFollowingModelOld carFollowingModel, final LaneBasedGTU gtu)
  48.     {
  49.         super(carFollowingModel, gtu, new CategoricalLanePerception(gtu));
  50.         getPerception().addPerceptionCategory(new DirectDefaultSimplePerception(getPerception()));
  51.     }

  52.     /** {@inheritDoc} */
  53.     @Override
  54.     public final OperationalPlan generateOperationalPlan(final Time startTime, final DirectedPoint locationAtStartTime)
  55.             throws OperationalPlanException, NetworkException, GTUException, ParameterException
  56.     {
  57.         // ask Perception for the local situation
  58.         LaneBasedGTU laneBasedGTU = getGtu();
  59.         LanePerception perception = getPerception();

  60.         // if the GTU's maximum speed is zero (block), generate a stand still plan for one second
  61.         if (laneBasedGTU.getMaximumSpeed().si < OperationalPlan.DRIFTING_SPEED_SI)
  62.         {
  63.             return new OperationalPlan(getGtu(), locationAtStartTime, startTime, new Duration(1.0, DurationUnit.SECOND));
  64.         }

  65.         // see how far we can drive
  66.         Length maxDistance = laneBasedGTU.getParameters().getParameter(LOOKAHEAD);
  67.         LanePathInfo lanePathInfo = buildLanePathInfo(laneBasedGTU, maxDistance);

  68.         // look at the conditions for headway from a GTU in front
  69.         DefaultSimplePerception simplePerception = perception.getPerceptionCategory(DefaultSimplePerception.class);
  70.         Headway headwayGTU = simplePerception.getForwardHeadwayGTU();
  71.         AccelerationStep accelerationStepGTU = null;
  72.         if (headwayGTU.getDistance().ge(maxDistance))
  73.         {
  74.             // TODO I really don't like this -- if there is a lane drop at 20 m, the GTU should stop...
  75.             accelerationStepGTU = ((GTUFollowingModelOld) getCarFollowingModel()).computeAccelerationStepWithNoLeader(
  76.                     laneBasedGTU, lanePathInfo.getPath().getLength(), simplePerception.getSpeedLimit());
  77.         }
  78.         else
  79.         {
  80.             accelerationStepGTU =
  81.                     ((GTUFollowingModelOld) getCarFollowingModel()).computeAccelerationStep(laneBasedGTU, headwayGTU.getSpeed(),
  82.                             headwayGTU.getDistance(), lanePathInfo.getPath().getLength(), simplePerception.getSpeedLimit());
  83.         }

  84.         // look at the conditions for headway from an object in front
  85.         Headway headwayObject = simplePerception.getForwardHeadwayObject();
  86.         AccelerationStep accelerationStepObject = null;
  87.         if (headwayObject.getDistance().ge(maxDistance))
  88.         {
  89.             accelerationStepObject = ((GTUFollowingModelOld) getCarFollowingModel()).computeAccelerationStepWithNoLeader(
  90.                     laneBasedGTU, lanePathInfo.getPath().getLength(), simplePerception.getSpeedLimit());
  91.         }
  92.         else
  93.         {
  94.             accelerationStepObject = ((GTUFollowingModelOld) getCarFollowingModel()).computeAccelerationStep(laneBasedGTU,
  95.                     headwayObject.getSpeed(), headwayObject.getDistance(), lanePathInfo.getPath().getLength(),
  96.                     simplePerception.getSpeedLimit());
  97.         }

  98.         // see which one is most limiting
  99.         AccelerationStep accelerationStep = accelerationStepGTU.getAcceleration().lt(accelerationStepObject.getAcceleration())
  100.                 ? accelerationStepGTU : accelerationStepObject;

  101.         // see if we have to continue standing still. In that case, generate a stand still plan
  102.         if (accelerationStep.getAcceleration().si < 1E-6 && laneBasedGTU.getSpeed().si < OperationalPlan.DRIFTING_SPEED_SI)
  103.         {
  104.             return new OperationalPlan(getGtu(), locationAtStartTime, startTime, accelerationStep.getDuration());
  105.         }

  106.         List<Segment> operationalPlanSegmentList = new ArrayList<>();
  107.         if (accelerationStep.getAcceleration().si == 0.0)
  108.         {
  109.             Segment segment = new OperationalPlan.SpeedSegment(accelerationStep.getDuration());
  110.             operationalPlanSegmentList.add(segment);
  111.         }
  112.         else
  113.         {
  114.             Segment segment =
  115.                     new OperationalPlan.AccelerationSegment(accelerationStep.getDuration(), accelerationStep.getAcceleration());
  116.             operationalPlanSegmentList.add(segment);
  117.         }
  118.         OperationalPlan op = new OperationalPlan(getGtu(), lanePathInfo.getPath(), startTime, getGtu().getSpeed(),
  119.                 operationalPlanSegmentList);
  120.         return op;
  121.     }

  122.     /** {@inheritDoc} */
  123.     @Override
  124.     public final String toString()
  125.     {
  126.         return "LaneBasedGTUFollowingTacticalPlanner [carFollowingModel=" + getCarFollowingModel() + "]";
  127.     }
  128. }