View Javadoc
1   package org.opentrafficsim.road.car;
2   
3   import static org.junit.Assert.assertEquals;
4   
5   import java.util.HashSet;
6   import java.util.LinkedHashSet;
7   import java.util.Set;
8   
9   import javax.naming.NamingException;
10  
11  import nl.tudelft.simulation.dsol.SimRuntimeException;
12  import nl.tudelft.simulation.dsol.experiment.Experiment;
13  import nl.tudelft.simulation.dsol.experiment.Replication;
14  import nl.tudelft.simulation.dsol.experiment.ReplicationMode;
15  import nl.tudelft.simulation.dsol.experiment.Treatment;
16  import nl.tudelft.simulation.dsol.simulators.SimulatorInterface;
17  
18  import org.djunits.unit.TimeUnit;
19  import org.djunits.unit.UNITS;
20  import org.djunits.value.vdouble.scalar.Acceleration;
21  import org.djunits.value.vdouble.scalar.DoubleScalar;
22  import org.djunits.value.vdouble.scalar.Duration;
23  import org.djunits.value.vdouble.scalar.Length;
24  import org.djunits.value.vdouble.scalar.Speed;
25  import org.djunits.value.vdouble.scalar.Time;
26  import org.junit.Test;
27  import org.opentrafficsim.core.dsol.OTSDEVSSimulator;
28  import org.opentrafficsim.core.dsol.OTSModelInterface;
29  import org.opentrafficsim.core.dsol.OTSSimTimeDouble;
30  import org.opentrafficsim.core.geometry.OTSGeometryException;
31  import org.opentrafficsim.core.geometry.OTSLine3D;
32  import org.opentrafficsim.core.geometry.OTSPoint3D;
33  import org.opentrafficsim.core.gtu.GTUDirectionality;
34  import org.opentrafficsim.core.gtu.GTUException;
35  import org.opentrafficsim.core.gtu.GTUType;
36  import org.opentrafficsim.core.gtu.behavioralcharacteristics.BehavioralCharacteristics;
37  import org.opentrafficsim.core.network.LinkType;
38  import org.opentrafficsim.core.network.LongitudinalDirectionality;
39  import org.opentrafficsim.core.network.NetworkException;
40  import org.opentrafficsim.core.network.OTSNetwork;
41  import org.opentrafficsim.core.network.OTSNode;
42  import org.opentrafficsim.road.DefaultTestParameters;
43  import org.opentrafficsim.road.gtu.lane.LaneBasedIndividualGTU;
44  import org.opentrafficsim.road.gtu.lane.tactical.LaneBasedCFLCTacticalPlanner;
45  import org.opentrafficsim.road.gtu.lane.tactical.following.FixedAccelerationModel;
46  import org.opentrafficsim.road.gtu.lane.tactical.following.GTUFollowingModelOld;
47  import org.opentrafficsim.road.gtu.lane.tactical.lanechangemobil.Egoistic;
48  import org.opentrafficsim.road.gtu.lane.tactical.lanechangemobil.LaneChangeModel;
49  import org.opentrafficsim.road.gtu.strategical.LaneBasedStrategicalPlanner;
50  import org.opentrafficsim.road.gtu.strategical.route.LaneBasedStrategicalRoutePlanner;
51  import org.opentrafficsim.road.network.lane.CrossSectionLink;
52  import org.opentrafficsim.road.network.lane.DirectedLanePosition;
53  import org.opentrafficsim.road.network.lane.Lane;
54  import org.opentrafficsim.road.network.lane.LaneType;
55  import org.opentrafficsim.road.network.lane.changing.LaneKeepingPolicy;
56  import org.opentrafficsim.road.network.lane.changing.OvertakingConditions;
57  
58  /**
59   * <p>
60   * Copyright (c) 2013-2016 Delft University of Technology, PO Box 5, 2600 AA, Delft, the Netherlands. All rights reserved. <br>
61   * BSD-style license. See <a href="http://opentrafficsim.org/docs/license.html">OpenTrafficSim License</a>.
62   * <p>
63   * $LastChangedDate: 2015-09-16 19:20:07 +0200 (Wed, 16 Sep 2015) $, @version $Revision: 1405 $, by $Author: averbraeck $,
64   * initial version Jul 11, 2014 <br>
65   * @author <a href="http://www.tudelft.nl/pknoppers">Peter Knoppers</a>
66   */
67  public class CarTest implements UNITS
68  {
69      /**
70       * Test some basics of the Car class.
71       * @throws NetworkException on ???
72       * @throws SimRuntimeException on ???
73       * @throws NamingException on ???
74       * @throws GTUException on ???
75       * @throws OTSGeometryException when center line or contour of a link or lane cannot be generated
76       */
77      @SuppressWarnings("static-method")
78      @Test
79      public final void carTest() throws NetworkException, SimRuntimeException, NamingException, GTUException,
80              OTSGeometryException
81      {
82          Time initialTime = new Time(0, SECOND);
83          GTUType gtuType = new GTUType("Car");
84          Set<GTUType> compatibility = new HashSet<GTUType>();
85          compatibility.add(gtuType);
86          LaneType laneType = new LaneType("CarLane", compatibility);
87          OTSNetwork network = new OTSNetwork("network");
88          Lane lane = makeLane(laneType);
89          Length initialPosition = new Length(12, METER);
90          Speed initialSpeed = new Speed(34, KM_PER_HOUR);
91          OTSDEVSSimulator simulator = makeSimulator();
92          GTUFollowingModelOld gtuFollowingModel =
93                  new FixedAccelerationModel(new Acceleration(0, METER_PER_SECOND_2), new Duration(10, SECOND));
94          LaneChangeModel laneChangeModel = new Egoistic();
95          LaneBasedIndividualGTU referenceCar =
96                  makeReferenceCar("12345", gtuType, lane, initialPosition, initialSpeed, simulator, gtuFollowingModel,
97                          laneChangeModel, network);
98          assertEquals("The car should store it's ID", "12345", referenceCar.getId());
99          assertEquals("At t=initialTime the car should be at it's initial position", initialPosition.getSI(), referenceCar
100                 .position(lane, referenceCar.getReference(), initialTime).getSI(), 0.0001);
101         assertEquals("The car should store it's initial speed", initialSpeed.getSI(), referenceCar.getSpeed().getSI(), 0.00001);
102         assertEquals("The car should have an initial acceleration equal to 0", 0, referenceCar.getAcceleration().getSI(),
103                 0.0001);
104         // TODO check with following model as part of tactical planner
105         // assertEquals("The gtu following model should be " + gtuFollowingModel, gtuFollowingModel, referenceCar
106         // .getBehavioralCharacteristics().getGTUFollowingModel());
107         // There is (currently) no way to retrieve the lane change model of a GTU.
108     }
109 
110     /**
111      * Create the simplest possible simulator.
112      * @return OTSDEVSSimulator
113      * @throws SimRuntimeException on ???
114      * @throws NamingException on ???
115      */
116     public static OTSDEVSSimulator makeSimulator() throws SimRuntimeException, NamingException
117     {
118         OTSDEVSSimulator simulator = new OTSDEVSSimulator();
119         Model model = new Model();
120         Experiment<DoubleScalar.Abs<TimeUnit>, DoubleScalar.Rel<TimeUnit>, OTSSimTimeDouble> exp =
121                 new Experiment<DoubleScalar.Abs<TimeUnit>, DoubleScalar.Rel<TimeUnit>, OTSSimTimeDouble>();
122         Treatment<DoubleScalar.Abs<TimeUnit>, DoubleScalar.Rel<TimeUnit>, OTSSimTimeDouble> tr =
123                 new Treatment<>(exp, "tr1", new OTSSimTimeDouble(new Time(0, SECOND)), new Duration(0, SECOND), new Duration(
124                         3600.0, SECOND));
125         exp.setTreatment(tr);
126         exp.setModel(model);
127         Replication<DoubleScalar.Abs<TimeUnit>, DoubleScalar.Rel<TimeUnit>, 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 =
164                 new LaneBasedStrategicalRoutePlanner(behavioralCharacteristics, new LaneBasedCFLCTacticalPlanner(
165                         gtuFollowingModel, laneChangeModel, gtu), gtu);
166         gtu.init(strategicalPlanner, initialLongitudinalPositions, initialSpeed);
167 
168         return gtu;
169     }
170 
171     /**
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 LaneType laneType) throws NetworkException, OTSGeometryException
178     {
179         OTSNode n1 = new OTSNode("n1", new OTSPoint3D(0, 0));
180         OTSNode n2 = new OTSNode("n2", new OTSPoint3D(100000.0, 0.0));
181         OTSPoint3D[] coordinates = new OTSPoint3D[] { new OTSPoint3D(0.0, 0.0), new OTSPoint3D(100000.0, 0.0) };
182         CrossSectionLink link12 =
183                 new CrossSectionLink("link12", n1, n2, LinkType.ALL, new OTSLine3D(coordinates),
184                         LongitudinalDirectionality.DIR_PLUS, LaneKeepingPolicy.KEEP_RIGHT);
185         Length latPos = new Length(0.0, METER);
186         Length width = new Length(4.0, METER);
187         return new Lane(link12, "lane.1", latPos, latPos, width, width, laneType, LongitudinalDirectionality.DIR_PLUS,
188                 new Speed(100, KM_PER_HOUR), new OvertakingConditions.LeftAndRight());
189     }
190 
191     /** The helper model. */
192     protected static class Model implements OTSModelInterface
193     {
194         /** */
195         private static final long serialVersionUID = 20141027L;
196 
197         /** The simulator. */
198         private OTSDEVSSimulator simulator;
199 
200         /** {@inheritDoc} */
201         @Override
202         public final void constructModel(
203                 final SimulatorInterface<DoubleScalar.Abs<TimeUnit>, DoubleScalar.Rel<TimeUnit>, OTSSimTimeDouble> theSimulator)
204                 throws SimRuntimeException
205         {
206             this.simulator = (OTSDEVSSimulator) theSimulator;
207         }
208 
209         /** {@inheritDoc} */
210         @Override
211         public final OTSDEVSSimulator getSimulator()
212         {
213             return this.simulator;
214         }
215 
216     }
217 }