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
60
61
62
63
64
65
66
67 public class CarTest implements UNITS
68 {
69
70
71
72
73
74
75
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
102
103
104
105 }
106
107
108
109
110
111
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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
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
169
170
171
172
173
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
189 protected static class Model implements OTSModelInterface
190 {
191
192 private static final long serialVersionUID = 20141027L;
193
194
195 private DEVSSimulator.TimeDoubleUnit simulator;
196
197
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
206 @Override
207 public final DEVSSimulator.TimeDoubleUnit getSimulator()
208 {
209 return this.simulator;
210 }
211
212
213 @Override
214 public final OTSNetwork getNetwork()
215 {
216 return null;
217 }
218
219 }
220 }