View Javadoc
1   package org.opentrafficsim.road.car;
2   
3   import static org.junit.jupiter.api.Assertions.assertEquals;
4   
5   import java.util.Map;
6   
7   import javax.naming.NamingException;
8   
9   import org.djunits.unit.DurationUnit;
10  import org.djunits.unit.TimeUnit;
11  import org.djunits.unit.util.UNITS;
12  import org.djunits.value.vdouble.scalar.Acceleration;
13  import org.djunits.value.vdouble.scalar.Direction;
14  import org.djunits.value.vdouble.scalar.Duration;
15  import org.djunits.value.vdouble.scalar.Length;
16  import org.djunits.value.vdouble.scalar.Speed;
17  import org.djunits.value.vdouble.scalar.Time;
18  import org.djutils.draw.point.Point2d;
19  import org.junit.jupiter.api.Test;
20  import org.opentrafficsim.base.geometry.OtsLine2d;
21  import org.opentrafficsim.base.parameters.Parameters;
22  import org.opentrafficsim.core.definitions.DefaultsNl;
23  import org.opentrafficsim.core.dsol.AbstractOtsModel;
24  import org.opentrafficsim.core.dsol.OtsSimulator;
25  import org.opentrafficsim.core.dsol.OtsSimulatorInterface;
26  import org.opentrafficsim.core.gtu.GtuException;
27  import org.opentrafficsim.core.gtu.GtuType;
28  import org.opentrafficsim.core.network.NetworkException;
29  import org.opentrafficsim.core.network.Node;
30  import org.opentrafficsim.core.perception.HistoryManagerDevs;
31  import org.opentrafficsim.road.DefaultTestParameters;
32  import org.opentrafficsim.road.definitions.DefaultsRoadNl;
33  import org.opentrafficsim.road.gtu.lane.LaneBasedGtu;
34  import org.opentrafficsim.road.gtu.lane.tactical.LaneBasedCfLcTacticalPlanner;
35  import org.opentrafficsim.road.gtu.lane.tactical.following.FixedAccelerationModel;
36  import org.opentrafficsim.road.gtu.lane.tactical.following.GtuFollowingModelOld;
37  import org.opentrafficsim.road.gtu.lane.tactical.lanechangemobil.Egoistic;
38  import org.opentrafficsim.road.gtu.lane.tactical.lanechangemobil.LaneChangeModel;
39  import org.opentrafficsim.road.gtu.strategical.LaneBasedStrategicalPlanner;
40  import org.opentrafficsim.road.gtu.strategical.LaneBasedStrategicalRoutePlanner;
41  import org.opentrafficsim.road.network.RoadNetwork;
42  import org.opentrafficsim.road.network.lane.CrossSectionLink;
43  import org.opentrafficsim.road.network.lane.Lane;
44  import org.opentrafficsim.road.network.lane.LaneGeometryUtil;
45  import org.opentrafficsim.road.network.lane.LanePosition;
46  import org.opentrafficsim.road.network.lane.LaneType;
47  import org.opentrafficsim.road.network.lane.changing.LaneKeepingPolicy;
48  
49  import nl.tudelft.simulation.dsol.SimRuntimeException;
50  
51  /**
52   * <p>
53   * Copyright (c) 2013-2024 Delft University of Technology, PO Box 5, 2600 AA, Delft, the Netherlands. All rights reserved. <br>
54   * BSD-style license. See <a href="https://opentrafficsim.org/docs/license.html">OpenTrafficSim License</a>.
55   * </p>
56   * @author <a href="https://github.com/peter-knoppers">Peter Knoppers</a>
57   */
58  public class CarTest implements UNITS
59  {
60      /**
61       * Test some basics of the Car class.
62       * @throws NetworkException on ???
63       * @throws SimRuntimeException on ???
64       * @throws NamingException on ???
65       * @throws GtuException on ???
66       */
67      @SuppressWarnings("static-method")
68      @Test
69      public final void carTest() throws NetworkException, SimRuntimeException, NamingException, GtuException
70      {
71          Time initialTime = new Time(0, TimeUnit.BASE_SECOND);
72          OtsSimulatorInterface simulator = makeSimulator();
73          RoadNetwork network = new RoadNetwork("network", simulator);
74          GtuType gtuType = DefaultsNl.CAR;
75          LaneType laneType = DefaultsRoadNl.TWO_WAY_LANE;
76          Lane lane = makeLane(network, laneType, simulator);
77          Length initialPosition = new Length(12, METER);
78          Speed initialSpeed = new Speed(34, KM_PER_HOUR);
79          GtuFollowingModelOld gtuFollowingModel =
80                  new FixedAccelerationModel(new Acceleration(0, METER_PER_SECOND_2), new Duration(10, SECOND));
81          LaneChangeModel laneChangeModel = new Egoistic();
82          LaneBasedGtu referenceCar = makeReferenceCar("12345", gtuType, lane, initialPosition, initialSpeed, gtuFollowingModel,
83                  laneChangeModel, network);
84          assertEquals("12345", referenceCar.getId(), "The car should store it's ID");
85          assertEquals(initialPosition.getSI(), referenceCar.position(lane, referenceCar.getReference(), initialTime).getSI(),
86                  0.0001, "At t=initialTime the car should be at it's initial position");
87          assertEquals(initialSpeed.getSI(), referenceCar.getSpeed().getSI(), 0.00001, "The car should store it's initial speed");
88          assertEquals(0, referenceCar.getAcceleration().getSI(), 0.0001,
89                  "The car should have an initial acceleration equal to 0");
90          // TODO check with following model as part of tactical planner
91          // assertEquals("The gtu following model should be " + gtuFollowingModel, gtuFollowingModel, referenceCar
92          // .getBehavioralCharacteristics().getGtuFollowingModel());
93          // There is (currently) no way to retrieve the lane change model of a GTU.
94      }
95  
96      /**
97       * Create the simplest possible simulator.
98       * @return DevsSimulator.TimeDoubleUnit
99       * @throws SimRuntimeException on ???
100      * @throws NamingException on ???
101      */
102     public static OtsSimulatorInterface makeSimulator() throws SimRuntimeException, NamingException
103     {
104         OtsSimulatorInterface simulator = new OtsSimulator("CarTest");
105         Model model = new Model(simulator);
106         simulator.initialize(Time.ZERO, Duration.ZERO, new Duration(3600.0, DurationUnit.SECOND), model,
107                 HistoryManagerDevs.noHistory(simulator));
108         return simulator;
109     }
110 
111     /**
112      * Create a new Car.
113      * @param id the name (number) of the Car
114      * @param gtuType the type of the new car
115      * @param lane the lane on which the new Car is positioned
116      * @param initialPosition the initial longitudinal position of the new Car
117      * @param initialSpeed the initial speed
118      * @param gtuFollowingModel the GTU following model
119      * @param laneChangeModel the lane change model
120      * @param network the network
121      * @return the new Car
122      * @throws NamingException on network error when making the animation
123      * @throws NetworkException when the GTU cannot be placed on the given lane.
124      * @throws SimRuntimeException when the move method cannot be scheduled.
125      * @throws GtuException when construction of the GTU fails (probably due to an invalid parameter)
126      */
127     public static LaneBasedGtu makeReferenceCar(final String id, final GtuType gtuType, final Lane lane,
128             final Length initialPosition, final Speed initialSpeed, final GtuFollowingModelOld gtuFollowingModel,
129             final LaneChangeModel laneChangeModel, final RoadNetwork network)
130             throws NamingException, NetworkException, SimRuntimeException, GtuException
131     {
132         Length length = new Length(5.0, METER);
133         Length width = new Length(2.0, METER);
134         Speed maxSpeed = new Speed(120, KM_PER_HOUR);
135         Parameters parameters = DefaultTestParameters.create();
136         LaneBasedGtu gtu = new LaneBasedGtu(id, gtuType, length, width, maxSpeed, length.times(0.5), network);
137         LaneBasedStrategicalPlanner strategicalPlanner = new LaneBasedStrategicalRoutePlanner(
138                 new LaneBasedCfLcTacticalPlanner(gtuFollowingModel, laneChangeModel, gtu), gtu);
139         gtu.setParameters(parameters);
140         gtu.init(strategicalPlanner, new LanePosition(lane, initialPosition), initialSpeed);
141 
142         return gtu;
143     }
144 
145     /**
146      * @param network the network
147      * @param laneType the type of the lane
148      * @param simulator simulator
149      * @return a lane of 1000 m long.
150      * @throws NetworkException on network error
151      */
152     public static Lane makeLane(final RoadNetwork network, final LaneType laneType, final OtsSimulatorInterface simulator)
153             throws NetworkException
154     {
155         Node n1 = new Node(network, "n1", new Point2d(0, 0), Direction.ZERO);
156         Node n2 = new Node(network, "n2", new Point2d(100000.0, 0.0), Direction.ZERO);
157         Point2d[] coordinates = new Point2d[] {new Point2d(0.0, 0.0), new Point2d(100000.0, 0.0)};
158         CrossSectionLink link12 = new CrossSectionLink(network, "link12", n1, n2, DefaultsNl.ROAD, new OtsLine2d(coordinates),
159                 null, LaneKeepingPolicy.KEEPRIGHT);
160         Length latPos = new Length(0.0, METER);
161         Length width = new Length(4.0, METER);
162         return LaneGeometryUtil.createStraightLane(link12, "lane.1", latPos, latPos, width, width, laneType,
163                 Map.of(DefaultsNl.VEHICLE, new Speed(100, KM_PER_HOUR)));
164     }
165 
166     /** The helper model. */
167     protected static class Model extends AbstractOtsModel
168     {
169         /** */
170         private static final long serialVersionUID = 20141027L;
171 
172         /**
173          * @param simulator the simulator to use
174          */
175         public Model(final OtsSimulatorInterface simulator)
176         {
177             super(simulator);
178         }
179 
180         @Override
181         public final void constructModel() throws SimRuntimeException
182         {
183             //
184         }
185 
186         @Override
187         public final RoadNetwork getNetwork()
188         {
189             return null;
190         }
191     }
192 }