Skip to content
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,7 @@ void flexDirect() {

// walk, flex
assertEquals(2, itin.getLegs().size());
assertEquals("2021-12-02T12:52:42-05:00[America/New_York]", itin.startTime().toString());
assertEquals("2021-12-02T12:52:54-05:00[America/New_York]", itin.startTime().toString());
assertEquals(3203, itin.getGeneralizedCost());

var walkToFlex = itin.getStreetLeg(0);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -162,10 +162,8 @@ public State[] traverse(State s0) {
s1.incrementWeight(
pickedUp ? preferences.pickupCost().toSeconds() : preferences.dropOffCost().toSeconds()
);
s1.incrementTimeInSeconds(
pickedUp
? (int) preferences.pickupTime().toSeconds()
: (int) preferences.dropOffTime().toSeconds()
s1.incrementTimeInMilliseconds(
pickedUp ? preferences.pickupTime().toMillis() : preferences.dropOffTime().toMillis()
);
s1.setBackMode(null);
return s1.makeStateArray();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ default void switchToWalkingBike(RoutingPreferences preferences, StateEditor edi
editor.setBackWalkingBike(true);
if (shouldIncludeCost) {
editor.incrementWeight(preferences.bike().walking().mountDismountCost().toSeconds());
editor.incrementTimeInSeconds(
(int) preferences.bike().walking().mountDismountTime().toSeconds()
editor.incrementTimeInMilliseconds(
preferences.bike().walking().mountDismountTime().toMillis()
);
}
}
Expand All @@ -31,8 +31,8 @@ default void switchToBiking(RoutingPreferences preferences, StateEditor editor)
editor.setBackWalkingBike(false);
if (shouldIncludeCost) {
editor.incrementWeight(preferences.bike().walking().mountDismountCost().toSeconds());
editor.incrementTimeInSeconds(
(int) preferences.bike().walking().mountDismountTime().toSeconds()
editor.incrementTimeInMilliseconds(
preferences.bike().walking().mountDismountTime().toMillis()
);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,13 +30,13 @@ default void dropOffAfterDriving(State state, StateEditor editor) {
? CarPickupState.WALK_TO_PICKUP
: CarPickupState.WALK_FROM_DROP_OFF
);
editor.incrementTimeInSeconds((int) state.getPreferences().car().pickupTime().toSeconds());
editor.incrementTimeInMilliseconds(state.getPreferences().car().pickupTime().toMillis());
editor.incrementWeight(state.getPreferences().car().pickupCost().toSeconds());
}

default void driveAfterPickup(State state, StateEditor editor) {
editor.setCarPickupState(CarPickupState.IN_CAR);
editor.incrementTimeInSeconds((int) state.getPreferences().car().pickupTime().toSeconds());
editor.incrementTimeInMilliseconds(state.getPreferences().car().pickupTime().toMillis());
editor.incrementWeight(state.getPreferences().car().pickupCost().toSeconds());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -136,11 +136,10 @@ public State[] traverse(State s0) {
? this.travelTime
: (preferences.street().elevator().hopCost() * this.levels)
);
s1.incrementTimeInSeconds(
this.travelTime > 0
? this.travelTime
: (int) (preferences.street().elevator().hopTime() * this.levels)
);
int seconds = this.travelTime > 0
? this.travelTime
: (int) (preferences.street().elevator().hopTime() * this.levels);
s1.incrementTimeInMilliseconds(1000L * seconds);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Use the seconds method here.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is not fixed. Please use the seconds method

return s1.makeStateArray();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ public State[] traverse(State s0) {
time = duration.toSeconds();
}
s1.incrementWeight(s0.getPreferences().walk().escalator().reluctance() * time);
s1.incrementTimeInSeconds((int) Math.round(time));
s1.incrementTimeInSeconds((long) time);
s1.incrementWalkDistance(getDistanceMeters());
return s1.makeStateArray();
} else return State.empty();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -111,19 +111,19 @@ public State[] traverse(State s0) {
RoutingPreferences preferences = s0.getPreferences();

/* TODO: Consider mode, so that passing through multiple fare gates is not possible */
int time = traversalTime;
long time_ms = 1000L * traversalTime;

if (time == 0) {
if (time_ms == 0) {
if (distance > 0) {
time = (int) (distance / preferences.walk().speed());
time_ms = (long) (1000.0 * distance / preferences.walk().speed());
} else if (isStairs()) {
// 1 step corresponds to 20cm, doubling that to compensate for elevation;
time = (int) (0.4 * Math.abs(steps) / preferences.walk().speed());
time_ms = (long) (1000.0 * 0.4 * Math.abs(steps) / preferences.walk().speed());
}
}

if (time > 0) {
double weight = time;
if (time_ms > 0) {
double weight = time_ms / 1000.0;
if (s0.getRequest().wheelchair()) {
weight *=
StreetEdgeReluctanceCalculator.computeWheelchairReluctance(
Expand All @@ -141,7 +141,7 @@ public State[] traverse(State s0) {
isStairs()
);
}
s1.incrementTimeInSeconds(time);
s1.incrementTimeInMilliseconds(time_ms);
s1.incrementWeight(weight);
} else {
// elevators often don't have a traversal time, distance or steps, so we need to add
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1169,7 +1169,7 @@ private StateEditor doTraverse(State s0, TraverseMode traverseMode, boolean walk
default -> otherTraversalCosts(preferences, traverseMode, walkingBike, speed);
};

int time = (int) Math.ceil(traversalCosts.time());
long time_ms = (long) Math.ceil(1000.0 * traversalCosts.time());
var weight = traversalCosts.weight();

/* Compute turn cost. */
Expand Down Expand Up @@ -1234,7 +1234,7 @@ private StateEditor doTraverse(State s0, TraverseMode traverseMode, boolean walk
s1.incrementWalkDistance(turnDuration / 100); // just a tie-breaker
}

time += (int) Math.ceil(turnDuration);
time_ms += (long) Math.ceil(1000.0 * turnDuration);
weight += preferences.street().turnReluctance() * turnDuration;
}

Expand All @@ -1246,7 +1246,7 @@ private StateEditor doTraverse(State s0, TraverseMode traverseMode, boolean walk
weight += costExtension.calculateExtraCost(s0, length_mm, traverseMode);
}

s1.incrementTimeInSeconds(time);
s1.incrementTimeInMilliseconds(time_ms);

s1.incrementWeight(weight);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ private State[] traverseUnPark(

StateEditor s0e = s0.edit(this);
s0e.incrementWeight(parkingCost.toSeconds());
s0e.incrementTimeInSeconds((int) parkingTime.toSeconds());
s0e.incrementTimeInMilliseconds(parkingTime.toMillis());
s0e.setVehicleParked(false, mode);

var parkingPreferences = s0.getRequest().preferences().parking(s0.currentMode());
Expand Down Expand Up @@ -156,7 +156,7 @@ private State[] traversePark(State s0, Cost parkingCost, Duration parkingTime) {

StateEditor s0e = s0.edit(this);
s0e.incrementWeight(parkingCost.toSeconds());
s0e.incrementTimeInSeconds((int) parkingTime.toSeconds());
s0e.incrementTimeInMilliseconds(parkingTime.toMillis());
s0e.setVehicleParked(true, TraverseMode.WALK);

var parkingPreferences = s0.getRequest().preferences().parking(s0.currentMode());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,8 @@ public class State implements AStarState<State, Edge, Vertex>, Cloneable {

/* Data which is likely to change at most traversals */

// the current time at this state, in seconds since UNIX epoch
protected long time;
// the current time at this state, in milliseconds since UNIX epoch
protected long time_ms;

// accumulated weight up to this state
public double weight;
Expand Down Expand Up @@ -76,7 +76,7 @@ public State(Vertex vertex, Instant startTime, StateData stateData, StreetSearch
vertex.rentalRestrictions().noDropOffNetworks();
}
this.walkDistance = 0;
this.time = startTime.getEpochSecond();
this.time_ms = startTime.toEpochMilli();
}

/**
Expand Down Expand Up @@ -167,14 +167,32 @@ public CarPickupState getCarPickupState() {
return stateData.carPickupState;
}

/** Always round the same way and in the same direction when converting milliseconds to seconds.
* This means that request.arriveBy must be taken into account. Used in many places. */
private long millisecondsToSeconds(long milliseconds) {
if (request.arriveBy()) {
return milliseconds / 1000L;
} else {
return (milliseconds + 999L) / 1000L;
}
}

/** Returns time in seconds since epoch */
public long getTimeSeconds() {
return time;
return millisecondsToSeconds(time_ms);
}

public long getTimeMilliseconds() {
return time_ms;
}

/** returns the length of the trip in seconds up to this state */
public long getElapsedTimeSeconds() {
return Math.abs(getTimeSeconds() - request.startTime().getEpochSecond());
return (getElapsedTimeMilliseconds() + 999L) / 1000L;
}

public long getElapsedTimeMilliseconds() {
return Math.abs(getTimeMilliseconds() - request.startTime().toEpochMilli());
}

public boolean isCompatibleVehicleRentalState(State state) {
Expand Down Expand Up @@ -267,7 +285,11 @@ public double getWeight() {
}

public int getTimeDeltaSeconds() {
return backState != null ? (int) (getTimeSeconds() - backState.getTimeSeconds()) : 0;
return (int) millisecondsToSeconds(getTimeDeltaMilliseconds());
}

public int getTimeDeltaMilliseconds() {
return backState != null ? (int) (getTimeMilliseconds() - backState.getTimeMilliseconds()) : 0;
}

public double getWeightDelta() {
Expand Down Expand Up @@ -311,7 +333,13 @@ public TraverseMode currentMode() {
}

public Instant getTime() {
return Instant.ofEpochSecond(time);
// We're not letting the subsecond time out right now, because everything else
// expects whole seconds.
return Instant.ofEpochSecond(millisecondsToSeconds(time_ms));
}

public Instant getTimeAccurate() {
return Instant.ofEpochMilli(time_ms);
}

public String getVehicleRentalNetwork() {
Expand Down Expand Up @@ -351,7 +379,7 @@ public State reverse() {
// note the distinction between setFromState and setBackState
editor.setFromState(orig);

editor.incrementTimeInSeconds(orig.getAbsTimeDeltaSeconds());
editor.incrementTimeInMilliseconds(orig.getAbsTimeDeltaMilliseconds());
editor.incrementWeight(orig.getWeightDelta());
editor.incrementWalkDistance(orig.getWalkDistanceDelta());

Expand Down Expand Up @@ -485,8 +513,8 @@ void checkNegativeWeight() {
}
}

private int getAbsTimeDeltaSeconds() {
return Math.abs(getTimeDeltaSeconds());
private int getAbsTimeDeltaMilliseconds() {
return Math.abs(getTimeDeltaMilliseconds());
}

private double getWalkDistanceDelta() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,9 @@ public State makeState() {
// check that time changes are coherent with edge traversal
// direction
if (
traversingBackward ? (child.getTimeDeltaSeconds() > 0) : (child.getTimeDeltaSeconds() < 0)
traversingBackward
? (child.getTimeDeltaMilliseconds() > 0)
: (child.getTimeDeltaMilliseconds() < 0)
) {
LOG.trace(
"Time was incremented the wrong direction during state editing. {}",
Expand Down Expand Up @@ -173,16 +175,20 @@ public void incrementWeight(double weight) {
* is inferred from the direction of traversal. This is the only element of state that runs
* backward when traversing backward.
*/
public void incrementTimeInSeconds(int seconds) {
if (seconds < 0) {
public void incrementTimeInMilliseconds(long milliseconds) {
if (milliseconds < 0) {
LOG.warn(
"A state's time is being incremented by a negative amount while traversing edge " +
child.getBackEdge()
);
defectiveTraversal = true;
return;
}
child.time += (traversingBackward ? -seconds : seconds);
child.time_ms += (traversingBackward ? -milliseconds : milliseconds);
}

public void incrementTimeInSeconds(long seconds) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Btw does this need to be long?

incrementTimeInMilliseconds(1000L * seconds);
}

public void incrementWalkDistance(double length) {
Expand Down Expand Up @@ -362,7 +368,11 @@ public void setCarPickupState(CarPickupState carPickupState) {
}

public void setTimeSeconds(long seconds) {
child.time = seconds;
child.time_ms = 1000 * seconds;
}

public void setTimeMilliseconds(long milliseconds) {
child.time_ms = milliseconds;
}

/* PUBLIC GETTER METHODS */
Expand Down
Loading
Loading