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