1 package org.opentrafficsim.graphs;
2
3 import static org.junit.Assert.assertEquals;
4
5 import java.util.ArrayList;
6 import java.util.HashMap;
7 import java.util.List;
8 import java.util.Map;
9
10 import org.djunits.unit.SpeedUnit;
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.jfree.data.DomainOrder;
19 import org.junit.Test;
20 import org.opentrafficsim.core.geometry.OTSPoint3D;
21 import org.opentrafficsim.core.gtu.GTUException;
22 import org.opentrafficsim.core.network.OTSNetwork;
23 import org.opentrafficsim.core.network.OTSNode;
24 import org.opentrafficsim.road.car.CarTest;
25 import org.opentrafficsim.road.gtu.lane.LaneBasedIndividualGTU;
26 import org.opentrafficsim.road.gtu.lane.tactical.following.FixedAccelerationModel;
27 import org.opentrafficsim.road.gtu.lane.tactical.following.GTUFollowingModelOld;
28 import org.opentrafficsim.road.gtu.lane.tactical.lanechangemobil.Egoistic;
29 import org.opentrafficsim.road.gtu.lane.tactical.lanechangemobil.LaneChangeModel;
30 import org.opentrafficsim.road.network.factory.LaneFactory;
31 import org.opentrafficsim.road.network.lane.Lane;
32 import org.opentrafficsim.road.network.lane.LaneType;
33
34 import nl.tudelft.simulation.dsol.simulators.DEVSSimulator;
35
36
37
38
39
40
41
42
43
44
45 public class TrajectoryPlotTest implements UNITS
46 {
47
48 Duration sampleInterval = new Duration(0.25, SECOND);
49
50
51
52
53
54 @Test
55 public final void trajectoryTest() throws Exception
56 {
57 DEVSSimulator.TimeDoubleUnit simulator = CarTest.makeSimulator();
58 LaneType laneType = LaneType.TWO_WAY_LANE;
59 OTSNetwork network = new OTSNetwork("trajectory plot test network");
60 OTSNode node1 = new OTSNode(network, "node 1", new OTSPoint3D(100, 100, 0));
61 OTSNode node2 = new OTSNode(network, "node 2", new OTSPoint3D(1100, 100, 0));
62 OTSNode node3 = new OTSNode(network, "node 3", new OTSPoint3D(10100, 100, 0));
63 List<Lane> trajectory = new ArrayList<Lane>();
64 Speed speedLimit = new Speed(50, SpeedUnit.KM_PER_HOUR);
65 Lane lane1 = LaneFactory.makeMultiLane(network, "12", node1, node2, null, 1, 0, 0, laneType, speedLimit, simulator)[0];
66 trajectory.add(lane1);
67 Lane lane2 = LaneFactory.makeMultiLane(network, "23", node2, node3, null, 1, 0, 0, laneType, speedLimit, simulator)[0];
68 trajectory.add(lane2);
69 TrajectoryPlot tp = new TrajectoryPlot("TestTrajectory", this.sampleInterval, trajectory, simulator);
70 assertEquals("Number of trajectories should initially be 0", 0, tp.getSeriesCount());
71 for (int i = -10; i <= 10; i++)
72 {
73 assertEquals("SeriesKey(" + i + ") should return " + i, i, tp.getSeriesKey(i));
74 }
75 assertEquals("Domain order should be ASCENDING", DomainOrder.ASCENDING, tp.getDomainOrder());
76
77 Length initialPosition = new Length(200, METER);
78 Speed initialSpeed = new Speed(50, KM_PER_HOUR);
79 Length length = new Length(5.0, METER);
80 Length width = new Length(2.0, METER);
81 Map<Lane, Length> initialLongitudinalPositions = new HashMap<>();
82 initialLongitudinalPositions.put(lane1, initialPosition);
83
84 simulator.runUpTo(new Time(100, TimeUnit.BASE_SECOND));
85 Speed maxSpeed = new Speed(120, KM_PER_HOUR);
86 GTUFollowingModelOld gtuFollowingModel =
87 new FixedAccelerationModel(new Acceleration(0, METER_PER_SECOND_2), new Duration(10, SECOND));
88 LaneChangeModel laneChangeModel = new Egoistic();
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192 }
193
194
195
196
197
198
199
200
201 private void verifyTrajectory(final LaneBasedIndividualGTU car, final int series, final TrajectoryPlot tp)
202 throws GTUException
203 {
204
205 Lane lane = car.positions(car.getFront()).keySet().iterator().next();
206 Time initialTime = car.getOperationalPlan().getStartTime();
207 Duration duration = car.getOperationalPlan().getTotalDuration();
208 int expectedNumberOfSamples = (int) (duration.getSI() / this.sampleInterval.getSI());
209 assertEquals("Number of samples in trajectory should be ", expectedNumberOfSamples, tp.getItemCount(series));
210
211 for (int sample = 0; sample < expectedNumberOfSamples; sample++)
212 {
213 Duration deltaTime = new Duration(this.sampleInterval.getSI() * sample, SECOND);
214 Time sampleTime = initialTime.plus(deltaTime);
215 double sampledTime = tp.getXValue(series, sample);
216 assertEquals("Sample should have been taken at " + sampleTime, sampleTime.getSI(), sampledTime, 0.0001);
217 sampledTime = tp.getX(series, sample).doubleValue();
218 assertEquals("Sample should have been taken at " + sampleTime, sampleTime.getSI(), sampledTime, 0.0001);
219 Length actualPosition = car.position(lane, car.getFront(), sampleTime);
220 double sampledPosition = tp.getYValue(series, sample);
221 assertEquals("Sample position should have been " + actualPosition, actualPosition.getSI(), sampledPosition, 0.0001);
222 sampledPosition = tp.getY(series, sample).doubleValue();
223 assertEquals("Sample position should have been " + actualPosition, actualPosition.getSI(), sampledPosition, 0.0001);
224 }
225 }
226
227 }