View Javadoc
1   package org.opentrafficsim.road.car;
2   
3   import static org.junit.Assert.assertEquals;
4   
5   import java.io.Serializable;
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.util.UNITS;
14  import org.djunits.value.vdouble.scalar.Acceleration;
15  import org.djunits.value.vdouble.scalar.Direction;
16  import org.djunits.value.vdouble.scalar.Duration;
17  import org.djunits.value.vdouble.scalar.Length;
18  import org.djunits.value.vdouble.scalar.Speed;
19  import org.djunits.value.vdouble.scalar.Time;
20  import org.junit.Test;
21  import org.opentrafficsim.base.parameters.Parameters;
22  import org.opentrafficsim.core.dsol.AbstractOTSModel;
23  import org.opentrafficsim.core.dsol.OTSSimulator;
24  import org.opentrafficsim.core.dsol.OTSSimulatorInterface;
25  import org.opentrafficsim.core.geometry.OTSGeometryException;
26  import org.opentrafficsim.core.geometry.OTSLine3D;
27  import org.opentrafficsim.core.geometry.OTSPoint3D;
28  import org.opentrafficsim.core.gtu.GTUDirectionality;
29  import org.opentrafficsim.core.gtu.GTUException;
30  import org.opentrafficsim.core.gtu.GTUType;
31  import org.opentrafficsim.core.network.LinkType;
32  import org.opentrafficsim.core.network.NetworkException;
33  import org.opentrafficsim.road.DefaultTestParameters;
34  import org.opentrafficsim.road.gtu.lane.LaneBasedIndividualGTU;
35  import org.opentrafficsim.road.gtu.lane.tactical.LaneBasedCFLCTacticalPlanner;
36  import org.opentrafficsim.road.gtu.lane.tactical.following.FixedAccelerationModel;
37  import org.opentrafficsim.road.gtu.lane.tactical.following.GTUFollowingModelOld;
38  import org.opentrafficsim.road.gtu.lane.tactical.lanechangemobil.Egoistic;
39  import org.opentrafficsim.road.gtu.lane.tactical.lanechangemobil.LaneChangeModel;
40  import org.opentrafficsim.road.gtu.strategical.LaneBasedStrategicalPlanner;
41  import org.opentrafficsim.road.gtu.strategical.route.LaneBasedStrategicalRoutePlanner;
42  import org.opentrafficsim.road.network.OTSRoadNetwork;
43  import org.opentrafficsim.road.network.RoadNetwork;
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.OTSRoadNode;
49  import org.opentrafficsim.road.network.lane.changing.LaneKeepingPolicy;
50  
51  import nl.tudelft.simulation.dsol.SimRuntimeException;
52  
53  /**
54   * <p>
55   * Copyright (c) 2013-2020 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          OTSRoadNetwork network = new OTSRoadNetwork("network", true);
79          GTUType gtuType = network.getGtuType(GTUType.DEFAULTS.CAR);
80          LaneType laneType = network.getLaneType(LaneType.DEFAULTS.TWO_WAY_LANE);
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("CarTest");
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 OTSRoadNetwork 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.times(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 RoadNetwork; 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 RoadNetwork network, final LaneType laneType, final OTSSimulatorInterface simulator)
165             throws NetworkException, OTSGeometryException
166     {
167         OTSRoadNode n1 = new OTSRoadNode(network, "n1", new OTSPoint3D(0, 0), Direction.ZERO);
168         OTSRoadNode n2 = new OTSRoadNode(network, "n2", new OTSPoint3D(100000.0, 0.0), Direction.ZERO);
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, network.getLinkType(LinkType.DEFAULTS.ROAD),
171                 new OTSLine3D(coordinates), simulator, LaneKeepingPolicy.KEEPRIGHT);
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     }
176 
177     /** The helper model. */
178     protected static class Model extends AbstractOTSModel
179     {
180         /** */
181         private static final long serialVersionUID = 20141027L;
182 
183         /**
184          * @param simulator the simulator to use
185          */
186         public Model(final OTSSimulatorInterface simulator)
187         {
188             super(simulator);
189         }
190 
191         /** {@inheritDoc} */
192         @Override
193         public final void constructModel() throws SimRuntimeException
194         {
195             //
196         }
197 
198         /** {@inheritDoc} */
199         @Override
200         public final OTSRoadNetwork getNetwork()
201         {
202             return null;
203         }
204 
205         /** {@inheritDoc} */
206         @Override
207         public Serializable getSourceId()
208         {
209             return "CarTestModel";
210         }
211     }
212 }