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.NetworkException;
40 import org.opentrafficsim.core.network.OTSNetwork;
41 import org.opentrafficsim.core.network.OTSNode;
42 import org.opentrafficsim.road.DefaultTestParameters;
43 import org.opentrafficsim.road.gtu.lane.LaneBasedIndividualGTU;
44 import org.opentrafficsim.road.gtu.lane.tactical.LaneBasedCFLCTacticalPlanner;
45 import org.opentrafficsim.road.gtu.lane.tactical.following.FixedAccelerationModel;
46 import org.opentrafficsim.road.gtu.lane.tactical.following.GTUFollowingModelOld;
47 import org.opentrafficsim.road.gtu.lane.tactical.lanechangemobil.Egoistic;
48 import org.opentrafficsim.road.gtu.lane.tactical.lanechangemobil.LaneChangeModel;
49 import org.opentrafficsim.road.gtu.strategical.LaneBasedStrategicalPlanner;
50 import org.opentrafficsim.road.gtu.strategical.route.LaneBasedStrategicalRoutePlanner;
51 import org.opentrafficsim.road.network.lane.CrossSectionLink;
52 import org.opentrafficsim.road.network.lane.DirectedLanePosition;
53 import org.opentrafficsim.road.network.lane.Lane;
54 import org.opentrafficsim.road.network.lane.LaneType;
55 import org.opentrafficsim.road.network.lane.changing.LaneKeepingPolicy;
56 import org.opentrafficsim.road.network.lane.changing.OvertakingConditions;
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() throws NetworkException, SimRuntimeException, NamingException, GTUException,
80 OTSGeometryException
81 {
82 Time initialTime = new Time(0, SECOND);
83 GTUType gtuType = new GTUType("Car");
84 Set<GTUType> compatibility = new HashSet<GTUType>();
85 compatibility.add(gtuType);
86 LaneType laneType = new LaneType("CarLane", compatibility);
87 OTSNetwork network = new OTSNetwork("network");
88 Lane lane = makeLane(laneType);
89 Length initialPosition = new Length(12, METER);
90 Speed initialSpeed = new Speed(34, KM_PER_HOUR);
91 OTSDEVSSimulator simulator = makeSimulator();
92 GTUFollowingModelOld gtuFollowingModel =
93 new FixedAccelerationModel(new Acceleration(0, METER_PER_SECOND_2), new Duration(10, SECOND));
94 LaneChangeModel laneChangeModel = new Egoistic();
95 LaneBasedIndividualGTU referenceCar =
96 makeReferenceCar("12345", gtuType, lane, initialPosition, initialSpeed, simulator, gtuFollowingModel,
97 laneChangeModel, network);
98 assertEquals("The car should store it's ID", "12345", referenceCar.getId());
99 assertEquals("At t=initialTime the car should be at it's initial position", initialPosition.getSI(), referenceCar
100 .position(lane, referenceCar.getReference(), initialTime).getSI(), 0.0001);
101 assertEquals("The car should store it's initial speed", initialSpeed.getSI(), referenceCar.getSpeed().getSI(), 0.00001);
102 assertEquals("The car should have an initial acceleration equal to 0", 0, referenceCar.getAcceleration().getSI(),
103 0.0001);
104
105
106
107
108 }
109
110
111
112
113
114
115
116 public static OTSDEVSSimulator makeSimulator() throws SimRuntimeException, NamingException
117 {
118 OTSDEVSSimulator simulator = new OTSDEVSSimulator();
119 Model model = new Model();
120 Experiment<DoubleScalar.Abs<TimeUnit>, DoubleScalar.Rel<TimeUnit>, OTSSimTimeDouble> exp =
121 new Experiment<DoubleScalar.Abs<TimeUnit>, DoubleScalar.Rel<TimeUnit>, OTSSimTimeDouble>();
122 Treatment<DoubleScalar.Abs<TimeUnit>, DoubleScalar.Rel<TimeUnit>, OTSSimTimeDouble> tr =
123 new Treatment<>(exp, "tr1", new OTSSimTimeDouble(new Time(0, SECOND)), new Duration(0, SECOND), new Duration(
124 3600.0, SECOND));
125 exp.setTreatment(tr);
126 exp.setModel(model);
127 Replication<DoubleScalar.Abs<TimeUnit>, DoubleScalar.Rel<TimeUnit>, OTSSimTimeDouble> rep = new Replication<>(exp);
128 simulator.initialize(rep, ReplicationMode.TERMINATING);
129 return simulator;
130 }
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151 public static LaneBasedIndividualGTU makeReferenceCar(final String id, final GTUType gtuType, final Lane lane,
152 final Length initialPosition, final Speed initialSpeed, final OTSDEVSSimulator simulator,
153 final GTUFollowingModelOld gtuFollowingModel, final LaneChangeModel laneChangeModel, final OTSNetwork network)
154 throws NamingException, NetworkException, SimRuntimeException, GTUException, OTSGeometryException
155 {
156 Length length = new Length(5.0, METER);
157 Length width = new Length(2.0, METER);
158 Set<DirectedLanePosition> initialLongitudinalPositions = new LinkedHashSet<>(1);
159 initialLongitudinalPositions.add(new DirectedLanePosition(lane, initialPosition, GTUDirectionality.DIR_PLUS));
160 Speed maxSpeed = new Speed(120, KM_PER_HOUR);
161 BehavioralCharacteristics behavioralCharacteristics = DefaultTestParameters.create();
162 LaneBasedIndividualGTU gtu = new LaneBasedIndividualGTU(id, gtuType, length, width, maxSpeed, simulator, network);
163 LaneBasedStrategicalPlanner strategicalPlanner =
164 new LaneBasedStrategicalRoutePlanner(behavioralCharacteristics, new LaneBasedCFLCTacticalPlanner(
165 gtuFollowingModel, laneChangeModel, gtu), gtu);
166 gtu.init(strategicalPlanner, initialLongitudinalPositions, initialSpeed);
167
168 return gtu;
169 }
170
171
172
173
174
175
176
177 public static Lane makeLane(final LaneType laneType) throws NetworkException, OTSGeometryException
178 {
179 OTSNode n1 = new OTSNode("n1", new OTSPoint3D(0, 0));
180 OTSNode n2 = new OTSNode("n2", new OTSPoint3D(100000.0, 0.0));
181 OTSPoint3D[] coordinates = new OTSPoint3D[] { new OTSPoint3D(0.0, 0.0), new OTSPoint3D(100000.0, 0.0) };
182 CrossSectionLink link12 =
183 new CrossSectionLink("link12", n1, n2, LinkType.ALL, new OTSLine3D(coordinates),
184 LongitudinalDirectionality.DIR_PLUS, LaneKeepingPolicy.KEEP_RIGHT);
185 Length latPos = new Length(0.0, METER);
186 Length width = new Length(4.0, METER);
187 return new Lane(link12, "lane.1", latPos, latPos, width, width, laneType, LongitudinalDirectionality.DIR_PLUS,
188 new Speed(100, KM_PER_HOUR), new OvertakingConditions.LeftAndRight());
189 }
190
191
192 protected static class Model implements OTSModelInterface
193 {
194
195 private static final long serialVersionUID = 20141027L;
196
197
198 private OTSDEVSSimulator simulator;
199
200
201 @Override
202 public final void constructModel(
203 final SimulatorInterface<DoubleScalar.Abs<TimeUnit>, DoubleScalar.Rel<TimeUnit>, OTSSimTimeDouble> theSimulator)
204 throws SimRuntimeException
205 {
206 this.simulator = (OTSDEVSSimulator) theSimulator;
207 }
208
209
210 @Override
211 public final OTSDEVSSimulator getSimulator()
212 {
213 return this.simulator;
214 }
215
216 }
217 }