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