1 package org.opentrafficsim.road.car;
2
3 import static org.junit.Assert.assertEquals;
4 import static org.opentrafficsim.road.gtu.lane.RoadGTUTypes.CAR;
5
6 import java.util.HashSet;
7 import java.util.LinkedHashSet;
8 import java.util.Set;
9
10 import javax.naming.NamingException;
11
12 import org.djunits.unit.TimeUnit;
13 import org.djunits.unit.UNITS;
14 import org.djunits.value.vdouble.scalar.Acceleration;
15 import org.djunits.value.vdouble.scalar.Duration;
16 import org.djunits.value.vdouble.scalar.Length;
17 import org.djunits.value.vdouble.scalar.Speed;
18 import org.djunits.value.vdouble.scalar.Time;
19 import org.junit.Test;
20 import org.opentrafficsim.core.dsol.OTSDEVSSimulator;
21 import org.opentrafficsim.core.dsol.OTSModelInterface;
22 import org.opentrafficsim.core.dsol.OTSSimTimeDouble;
23 import org.opentrafficsim.core.dsol.OTSSimulatorInterface;
24 import org.opentrafficsim.core.geometry.OTSGeometryException;
25 import org.opentrafficsim.core.geometry.OTSLine3D;
26 import org.opentrafficsim.core.geometry.OTSPoint3D;
27 import org.opentrafficsim.core.gtu.GTUDirectionality;
28 import org.opentrafficsim.core.gtu.GTUException;
29 import org.opentrafficsim.core.gtu.GTUType;
30 import org.opentrafficsim.core.gtu.behavioralcharacteristics.BehavioralCharacteristics;
31 import org.opentrafficsim.core.network.LinkType;
32 import org.opentrafficsim.core.network.LongitudinalDirectionality;
33 import org.opentrafficsim.core.network.Network;
34 import org.opentrafficsim.core.network.NetworkException;
35 import org.opentrafficsim.core.network.OTSNetwork;
36 import org.opentrafficsim.core.network.OTSNode;
37 import org.opentrafficsim.road.DefaultTestParameters;
38 import org.opentrafficsim.road.gtu.lane.LaneBasedIndividualGTU;
39 import org.opentrafficsim.road.gtu.lane.tactical.LaneBasedCFLCTacticalPlanner;
40 import org.opentrafficsim.road.gtu.lane.tactical.following.FixedAccelerationModel;
41 import org.opentrafficsim.road.gtu.lane.tactical.following.GTUFollowingModelOld;
42 import org.opentrafficsim.road.gtu.lane.tactical.lanechangemobil.Egoistic;
43 import org.opentrafficsim.road.gtu.lane.tactical.lanechangemobil.LaneChangeModel;
44 import org.opentrafficsim.road.gtu.strategical.LaneBasedStrategicalPlanner;
45 import org.opentrafficsim.road.gtu.strategical.route.LaneBasedStrategicalRoutePlanner;
46 import org.opentrafficsim.road.network.lane.CrossSectionLink;
47 import org.opentrafficsim.road.network.lane.DirectedLanePosition;
48 import org.opentrafficsim.road.network.lane.Lane;
49 import org.opentrafficsim.road.network.lane.LaneType;
50 import org.opentrafficsim.road.network.lane.changing.LaneKeepingPolicy;
51 import org.opentrafficsim.road.network.lane.changing.OvertakingConditions;
52
53 import mockit.MockUp;
54 import nl.tudelft.simulation.dsol.SimRuntimeException;
55 import nl.tudelft.simulation.dsol.experiment.Experiment;
56 import nl.tudelft.simulation.dsol.experiment.Replication;
57 import nl.tudelft.simulation.dsol.experiment.ReplicationMode;
58 import nl.tudelft.simulation.dsol.experiment.Treatment;
59 import nl.tudelft.simulation.dsol.simulators.SimulatorInterface;
60
61
62
63
64
65
66
67
68
69
70 public class CarTest implements UNITS
71 {
72
73
74
75
76
77
78
79
80 @SuppressWarnings("static-method")
81 @Test
82 public final void carTest()
83 throws NetworkException, SimRuntimeException, NamingException, GTUException, OTSGeometryException
84 {
85 Time initialTime = new Time(0, TimeUnit.BASE_SECOND);
86 GTUType gtuType = CAR;
87 Set<GTUType> compatibility = new HashSet<GTUType>();
88 compatibility.add(gtuType);
89 LaneType laneType = new LaneType("CarLane", compatibility);
90 OTSNetwork network = new OTSNetwork("network");
91 Lane lane = makeLane(network, laneType);
92 Length initialPosition = new Length(12, METER);
93 Speed initialSpeed = new Speed(34, KM_PER_HOUR);
94 OTSDEVSSimulator simulator = makeSimulator();
95 GTUFollowingModelOld gtuFollowingModel =
96 new FixedAccelerationModel(new Acceleration(0, METER_PER_SECOND_2), new Duration(10, SECOND));
97 LaneChangeModel laneChangeModel = new Egoistic();
98 LaneBasedIndividualGTU referenceCar = makeReferenceCar("12345", gtuType, lane, initialPosition, initialSpeed, simulator,
99 gtuFollowingModel, laneChangeModel, network);
100 assertEquals("The car should store it's ID", "12345", referenceCar.getId());
101 assertEquals("At t=initialTime the car should be at it's initial position", initialPosition.getSI(),
102 referenceCar.position(lane, referenceCar.getReference(), initialTime).getSI(), 0.0001);
103 assertEquals("The car should store it's initial speed", initialSpeed.getSI(), referenceCar.getSpeed().getSI(), 0.00001);
104 assertEquals("The car should have an initial acceleration equal to 0", 0, referenceCar.getAcceleration().getSI(),
105 0.0001);
106
107
108
109
110 }
111
112
113
114
115
116
117
118 public static OTSDEVSSimulator makeSimulator() throws SimRuntimeException, NamingException
119 {
120 OTSDEVSSimulator simulator = new OTSDEVSSimulator();
121 Model model = new Model();
122 Experiment<Time, Duration, OTSSimTimeDouble> exp = new Experiment<Time, Duration, OTSSimTimeDouble>();
123 Treatment<Time, Duration, OTSSimTimeDouble> tr = new Treatment<>(exp, "tr1",
124 new OTSSimTimeDouble(new Time(0, TimeUnit.BASE_SECOND)), new Duration(0, SECOND), new Duration(3600.0, SECOND));
125 exp.setTreatment(tr);
126 exp.setModel(model);
127 Replication<Time, Duration, OTSSimTimeDouble> rep = new Replication<>(exp);
128 simulator.initialize(rep, ReplicationMode.TERMINATING);
129 return simulator;
130 }
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151 public static LaneBasedIndividualGTU makeReferenceCar(final String id, final GTUType gtuType, final Lane lane,
152 final Length initialPosition, final Speed initialSpeed, final OTSDEVSSimulator simulator,
153 final GTUFollowingModelOld gtuFollowingModel, final LaneChangeModel laneChangeModel, final OTSNetwork network)
154 throws NamingException, NetworkException, SimRuntimeException, GTUException, OTSGeometryException
155 {
156 Length length = new Length(5.0, METER);
157 Length width = new Length(2.0, METER);
158 Set<DirectedLanePosition> initialLongitudinalPositions = new LinkedHashSet<>(1);
159 initialLongitudinalPositions.add(new DirectedLanePosition(lane, initialPosition, GTUDirectionality.DIR_PLUS));
160 Speed maxSpeed = new Speed(120, KM_PER_HOUR);
161 BehavioralCharacteristics behavioralCharacteristics = DefaultTestParameters.create();
162 LaneBasedIndividualGTU gtu = new LaneBasedIndividualGTU(id, gtuType, length, width, maxSpeed, simulator, network);
163 LaneBasedStrategicalPlanner strategicalPlanner = new LaneBasedStrategicalRoutePlanner(behavioralCharacteristics,
164 new LaneBasedCFLCTacticalPlanner(gtuFollowingModel, laneChangeModel, gtu), gtu);
165 gtu.init(strategicalPlanner, initialLongitudinalPositions, initialSpeed);
166
167 return gtu;
168 }
169
170
171
172
173
174
175
176
177 public static Lane makeLane(final Network network, final LaneType laneType) throws NetworkException, OTSGeometryException
178 {
179 OTSSimulatorInterface simulator = new MockUp<OTSSimulatorInterface>()
180 {
181
182 }.getMockInstance();
183 OTSNode n1 = new OTSNode(network, "n1", new OTSPoint3D(0, 0));
184 OTSNode n2 = new OTSNode(network, "n2", new OTSPoint3D(100000.0, 0.0));
185 OTSPoint3D[] coordinates = new OTSPoint3D[] { new OTSPoint3D(0.0, 0.0), new OTSPoint3D(100000.0, 0.0) };
186 CrossSectionLink link12 = new CrossSectionLink(network, "link12", n1, n2, LinkType.ALL, new OTSLine3D(coordinates),
187 simulator, LongitudinalDirectionality.DIR_PLUS, LaneKeepingPolicy.KEEP_RIGHT);
188 Length latPos = new Length(0.0, METER);
189 Length width = new Length(4.0, METER);
190 return new Lane(link12, "lane.1", latPos, latPos, width, width, laneType, LongitudinalDirectionality.DIR_PLUS,
191 new Speed(100, KM_PER_HOUR), new OvertakingConditions.LeftAndRight());
192 }
193
194
195 protected static class Model implements OTSModelInterface
196 {
197
198 private static final long serialVersionUID = 20141027L;
199
200
201 private OTSDEVSSimulator simulator;
202
203
204 @Override
205 public final void constructModel(final SimulatorInterface<Time, Duration, OTSSimTimeDouble> theSimulator)
206 throws SimRuntimeException
207 {
208 this.simulator = (OTSDEVSSimulator) theSimulator;
209 }
210
211
212 @Override
213 public final OTSDEVSSimulator getSimulator()
214 {
215 return this.simulator;
216 }
217
218
219 @Override
220 public OTSNetwork getNetwork()
221 {
222 return null;
223 }
224
225 }
226 }