View Javadoc
1   package org.opentrafficsim.road.car;
2   
3   import static org.junit.Assert.assertEquals;
4   import static org.opentrafficsim.core.gtu.GTUType.CAR;
5   
6   import java.util.LinkedHashSet;
7   import java.util.Set;
8   
9   import javax.naming.NamingException;
10  
11  import org.djunits.unit.TimeUnit;
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.base.parameters.Parameters;
20  import org.opentrafficsim.core.dsol.OTSModelInterface;
21  import org.opentrafficsim.core.geometry.OTSGeometryException;
22  import org.opentrafficsim.core.geometry.OTSLine3D;
23  import org.opentrafficsim.core.geometry.OTSPoint3D;
24  import org.opentrafficsim.core.gtu.GTUDirectionality;
25  import org.opentrafficsim.core.gtu.GTUException;
26  import org.opentrafficsim.core.gtu.GTUType;
27  import org.opentrafficsim.core.network.LinkType;
28  import org.opentrafficsim.core.network.Network;
29  import org.opentrafficsim.core.network.NetworkException;
30  import org.opentrafficsim.core.network.OTSNetwork;
31  import org.opentrafficsim.core.network.OTSNode;
32  import org.opentrafficsim.road.DefaultTestParameters;
33  import org.opentrafficsim.road.gtu.lane.LaneBasedIndividualGTU;
34  import org.opentrafficsim.road.gtu.lane.tactical.LaneBasedCFLCTacticalPlanner;
35  import org.opentrafficsim.road.gtu.lane.tactical.following.FixedAccelerationModel;
36  import org.opentrafficsim.road.gtu.lane.tactical.following.GTUFollowingModelOld;
37  import org.opentrafficsim.road.gtu.lane.tactical.lanechangemobil.Egoistic;
38  import org.opentrafficsim.road.gtu.lane.tactical.lanechangemobil.LaneChangeModel;
39  import org.opentrafficsim.road.gtu.strategical.LaneBasedStrategicalPlanner;
40  import org.opentrafficsim.road.gtu.strategical.route.LaneBasedStrategicalRoutePlanner;
41  import org.opentrafficsim.road.network.lane.CrossSectionLink;
42  import org.opentrafficsim.road.network.lane.DirectedLanePosition;
43  import org.opentrafficsim.road.network.lane.Lane;
44  import org.opentrafficsim.road.network.lane.LaneType;
45  import org.opentrafficsim.road.network.lane.changing.LaneKeepingPolicy;
46  import org.opentrafficsim.road.network.lane.changing.OvertakingConditions;
47  
48  import nl.tudelft.simulation.dsol.SimRuntimeException;
49  import nl.tudelft.simulation.dsol.experiment.Experiment;
50  import nl.tudelft.simulation.dsol.experiment.Replication;
51  import nl.tudelft.simulation.dsol.experiment.ReplicationMode;
52  import nl.tudelft.simulation.dsol.experiment.Treatment;
53  import nl.tudelft.simulation.dsol.simtime.SimTimeDoubleUnit;
54  import nl.tudelft.simulation.dsol.simulators.DEVSSimulator;
55  import nl.tudelft.simulation.dsol.simulators.DEVSSimulatorInterface;
56  import nl.tudelft.simulation.dsol.simulators.SimulatorInterface;
57  
58  /**
59   * <p>
60   * Copyright (c) 2013-2018 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()
80              throws NetworkException, SimRuntimeException, NamingException, GTUException, OTSGeometryException
81      {
82          Time initialTime = new Time(0, TimeUnit.BASE_SECOND);
83          GTUType gtuType = CAR;
84          LaneType laneType = LaneType.TWO_WAY_LANE;
85          OTSNetwork network = new OTSNetwork("network");
86          DEVSSimulator.TimeDoubleUnit simulator = makeSimulator();
87          Lane lane = makeLane(network, laneType, simulator);
88          Length initialPosition = new Length(12, METER);
89          Speed initialSpeed = new Speed(34, KM_PER_HOUR);
90          GTUFollowingModelOld gtuFollowingModel =
91                  new FixedAccelerationModel(new Acceleration(0, METER_PER_SECOND_2), new Duration(10, SECOND));
92          LaneChangeModel laneChangeModel = new Egoistic();
93          LaneBasedIndividualGTU referenceCar = makeReferenceCar("12345", gtuType, lane, initialPosition, initialSpeed, simulator,
94                  gtuFollowingModel, laneChangeModel, network);
95          assertEquals("The car should store it's ID", "12345", referenceCar.getId());
96          assertEquals("At t=initialTime the car should be at it's initial position", initialPosition.getSI(),
97                  referenceCar.position(lane, referenceCar.getReference(), initialTime).getSI(), 0.0001);
98          assertEquals("The car should store it's initial speed", initialSpeed.getSI(), referenceCar.getSpeed().getSI(), 0.00001);
99          assertEquals("The car should have an initial acceleration equal to 0", 0, referenceCar.getAcceleration().getSI(),
100                 0.0001);
101         // TODO check with following model as part of tactical planner
102         // assertEquals("The gtu following model should be " + gtuFollowingModel, gtuFollowingModel, referenceCar
103         // .getBehavioralCharacteristics().getGTUFollowingModel());
104         // There is (currently) no way to retrieve the lane change model of a GTU.
105     }
106 
107     /**
108      * Create the simplest possible simulator.
109      * @return DEVSSimulator.TimeDoubleUnit
110      * @throws SimRuntimeException on ???
111      * @throws NamingException on ???
112      */
113     public static DEVSSimulator.TimeDoubleUnit makeSimulator() throws SimRuntimeException, NamingException
114     {
115         DEVSSimulator.TimeDoubleUnit simulator = new DEVSSimulator.TimeDoubleUnit();
116         Model model = new Model();
117         Experiment<Time, Duration, SimTimeDoubleUnit> exp = new Experiment<Time, Duration, SimTimeDoubleUnit>();
118         Treatment<Time, Duration, SimTimeDoubleUnit> tr = new Treatment<>(exp, "tr1",
119                 new SimTimeDoubleUnit(new Time(0, TimeUnit.BASE_SECOND)), new Duration(0, SECOND), new Duration(3600.0, SECOND));
120         exp.setTreatment(tr);
121         exp.setModel(model);
122         Replication<Time, Duration, SimTimeDoubleUnit> rep = new Replication<>(exp);
123         simulator.initialize(rep, ReplicationMode.TERMINATING);
124         return simulator;
125     }
126 
127     /**
128      * Create a new Car.
129      * @param id String; the name (number) of the Car
130      * @param gtuType GTUType; the type of the new car
131      * @param lane Lane; the lane on which the new Car is positioned
132      * @param initialPosition Length; the initial longitudinal position of the new Car
133      * @param initialSpeed Speed; the initial speed
134      * @param simulator OTSDEVVSimulator; the simulator that controls the new Car (and supplies the initial value for
135      *            getLastEvalutionTime())
136      * @param gtuFollowingModel GTUFollowingModel; the GTU following model
137      * @param laneChangeModel LaneChangeModel; the lane change model
138      * @param network the network
139      * @return Car; the new Car
140      * @throws NamingException on network error when making the animation
141      * @throws NetworkException when the GTU cannot be placed on the given lane.
142      * @throws SimRuntimeException when the move method cannot be scheduled.
143      * @throws GTUException when construction of the GTU fails (probably due to an invalid parameter)
144      * @throws OTSGeometryException when the initial path is wrong
145      */
146     public static LaneBasedIndividualGTU makeReferenceCar(final String id, final GTUType gtuType, final Lane lane,
147             final Length initialPosition, final Speed initialSpeed, final DEVSSimulator.TimeDoubleUnit simulator,
148             final GTUFollowingModelOld gtuFollowingModel, final LaneChangeModel laneChangeModel, final OTSNetwork network)
149             throws NamingException, NetworkException, SimRuntimeException, GTUException, OTSGeometryException
150     {
151         Length length = new Length(5.0, METER);
152         Length width = new Length(2.0, METER);
153         Set<DirectedLanePosition> initialLongitudinalPositions = new LinkedHashSet<>(1);
154         initialLongitudinalPositions.add(new DirectedLanePosition(lane, initialPosition, GTUDirectionality.DIR_PLUS));
155         Speed maxSpeed = new Speed(120, KM_PER_HOUR);
156         Parameters parameters = DefaultTestParameters.create();
157         LaneBasedIndividualGTU gtu =
158                 new LaneBasedIndividualGTU(id, gtuType, length, width, maxSpeed, length.multiplyBy(0.5), simulator, network);
159         LaneBasedStrategicalPlanner strategicalPlanner = new LaneBasedStrategicalRoutePlanner(
160                 new LaneBasedCFLCTacticalPlanner(gtuFollowingModel, laneChangeModel, gtu), gtu);
161         gtu.setParameters(parameters);
162         gtu.init(strategicalPlanner, initialLongitudinalPositions, initialSpeed);
163 
164         return gtu;
165     }
166 
167     /**
168      * @param network Network; the network
169      * @param laneType LaneType&lt;String&gt;; the type of the lane
170      * @param simulator DEVSSimulatorInterface.TimeDoubleUnit; simulator
171      * @return a lane of 1000 m long.
172      * @throws NetworkException on network error
173      * @throws OTSGeometryException when center line or contour of a link or lane cannot be generated
174      */
175     public static Lane makeLane(final Network network, final LaneType laneType, final DEVSSimulatorInterface.TimeDoubleUnit simulator) throws NetworkException, OTSGeometryException
176     {
177         OTSNode n1 = new OTSNode(network, "n1", new OTSPoint3D(0, 0));
178         OTSNode n2 = new OTSNode(network, "n2", new OTSPoint3D(100000.0, 0.0));
179         OTSPoint3D[] coordinates = new OTSPoint3D[] { new OTSPoint3D(0.0, 0.0), new OTSPoint3D(100000.0, 0.0) };
180         CrossSectionLink link12 = new CrossSectionLink(network, "link12", n1, n2, LinkType.ROAD, new OTSLine3D(coordinates),
181                 simulator, LaneKeepingPolicy.KEEP_RIGHT);
182         Length latPos = new Length(0.0, METER);
183         Length width = new Length(4.0, METER);
184         return new Lane(link12, "lane.1", latPos, latPos, width, width, laneType, new Speed(100, KM_PER_HOUR),
185                 new OvertakingConditions.LeftAndRight());
186     }
187 
188     /** The helper model. */
189     protected static class Model implements OTSModelInterface
190     {
191         /** */
192         private static final long serialVersionUID = 20141027L;
193 
194         /** The simulator. */
195         private DEVSSimulator.TimeDoubleUnit simulator;
196 
197         /** {@inheritDoc} */
198         @Override
199         public final void constructModel(final SimulatorInterface<Time, Duration, SimTimeDoubleUnit> theSimulator)
200                 throws SimRuntimeException
201         {
202             this.simulator = (DEVSSimulator.TimeDoubleUnit) theSimulator;
203         }
204 
205         /** {@inheritDoc} */
206         @Override
207         public final DEVSSimulator.TimeDoubleUnit getSimulator()
208         {
209             return this.simulator;
210         }
211 
212         /** {@inheritDoc} */
213         @Override
214         public final OTSNetwork getNetwork()
215         {
216             return null;
217         }
218 
219     }
220 }