20 package org.matsim.core.mobsim.hermes;
26 import java.util.ArrayList;
27 import java.util.Arrays;
28 import java.util.Collections;
29 import java.util.List;
38 this.array =
new long[32];
41 public void add(
long element) {
42 if (size == array.length) {
43 array = Arrays.copyOf(array, array.length * 2);
45 array[size++] = element;
51 public long get(
int index) {
56 for (
int i = 0; i <
size; i++) {
65 public static final int SleepForType = 0;
67 public static final int SleepUntilType = 1;
69 public static final int LinkType = 2;
71 public static final int AccessType = 3;
73 public static final int EgressType = 4;
75 public static final int StopArriveType = 5;
77 public static final int StopDepartType = 6;
79 public static final int WaitType = 7;
81 public static final int StopDelayType = 8;
84 protected final int id;
105 protected int planIndex;
107 protected int eventsIndex;
110 protected int linkFinishTime;
113 private int capacity;
116 private int passengersInside;
118 private float storageCapacityPCUE = -1;
119 private float flowCapacityPCUE = -1;
124 private final static List<Agent> NO_PASSENGERS = Collections.emptyList();
129 this.events = events;
130 this.capacity = capacity;
132 this.passengersByStop = null;
138 public static long preparePlanEventEntry(
long type,
long element) {
139 long planEntry = (type << 60) | element;
141 validatePlanEntry(planEntry);
150 public int linkFinishTime() {
151 return this.linkFinishTime;
154 public int planIndex() {
155 return this.planIndex;
166 public long currPlan() {
167 return this.plan.
get(planIndex);
170 public long prevPlan() {
171 return this.plan.
get(planIndex - 1);
174 public long nextPlan() {
175 return this.plan.
get(planIndex + 1);
178 public boolean finished() {
179 return planIndex >= (plan.size() - 1);
182 public float getFlowCapacityPCUE() {
183 return flowCapacityPCUE;
186 public void setFlowCapacityPCUE(
float flowCapacityPCUE) {
187 this.flowCapacityPCUE = flowCapacityPCUE;
190 public float getStorageCapacityPCUE() {
191 return storageCapacityPCUE;
194 public void setStorageCapacityPCUE(
float storageCapacityPCUE) {
195 this.storageCapacityPCUE = storageCapacityPCUE;
201 public int getCapacity() {
202 return this.capacity;
205 public boolean isTransitVehicle() {
206 return this.passengersByStop != null;
209 public List<Agent> egress(
int stopid) {
210 List<Agent> ret = passengersByStop.
remove(stopid);
212 return NO_PASSENGERS;
214 passengersInside -= ret.size();
218 public void setServeStop(
int stopId) {
219 this.passengersByStop.
addStop(stopId);
222 public boolean willServeStop(
int stopId) {
226 public boolean access(
int stopid, Agent agent) {
227 if (passengersInside == capacity) {
230 passengersByStop.
add(stopid, agent);
236 public int getNextStopPlanEntry() {
240 return getStopPlanEntry(plan.
get(planIndex + 2));
243 public static int getPlanHeader (
long plan) {
return (
int)((plan >> 60) & 0x000000000000000FL); }
244 public static int getPlanEvent (
long plan) {
return (
int)((plan >> 40) & 0x000000000000FFFFL); }
245 public static int getDeparture (
long plan) {
return (
int)((plan >> 40) & 0x00000000000FFFFFL); }
246 public static int getLinkPlanEntry (
long plan) {
return (
int) ((plan >> 8) & 0x00000000FFFFFFFFL); }
247 public static int getLinkPCEEntry (
long plan) {
248 return (
int) ((plan >> 56) & 0x000000000000000FL);
250 public static double getVelocityPlanEntry (
long plan) {
return decodeVelocityFromLinkEntry((
int) (plan & 0x00000000000000FFL)); }
251 public static int getRoutePlanEntry (
long plan) {
return (
int)((plan >> 16) & 0x000000000000FFFFL); }
252 public static int getStopPlanEntry (
long plan) {
return (
int)( plan & 0x000000000000FFFFL); }
253 public static int getSleepPlanEntry (
long plan) {
return (
int)( plan & 0x00000000FFFFFFFFL); }
255 private static void validatePlanEntry(
long planEntry) {
256 int event = Agent.getPlanEvent(planEntry);
257 int type = Agent.getPlanHeader(planEntry);
260 case Agent.SleepForType:
261 case Agent.SleepUntilType:
262 case Agent.AccessType:
263 case Agent.StopArriveType:
264 case Agent.StopDelayType:
265 case Agent.StopDepartType:
266 case Agent.EgressType:
274 public static long preparePlanEventEntry(
long type,
long eventid,
long element) {
276 throw new RuntimeException(String.format(
"eventid above limit: %d", eventid));
278 return preparePlanEventEntry(type, (eventid << 40) | element);
281 private static long prepareLinkEntryElement(
long linkid,
double velocity,
long pcecategory) {
285 int encodedVelocity = prepareVelocityForLinkEntry(velocity);
287 return (pcecategory << 56) | (linkid << 8) | encodedVelocity;
290 public static int prepareVelocityForLinkEntry(
double velocity) {
298 velocity = velocity * 10;
301 velocity = velocity + 90;
303 return (
int) Math.round(velocity);
306 public static double decodeVelocityFromLinkEntry(
int encodedVelocity) {
308 if (encodedVelocity < 100) {
310 velocity = ((double) encodedVelocity) / 10.0;
313 velocity = ((double) encodedVelocity) - 90;
319 public static long prepareStopDelay(
long type,
long departure,
long element) {
320 return preparePlanEventEntry(type, (departure << 40) | element);
323 private static long prepareRouteStopEntry(
long routeid,
long stopid) {
325 throw new RuntimeException(String.format(
"stopid above limit: %d", stopid));
328 throw new RuntimeException(String.format(
"routeid above limit: %d", routeid));
330 return (routeid << 16) | stopid;
333 public void reset() {
339 if (this.passengersByStop != null) {
340 passengersInside = 0;
341 this.passengersByStop.
clear();
345 public static long prepareLinkEntry(
int eventid,
int linkid,
double velocity,
int pcecategory) {
346 long l = preparePlanEventEntry(LinkType, eventid, prepareLinkEntryElement(linkid, velocity, pcecategory));
350 public static long prepareSleepForEntry(
int eventid,
int element) {
351 return preparePlanEventEntry(SleepForType, eventid, element);
354 public static long prepareSleepUntilEntry(
int eventid,
int element) {
355 return preparePlanEventEntry(SleepUntilType, eventid, element);
358 public static long prepareAccessEntry(
int eventid,
int routeid,
int stopid) {
359 return preparePlanEventEntry(AccessType, eventid, prepareRouteStopEntry(routeid, stopid));
362 public static long prepareEgressEntry(
int eventid,
int routeid,
int stopid) {
363 return preparePlanEventEntry(EgressType, eventid, prepareRouteStopEntry(routeid, stopid));
366 public static long prepareWaitEntry(
int eventid,
int routeid,
int stopid) {
367 return preparePlanEventEntry(WaitType, eventid, prepareRouteStopEntry(routeid, stopid));
370 public static long prepareStopArrivalEntry(
int eventid,
int routeid,
int stopid) {
371 return preparePlanEventEntry(StopArriveType, eventid, prepareRouteStopEntry(routeid, stopid));
374 public static long prepareStopDelayEntry(
int departure,
int routeid,
int stopid) {
375 return prepareStopDelay(StopDelayType, departure, prepareRouteStopEntry(routeid, stopid));
378 public static long prepareStopDepartureEntry(
int eventid,
int routeid,
int stopid) {
379 return preparePlanEventEntry(StopDepartType, eventid, prepareRouteStopEntry(routeid, stopid));
382 public static String toString(
long planEntry) {
383 int type = Agent.getPlanHeader(planEntry);
386 return String.format(
"type=link; event=%d; link=%d; vel=%d",
387 getPlanEvent(planEntry), getLinkPlanEntry(planEntry), getVelocityPlanEntry(planEntry));
388 case Agent.SleepForType:
389 return String.format(
"type=sleepfor; event=%d; sleep=%d",
390 getPlanEvent(planEntry), getSleepPlanEntry(planEntry));
391 case Agent.SleepUntilType:
392 return String.format(
"type=sleepuntil; event=%d; sleep=%d",
393 getPlanEvent(planEntry), getSleepPlanEntry(planEntry));
394 case Agent.AccessType:
395 return String.format(
"type=access; event=%d; route=%d stopid=%d",
396 getPlanEvent(planEntry), getRoutePlanEntry(planEntry), getStopPlanEntry(planEntry));
397 case Agent.StopArriveType:
398 return String.format(
"type=stoparrive; event=%d; route=%d stopid=%d",
399 getPlanEvent(planEntry), getRoutePlanEntry(planEntry), getStopPlanEntry(planEntry));
400 case Agent.StopDelayType:
401 return String.format(
"type=stopdelay; departure=%d; route=%d stopid=%d",
402 getDeparture(planEntry), getRoutePlanEntry(planEntry), getStopPlanEntry(planEntry));
403 case Agent.StopDepartType:
404 return String.format(
"type=stopdepart; event=%d; route=%d stopid=%d",
405 getPlanEvent(planEntry), getRoutePlanEntry(planEntry), getStopPlanEntry(planEntry));
406 case Agent.EgressType:
407 return String.format(
"type=egress; event=%d; route=%d stopid=%d",
408 getPlanEvent(planEntry), getRoutePlanEntry(planEntry), getStopPlanEntry(planEntry));
410 return String.format(
"type=wait; event=%d; route=%d stopid=%d",
411 getPlanEvent(planEntry), getRoutePlanEntry(planEntry), getStopPlanEntry(planEntry));
413 return String.format(
"unknown plan type %d", type);
419 int[] stopIds =
new int[10];
420 List<Agent>[] passengers =
new ArrayList[10];
422 int currentStopIdx = -1;
425 Arrays.fill(this.stopIds, -1);
426 Arrays.fill(this.passengers, null);
428 this.currentStopIdx = -1;
432 if (this.size == this.stopIds.length) {
433 this.stopIds = Arrays.copyOf(this.stopIds, this.stopIds.length * 2);
435 this.stopIds[this.
size] = stopId;
437 if (this.size == this.passengers.length) {
438 this.passengers = Arrays.copyOf(this.passengers, this.passengers.length * 2);
440 this.passengers[this.
size] =
new ArrayList<>();
445 public void add(
int stopId, Agent agent) {
446 for (
int i = this.currentStopIdx + 1; i < this.
size; i++) {
447 if (this.stopIds[i] == stopId) {
448 this.passengers[i].add(agent);
456 for (
int i = this.currentStopIdx + 1; i < this.
size; i++) {
457 if (this.stopIds[i] == stopId) {
464 public List<Agent>
remove(
int stopId) {
465 for (
int i = this.currentStopIdx + 1; i < this.
size; i++) {
466 if (this.stopIds[i] == stopId) {
467 this.currentStopIdx = i;
468 return this.passengers[this.currentStopIdx];
static< T > Id< T > get(int index, final Class< T > type)
static final int MAX_VEHICLE_VELOCITY
boolean hasUpcoming(int stopId)
static final int MAX_STOP_ROUTE_ID
void add(int stopId, Agent agent)
static final int MAX_EVENTS_AGENT
List< Agent > remove(int stopId)
static final boolean DEBUG_EVENTS
static final int MAX_LINK_ID