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
22
23
24
25
26
27
28
29
30 @Deprecated
31 public class CanPlaceDemoCode implements LaneBasedGTUGenerator.RoomChecker
32 {
33
34 private static Length maxDistance = new Length(Double.MAX_VALUE, LengthUnit.SI);
35
36
37 private static Length precision = new Length(0.1, LengthUnit.METER);
38
39
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
50
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
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 }