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