1 package org.opentrafficsim.road.gtu;
2
3 import static org.junit.Assert.assertEquals;
4 import static org.junit.Assert.assertTrue;
5 import static org.junit.Assert.fail;
6
7 import java.io.Serializable;
8 import java.util.ArrayList;
9 import java.util.LinkedHashSet;
10 import java.util.List;
11 import java.util.Map;
12 import java.util.Set;
13
14 import org.djunits.unit.DurationUnit;
15 import org.djunits.unit.LengthUnit;
16 import org.djunits.unit.TimeUnit;
17 import org.djunits.unit.util.UNITS;
18 import org.djunits.value.vdouble.scalar.Acceleration;
19 import org.djunits.value.vdouble.scalar.Direction;
20 import org.djunits.value.vdouble.scalar.Duration;
21 import org.djunits.value.vdouble.scalar.Length;
22 import org.djunits.value.vdouble.scalar.Speed;
23 import org.djunits.value.vdouble.scalar.Time;
24 import org.junit.Test;
25 import org.opentrafficsim.base.parameters.Parameters;
26 import org.opentrafficsim.core.dsol.AbstractOTSModel;
27 import org.opentrafficsim.core.dsol.OTSModelInterface;
28 import org.opentrafficsim.core.dsol.OTSSimulator;
29 import org.opentrafficsim.core.dsol.OTSSimulatorInterface;
30 import org.opentrafficsim.core.geometry.OTSPoint3D;
31 import org.opentrafficsim.core.gtu.GTUDirectionality;
32 import org.opentrafficsim.core.gtu.GTUException;
33 import org.opentrafficsim.core.gtu.GTUType;
34 import org.opentrafficsim.core.gtu.RelativePosition;
35 import org.opentrafficsim.core.network.Node;
36 import org.opentrafficsim.core.network.route.CompleteRoute;
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.FixedLaneChangeModel;
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.OTSRoadNetwork;
47 import org.opentrafficsim.road.network.factory.LaneFactory;
48 import org.opentrafficsim.road.network.lane.DirectedLanePosition;
49 import org.opentrafficsim.road.network.lane.Lane;
50 import org.opentrafficsim.road.network.lane.LaneType;
51 import org.opentrafficsim.road.network.lane.OTSRoadNode;
52
53 import nl.tudelft.simulation.dsol.SimRuntimeException;
54
55
56
57
58
59
60
61
62
63
64
65
66
67 public class AbstractLaneBasedGTUTest implements UNITS
68 {
69
70
71
72
73 @Test
74 public final void abstractLaneBasedGTUTest() throws Exception
75 {
76
77
78
79
80 OTSSimulatorInterface simulator = new OTSSimulator("abstractLaneBasedGTUTest");
81 OTSRoadNetwork network = new OTSRoadNetwork("lane base gtu test network", true, simulator);
82 OTSModelInterface model = new DummyModel(simulator);
83 simulator.initialize(Time.ZERO, Duration.ZERO, new Duration(1, DurationUnit.HOUR), model);
84 OTSRoadNode nodeAFrom = new OTSRoadNode(network, "AFrom", new OTSPoint3D(0, 0, 0), Direction.ZERO);
85 OTSRoadNode nodeATo = new OTSRoadNode(network, "ATo", new OTSPoint3D(1000, 0, 0), Direction.ZERO);
86 GTUType gtuType = network.getGtuType(GTUType.DEFAULTS.CAR);
87 LaneType laneType = network.getLaneType(LaneType.DEFAULTS.TWO_WAY_LANE);
88
89 Lane[] lanesGroupA = LaneFactory.makeMultiLane(network, "A", nodeAFrom, nodeATo, null, 3, laneType,
90 new Speed(100, KM_PER_HOUR), simulator);
91
92 OTSRoadNode nodeBFrom = new OTSRoadNode(network, "BFrom", new OTSPoint3D(10, 0, 0), Direction.ZERO);
93 OTSRoadNode nodeBTo = new OTSRoadNode(network, "BTo", new OTSPoint3D(1000, 0, 0), Direction.ZERO);
94 Lane[] lanesGroupB = LaneFactory.makeMultiLane(network, "B", nodeBFrom, nodeBTo, null, 3, laneType,
95 new Speed(100, KM_PER_HOUR), simulator);
96 Set<DirectedLanePosition> initialLongitudinalPositions = new LinkedHashSet<>(2);
97
98 Length positionA = new Length(100, METER);
99 initialLongitudinalPositions.add(new DirectedLanePosition(lanesGroupA[1], positionA, GTUDirectionality.DIR_PLUS));
100 Length positionB = new Length(90, METER);
101 initialLongitudinalPositions.add(new DirectedLanePosition(lanesGroupB[1], positionB, GTUDirectionality.DIR_PLUS));
102
103 Acceleration acceleration = new Acceleration(2, METER_PER_SECOND_2);
104 Duration validFor = new Duration(10, SECOND);
105 GTUFollowingModelOld gfm = new FixedAccelerationModel(acceleration, validFor);
106
107
108 LaneChangeModel laneChangeModel = new FixedLaneChangeModel(null);
109
110 Speed initialSpeed = new Speed(50, KM_PER_HOUR);
111
112 Length carLength = new Length(4, METER);
113
114 Length carWidth = new Length(1.8, METER);
115
116 Speed maximumSpeed = new Speed(200, KM_PER_HOUR);
117
118 String carID = "theCar";
119
120 List<Node> nodeList = new ArrayList<Node>();
121 nodeList.add(nodeAFrom);
122 nodeList.add(nodeATo);
123
124 CompleteRoute route = new CompleteRoute("Route", gtuType, nodeList);
125
126 Parameters parameters = DefaultTestParameters.create();
127
128
129
130 LaneBasedIndividualGTU car = new LaneBasedIndividualGTU(carID, gtuType, carLength, carWidth, maximumSpeed,
131 carLength.times(0.5), simulator, network);
132 LaneBasedStrategicalPlanner strategicalPlanner =
133 new LaneBasedStrategicalRoutePlanner(new LaneBasedCFLCTacticalPlanner(gfm, laneChangeModel, car), car);
134 car.setParameters(parameters);
135 car.init(strategicalPlanner, initialLongitudinalPositions, initialSpeed);
136
137 assertEquals("ID of the car should be identical to the provided one", carID, car.getId());
138
139
140
141 assertEquals("Width should be identical to the provided width", carWidth, car.getWidth());
142 assertEquals("Length should be identical to the provided length", carLength, car.getLength());
143 assertEquals("GTU type should be identical to the provided one", gtuType, car.getGTUType());
144 assertEquals("front in lanesGroupA[1] is positionA", positionA.getSI(),
145 car.position(lanesGroupA[1], car.getReference()).getSI(), 0.0001);
146 assertEquals("front in lanesGroupB[1] is positionB", positionB.getSI(),
147 car.position(lanesGroupB[1], car.getReference()).getSI(), 0.0001);
148
149
150 assertEquals("acceleration is 2", 2.0, car.getAcceleration().getSI(), 0.00001);
151 assertEquals("longitudinal speed is " + initialSpeed, initialSpeed.getSI(), car.getSpeed().getSI(), 0.00001);
152 assertEquals("lastEvaluation time is 0", 0, car.getOperationalPlan().getStartTime().getSI(), 0.00001);
153
154
155
156
157
158
159
160
161
162
163
164 for (Lane[] laneGroup : new Lane[][] {lanesGroupA, lanesGroupB})
165 {
166 for (int laneIndex = 0; laneIndex < laneGroup.length; laneIndex++)
167 {
168 Lane lane = laneGroup[laneIndex];
169 boolean expectException = 1 != laneIndex;
170 for (RelativePosition relativePosition : new RelativePosition[] {car.getFront(), car.getReference(),
171 car.getRear()})
172 {
173
174
175 try
176 {
177 Length position = car.position(lane, relativePosition);
178 if (expectException)
179 {
180
181 fail("Calling position on lane that the car is NOT on should have thrown a NetworkException");
182 }
183 else
184 {
185 Length expectedPosition = laneGroup == lanesGroupA ? positionA : positionB;
186 expectedPosition = expectedPosition.plus(relativePosition.getDx());
187
188
189 assertEquals("Position should match initial position", expectedPosition.getSI(), position.getSI(),
190 0.0001);
191 }
192 }
193 catch (GTUException ne)
194 {
195 if (!expectException)
196 {
197 System.out.println(ne);
198 fail("Calling position on lane that the car is on should NOT have thrown a NetworkException");
199 }
200 }
201 }
202 }
203 }
204
205
206 assertEquals("lastEvaluation time is 0", 0, car.getOperationalPlan().getStartTime().getSI(), 0.00001);
207
208
209 assertEquals("nextEvaluation time is 10", 10.0, car.getOperationalPlan().getEndTime().getSI(), 0.00001);
210
211 double step = 0.01d;
212 for (int i = 0;; i++)
213 {
214 Time stepTime = new Time(i * step, TimeUnit.BASE_SECOND);
215 if (stepTime.getSI() > validFor.getSI())
216 {
217 break;
218 }
219 if (stepTime.getSI() > 0.5)
220 {
221 step = 0.1;
222 }
223
224 simulator.runUpTo(stepTime);
225 while (simulator.isStartingOrRunning())
226 {
227 try
228 {
229 Thread.sleep(1);
230 }
231 catch (InterruptedException ie)
232 {
233 ie = null;
234 }
235 }
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253 Speed longitudinalSpeed = car.getSpeed();
254 double expectedLongitudinalSpeed = initialSpeed.getSI() + stepTime.getSI() * acceleration.getSI();
255 assertEquals("longitudinal speed is " + expectedLongitudinalSpeed, expectedLongitudinalSpeed,
256 longitudinalSpeed.getSI(), 0.00001);
257 for (RelativePosition relativePosition : new RelativePosition[] {car.getFront(), car.getRear()})
258 {
259 Map<Lane, Double> positions = car.fractionalPositions(relativePosition);
260 assertEquals("Car should be in two lanes", 2, positions.size());
261 Double pos = positions.get(lanesGroupA[1]);
262
263 assertTrue("Car should be in lane 1 of lane group A", null != pos);
264 assertEquals("fractional position should be equal to result of fractionalPosition(lane, ...)", pos,
265 car.fractionalPosition(lanesGroupA[1], relativePosition), 0.0000001);
266 pos = positions.get(lanesGroupB[1]);
267 assertTrue("Car should be in lane 1 of lane group B", null != pos);
268 assertEquals("fractional position should be equal to result of fractionalPosition(lane, ...)", pos,
269 car.fractionalPosition(lanesGroupB[1], relativePosition), 0.0000001);
270 }
271 for (Lane[] laneGroup : new Lane[][] {lanesGroupA, lanesGroupB})
272 {
273 for (int laneIndex = 0; laneIndex < laneGroup.length; laneIndex++)
274 {
275 Lane lane = laneGroup[laneIndex];
276 boolean expectException = 1 != laneIndex;
277 for (RelativePosition relativePosition : new RelativePosition[] {car.getFront(), car.getReference(),
278 car.getRear()})
279 {
280
281
282 try
283 {
284 Length position = car.position(lane, relativePosition);
285 if (expectException)
286 {
287
288 fail("Calling position on lane that the car is NOT on should have thrown a "
289 + "NetworkException");
290 }
291 else
292 {
293 Length expectedPosition = laneGroup == lanesGroupA ? positionA : positionB;
294 expectedPosition = expectedPosition
295 .plus(new Length(stepTime.getSI() * initialSpeed.getSI(), LengthUnit.SI));
296 expectedPosition = expectedPosition.plus(new Length(
297 0.5 * acceleration.getSI() * stepTime.getSI() * stepTime.getSI(), LengthUnit.SI));
298 expectedPosition = expectedPosition.plus(relativePosition.getDx());
299
300
301 assertEquals("Position should match initial position", expectedPosition.getSI(),
302 position.getSI(), 0.0001);
303 }
304 }
305 catch (GTUException ne)
306 {
307 if (!expectException)
308 {
309 System.out.println(ne);
310 fail("Calling position on lane that the car is on should NOT have thrown a NetworkException");
311 }
312 }
313 try
314 {
315 double fractionalPosition = car.fractionalPosition(lane, relativePosition);
316 if (expectException)
317 {
318
319 fail("Calling position on lane that the car is NOT on should have thrown a NetworkException");
320 }
321 else
322 {
323 Length expectedPosition = laneGroup == lanesGroupA ? positionA : positionB;
324 expectedPosition = expectedPosition
325 .plus(new Length(stepTime.getSI() * initialSpeed.getSI(), LengthUnit.SI));
326 expectedPosition = expectedPosition.plus(new Length(
327 0.5 * acceleration.getSI() * stepTime.getSI() * stepTime.getSI(), LengthUnit.SI));
328 expectedPosition = expectedPosition.plus(relativePosition.getDx());
329
330
331 double expectedFractionalPosition = expectedPosition.getSI() / lane.getLength().getSI();
332 assertEquals("Position should match initial position", expectedFractionalPosition,
333 fractionalPosition, 0.000001);
334 }
335 }
336 catch (GTUException ne)
337 {
338 if (!expectException)
339 {
340 System.out.println(ne);
341 fail("Calling fractionalPosition on lane that the car is on should NOT have thrown a "
342 + "NetworkException");
343 }
344 }
345 }
346 }
347 }
348 }
349
350 OTSRoadNode nodeCFrom = new OTSRoadNode(network, "CFrom", new OTSPoint3D(10, 100, 0), Direction.ZERO);
351 OTSRoadNode nodeCTo = new OTSRoadNode(network, "CTo", new OTSPoint3D(1000, 0, 0), Direction.ZERO);
352 Lane[] lanesGroupC = LaneFactory.makeMultiLane(network, "C", nodeCFrom, nodeCTo, null, 3, laneType,
353 new Speed(100, KM_PER_HOUR), simulator);
354
355 for (RelativePosition relativePosition : new RelativePosition[] {car.getFront(), car.getRear()})
356 {
357 Map<Lane, Double> positions = car.fractionalPositions(relativePosition);
358
359 Double pos = positions.get(lanesGroupA[1]);
360 assertTrue("Car should be in lane 1 of lane group A", null != pos);
361 assertEquals("fractional position should be equal to result of fractionalPosition(lane, ...)", pos,
362 car.fractionalPosition(lanesGroupA[1], relativePosition), 0.0000001);
363 pos = positions.get(lanesGroupB[1]);
364 assertTrue("Car should be in lane 1 of lane group B", null != pos);
365 assertEquals("fractional position should be equal to result of fractionalPosition(lane, ...)", pos,
366 car.fractionalPosition(lanesGroupB[1], relativePosition), 0.0000001);
367 pos = positions.get(lanesGroupC[0]);
368
369
370
371
372
373
374
375 }
376
377 for (RelativePosition relativePosition : new RelativePosition[] {car.getFront(), car.getRear()})
378 {
379 Map<Lane, Double> positions = car.fractionalPositions(relativePosition);
380 assertEquals("Car should be in two lanes", 2, positions.size());
381 Double pos = positions.get(lanesGroupB[1]);
382 assertTrue("Car should be in lane 1 of lane group B", null != pos);
383 assertEquals("fractional position should be equal to result of fractionalPosition(lane, ...)", pos,
384 car.fractionalPosition(lanesGroupB[1], relativePosition), 0.0000001);
385 pos = positions.get(lanesGroupC[0]);
386
387
388
389
390
391
392
393 }
394
395
396 }
397 }
398
399
400
401
402
403
404
405
406
407
408
409 class DummyModel extends AbstractOTSModel
410 {
411
412 private static final long serialVersionUID = 20150114L;
413
414
415
416
417 DummyModel(final OTSSimulatorInterface simulator)
418 {
419 super(simulator);
420 }
421
422
423 @Override
424 public final void constructModel() throws SimRuntimeException
425 {
426
427 }
428
429
430 @Override
431 public final OTSRoadNetwork getNetwork()
432 {
433 return null;
434 }
435
436
437 @Override
438 public Serializable getSourceId()
439 {
440 return "AbstractLaneBasedGTUTest.DummyModel";
441 }
442
443 }