1 package org.opentrafficsim.road.car;
2
3 import static org.junit.Assert.assertEquals;
4 import static org.opentrafficsim.road.gtu.lane.RoadGTUTypes.CAR;
5
6 import java.util.HashSet;
7 import java.util.LinkedHashSet;
8 import java.util.Set;
9
10 import javax.naming.NamingException;
11
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.core.dsol.OTSDEVSSimulator;
20 import org.opentrafficsim.core.dsol.OTSModelInterface;
21 import org.opentrafficsim.core.dsol.OTSSimTimeDouble;
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.gtu.behavioralcharacteristics.BehavioralCharacteristics;
30 import org.opentrafficsim.core.network.LinkType;
31 import org.opentrafficsim.core.network.LongitudinalDirectionality;
32 import org.opentrafficsim.core.network.Network;
33 import org.opentrafficsim.core.network.NetworkException;
34 import org.opentrafficsim.core.network.OTSNetwork;
35 import org.opentrafficsim.core.network.OTSNode;
36 import org.opentrafficsim.road.DefaultTestParameters;
37 import org.opentrafficsim.road.gtu.lane.LaneBasedIndividualGTU;
38 import org.opentrafficsim.road.gtu.lane.tactical.LaneBasedCFLCTacticalPlanner;
39 import org.opentrafficsim.road.gtu.lane.tactical.following.FixedAccelerationModel;
40 import org.opentrafficsim.road.gtu.lane.tactical.following.GTUFollowingModelOld;
41 import org.opentrafficsim.road.gtu.lane.tactical.lanechangemobil.Egoistic;
42 import org.opentrafficsim.road.gtu.lane.tactical.lanechangemobil.LaneChangeModel;
43 import org.opentrafficsim.road.gtu.strategical.LaneBasedStrategicalPlanner;
44 import org.opentrafficsim.road.gtu.strategical.route.LaneBasedStrategicalRoutePlanner;
45 import org.opentrafficsim.road.network.lane.CrossSectionLink;
46 import org.opentrafficsim.road.network.lane.DirectedLanePosition;
47 import org.opentrafficsim.road.network.lane.Lane;
48 import org.opentrafficsim.road.network.lane.LaneType;
49 import org.opentrafficsim.road.network.lane.changing.LaneKeepingPolicy;
50 import org.opentrafficsim.road.network.lane.changing.OvertakingConditions;
51
52 import mockit.MockUp;
53 import nl.tudelft.simulation.dsol.SimRuntimeException;
54 import nl.tudelft.simulation.dsol.experiment.Experiment;
55 import nl.tudelft.simulation.dsol.experiment.Replication;
56 import nl.tudelft.simulation.dsol.experiment.ReplicationMode;
57 import nl.tudelft.simulation.dsol.experiment.Treatment;
58 import nl.tudelft.simulation.dsol.simulators.SimulatorInterface;
59
60
61
62
63
64
65
66
67
68
69 public class CarTest implements UNITS
70 {
71
72
73
74
75
76
77
78
79 @SuppressWarnings("static-method")
80 @Test
81 public final void carTest()
82 throws NetworkException, SimRuntimeException, NamingException, GTUException, OTSGeometryException
83 {
84 Time initialTime = new Time(0, SECOND);
85 GTUType gtuType = CAR;
86 Set<GTUType> compatibility = new HashSet<GTUType>();
87 compatibility.add(gtuType);
88 LaneType laneType = new LaneType("CarLane", compatibility);
89 OTSNetwork network = new OTSNetwork("network");
90 Lane lane = makeLane(network, laneType);
91 Length initialPosition = new Length(12, METER);
92 Speed initialSpeed = new Speed(34, KM_PER_HOUR);
93 OTSDEVSSimulator simulator = makeSimulator();
94 GTUFollowingModelOld gtuFollowingModel =
95 new FixedAccelerationModel(new Acceleration(0, METER_PER_SECOND_2), new Duration(10, SECOND));
96 LaneChangeModel laneChangeModel = new Egoistic();
97 LaneBasedIndividualGTU referenceCar = makeReferenceCar("12345", gtuType, lane, initialPosition, initialSpeed, simulator,
98 gtuFollowingModel, 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(),
101 referenceCar.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
106
107
108
109 }
110
111
112
113
114
115
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 = new Experiment<Time, Duration, OTSSimTimeDouble>();
122 Treatment<Time, Duration, OTSSimTimeDouble> tr = new Treatment<>(exp, "tr1", new OTSSimTimeDouble(new Time(0, SECOND)),
123 new Duration(0, SECOND), new Duration(3600.0, SECOND));
124 exp.setTreatment(tr);
125 exp.setModel(model);
126 Replication<Time, Duration, OTSSimTimeDouble> rep = new Replication<>(exp);
127 simulator.initialize(rep, ReplicationMode.TERMINATING);
128 return simulator;
129 }
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150 public static LaneBasedIndividualGTU makeReferenceCar(final String id, final GTUType gtuType, final Lane lane,
151 final Length initialPosition, final Speed initialSpeed, final OTSDEVSSimulator simulator,
152 final GTUFollowingModelOld gtuFollowingModel, final LaneChangeModel laneChangeModel, final OTSNetwork network)
153 throws NamingException, NetworkException, SimRuntimeException, GTUException, OTSGeometryException
154 {
155 Length length = new Length(5.0, METER);
156 Length width = new Length(2.0, METER);
157 Set<DirectedLanePosition> initialLongitudinalPositions = new LinkedHashSet<>(1);
158 initialLongitudinalPositions.add(new DirectedLanePosition(lane, initialPosition, GTUDirectionality.DIR_PLUS));
159 Speed maxSpeed = new Speed(120, KM_PER_HOUR);
160 BehavioralCharacteristics behavioralCharacteristics = DefaultTestParameters.create();
161 LaneBasedIndividualGTU gtu = new LaneBasedIndividualGTU(id, gtuType, length, width, maxSpeed, simulator, network);
162 LaneBasedStrategicalPlanner strategicalPlanner = new LaneBasedStrategicalRoutePlanner(behavioralCharacteristics,
163 new LaneBasedCFLCTacticalPlanner(gtuFollowingModel, laneChangeModel, gtu), gtu);
164 gtu.init(strategicalPlanner, initialLongitudinalPositions, initialSpeed);
165
166 return gtu;
167 }
168
169
170
171
172
173
174
175
176 public static Lane makeLane(final Network network, final LaneType laneType) throws NetworkException, OTSGeometryException
177 {
178 OTSSimulatorInterface simulator = new MockUp<OTSSimulatorInterface>()
179 {
180
181 }.getMockInstance();
182 OTSNode n1 = new OTSNode(network, "n1", new OTSPoint3D(0, 0));
183 OTSNode n2 = new OTSNode(network, "n2", new OTSPoint3D(100000.0, 0.0));
184 OTSPoint3D[] coordinates = new OTSPoint3D[] { new OTSPoint3D(0.0, 0.0), new OTSPoint3D(100000.0, 0.0) };
185 CrossSectionLink link12 = new CrossSectionLink(network, "link12", n1, n2, LinkType.ALL, new OTSLine3D(coordinates),
186 simulator, 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
194 protected static class Model implements OTSModelInterface
195 {
196
197 private static final long serialVersionUID = 20141027L;
198
199
200 private OTSDEVSSimulator simulator;
201
202
203 @Override
204 public final void constructModel(final SimulatorInterface<Time, Duration, OTSSimTimeDouble> theSimulator)
205 throws SimRuntimeException
206 {
207 this.simulator = (OTSDEVSSimulator) theSimulator;
208 }
209
210
211 @Override
212 public final OTSDEVSSimulator getSimulator()
213 {
214 return this.simulator;
215 }
216
217
218 @Override
219 public OTSNetwork getNetwork()
220 {
221 return null;
222 }
223
224 }
225 }