package com.example.demo2.utils.dtw_lcss;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
@Data
@AllArgsConstructor
@NoArgsConstructor
public class Coordinate {
private double x;
private double y;
}
package com.example.demo2.utils.dtw_lcss;
import java.text.DecimalFormat;
import java.util.Arrays;
import java.util.List;
public class DTW {
//一维比较
//s1 = [1, 2, 3, 4, 5, 5, 5, 4]
//s2 = [3, 4, 5, 5, 5, 4]
public void DTW_1(int[] s1, int[] s2) {
int r = s1.length;
int c = s2.length;
//计算距离矩阵M
int[][] D0 = new int[r + 1][c + 1];
for (int i = 0; i < r + 1; i++) {
for (int j = 0; j < c + 1; j++) {
if (i == 0 && j == 0) {
D0[i][j] = 0;
} else if (i == 0) {
D0[i][j] = Integer.MAX_VALUE;
} else if (j == 0) {
D0[i][j] = Integer.MAX_VALUE;
} else {
D0[i][j] = Math.abs(s1[i - 1] - s2[j - 1]);
}
}
}
int[][] D = new int[r][c];
for (int i = 0; i < r; i++) {
for (int j = 0; j < c; j++) {
D[i][j] = D0[i + 1][j + 1];
}
}
System.out.println("距离矩阵D:");
System.out.println(Arrays.deepToString(D).replaceAll("],", "]," + System.getProperty("line.separator")));
//计算损失矩阵M
int[][] MC = D0.clone();
for (int i = 1; i < r + 1; i++) {
for (int j = 1; j < c + 1; j++) {
MC[i][j] += Math.min(Math.min(MC[i - 1][j - 1], MC[i][j - 1]), MC[i - 1][j]);
}
}
int[][] M = new int[r][c];
for (int i = 0; i < r; i++) {
for (int j = 0; j < c; j++) {
M[i][j] = MC[i + 1][j + 1];
}
}
System.out.println("损失矩阵M:");
System.out.println(Arrays.deepToString(M).replaceAll("],", "]," + System.getProperty("line.separator")));
System.out.println("序列距离:" + M[r - 1][c - 1]);
}
public static double getDistance(Coordinate p, Coordinate q) {
double dx = p.getX() - q.getX();
double dy = p.getY() - q.getY();
double distance = Math.sqrt(dx * dx + dy * dy);
return distance;
}
public void DTW_2(List<Coordinate> coords1, List<Coordinate> coords2) {
int r = coords1.size();
int c = coords2.size();
//计算距离矩阵M
double[][] D0 = new double[r + 1][c + 1];
for (int i = 0; i < r + 1; i++) {
for (int j = 0; j < c + 1; j++) {
if (i == 0 && j == 0) {
D0[i][j] = 0;
} else if (i == 0) {
D0[i][j] = Double.MAX_VALUE;
} else if (j == 0) {
D0[i][j] = Double.MAX_VALUE;
} else {
// D0[i][j] = coords1.get(i - 1).distance(coords2.get(j - 1));
D0[i][j] =getDistance(coords1.get(i - 1), coords2.get(j - 1));
}
}
}
DecimalFormat df = new DecimalFormat("#.00");
double[][] D = new double[r][c];
for (int i = 0; i < r; i++) {
for (int j = 0; j < c; j++) {
D[i][j] = Double.parseDouble(df.format(D0[i + 1][j + 1]));
}
}
System.out.println("距离矩阵D:");
System.out.println(Arrays.deepToString(D).replaceAll("],", "]," + System.getProperty("line.separator")));
//计算损失矩阵M
double[][] MC = D0.clone();
for (int i = 1; i < r + 1; i++) {
for (int j = 1; j < c + 1; j++) {
MC[i][j] += Math.min(Math.min(MC[i - 1][j - 1], MC[i][j - 1]), MC[i - 1][j]);
}
}
double[][] M = new double[r][c];
for (int i = 0; i < r; i++) {
for (int j = 0; j < c; j++) {
M[i][j] = MC[i + 1][j + 1];
}
}
System.out.println("损失矩阵M:");
System.out.println(Arrays.deepToString(M).replaceAll("],", "]," + System.getProperty("line.separator")));
System.out.println("序列距离:" + M[r - 1][c - 1]);
}
}
package com.example.demo2.utils.dtw_lcss;
import java.util.ArrayList;
import java.util.List;
public class LCSS {
private List<Coordinate> l1;
private List<Coordinate> l2;
private List<Coordinate> lcs = new ArrayList<>();
public static double getDistance(Coordinate p, Coordinate q) {
double dx = p.getX() - q.getX();
double dy = p.getY() - q.getY();
double distance = Math.sqrt(dx * dx + dy * dy);
return distance;
}
public boolean isNearby(Coordinate a, Coordinate b) {
if (getDistance(a, b) < 50) return true;
return false;
}
public void printLcs(int[][] flag, List<Coordinate> a, int i, int j) {
if (i == 0 || j == 0) return;
if (flag[i][j] == 1) {
printLcs(flag, a, i - 1, j - 1);
lcs.add(a.get(i - 1));
} else if (flag[i][j] == 2) {
printLcs(flag, a, i, j - 1);
} else {
printLcs(flag, a, i - 1, j);
}
}
public double lcs(List<Coordinate> l1, List<Coordinate> l2) {
int len1 = l1.size();
int len2 = l2.size();
int[][] c = new int[len1 + 1][len2 + 1];
int[][] flag = new int[len1 + 1][len2 + 1];
for (int i = 0; i < len1; i++) {
for (int j = 0; j < len2; j++) {
if (isNearby(l1.get(i), l2.get(j))) {
c[i + 1][j + 1] = c[i][j] + 1;
flag[i + 1][j + 1] = 1;//1='ok'
} else if (c[i + 1][j] > c[i][j + 1]) {
c[i + 1][j + 1] = c[i + 1][j];
flag[i + 1][j + 1] = 2;//2='left'
} else {
c[i + 1][j + 1] = c[i][j + 1];
flag[i + 1][j + 1] = 3;//3='up'
}
}
}
printLcs(flag, l1, len1, len2);
//归一化处理
return (lcs.size() * 1.0 / Math.min(len1, len2));
}
public static void main(String[] args) {
List<Coordinate> l1 = new ArrayList<>();
l1.add(new Coordinate(114.300, 30.1));
l1.add(new Coordinate(114.302, 30.101));
l1.add(new Coordinate(114.3023, 30.1002));
l1.add(new Coordinate(114.30235, 30.1011));
l1.add(new Coordinate(114.304, 30.1003));
List<Coordinate> l2 = new ArrayList<>();
l2.add(new Coordinate(114.301, 30.1002));
l2.add(new Coordinate(114.3023, 30.1015));
LCSS lcss = new LCSS();
System.out.println(lcss.lcs(l1, l2));
}
}