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