/
RoadSkill.java
403 lines (365 loc) · 12.5 KB
/
RoadSkill.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
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
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
/*******************************************************************************************************
*
* simtools.gaml.extensions.traffic.RoadSkill.java, in plugin simtools.gaml.extensions.traffic, is part of the source
* code of the GAMA modeling and simulation platform (v. 1.8.1)
*
* (c) 2007-2020 UMI 209 UMMISCO IRD/SU & Partners
*
* Visit https://github.com/gama-platform/gama for license information and contacts.
*
********************************************************************************************************/
package simtools.gaml.extensions.traffic;
import java.util.Collections;
import java.util.Comparator;
import java.util.LinkedList;
import java.util.List;
import java.util.Set;
import org.apache.commons.collections4.OrderedBidiMap;
import org.locationtech.jts.geom.Coordinate;
import org.locationtech.jts.geom.Geometry;
import msi.gama.common.geometry.GeometryUtils;
import msi.gama.common.interfaces.IKeyword;
import msi.gama.metamodel.agent.IAgent;
import msi.gama.metamodel.shape.GamaPoint;
import msi.gama.precompiler.GamlAnnotations.action;
import msi.gama.precompiler.GamlAnnotations.arg;
import msi.gama.precompiler.GamlAnnotations.doc;
import msi.gama.precompiler.GamlAnnotations.example;
import msi.gama.precompiler.GamlAnnotations.getter;
import msi.gama.precompiler.GamlAnnotations.setter;
import msi.gama.precompiler.GamlAnnotations.skill;
import msi.gama.precompiler.GamlAnnotations.variable;
import msi.gama.precompiler.GamlAnnotations.vars;
import msi.gama.precompiler.IConcept;
import msi.gama.precompiler.ITypeProvider;
import msi.gama.runtime.GAMA;
import msi.gama.runtime.IScope;
import msi.gama.runtime.exceptions.GamaRuntimeException;
import msi.gama.util.GamaList;
import msi.gama.util.GamaListFactory;
import msi.gama.util.IList;
import msi.gaml.operators.Containers;
import msi.gaml.skills.Skill;
import msi.gaml.types.IType;
import msi.gaml.types.Types;
import simtools.gaml.extensions.traffic.carfollowing.CustomDualTreeBidiMap;
@vars({
@variable(
name = RoadSkill.AGENTS_ON,
type = IType.LIST,
of = IType.LIST,
doc = @doc("for each lane of the road, the list of agents for each segment")
),
@variable(
name = RoadSkill.ALL_AGENTS,
type = IType.LIST,
of = IType.AGENT,
doc = @doc("the list of agents on the road")
),
@variable(
name = RoadSkill.SOURCE_NODE,
type = IType.AGENT,
doc = @doc("the source node of the road")
),
@variable(
name = RoadSkill.TARGET_NODE,
type = IType.AGENT,
doc = @doc("the target node of the road")
),
@variable(
name = RoadSkill.NUM_LANES,
type = IType.INT,
init = "2",
doc = @doc("the number of lanes")
),
@variable(
name = RoadSkill.NUM_SEGMENTS,
type = IType.INT,
doc = @doc("the number of road segments")
),
@variable(
name = RoadSkill.LINKED_ROAD,
type = ITypeProvider.OWNER_TYPE,
doc = @doc("the linked road: the lanes of this linked road will be usable by drivers on the road")
),
@variable(
name = RoadSkill.MAXSPEED,
type = IType.FLOAT,
init = "50#km/#h",
doc = @doc("the maximal speed on the road")
),
@variable(
name = RoadSkill.SEGMENT_LENGTHS,
type = IType.LIST,
doc = @doc("stores the length of each road segment. " +
"The index of each element corresponds to the segment index.")
),
@variable(
name = RoadSkill.VEHICLE_ORDERING,
type = IType.LIST,
depends_on = {RoadSkill.NUM_LANES},
doc = @doc("provides information about the ordering of vehicle on any given lane")
),
})
@skill(
name = RoadSkill.SKILL_ROAD,
concept = { IConcept.TRANSPORT, IConcept.SKILL },
doc = @doc ("A skill for agents representing roads in traffic simulations")
)
@SuppressWarnings ({ "unchecked", "rawtypes" })
public class RoadSkill extends Skill {
public static final String SKILL_ROAD = "skill_road";
// TODO: rename these two lists?
public static final String ALL_AGENTS = "all_agents";
public static final String AGENTS_ON = "agents_on";
public static final String SOURCE_NODE = "source_node";
public static final String TARGET_NODE = "target_node";
// TODO: rename to speed_limit
public static final String MAXSPEED = "maxspeed";
public static final String LINKED_ROAD = "linked_road";
@Deprecated public static final String LANES = "lanes";
public static final String NUM_LANES = "num_lanes";
public static final String NUM_SEGMENTS = "num_segments";
public static final String SEGMENT_LENGTHS = "segment_lengths";
public static final String VEHICLE_ORDERING = "vehicle_ordering";
@getter(AGENTS_ON)
public static List getAgentsOn(final IAgent agent) {
return (List) agent.getAttribute(AGENTS_ON);
}
@setter(AGENTS_ON)
public static void setAgentsOn(final IAgent agent, final List agents) {
agent.setAttribute(AGENTS_ON, agents);
}
@getter(ALL_AGENTS)
public static IList<IAgent> getAgents(final IAgent agent) {
IList<IAgent> res = GamaListFactory.create(Types.AGENT);
for (OrderedBidiMap<IAgent, Double> map : getVehicleOrdering(agent)) {
res.addAll(map.keySet());
}
return Containers.remove_duplicates(GAMA.getRuntimeScope(), res);
}
@setter(ALL_AGENTS)
public static void setAgents(final IAgent agent, final List agents) {
// read-only
}
@getter(SOURCE_NODE)
public static IAgent getSourceNode(final IAgent agent) {
return (IAgent) agent.getAttribute(SOURCE_NODE);
}
@setter(SOURCE_NODE)
public static void setSourceNode(final IAgent agent, final IAgent nd) {
agent.setAttribute(SOURCE_NODE, nd);
}
@getter(TARGET_NODE)
public static IAgent getTargetNode(final IAgent agent) {
return (IAgent) agent.getAttribute(TARGET_NODE);
}
@setter(TARGET_NODE)
public void setTargetNode(final IAgent agent, final IAgent nd) {
agent.setAttribute(TARGET_NODE, nd);
}
@getter(NUM_LANES)
public static int getNumLanes(final IAgent agent) {
return (int) agent.getAttribute(NUM_LANES);
}
@setter(NUM_LANES)
public static void setNumLanes(final IAgent agent, final int numLanes) {
if (numLanes == 0) {
throw GamaRuntimeException.error(agent.getName() + " has zero lanes",
GAMA.getRuntimeScope());
}
agent.setAttribute(NUM_LANES, numLanes);
}
public static int getNumLanesTotal(final IAgent road) {
IAgent linkedRoad = getLinkedRoad(road);
int numLanesLinked = linkedRoad != null ? getNumLanes(linkedRoad) : 0;
return getNumLanes(road) + numLanesLinked;
}
@getter(MAXSPEED)
public static Double getMaxSpeed(final IAgent agent) {
return (Double) agent.getAttribute(MAXSPEED);
}
@setter(MAXSPEED)
public static void setMaxSpeed(final IAgent agent, final Double sp) {
agent.setAttribute(MAXSPEED, sp);
}
@getter(LINKED_ROAD)
public static IAgent getLinkedRoad(final IAgent agent) {
return (IAgent) agent.getAttribute(LINKED_ROAD);
}
@setter(LINKED_ROAD)
public static void setLinkedRoad(final IAgent agent, final IAgent rd) {
agent.setAttribute(LINKED_ROAD, rd);
}
@getter(NUM_SEGMENTS)
public static int getNumSegments(final IAgent road) {
return GeometryUtils.getPointsOf(road).length - 1;
}
@setter(NUM_SEGMENTS)
public static void setNumSegments(final IAgent road, final int numSegments) {
// read-only
}
@getter(value = SEGMENT_LENGTHS)
public static List<Double> getSegmentLengths(final IAgent road) {
List<Double> res = (List<Double>) road.getAttribute(SEGMENT_LENGTHS);
if (res.isEmpty()) {
Geometry geom = road.getInnerGeometry();
if (road.getInnerGeometry() == null) {
throw GamaRuntimeException.error(
"The shape of the road has not been initialized",
GAMA.getRuntimeScope());
}
Coordinate[] coords = geom.getCoordinates();
for (int i = 0; i < coords.length - 1; i += 1) {
res.add(coords[i].distance(coords[i + 1]));
}
}
return res;
}
public static double getTotalLength(final IAgent road) {
List<Double> lengths = getSegmentLengths(road);
return lengths.stream().reduce(0.0, Double::sum);
}
@getter(VEHICLE_ORDERING)
public static List<OrderedBidiMap<IAgent, Double>> getVehicleOrdering(final IAgent road) {
List<OrderedBidiMap<IAgent, Double>> res =
(List<OrderedBidiMap<IAgent, Double>>) road.getAttribute(VEHICLE_ORDERING);
if (res.isEmpty()) {
for (int i = 0; i < getNumLanes(road); i += 1) {
res.add(
new CustomDualTreeBidiMap<IAgent, Double>(new Comparator<IAgent>() {
@Override
public int compare(IAgent a, IAgent b) {
int r = a.getSpeciesName().compareTo(b.getSpeciesName());
if (r != 0) {
return r;
} else {
return Integer.compare(a.getIndex(), b.getIndex());
}
}
}, Collections.reverseOrder())
);
}
}
return res;
}
@setter(VEHICLE_ORDERING)
public static void setVehicleOrdering(final IAgent road, List<OrderedBidiMap<IAgent, Double>> list) {
road.setAttribute(VEHICLE_ORDERING, list);
}
//TODO: update doc string
/**
* Converts the "overflow" lane to the correct lane index on the linked road.
* Simply returns the input lane if it is already a valid lane on the current road.
* Computes the correct segment index on the linked road.
* @param scope
* @param lane the input lane index
* @return
*/
public static OrderedBidiMap<IAgent, Double> getVehiclesOnLaneSegment(final IScope scope,
final IAgent correctRoad,
final int lane) {
int numLanesTotal = getNumLanesTotal(correctRoad);
if (lane >= numLanesTotal) {
String msg = String.format(
"Trying to access lane with index %d on road %s, which only have %d valid lanes",
lane, correctRoad.getName(), numLanesTotal);
throw GamaRuntimeException.error(msg, scope);
}
int numLanesCorrect = getNumLanes(correctRoad);
IAgent actualRoad;
int actualLane;
if (lane < numLanesCorrect) {
actualRoad = correctRoad;
actualLane = lane;
} else {
IAgent linkedRoad = getLinkedRoad(correctRoad);
int numLanesLinked = getNumLanes(linkedRoad);
actualRoad = linkedRoad;
actualLane = numLanesCorrect + numLanesLinked - 1 - lane;
}
return getVehicleOrdering(actualRoad).get(actualLane);
}
@action(
name = "register",
args = {
@arg(
name = "agent",
type = IType.AGENT,
optional = false,
doc = @doc("the agent to register on the road.")
),
@arg(
name = "lane",
type = IType.INT,
optional = false,
doc = @doc("the lane index on which to register; if lane index >= number of lanes, then register on the linked road")
)
},
doc = @doc(
value = "register the agent on the road at the given lane",
examples = { @example ("do register agent: the_driver lane: 0") }
)
)
public void primRegister(final IScope scope) throws GamaRuntimeException {
final IAgent road = getCurrentAgent(scope);
final IAgent driver = (IAgent) scope.getArg("agent", IType.AGENT);
int lane = scope.getIntArg("lane");
register(scope, driver, road, lane);
}
/**
* Registers the driver on the specified road and starting lane.
*
* @param scope
* @param vehicle the agent to register
* @param road the new road
* @param lowestLane the new starting lane
*
* @throws GamaRuntimeException
*/
public static void register(IScope scope, IAgent vehicle, IAgent road, int lowestLane)
throws GamaRuntimeException {
if (vehicle == null) return;
int numLanesOccupied = DrivingSkill.getNumLanesOccupied(vehicle);
int numSegments = getNumSegments(road);
List<Double> lengths = getSegmentLengths(road);
GamaPoint roadEndPt = (GamaPoint) getTargetNode(road).getLocation();
boolean violatingOneway = !DrivingSkill.getCurrentTarget(vehicle).getLocation().equals(roadEndPt);
int segmentIdx = !violatingOneway ? 0 : getNumSegments(road) - 1;
for (int i = 0; i < numLanesOccupied; i += 1) {
int lane = lowestLane + i;
getVehiclesOnLaneSegment(scope, road, lane).put(vehicle, getTotalLength(road));
}
DrivingSkill.setViolatingOneway(vehicle, violatingOneway);
if (!violatingOneway) {
DrivingSkill.setDistanceToGoal(vehicle, lengths.get(0));
} else {
DrivingSkill.setDistanceToGoal(vehicle, lengths.get(numSegments - 1));
}
DrivingSkill.setDistanceToCurrentTarget(vehicle, getTotalLength(road));
DrivingSkill.setCurrentRoad(vehicle, road);
DrivingSkill.setLowestLane(vehicle, lowestLane);
DrivingSkill.setSegmentIndex(vehicle, segmentIdx);
}
@action(
name = "unregister",
args = {
@arg(
name = "agent",
type = IType.AGENT,
optional = false,
doc = @doc ("the agent to unregister on the road.")
)
},
doc = @doc(
value = "unregister the agent on the road",
examples = { @example ("do unregister agent: the_driver") },
deprecated = "use the `unregister` action in advanced_driving skill instead"
)
)
@Deprecated
public void primUnregister(final IScope scope) throws GamaRuntimeException {
IAgent driver = (IAgent) scope.getArg("agent", IType.AGENT);
DrivingSkill.unregister(scope, driver);
}
}