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.Network;
40  import org.opentrafficsim.core.network.NetworkException;
41  import org.opentrafficsim.core.network.OTSNetwork;
42  import org.opentrafficsim.core.network.OTSNode;
43  import org.opentrafficsim.road.DefaultTestParameters;
44  import org.opentrafficsim.road.gtu.lane.LaneBasedIndividualGTU;
45  import org.opentrafficsim.road.gtu.lane.tactical.LaneBasedCFLCTacticalPlanner;
46  import org.opentrafficsim.road.gtu.lane.tactical.following.FixedAccelerationModel;
47  import org.opentrafficsim.road.gtu.lane.tactical.following.GTUFollowingModelOld;
48  import org.opentrafficsim.road.gtu.lane.tactical.lanechangemobil.Egoistic;
49  import org.opentrafficsim.road.gtu.lane.tactical.lanechangemobil.LaneChangeModel;
50  import org.opentrafficsim.road.gtu.strategical.LaneBasedStrategicalPlanner;
51  import org.opentrafficsim.road.gtu.strategical.route.LaneBasedStrategicalRoutePlanner;
52  import org.opentrafficsim.road.network.lane.CrossSectionLink;
53  import org.opentrafficsim.road.network.lane.DirectedLanePosition;
54  import org.opentrafficsim.road.network.lane.Lane;
55  import org.opentrafficsim.road.network.lane.LaneType;
56  import org.opentrafficsim.road.network.lane.changing.LaneKeepingPolicy;
57  import org.opentrafficsim.road.network.lane.changing.OvertakingConditions;
58  
59  /**
60   * <p>
61   * Copyright (c) 2013-2016 Delft University of Technology, PO Box 5, 2600 AA, Delft, the Netherlands. All rights reserved. <br>
62   * BSD-style license. See <a href="http://opentrafficsim.org/docs/license.html">OpenTrafficSim License</a>.
63   * <p>
64   * $LastChangedDate: 2015-09-16 19:20:07 +0200 (Wed, 16 Sep 2015) $, @version $Revision: 1405 $, by $Author: averbraeck $,
65   * initial version Jul 11, 2014 <br>
66   * @author <a href="http://www.tudelft.nl/pknoppers">Peter Knoppers</a>
67   */
68  public class CarTest implements UNITS
69  {
70      /**
71       * Test some basics of the Car class.
72       * @throws NetworkException on ???
73       * @throws SimRuntimeException on ???
74       * @throws NamingException on ???
75       * @throws GTUException on ???
76       * @throws OTSGeometryException when center line or contour of a link or lane cannot be generated
77       */
78      @SuppressWarnings("static-method")
79      @Test
80      public final void carTest() throws NetworkException, SimRuntimeException, NamingException, GTUException,
81              OTSGeometryException
82      {
83          Time initialTime = new Time(0, SECOND);
84          GTUType gtuType = new GTUType("Car");
85          Set<GTUType> compatibility = new HashSet<GTUType>();
86          compatibility.add(gtuType);
87          LaneType laneType = new LaneType("CarLane", compatibility);
88          OTSNetwork network = new OTSNetwork("network");
89          Lane lane = makeLane(network, laneType);
90          Length initialPosition = new Length(12, METER);
91          Speed initialSpeed = new Speed(34, KM_PER_HOUR);
92          OTSDEVSSimulator simulator = makeSimulator();
93          GTUFollowingModelOld gtuFollowingModel =
94                  new FixedAccelerationModel(new Acceleration(0, METER_PER_SECOND_2), new Duration(10, SECOND));
95          LaneChangeModel laneChangeModel = new Egoistic();
96          LaneBasedIndividualGTU referenceCar =
97                  makeReferenceCar("12345", gtuType, lane, initialPosition, initialSpeed, simulator, gtuFollowingModel,
98                          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(), referenceCar
101                 .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 =
122                 new Experiment<Time, Duration, OTSSimTimeDouble>();
123         Treatment<Time, Duration, OTSSimTimeDouble> tr =
124                 new Treatment<>(exp, "tr1", new OTSSimTimeDouble(new Time(0, SECOND)), new Duration(0, SECOND), new Duration(
125                         3600.0, SECOND));
126         exp.setTreatment(tr);
127         exp.setModel(model);
128         Replication<Time, Duration, OTSSimTimeDouble> rep = new Replication<>(exp);
129         simulator.initialize(rep, ReplicationMode.TERMINATING);
130         return simulator;
131     }
132 
133     /**
134      * Create a new Car.
135      * @param id String; the name (number) of the Car
136      * @param gtuType GTUType; the type of the new car
137      * @param lane Lane; the lane on which the new Car is positioned
138      * @param initialPosition Length; the initial longitudinal position of the new Car
139      * @param initialSpeed Speed; the initial speed
140      * @param simulator OTSDEVVSimulator; the simulator that controls the new Car (and supplies the initial value for
141      *            getLastEvalutionTime())
142      * @param gtuFollowingModel GTUFollowingModel; the GTU following model
143      * @param laneChangeModel LaneChangeModel; the lane change model
144      * @param network the network
145      * @return Car; the new Car
146      * @throws NamingException on network error when making the animation
147      * @throws NetworkException when the GTU cannot be placed on the given lane.
148      * @throws SimRuntimeException when the move method cannot be scheduled.
149      * @throws GTUException when construction of the GTU fails (probably due to an invalid parameter)
150      * @throws OTSGeometryException when the initial path is wrong
151      */
152     public static LaneBasedIndividualGTU makeReferenceCar(final String id, final GTUType gtuType, final Lane lane,
153             final Length initialPosition, final Speed initialSpeed, final OTSDEVSSimulator simulator,
154             final GTUFollowingModelOld gtuFollowingModel, final LaneChangeModel laneChangeModel, final OTSNetwork network)
155             throws NamingException, NetworkException, SimRuntimeException, GTUException, OTSGeometryException
156     {
157         Length length = new Length(5.0, METER);
158         Length width = new Length(2.0, METER);
159         Set<DirectedLanePosition> initialLongitudinalPositions = new LinkedHashSet<>(1);
160         initialLongitudinalPositions.add(new DirectedLanePosition(lane, initialPosition, GTUDirectionality.DIR_PLUS));
161         Speed maxSpeed = new Speed(120, KM_PER_HOUR);
162         BehavioralCharacteristics behavioralCharacteristics = DefaultTestParameters.create();
163         LaneBasedIndividualGTU gtu = new LaneBasedIndividualGTU(id, gtuType, length, width, maxSpeed, simulator, network);
164         LaneBasedStrategicalPlanner strategicalPlanner =
165                 new LaneBasedStrategicalRoutePlanner(behavioralCharacteristics, new LaneBasedCFLCTacticalPlanner(
166                         gtuFollowingModel, laneChangeModel, gtu), gtu);
167         gtu.init(strategicalPlanner, initialLongitudinalPositions, initialSpeed);
168 
169         return gtu;
170     }
171 
172     /**
173      * @param network Network; the network
174      * @param laneType LaneType&lt;String&gt;; the type of the lane
175      * @return a lane of 1000 m long.
176      * @throws NetworkException on network error
177      * @throws OTSGeometryException when center line or contour of a link or lane cannot be generated
178      */
179     public static Lane makeLane(final Network network, final LaneType laneType) throws NetworkException, OTSGeometryException
180     {
181         OTSNode n1 = new OTSNode(network, "n1", new OTSPoint3D(0, 0));
182         OTSNode n2 = new OTSNode(network, "n2", new OTSPoint3D(100000.0, 0.0));
183         OTSPoint3D[] coordinates = new OTSPoint3D[] { new OTSPoint3D(0.0, 0.0), new OTSPoint3D(100000.0, 0.0) };
184         CrossSectionLink link12 =
185                 new CrossSectionLink(network, "link12", n1, n2, LinkType.ALL, new OTSLine3D(coordinates),
186                         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(
205                 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     }
219 }