View Javadoc
1   package org.opentrafficsim.demo.carFollowing;
2   
3   import org.djunits.unit.LengthUnit;
4   import org.djunits.value.vdouble.scalar.Length;
5   import org.djunits.value.vdouble.scalar.Speed;
6   import org.opentrafficsim.core.network.NetworkException;
7   import org.opentrafficsim.road.gtu.generator.LaneBasedGTUGenerator;
8   import org.opentrafficsim.road.gtu.lane.LaneBasedGTUCharacteristics;
9   import org.opentrafficsim.road.gtu.lane.tactical.following.GTUFollowingModelOld;
10  import org.opentrafficsim.road.gtu.lane.tactical.following.IDMOld;
11  import org.opentrafficsim.road.network.lane.DirectedLanePosition;
12  import org.opentrafficsim.road.network.lane.Lane;
13  
14  /**
15   * Demo implementation of the canPlace method required by the LaneBasedGTUGenerator.RoomChecker interface.
16   * <p>
17   * Copyright (c) 2013-2016 Delft University of Technology, PO Box 5, 2600 AA, Delft, the Netherlands. All rights reserved. <br>
18   * BSD-style license. See <a href="http://opentrafficsim.org/docs/current/license.html">OpenTrafficSim License</a>.
19   * <p>
20   * @version $Revision$, $LastChangedDate$, by $Author$, initial version Mar 15, 2016 <br>
21   * @author <a href="http://www.tudelft.nl/pknoppers">Peter Knoppers</a>
22   */
23  public class CanPlaceDemoCode implements LaneBasedGTUGenerator.RoomChecker
24  {
25      /** Maximum distance supplied to the minimumHeadway method of the GTUFollowingModel. */
26      private static Length maxDistance = new Length(Double.MAX_VALUE, LengthUnit.SI);
27  
28      /** Precision requested of the minimumHeadway method of the GTUFollowingModel. */
29      private static Length precision = new Length(0.1, LengthUnit.METER);
30  
31      /** {@inheritDoc} */
32      @Override
33      public Speed canPlace(Speed leaderSpeed, org.djunits.value.vdouble.scalar.Length headway,
34              LaneBasedGTUCharacteristics laneBasedGTUCharacteristics) throws NetworkException
35      {
36          // This simple minded implementation returns null if the headway is less than the headway wanted for driving at
37          // the current speed of the leader
38          Lane lane = null;
39          for (DirectedLanePosition dlp : laneBasedGTUCharacteristics.getInitialLongitudinalPositions())
40          {
41              if (dlp.getLane().getLaneType().isCompatible(laneBasedGTUCharacteristics.getGTUType()))
42              {
43                  lane = dlp.getLane();
44                  break;
45              }
46          }
47          if (null == lane)
48          {
49              throw new NetworkException(
50                      "No " + laneBasedGTUCharacteristics.getGTUType() + "-compatible lane in initial longitudinal positions");
51          }
52          // Use the speed limit of the first compatible lane in the initial longitudinal positions.
53          Speed speedLimit = lane.getSpeedLimit(laneBasedGTUCharacteristics.getGTUType());
54          Speed maximumSpeed = laneBasedGTUCharacteristics.getMaximumSpeed();
55          GTUFollowingModelOld gfm = new IDMOld();
56          if (headway.lt(// laneBasedGTUCharacteristics.getStrategicalPlanner().getBehavioralCharacteristics().getGTUFollowingModel()
57                  gfm.minimumHeadway(leaderSpeed, leaderSpeed, precision, maxDistance, speedLimit, maximumSpeed)))
58          {
59              return null;
60          }
61          return leaderSpeed;
62      }
63  
64  }