Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
clean up round based profile routing.
- Loading branch information
1 parent
1c4e782
commit d5096bb
Showing
4 changed files
with
281 additions
and
122 deletions.
There are no files selected for viewing
110 changes: 110 additions & 0 deletions
110
src/main/java/org/opentripplanner/profile/MultiProfileStateStore.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Original file line | Diff line number | Diff line change |
---|---|---|---|
@@ -0,0 +1,110 @@ | |||
package org.opentripplanner.profile; | |||
|
|||
import gnu.trove.map.TObjectIntMap; | |||
import gnu.trove.map.hash.TObjectIntHashMap; | |||
|
|||
import java.util.Collection; | |||
import java.util.HashSet; | |||
import java.util.Iterator; | |||
import java.util.Set; | |||
|
|||
import org.opentripplanner.routing.vertextype.TransitStop; | |||
|
|||
import com.google.common.collect.ArrayListMultimap; | |||
import com.google.common.collect.Multimap; | |||
|
|||
public class MultiProfileStateStore implements ProfileStateStore { | |||
private Multimap<TransitStop, ProfileState> states = ArrayListMultimap.create(); | |||
|
|||
/** cache the minimum upper bounds for each stop, for performance */ | |||
private TObjectIntMap<TransitStop> minUpperBounds; | |||
|
|||
@Override | |||
public boolean put(ProfileState ps) { | |||
if (ps.lowerBound < minUpperBounds.get(ps.stop)) { | |||
states.put(ps.stop, ps); | |||
|
|||
// TODO: we're using strictly-less-than here, and geq below, which means that bounds that douch but do not overlap | |||
// get dropped. This is probably what we want. | |||
if (ps.upperBound < minUpperBounds.get(ps.stop)) { | |||
minUpperBounds.put(ps.stop, ps.upperBound); | |||
|
|||
// kick out old states | |||
for (Iterator<ProfileState> it = states.get(ps.stop).iterator(); it.hasNext();) { | |||
if (it.next().lowerBound >= ps.upperBound) { | |||
it.remove(); | |||
} | |||
} | |||
} | |||
|
|||
return true; | |||
} | |||
else { | |||
return false; | |||
} | |||
} | |||
|
|||
@Override | |||
public Collection<ProfileState> get(TransitStop tstop) { | |||
return states.get(tstop); | |||
} | |||
|
|||
@Override | |||
public Collection<ProfileState> getAll() { | |||
// TODO Auto-generated method stub | |||
return states.values(); | |||
} | |||
|
|||
@Override | |||
public int size() { | |||
return states.size(); | |||
} | |||
|
|||
/** merge similar states (states that have come from the same place on different patterns) */ | |||
public void mergeStates() { | |||
Set<TransitStop> touchedStopVertices = new HashSet<TransitStop>(states.keySet()); | |||
for (TransitStop tstop : touchedStopVertices) { | |||
Collection<ProfileState> pss = states.get(tstop); | |||
|
|||
// find states that have come from the same place | |||
Multimap<ProfileState, ProfileState> foundStates = ArrayListMultimap.create(); | |||
|
|||
for (Iterator<ProfileState> it = pss.iterator(); it.hasNext();) { | |||
ProfileState ps = it.next(); | |||
foundStates.put(ps.previous, ps); | |||
} | |||
|
|||
pss.clear(); | |||
|
|||
// merge them now | |||
for (Collection<ProfileState> states : foundStates.asMap().values()) { | |||
if (states.size() == 1) | |||
pss.addAll(states); | |||
else | |||
pss.add(ProfileState.merge(states, true)); | |||
} | |||
} | |||
} | |||
|
|||
/** | |||
* initialize a multi profile state store for a new round based on the minimum upper bounds from a previous round. | |||
* Note that the min upper bound object is not copied, so the other profile state store can no longer be added to. | |||
*/ | |||
public MultiProfileStateStore (MultiProfileStateStore other) { | |||
minUpperBounds = other.minUpperBounds; | |||
} | |||
|
|||
public MultiProfileStateStore () { | |||
minUpperBounds = new TObjectIntHashMap<TransitStop>(5000, 0.75f, Integer.MAX_VALUE); | |||
} | |||
|
|||
@Override | |||
public Collection<TransitStop> keys() { | |||
return states.keySet(); | |||
} | |||
|
|||
@Override | |||
public boolean containsKey(TransitStop transitStop) { | |||
return states.containsKey(transitStop); | |||
} | |||
} |
28 changes: 28 additions & 0 deletions
28
src/main/java/org/opentripplanner/profile/ProfileStateStore.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Original file line | Diff line number | Diff line change |
---|---|---|---|
@@ -0,0 +1,28 @@ | |||
package org.opentripplanner.profile; | |||
|
|||
import java.util.Collection; | |||
|
|||
import org.opentripplanner.routing.vertextype.TransitStop; | |||
|
|||
/** Stores profile states for a search */ | |||
public interface ProfileStateStore { | |||
/** | |||
* store a profile state, if it is not dominated. | |||
* @return true if state was nondominated | |||
*/ | |||
public boolean put(ProfileState ps); | |||
|
|||
/** get the nondominated states at a particular vertex */ | |||
public Collection<ProfileState> get(TransitStop tstop); | |||
|
|||
/** get all nondominated states */ | |||
public Collection<ProfileState> getAll(); | |||
|
|||
/** the number of profile states stored */ | |||
public int size(); | |||
|
|||
/** the transit stops represented */ | |||
public Collection<TransitStop> keys (); | |||
|
|||
public boolean containsKey(TransitStop transitStop); | |||
} |
Oops, something went wrong.