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