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