MoveSplitMerge implements IDistanceMetric { @Override public double distance(double[] a, double[] b) { int n = a.length; int m = b.length; double[][] cost = new double[n][m]; cost[0][0] = Math.abs(a[0] - b[0]); for (int i = 1; i < n; i++) { cost[i][0] = cost[i - 1][0] + c(a[i], a[i - 1], b[0]); } for (int j = 1; j < m; j++) { cost[0][j] = cost[0][j - 1] + c(b[j], a[0], b[j - 1]); } for (int i = 1; i < n; i++) { for (int j = 1; j < m; j++) { double costMove = cost[i - 1][j - 1] + Math.abs(a[i] - b[j]); double cost2 = cost[i - 1][j] + c(a[i], a[i - 1], b[j]); double cost3 = cost[i][j - 1] + c(b[j], a[i], b[j - 1]); cost[i][j] = Math.min(costMove, Math.min(cost2, cost3)); } } return cost[n - 1][m - 1]; } MoveSplitMerge(double c); @Override double distance(double[] a, double[] b);  }
@Test public void testCorrectnessForDistanceCalculation() { double[] timeSeries1 = { 1, 1, 1, 1, 1, 1 }; double[] timeSeries2 = { 1, 1, 1, 1, 1, 1 }; double expectation = 0; MoveSplitMerge msm = new MoveSplitMerge(1.0); double distance = msm.distance(timeSeries1, timeSeries2); assertEquals(expectation, distance, 0); }
@Test public void testCorrectnessForDistanceCalculation2() { double[] timeSeries1 = { 7.0, 8.0, 12.0, 15.0, 9.0, 3.0, 5.0 }; double[] timeSeries2 = { 7.0, 9.0, 12.0, 15.0, 15.0, 9.0, 3.0 }; double expectation = 11; double c = 4.0; MoveSplitMerge msm = new MoveSplitMerge(c); double distance = msm.distance(timeSeries1, timeSeries2); assertEquals(expectation, distance, 0); }