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