View Javadoc
1   package org.opentrafficsim.road.car;
2   
3   import static org.junit.Assert.assertEquals;
4   
5   import java.io.Serializable;
6   import java.util.LinkedHashSet;
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.dsol.AbstractOTSModel;
23  import org.opentrafficsim.core.dsol.OTSSimulator;
24  import org.opentrafficsim.core.dsol.OTSSimulatorInterface;
25  import org.opentrafficsim.core.geometry.OTSGeometryException;
26  import org.opentrafficsim.core.geometry.OTSLine3D;
27  import org.opentrafficsim.core.geometry.OTSPoint3D;
28  import org.opentrafficsim.core.gtu.GTUDirectionality;
29  import org.opentrafficsim.core.gtu.GTUException;
30  import org.opentrafficsim.core.gtu.GTUType;
31  import org.opentrafficsim.core.network.LinkType;
32  import org.opentrafficsim.core.network.NetworkException;
33  import org.opentrafficsim.road.DefaultTestParameters;
34  import org.opentrafficsim.road.gtu.lane.LaneBasedIndividualGTU;
35  import org.opentrafficsim.road.gtu.lane.tactical.LaneBasedCFLCTacticalPlanner;
36  import org.opentrafficsim.road.gtu.lane.tactical.following.FixedAccelerationModel;
37  import org.opentrafficsim.road.gtu.lane.tactical.following.GTUFollowingModelOld;
38  import org.opentrafficsim.road.gtu.lane.tactical.lanechangemobil.Egoistic;
39  import org.opentrafficsim.road.gtu.lane.tactical.lanechangemobil.LaneChangeModel;
40  import org.opentrafficsim.road.gtu.strategical.LaneBasedStrategicalPlanner;
41  import org.opentrafficsim.road.gtu.strategical.route.LaneBasedStrategicalRoutePlanner;
42  import org.opentrafficsim.road.network.OTSRoadNetwork;
43  import org.opentrafficsim.road.network.lane.CrossSectionLink;
44  import org.opentrafficsim.road.network.lane.DirectedLanePosition;
45  import org.opentrafficsim.road.network.lane.Lane;
46  import org.opentrafficsim.road.network.lane.LaneType;
47  import org.opentrafficsim.road.network.lane.OTSRoadNode;
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-2022 Delft University of Technology, PO Box 5, 2600 AA, Delft, the Netherlands. All rights reserved. <br>
55   * BSD-style license. See <a href="http://opentrafficsim.org/docs/license.html">OpenTrafficSim License</a>.
56   * <p>
57   * $LastChangedDate: 2015-09-16 19:20:07 +0200 (Wed, 16 Sep 2015) $, @version $Revision: 1405 $, by $Author: averbraeck $,
58   * initial version Jul 11, 2014 <br>
59   * @author <a href="http://www.tudelft.nl/pknoppers">Peter Knoppers</a>
60   */
61  public class CarTest implements UNITS
62  {
63      /**
64       * Test some basics of the Car class.
65       * @throws NetworkException on ???
66       * @throws SimRuntimeException on ???
67       * @throws NamingException on ???
68       * @throws GTUException on ???
69       * @throws OTSGeometryException when center line or contour of a link or lane cannot be generated
70       */
71      @SuppressWarnings("static-method")
72      @Test
73      public final void carTest()
74              throws NetworkException, SimRuntimeException, NamingException, GTUException, OTSGeometryException
75      {
76          Time initialTime = new Time(0, TimeUnit.BASE_SECOND);
77          OTSSimulatorInterface simulator = makeSimulator();
78          OTSRoadNetwork network = new OTSRoadNetwork("network", true, simulator);
79          GTUType gtuType = network.getGtuType(GTUType.DEFAULTS.CAR);
80          LaneType laneType = network.getLaneType(LaneType.DEFAULTS.TWO_WAY_LANE);
81          Lane lane = makeLane(network, laneType, simulator);
82          Length initialPosition = new Length(12, METER);
83          Speed initialSpeed = new Speed(34, KM_PER_HOUR);
84          GTUFollowingModelOld gtuFollowingModel =
85                  new FixedAccelerationModel(new Acceleration(0, METER_PER_SECOND_2), new Duration(10, SECOND));
86          LaneChangeModel laneChangeModel = new Egoistic();
87          LaneBasedIndividualGTU referenceCar = makeReferenceCar("12345", gtuType, lane, initialPosition, initialSpeed, simulator,
88                  gtuFollowingModel, laneChangeModel, network);
89          assertEquals("The car should store it's ID", "12345", referenceCar.getId());
90          assertEquals("At t=initialTime the car should be at it's initial position", initialPosition.getSI(),
91                  referenceCar.position(lane, referenceCar.getReference(), initialTime).getSI(), 0.0001);
92          assertEquals("The car should store it's initial speed", initialSpeed.getSI(), referenceCar.getSpeed().getSI(), 0.00001);
93          assertEquals("The car should have an initial acceleration equal to 0", 0, referenceCar.getAcceleration().getSI(),
94                  0.0001);
95          // TODO check with following model as part of tactical planner
96          // assertEquals("The gtu following model should be " + gtuFollowingModel, gtuFollowingModel, referenceCar
97          // .getBehavioralCharacteristics().getGTUFollowingModel());
98          // There is (currently) no way to retrieve the lane change model of a GTU.
99      }
100 
101     /**
102      * Create the simplest possible simulator.
103      * @return DEVSSimulator.TimeDoubleUnit
104      * @throws SimRuntimeException on ???
105      * @throws NamingException on ???
106      */
107     public static OTSSimulatorInterface makeSimulator() throws SimRuntimeException, NamingException
108     {
109         OTSSimulatorInterface simulator = new OTSSimulator("CarTest");
110         Model model = new Model(simulator);
111         simulator.initialize(Time.ZERO, Duration.ZERO, new Duration(3600.0, DurationUnit.SECOND), model);
112         return simulator;
113     }
114 
115     /**
116      * Create a new Car.
117      * @param id String; the name (number) of the Car
118      * @param gtuType GTUType; the type of the new car
119      * @param lane Lane; the lane on which the new Car is positioned
120      * @param initialPosition Length; the initial longitudinal position of the new Car
121      * @param initialSpeed Speed; the initial speed
122      * @param simulator OTSDEVVSimulator; the simulator that controls the new Car (and supplies the initial value for
123      *            getLastEvalutionTime())
124      * @param gtuFollowingModel GTUFollowingModel; the GTU following model
125      * @param laneChangeModel LaneChangeModel; the lane change model
126      * @param network the network
127      * @return Car; the new Car
128      * @throws NamingException on network error when making the animation
129      * @throws NetworkException when the GTU cannot be placed on the given lane.
130      * @throws SimRuntimeException when the move method cannot be scheduled.
131      * @throws GTUException when construction of the GTU fails (probably due to an invalid parameter)
132      * @throws OTSGeometryException when the initial path is wrong
133      */
134     public static LaneBasedIndividualGTU makeReferenceCar(final String id, final GTUType gtuType, final Lane lane,
135             final Length initialPosition, final Speed initialSpeed, final OTSSimulatorInterface simulator,
136             final GTUFollowingModelOld gtuFollowingModel, final LaneChangeModel laneChangeModel, final OTSRoadNetwork network)
137             throws NamingException, NetworkException, SimRuntimeException, GTUException, OTSGeometryException
138     {
139         Length length = new Length(5.0, METER);
140         Length width = new Length(2.0, METER);
141         Set<DirectedLanePosition> initialLongitudinalPositions = new LinkedHashSet<>(1);
142         initialLongitudinalPositions.add(new DirectedLanePosition(lane, initialPosition, GTUDirectionality.DIR_PLUS));
143         Speed maxSpeed = new Speed(120, KM_PER_HOUR);
144         Parameters parameters = DefaultTestParameters.create();
145         LaneBasedIndividualGTU gtu =
146                 new LaneBasedIndividualGTU(id, gtuType, length, width, maxSpeed, length.times(0.5), simulator, network);
147         LaneBasedStrategicalPlanner strategicalPlanner = new LaneBasedStrategicalRoutePlanner(
148                 new LaneBasedCFLCTacticalPlanner(gtuFollowingModel, laneChangeModel, gtu), gtu);
149         gtu.setParameters(parameters);
150         gtu.init(strategicalPlanner, initialLongitudinalPositions, initialSpeed);
151 
152         return gtu;
153     }
154 
155     /**
156      * @param network RoadNetwork; the network
157      * @param laneType LaneType&lt;String&gt;; the type of the lane
158      * @param simulator OTSSimulatorInterface; simulator
159      * @return a lane of 1000 m long.
160      * @throws NetworkException on network error
161      * @throws OTSGeometryException when center line or contour of a link or lane cannot be generated
162      */
163     public static Lane makeLane(final OTSRoadNetwork network, final LaneType laneType, final OTSSimulatorInterface simulator)
164             throws NetworkException, OTSGeometryException
165     {
166         OTSRoadNode n1 = new OTSRoadNode(network, "n1", new OTSPoint3D(0, 0), Direction.ZERO);
167         OTSRoadNode n2 = new OTSRoadNode(network, "n2", new OTSPoint3D(100000.0, 0.0), Direction.ZERO);
168         OTSPoint3D[] coordinates = new OTSPoint3D[] {new OTSPoint3D(0.0, 0.0), new OTSPoint3D(100000.0, 0.0)};
169         CrossSectionLink link12 = new CrossSectionLink(network, "link12", n1, n2, network.getLinkType(LinkType.DEFAULTS.ROAD),
170                 new OTSLine3D(coordinates), LaneKeepingPolicy.KEEPRIGHT);
171         Length latPos = new Length(0.0, METER);
172         Length width = new Length(4.0, METER);
173         return new Lane(link12, "lane.1", latPos, latPos, width, width, laneType, new Speed(100, KM_PER_HOUR));
174     }
175 
176     /** The helper model. */
177     protected static class Model extends AbstractOTSModel
178     {
179         /** */
180         private static final long serialVersionUID = 20141027L;
181 
182         /**
183          * @param simulator the simulator to use
184          */
185         public Model(final OTSSimulatorInterface simulator)
186         {
187             super(simulator);
188         }
189 
190         /** {@inheritDoc} */
191         @Override
192         public final void constructModel() throws SimRuntimeException
193         {
194             //
195         }
196 
197         /** {@inheritDoc} */
198         @Override
199         public final OTSRoadNetwork getNetwork()
200         {
201             return null;
202         }
203 
204         /** {@inheritDoc} */
205         @Override
206         public Serializable getSourceId()
207         {
208             return "CarTestModel";
209         }
210     }
211 }