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