19 package org.matsim.core.mobsim.hermes;
21 import java.util.Iterator;
25 private float currentCapacity;
26 private final int initialCapacity;
48 public AgentQueue(
int maxCapacity,
int initialcapacity) {
50 this.array =
new Agent[initialcapacity];
53 private int inc(
int number) {
54 if (++number == array.length) {
65 public boolean push(Agent agent) {
71 }
else if (array.length > size) {
72 array[tail =
inc(tail)] = agent;
77 Agent[] narray =
new Agent[array.length * 2];
78 for (
int i = head, left = size, dst = 0; left > 0; i =
inc(i), left--, dst++) {
79 narray[dst] = array[i];
85 array[tail =
inc(tail)] = agent;
92 return size == 0 ? null : array[
head];
110 for (
int i = head, left = size; left > 0; i =
inc(i), left--) {
113 head = tail = size = 0;
118 return new Iterator<>() {
120 private int idx =
head;
121 private int left =
size;
124 public boolean hasNext() {
129 public Agent next() {
130 Agent agent = array[idx];
142 private final int length;
144 private final int velocity;
148 private final float flowCapacityPerS;
149 private float flowLeftInTimestep;
150 private int lastUpdate;
152 private int nextFreeFlowSlot;
153 private int lastPush;
154 private final int stuckTimePeriod;
156 public HLink(
int id,
int capacity,
int length,
int velocity,
float flowCapacityperSecond,
int stuckTimePeriod) {
158 this.length = length;
159 this.velocity = velocity;
160 this.flowCapacityPerS = flowCapacityperSecond;
161 this.stuckTimePeriod = stuckTimePeriod;
164 this.nextFreeFlowSlot = 0;
167 this.flowLeftInTimestep = flowCapacityperSecond;
171 this.queue =
new AgentQueue(Math.max(1, capacity), Math.min(capacity, 16));
174 public void reset() {
176 this.nextFreeFlowSlot = 0;
179 this.currentCapacity = initialCapacity;
180 this.flowLeftInTimestep = flowCapacityPerS;
184 public boolean push(Agent agent,
int timestep,
float storageCapacityPCU) {
186 float effectiveStorageCapacity = Math.min(storageCapacityPCU, initialCapacity);
187 if (currentCapacity - effectiveStorageCapacity >= 0) {
188 if (queue.
push(agent)) {
190 currentCapacity = currentCapacity - effectiveStorageCapacity;
195 }
else if (stuckTimePeriod != Integer.MAX_VALUE && (lastPush + stuckTimePeriod) < timestep) {
198 currentCapacity = currentCapacity - effectiveStorageCapacity;
205 public void pop(
float storageCapacityPCE) {
207 currentCapacity += storageCapacityPCE;
210 public int nexttime () {
211 if (queue.
size() == 0) {
214 return queue.
peek().linkFinishTime;
218 public int length() {
222 public boolean flow(
int timestep,
float requestedFlow) {
223 if (timestep >= nextFreeFlowSlot) {
225 if (lastUpdate == timestep){
226 if (flowLeftInTimestep >= 0) {
227 flowLeftInTimestep-=requestedFlow;
228 nextFreeFlowSlot = timestep + (int) Math.floor( requestedFlow / flowCapacityPerS);
232 flowLeftInTimestep=flowLeftInTimestep+flowCapacityPerS-requestedFlow;
233 lastUpdate = timestep;
234 nextFreeFlowSlot = timestep + (int) Math.floor( requestedFlow / flowCapacityPerS);
243 public int velocity() {
244 return this.velocity;
boolean forcePush(Agent agent)
AgentQueue(int maxCapacity, int initialcapacity)
boolean push(Agent agent)
Iterator< Agent > iterator()