Skip to content
Open
Show file tree
Hide file tree
Changes from all 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 @@ -124,5 +124,6 @@ public OnlineExtendedState getParent() {
public void setParent(OnlineExtendedState parent) {
this.parent = parent;
}

}

Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package org.urbcomp.cupid.db.algorithm.mapmatch.onlinemm;

import org.urbcomp.cupid.db.algorithm.mapmatch.tihmm.inner.SequenceState;
import org.urbcomp.cupid.db.model.point.CandidatePoint;
import org.urbcomp.cupid.db.model.point.GPSPoint;

public class OnlineSequenceState extends SequenceState {

public int time;
/**
* 构造函数
*
* @param state 原始point的candidate
* @param observation 原始point
*/
public OnlineSequenceState(CandidatePoint state, GPSPoint observation) {
super(state, observation);
}

public OnlineSequenceState(CandidatePoint state, GPSPoint observation, int time) {
super(state, observation);
this.time = time;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

import org.urbcomp.cupid.db.algorithm.mapmatch.tihmm.inner.ExtendedState;
import org.urbcomp.cupid.db.algorithm.mapmatch.tihmm.inner.ForwardStepResult;
import org.urbcomp.cupid.db.algorithm.mapmatch.tihmm.inner.SequenceState;
import org.urbcomp.cupid.db.algorithm.mapmatch.tihmm.inner.TiViterbi;
import org.urbcomp.cupid.db.model.point.CandidatePoint;
import org.urbcomp.cupid.db.model.point.GPSPoint;
Expand All @@ -21,7 +20,7 @@ public class OnlineViterbi extends TiViterbi {
// Collection of Viterbi states
private final LinkedList<OnlineExtendedState> stateList;
// Local solutions for the current sequence
private final List<SequenceState> sequenceStates;
private final List<OnlineSequenceState> sequenceStates;

// Convergence status
public boolean isConverge;
Expand All @@ -36,7 +35,9 @@ public class OnlineViterbi extends TiViterbi {
// Time diff between root and previous root
public int timeDelta;
// Starting insert position for global sequence after algorithm interruption
public int insertPosition;
public int validStartInsertIndex;
// Boundary size of convergence from the current time step
public int windowSize;

/**
* Constructs an OnlineViterbi instance with an initial insert position of zero.
Expand All @@ -49,44 +50,70 @@ public OnlineViterbi() {
currentRoot = null;
previousRoot = null;
timeDelta = 0;
insertPosition = 0;
validStartInsertIndex = 0;
windowSize = 0;
}

/**
* Constructs an OnlineViterbi instance with a specified insert position.
* For recording insert position of global sequence when algorithm breaks.
*
* @param insertPosition The starting insert position for the global sequence.
* @param validStartInsertIndex The starting insert position for the global sequence.
*/
public OnlineViterbi(int insertPosition) {
public OnlineViterbi(int validStartInsertIndex) {
stateList = new LinkedList<>();
sequenceStates = new ArrayList<>();
isConverge = false;
isBrokenBefore = false;
currentRoot = null;
previousRoot = null;
timeDelta = 0;
this.insertPosition = insertPosition;
this.validStartInsertIndex = validStartInsertIndex;
windowSize = 0;
}

/**
* Constructs an OnlineViterbi instance with a specified insert position and broken state indicator.
* This constructor is used for recording the insert position of the global sequence when the algorithm breaks.
*
* @param insertPosition The starting insert position for the global sequence.
* @param isBrokenBefore A boolean indicating whether the algorithm was broken before this instance was created.
* @param validStartInsertIndex The starting insert position for the global sequence.
* @param isBrokenBefore A boolean indicating whether the algorithm was broken before this instance was created.
*/
public OnlineViterbi(int insertPosition, boolean isBrokenBefore) {
public OnlineViterbi(int validStartInsertIndex, boolean isBrokenBefore) {
stateList = new LinkedList<>();
sequenceStates = new ArrayList<>();
isConverge = false;
this.isBrokenBefore = isBrokenBefore;
currentRoot = null;
previousRoot = null;
timeDelta = 0;
this.insertPosition = insertPosition;
this.validStartInsertIndex = validStartInsertIndex;
windowSize = 0;
}

public OnlineViterbi(int validStartInsertIndex, int windowSize) {
stateList = new LinkedList<>();
sequenceStates = new ArrayList<>();
isConverge = false;
this.isBrokenBefore = false;
currentRoot = null;
previousRoot = null;
timeDelta = 0;
this.validStartInsertIndex = validStartInsertIndex;
this.windowSize = windowSize;
}

public OnlineViterbi(int validStartInsertIndex, int windowSize, boolean isBrokenBefore) {
stateList = new LinkedList<>();
sequenceStates = new ArrayList<>();
isConverge = false;
this.isBrokenBefore = isBrokenBefore;
currentRoot = null;
previousRoot = null;
timeDelta = 0;
this.validStartInsertIndex = validStartInsertIndex;
this.windowSize = windowSize;
}

/**
* Processes the next step in the Viterbi algorithm using the given observation
Expand All @@ -95,10 +122,10 @@ public OnlineViterbi(int insertPosition, boolean isBrokenBefore) {
* @param observation The current GPS observation.
* @param candidates List of candidate points for the current step.
* @param emissionLogProbabilities Map of emission log probabilities for candidate points.
* @param transitionLogProbabilities Map of transition log probabilities between candidate points.
* @param transitionLogProbabilities Map of transition log probabilities between candidate points.
* @param time The current time step.
* @throws IllegalStateException if the method is called without initializing
* with an observation or after an HMM break.
* with an observation or after an HMM break.
*/
public void nextStep(
GPSPoint observation,
Expand All @@ -123,7 +150,6 @@ public void nextStep(

isBroken = hmmBreak(forwardStepResult.getNewMessage());
if (isBroken) {
System.out.println("viterbi stops when executing [FORWARD STEP]");
return;
}

Expand All @@ -141,7 +167,7 @@ public void nextStep(
* @param curCandidates List of current candidate points.
* @param message Map of state probabilities.
* @param emissionLogProbabilities Map of emission probabilities for each candidate.
* @param transitionLogProbabilities Map of transition probabilities between candidates.
* @param transitionLogProbabilities Map of transition probabilities between candidates.
* @param time The current time step.
* @return Results after forward extension, including updated state probabilities and states.
*/
Expand Down Expand Up @@ -392,23 +418,28 @@ private boolean searchForNewRoot() {
* current root state to the previous root state.
*/
private void traceback() {
int currentTime = currentRoot.getTime();

System.out.println("Local path found!");
List<SequenceState> interLocalPath = new ArrayList<>();
interLocalPath.add(new SequenceState(currentRoot.getState(), currentRoot.getObservation()));
System.out.println("Current root time: " + currentTime);

List<OnlineSequenceState> interLocalPath = new ArrayList<>();
interLocalPath.add(new org.urbcomp.cupid.db.algorithm.mapmatch.onlinemm.OnlineSequenceState(currentRoot.getState(), currentRoot.getObservation(), currentTime--));

int depth = isConvergedBefore() ? currentRoot.getTime() - previousRoot.getTime() - 1 : currentRoot.getTime();
System.out.println("current root time: " + currentRoot.getTime());
ExtendedState current = currentRoot.getBackPointer();

for (int i = 0; i < depth; i++) {
interLocalPath.add(new SequenceState(current.getState(), current.getObservation()));
interLocalPath.add(new OnlineSequenceState(current.getState(), current.getObservation(), currentTime--));
current = current.getBackPointer();
}

assert current == null || current.getState() == previousRoot.getState();
System.out.println("local added sequence length: " + interLocalPath.size());

Collections.reverse(interLocalPath);
sequenceStates.addAll(interLocalPath);

System.out.println("Local added sequence length: " + interLocalPath.size());
}

/**
Expand All @@ -420,18 +451,16 @@ private void traceback() {
*/
public void tracebackLastPart(GPSPoint observation) {
System.out.println("traceback last part");
List<SequenceState> interLocalPath = new ArrayList<>();

List<OnlineSequenceState> interLocalPath = new ArrayList<>();
CandidatePoint maxValuePoint = findMaxValuePoint(message);

interLocalPath.add(new SequenceState(maxValuePoint, observation));
interLocalPath.add(new OnlineSequenceState(maxValuePoint, observation));

if (lastExtendedStates == null) return;
ExtendedState current = lastExtendedStates.get(maxValuePoint);


while (current != currentRoot) {
interLocalPath.add(new SequenceState(current.getState(), current.getObservation()));
interLocalPath.add(new OnlineSequenceState(current.getState(), current.getObservation()));
current = current.getBackPointer();
}

Expand All @@ -453,7 +482,7 @@ public LinkedList<OnlineExtendedState> getStateList() {
*
* @return A List of SequenceState objects.
*/
public List<SequenceState> getSequenceStates() {
public List<OnlineSequenceState> getSequenceStates() {
return sequenceStates;
}

Expand Down
Loading