Skip to content

Commit

Permalink
change dominance functions to use <= not strict < #1800
Browse files Browse the repository at this point in the history
  • Loading branch information
abyrd committed Mar 5, 2015
1 parent 52999d3 commit bdf6c05
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 13 deletions.
Expand Up @@ -17,15 +17,20 @@
*/ */
public abstract class DominanceFunction { public abstract class DominanceFunction {


/** Return true if the first state "defeats" the second state. Provide this custom logic in subclasses. */ /**
protected abstract boolean dominates0 (State a, State b); * Return true if the first state "defeats" the second state or at least ties with it in terms of suitability.
* In the case that they are tied, we still want to return true so that an existing state will kick out a new one.
* Provide this custom logic in subclasses. You would think this could be static, but in Java for some reason
* calling a static function will call the one on the declared type, not the runtime instance type.
*/
protected abstract boolean betterOrEqual(State a, State b);


/** /**
* For bike rental, parking, and approaching turn-restricted intersections states are incomparable: * For bike rental, parking, and approaching turn-restricted intersections states are incomparable:
* they exist on separate planes. The core state dominance logic is wrapped in this public function and only * they exist on separate planes. The core state dominance logic is wrapped in this public function and only
* applied when the two states have all these variables in common (are on the same plane). * applied when the two states have all these variables in common (are on the same plane).
*/ */
public boolean dominates(State a, State b) { public boolean betterOrEqualAndComparable(State a, State b) {


// Does one state represent riding a rented bike and the other represent walking before/after rental? // Does one state represent riding a rented bike and the other represent walking before/after rental?
if (a.isBikeRenting() != b.isBikeRenting()) { if (a.isBikeRenting() != b.isBikeRenting()) {
Expand All @@ -45,7 +50,7 @@ public boolean dominates(State a, State b) {
} }


// These two states are comparable (they are on the same "plane" or "copy" of the graph). // These two states are comparable (they are on the same "plane" or "copy" of the graph).
return dominates0 (a, b); return betterOrEqual(a, b);


} }


Expand All @@ -59,7 +64,8 @@ public ShortestPathTree getNewShortestPathTree(RoutingRequest routingRequest) {


public static class MinimumWeight extends DominanceFunction { public static class MinimumWeight extends DominanceFunction {
/** Return true if the first state has lower weight than the second state. */ /** Return true if the first state has lower weight than the second state. */
public boolean dominates0 (State a, State b) { return a.weight < b.weight; } @Override
public boolean betterOrEqual (State a, State b) { return a.weight <= b.weight; }
} }


/** /**
Expand All @@ -68,7 +74,8 @@ public static class MinimumWeight extends DominanceFunction {
*/ */
public static class EarliestArrival extends DominanceFunction { public static class EarliestArrival extends DominanceFunction {
/** Return true if the first state has lower elapsed time than the second state. */ /** Return true if the first state has lower elapsed time than the second state. */
public boolean dominates0 (State a, State b) { return a.getElapsedTimeSeconds() < b.getElapsedTimeSeconds(); } @Override
public boolean betterOrEqual (State a, State b) { return a.getElapsedTimeSeconds() <= b.getElapsedTimeSeconds(); }
} }


/** In this implementation the relation is not symmetric. There are sets of mutually co-dominant states. */ /** In this implementation the relation is not symmetric. There are sets of mutually co-dominant states. */
Expand All @@ -80,9 +87,8 @@ public static class Pareto extends DominanceFunction {
private static final double TIME_EPSILON = 0.02; private static final double TIME_EPSILON = 0.02;
private static final int TIME_DIFF_MARGIN = 30; private static final int TIME_DIFF_MARGIN = 30;


// You would think this could be static, but in Java for some reason calling a static function will @Override
// call the one on the declared type, not the instance type. public boolean betterOrEqual (State a, State b) {
public boolean dominates0 (State a, State b) {


// The key problem in pareto-dominance in OTP is that the elements of the state vector are not orthogonal. // The key problem in pareto-dominance in OTP is that the elements of the state vector are not orthogonal.
// When walk distance increases, weight increases. When time increases weight increases. // When walk distance increases, weight increases. When time increases weight increases.
Expand Down
Expand Up @@ -140,9 +140,9 @@ public boolean add(State newState) {
State oldState = it.next(); State oldState = it.next();
// order is important, because in the case of a tie // order is important, because in the case of a tie
// we want to reject the new state // we want to reject the new state
if (dominanceFunction.dominates(oldState, newState)) if (dominanceFunction.betterOrEqualAndComparable(oldState, newState))
return false; return false;
if (dominanceFunction.dominates(newState, oldState)) if (dominanceFunction.betterOrEqualAndComparable(newState, oldState))
it.remove(); it.remove();
} }


Expand Down
Expand Up @@ -548,8 +548,8 @@ public void actionPerformed(ActionEvent e) {
State s1 = firstComparePathStates.getSelectedValue(); State s1 = firstComparePathStates.getSelectedValue();
State s2 = secondComparePathStates.getSelectedValue(); State s2 = secondComparePathStates.getSelectedValue();
DominanceFunction pareto = new DominanceFunction.Pareto(); DominanceFunction pareto = new DominanceFunction.Pareto();
System.out.println("s1 dominates s2:" + pareto.dominates(s1, s2)); System.out.println("s1 dominates s2:" + pareto.betterOrEqualAndComparable(s1, s2));
System.out.println("s2 dominates s1:" + pareto.dominates(s2, s1)); System.out.println("s2 dominates s1:" + pareto.betterOrEqualAndComparable(s2, s1));
} }
}); });
pane.add(dominateButton); pane.add(dominateButton);
Expand Down

0 comments on commit bdf6c05

Please sign in to comment.