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