View Javadoc
1   package org.opentrafficsim.road.gtu;
2   
3   import static org.junit.jupiter.api.Assertions.assertEquals;
4   import static org.junit.jupiter.api.Assertions.assertTrue;
5   import static org.junit.jupiter.api.Assertions.fail;
6   
7   import java.util.ArrayList;
8   import java.util.LinkedHashSet;
9   import java.util.List;
10  import java.util.Map;
11  import java.util.Set;
12  
13  import org.djunits.unit.DurationUnit;
14  import org.djunits.unit.LengthUnit;
15  import org.djunits.unit.TimeUnit;
16  import org.djunits.unit.util.UNITS;
17  import org.djunits.value.vdouble.scalar.Acceleration;
18  import org.djunits.value.vdouble.scalar.Direction;
19  import org.djunits.value.vdouble.scalar.Duration;
20  import org.djunits.value.vdouble.scalar.Length;
21  import org.djunits.value.vdouble.scalar.Speed;
22  import org.djunits.value.vdouble.scalar.Time;
23  import org.djutils.draw.point.Point2d;
24  import org.junit.jupiter.api.Test;
25  import org.opentrafficsim.base.parameters.Parameters;
26  import org.opentrafficsim.core.definitions.DefaultsNl;
27  import org.opentrafficsim.core.dsol.AbstractOtsModel;
28  import org.opentrafficsim.core.dsol.OtsModelInterface;
29  import org.opentrafficsim.core.dsol.OtsSimulator;
30  import org.opentrafficsim.core.dsol.OtsSimulatorInterface;
31  import org.opentrafficsim.core.gtu.GtuException;
32  import org.opentrafficsim.core.gtu.GtuType;
33  import org.opentrafficsim.core.gtu.RelativePosition;
34  import org.opentrafficsim.core.network.Node;
35  import org.opentrafficsim.core.network.route.Route;
36  import org.opentrafficsim.core.perception.HistoryManagerDevs;
37  import org.opentrafficsim.road.DefaultTestParameters;
38  import org.opentrafficsim.road.definitions.DefaultsRoadNl;
39  import org.opentrafficsim.road.gtu.lane.LaneBasedGtu;
40  import org.opentrafficsim.road.gtu.lane.tactical.LaneBasedCfLcTacticalPlanner;
41  import org.opentrafficsim.road.gtu.lane.tactical.following.FixedAccelerationModel;
42  import org.opentrafficsim.road.gtu.lane.tactical.following.GtuFollowingModelOld;
43  import org.opentrafficsim.road.gtu.lane.tactical.lanechangemobil.FixedLaneChangeModel;
44  import org.opentrafficsim.road.gtu.lane.tactical.lanechangemobil.LaneChangeModel;
45  import org.opentrafficsim.road.gtu.strategical.LaneBasedStrategicalPlanner;
46  import org.opentrafficsim.road.gtu.strategical.LaneBasedStrategicalRoutePlanner;
47  import org.opentrafficsim.road.network.RoadNetwork;
48  import org.opentrafficsim.road.network.factory.LaneFactory;
49  import org.opentrafficsim.road.network.lane.Lane;
50  import org.opentrafficsim.road.network.lane.LanePosition;
51  import org.opentrafficsim.road.network.lane.LaneType;
52  
53  import nl.tudelft.simulation.dsol.SimRuntimeException;
54  
55  /**
56   * Test the various methods of an AbstractLaneBasedGtu.<br>
57   * As abstract classes cannot be directly
58   * <p>
59   * Copyright (c) 2013-2024 Delft University of Technology, PO Box 5, 2600 AA, Delft, the Netherlands. All rights reserved. <br>
60   * BSD-style license. See <a href="https://opentrafficsim.org/docs/license.html">OpenTrafficSim License</a>.
61   * </p>
62   * @author <a href="https://github.com/averbraeck">Alexander Verbraeck</a>
63   * @author <a href="https://github.com/peter-knoppers">Peter Knoppers</a>
64   */
65  public class AbstractLaneBasedGtuTest implements UNITS
66  {
67      /**
68       * Test that the constructor puts the supplied values in the correct fields, then check the motion of the GTU.
69       * @throws Exception when something goes wrong (should not happen)
70       */
71      @Test
72      public final void abstractLaneBasedGtuTest() throws Exception
73      {
74          // This initialization code should probably be moved to a helper method that will be used in several tests.
75          // First we need a set of Lanes
76          // To create Lanes we need Nodes and a LaneType
77          // And a simulator, but for that we first need something that implements OtsModelInterface
78          OtsSimulatorInterface simulator = new OtsSimulator("abstractLaneBasedGtuTest");
79          RoadNetwork network = new RoadNetwork("lane base gtu test network", simulator);
80          OtsModelInterface model = new DummyModel(simulator);
81          simulator.initialize(Time.ZERO, Duration.ZERO, new Duration(1, DurationUnit.HOUR), model,
82                  HistoryManagerDevs.noHistory(simulator));
83          Node nodeAFrom = new Node(network, "AFrom", new Point2d(0, 0), Direction.ZERO);
84          Node nodeATo = new Node(network, "ATo", new Point2d(1000, 0), Direction.ZERO);
85          GtuType gtuType = DefaultsNl.CAR;
86          LaneType laneType = DefaultsRoadNl.TWO_WAY_LANE;
87  
88          Lane[] lanesGroupA = LaneFactory.makeMultiLane(network, "A", nodeAFrom, nodeATo, null, 3, laneType,
89                  new Speed(100, KM_PER_HOUR), simulator, DefaultsNl.VEHICLE);
90          // A GTU can exist on several lanes at once; create another lane group to test that
91          Node nodeBFrom = new Node(network, "BFrom", new Point2d(10, 0), Direction.ZERO);
92          Node nodeBTo = new Node(network, "BTo", new Point2d(1000, 0), Direction.ZERO);
93          Lane[] lanesGroupB = LaneFactory.makeMultiLane(network, "B", nodeBFrom, nodeBTo, null, 3, laneType,
94                  new Speed(100, KM_PER_HOUR), simulator, DefaultsNl.VEHICLE);
95          Set<LanePosition> initialLongitudinalPositions = new LinkedHashSet<>(2);
96  
97          Length positionA = new Length(100, METER);
98          initialLongitudinalPositions.add(new LanePosition(lanesGroupA[1], positionA));
99          Length positionB = new Length(90, METER);
100         initialLongitudinalPositions.add(new LanePosition(lanesGroupB[1], positionB));
101         // A Car needs a CarFollowingModel
102         Acceleration acceleration = new Acceleration(2, METER_PER_SECOND_2);
103         Duration validFor = new Duration(10, SECOND);
104         GtuFollowingModelOld gfm = new FixedAccelerationModel(acceleration, validFor);
105         // A Car needs a lane change model
106         // AbstractLaneChangeModel laneChangeModel = new Egoistic();
107         LaneChangeModel laneChangeModel = new FixedLaneChangeModel(null);
108         // A Car needs an initial speed
109         Speed initialSpeed = new Speed(50, KM_PER_HOUR);
110         // Length of the Car
111         Length carLength = new Length(4, METER);
112         // Width of the Car
113         Length carWidth = new Length(1.8, METER);
114         // Maximum speed of the Car
115         Speed maximumSpeed = new Speed(200, KM_PER_HOUR);
116         // ID of the Car
117         String carID = "theCar";
118         // List of Nodes visited by the Car
119         List<Node> nodeList = new ArrayList<Node>();
120         nodeList.add(nodeAFrom);
121         nodeList.add(nodeATo);
122         // Route of the Car
123         Route route = new Route("Route", gtuType, nodeList);
124         // Now we can make a GTU
125         Parameters parameters = DefaultTestParameters.create(); // new
126                                                                 // BehavioralCharacteristics();
127         // LaneBasedBehavioralCharacteristics drivingCharacteristics =
128         // new LaneBasedBehavioralCharacteristics(gfm, laneChangeModel);
129         LaneBasedGtu car = new LaneBasedGtu(carID, gtuType, carLength, carWidth, maximumSpeed, carLength.times(0.5), network);
130         LaneBasedStrategicalPlanner strategicalPlanner =
131                 new LaneBasedStrategicalRoutePlanner(new LaneBasedCfLcTacticalPlanner(gfm, laneChangeModel, car), car);
132         car.setParameters(parameters);
133         car.init(strategicalPlanner, new LanePosition(lanesGroupA[1], positionA), initialSpeed);
134         // Now we can verify the various fields in the newly created Car
135         assertEquals(carID, car.getId(), "ID of the car should be identical to the provided one");
136         // TODO: Test with gfm as part of tactical planner
137         // assertEquals("GTU following model should be identical to the provided one", gfm, car
138         // .getBehavioralCharacteristics().getGtuFollowingModel());
139         assertEquals(carWidth, car.getWidth(), "Width should be identical to the provided width");
140         assertEquals(carLength, car.getLength(), "Length should be identical to the provided length");
141         assertEquals(gtuType, car.getType(), "GTU type should be identical to the provided one");
142         assertEquals(positionA.getSI(), car.position(lanesGroupA[1], car.getReference()).getSI(), 0.0001,
143                 "front in lanesGroupA[1] is positionA");
144         // assertEquals("acceleration is 0", 0, car.getAcceleration().getSI(), 0.00001);
145         // edit wouter schakel: fixed acceleration model has a=2.0m/s^2, first plan is made during initialization
146         assertEquals(2.0, car.getAcceleration().getSI(), 0.00001, "acceleration is 2");
147         assertEquals(initialSpeed.getSI(), car.getSpeed().getSI(), 0.00001, "longitudinal speed is " + initialSpeed);
148         assertEquals(0, car.getOperationalPlan().getStartTime().getSI(), 0.00001, "lastEvaluation time is 0");
149         // Test the position(Lane, RelativePosition) method
150         // WS: Removed as null check has been removed from position(...)
151         // try
152         // {
153         // car.position(null, car.getFront());
154         // fail("position on null lane should have thrown a NetworkException");
155         // }
156         // catch (GTUException ne)
157         // {
158         // // Ignore
159         // }
160         for (Lane[] laneGroup : new Lane[][] {lanesGroupA})// , lanesGroupB})
161         {
162             for (int laneIndex = 0; laneIndex < laneGroup.length; laneIndex++)
163             {
164                 Lane lane = laneGroup[laneIndex];
165                 boolean expectException = 1 != laneIndex;
166                 for (RelativePosition relativePosition : new RelativePosition[] {car.getFront(), car.getReference(),
167                         car.getRear()})
168                 {
169                     // System.out.println("lane:" + lane + ", expectedException: " + expectException
170                     // + ", relativePostion: " + relativePosition);
171                     try
172                     {
173                         Length position = car.position(lane, relativePosition);
174                         if (expectException)
175                         {
176                             // System.out.println("position: " + position);
177                             fail("Calling position on lane that the car is NOT on should have thrown a NetworkException");
178                         }
179                         else
180                         {
181                             Length expectedPosition = laneGroup == lanesGroupA ? positionA : positionB;
182                             expectedPosition = expectedPosition.plus(relativePosition.dx());
183                             // System.out.println("reported position: " + position);
184                             // System.out.println("expected position: " + expectedPosition);
185                             assertEquals(expectedPosition.getSI(), position.getSI(), 0.0001,
186                                     "Position should match initial position");
187                         }
188                     }
189                     catch (GtuException ne)
190                     {
191                         if (!expectException)
192                         {
193                             System.out.println(ne);
194                             fail("Calling position on lane that the car is on should NOT have thrown a NetworkException");
195                         }
196                     }
197                 }
198             }
199         }
200         // Assign a movement to the car (10 seconds of acceleration of 2 m/s/s)
201         // scheduled event that moves the car at t=0
202         assertEquals(0, car.getOperationalPlan().getStartTime().getSI(), 0.00001, "lastEvaluation time is 0");
203         // assertEquals("nextEvaluation time is 0", 0, car.getOperationalPlan().getEndTime().getSI(), 0.00001);
204         // edit wouter schakel: fixed acceleration model has t=10s, first plan is made during initialization
205         assertEquals(10.0, car.getOperationalPlan().getEndTime().getSI(), 0.00001, "nextEvaluation time is 10");
206         // Increase the simulator clock in small steps and verify the both positions on all lanes at each step
207         double step = 0.01d;
208         for (int i = 0;; i++)
209         {
210             Time stepTime = new Time(i * step, TimeUnit.BASE_SECOND);
211             if (stepTime.getSI() > validFor.getSI())
212             {
213                 break;
214             }
215             if (stepTime.getSI() > 0.5)
216             {
217                 step = 0.1; // Reduce testing time by increasing the step size
218             }
219             // System.out.println("Simulating until " + stepTime.getSI());
220             simulator.runUpTo(stepTime);
221             while (simulator.isStartingOrRunning())
222             {
223                 try
224                 {
225                     Thread.sleep(1);
226                 }
227                 catch (InterruptedException ie)
228                 {
229                     ie = null; // ignore
230                 }
231             }
232             // Debugging code that helped locate a problem in the DSOL runUpTo code.
233             // System.out.println("stepTime is " + stepTime);
234             // System.out.println("Car simulator time " + car.getSimulator().getSimulatorTime());
235             // System.out.println("Simulator time is now " + simulator.getSimulatorTime());
236             // if (simulator != car.getSimulator())
237             // {
238             // System.err.println("Car runs on a different simulator!");
239             // }
240             // System.out.println("operational plan is " + car.getOperationalPlan());
241             // System.out.println("operational plan end time is " + car.getOperationalPlan().getEndTime());
242             // car.getOperationalPlan().getEndTime();
243             // if (stepTime.getSI() > 0)
244             // {
245             // assertEquals("nextEvaluation time is " + validFor, validFor.getSI(),
246             // car.getOperationalPlan().getEndTime().getSI(), 0.0001);
247             // assertEquals("acceleration is " + acceleration, acceleration.getSI(), car.getAcceleration().getSI(), 0.00001);
248             // }
249             Speed longitudinalSpeed = car.getSpeed();
250             double expectedLongitudinalSpeed = initialSpeed.getSI() + stepTime.getSI() * acceleration.getSI();
251             assertEquals(expectedLongitudinalSpeed, longitudinalSpeed.getSI(), 0.00001,
252                     "longitudinal speed is " + expectedLongitudinalSpeed);
253             for (RelativePosition relativePosition : new RelativePosition[] {car.getFront(), car.getRear()})
254             {
255                 Map<Lane, Double> positions = car.fractionalPositions(relativePosition);
256                 assertEquals(1, positions.size(), "Car should be in one lane");
257                 Double pos = positions.get(lanesGroupA[1]);
258                 // System.out.println("Fractional positions: " + positions);
259                 assertTrue(null != pos, "Car should be in lane 1 of lane group A");
260                 assertEquals(pos, car.fractionalPosition(lanesGroupA[1], relativePosition), 0.0000001,
261                         "fractional position should be equal to result of fractionalPosition(lane, ...)");
262             }
263             for (Lane[] laneGroup : new Lane[][] {lanesGroupA})// , lanesGroupB})
264             {
265                 for (int laneIndex = 0; laneIndex < laneGroup.length; laneIndex++)
266                 {
267                     Lane lane = laneGroup[laneIndex];
268                     boolean expectException = 1 != laneIndex;
269                     for (RelativePosition relativePosition : new RelativePosition[] {car.getFront(), car.getReference(),
270                             car.getRear()})
271                     {
272                         // System.out.println("lane:" + lane + ", expectedException: " + expectException
273                         // + ", relativePostion: " + relativePosition);
274                         try
275                         {
276                             Length position = car.position(lane, relativePosition);
277                             if (expectException)
278                             {
279                                 // System.out.println("position: " + position);
280                                 fail("Calling position on lane that the car is NOT on should have thrown a "
281                                         + "NetworkException");
282                             }
283                             else
284                             {
285                                 Length expectedPosition = laneGroup == lanesGroupA ? positionA : positionB;
286                                 expectedPosition = expectedPosition
287                                         .plus(new Length(stepTime.getSI() * initialSpeed.getSI(), LengthUnit.SI));
288                                 expectedPosition = expectedPosition.plus(new Length(
289                                         0.5 * acceleration.getSI() * stepTime.getSI() * stepTime.getSI(), LengthUnit.SI));
290                                 expectedPosition = expectedPosition.plus(relativePosition.dx());
291                                 // System.out.println("reported position: " + position);
292                                 // System.out.println("expected position: " + expectedPosition);
293                                 assertEquals(expectedPosition.getSI(), position.getSI(), 0.0001,
294                                         "Position should match initial position");
295                             }
296                         }
297                         catch (GtuException ne)
298                         {
299                             if (!expectException)
300                             {
301                                 System.out.println(ne);
302                                 fail("Calling position on lane that the car is on should NOT have thrown a NetworkException");
303                             }
304                         }
305                         try
306                         {
307                             double fractionalPosition = car.fractionalPosition(lane, relativePosition);
308                             if (expectException)
309                             {
310                                 // System.out.println("position: " + position);
311                                 fail("Calling position on lane that the car is NOT on should have thrown a NetworkException");
312                             }
313                             else
314                             {
315                                 Length expectedPosition = laneGroup == lanesGroupA ? positionA : positionB;
316                                 expectedPosition = expectedPosition
317                                         .plus(new Length(stepTime.getSI() * initialSpeed.getSI(), LengthUnit.SI));
318                                 expectedPosition = expectedPosition.plus(new Length(
319                                         0.5 * acceleration.getSI() * stepTime.getSI() * stepTime.getSI(), LengthUnit.SI));
320                                 expectedPosition = expectedPosition.plus(relativePosition.dx());
321                                 // System.out.println("reported position: " + position);
322                                 // System.out.println("expected position: " + expectedPosition);
323                                 double expectedFractionalPosition = expectedPosition.getSI() / lane.getLength().getSI();
324                                 assertEquals(expectedFractionalPosition, fractionalPosition, 0.000001,
325                                         "Position should match initial position");
326                             }
327                         }
328                         catch (GtuException ne)
329                         {
330                             if (!expectException)
331                             {
332                                 System.out.println(ne);
333                                 fail("Calling fractionalPosition on lane that the car is on should NOT have thrown a "
334                                         + "NetworkException");
335                             }
336                         }
337                     }
338                 }
339             }
340         }
341         // A GTU can exist on several lanes at once; create another lane group to test that
342         Node nodeCFrom = new Node(network, "CFrom", new Point2d(10, 100), Direction.ZERO);
343         Node nodeCTo = new Node(network, "CTo", new Point2d(1000, 0), Direction.ZERO);
344         Lane[] lanesGroupC = LaneFactory.makeMultiLane(network, "C", nodeCFrom, nodeCTo, null, 3, laneType,
345                 new Speed(100, KM_PER_HOUR), simulator, DefaultsNl.VEHICLE);
346         for (RelativePosition relativePosition : new RelativePosition[] {car.getFront(), car.getRear()})
347         {
348             Map<Lane, Double> positions = car.fractionalPositions(relativePosition);
349             Double pos = positions.get(lanesGroupA[1]);
350             assertTrue(null != pos, "Car should be in lane 1 of lane group A");
351             assertEquals(pos, car.fractionalPosition(lanesGroupA[1], relativePosition), 0.0000001,
352                     "fractional position should be equal to result of fractionalPosition(lane, ...)");
353             pos = positions.get(lanesGroupC[0]);
354         }
355         // TODO removeLane should throw an Error when the car is not on that lane (currently this is silently ignored)
356         // TODO figure out why the added lane has a non-zero position
357     }
358 }
359 
360 /**
361  * Dummy OtsModelInterface.
362  * <p>
363  * Copyright (c) 2013-2024 Delft University of Technology, PO Box 5, 2600 AA, Delft, the Netherlands. All rights reserved. <br>
364  * BSD-style license. See <a href="https://opentrafficsim.org/docs/license.html">OpenTrafficSim License</a>.
365  * </p>
366  * @author <a href="https://github.com/peter-knoppers">Peter Knoppers</a>
367  */
368 class DummyModel extends AbstractOtsModel
369 {
370     /** */
371     private static final long serialVersionUID = 20150114L;
372 
373     /**
374      * @param simulator the simulator to use
375      */
376     DummyModel(final OtsSimulatorInterface simulator)
377     {
378         super(simulator);
379     }
380 
381     @Override
382     public final void constructModel() throws SimRuntimeException
383     {
384         //
385     }
386 
387     @Override
388     public final RoadNetwork getNetwork()
389     {
390         return null;
391     }
392 
393 }