-
Notifications
You must be signed in to change notification settings - Fork 0
/
exactS.cpp
187 lines (172 loc) · 6.95 KB
/
exactS.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
//
// Created by 86173 on 2022/1/27.
//
#include "exactS.h"
int pathend = 0;
int tmpend = 0;
int pathstart = 0;
double exactWedDistance(path path1, path path2, int start, int end) {
auto distanceMatrix = new double[end - start + 1];
auto distanceMatrixTmp = new double[end - start + 1];
double minDistance = 1000000;
distanceMatrixTmp[0] = 0;
for (int j = start + 1; j < end + 1; ++j) {
distanceMatrixTmp[j - start] = distanceMatrixTmp[j - start - 1] + distance(path2[j - 1], nullPoint);
}
for (int i = 1; i < path1.size() + 1; ++i) {
for (int j = start; j < end + 1; ++j) {
if (j == start) {
distanceMatrix[0] = distanceMatrixTmp[0] + distance(path1[i-1], nullPoint);
continue;
}
distanceMatrix[j - start] = min(distanceMatrixTmp[j - start - 1] + distance(path1[i-1], path2[j-1]),
min(distanceMatrix[j - start - 1] + distance(nullPoint, path2[j-1]),
distanceMatrixTmp[j - start] + distance(nullPoint, path1[i-1])));
}
swap(distanceMatrixTmp, distanceMatrix);
}
for (int i = 1; i < end - start + 1; ++i) {
if (distanceMatrixTmp[i] < AR) MR += 1;
if (distanceMatrixTmp[i] < minDistance) {
tmpend = i - 1;
minDistance = distanceMatrixTmp[i];
}
}
delete[] distanceMatrixTmp;
delete[] distanceMatrix;
return minDistance;
}
double exactFC(path path1, path path2, int start, int end) {
auto distanceMatrix = new double[end - start + 1];
auto distanceMatrixTmp = new double[end - start + 1];
double minDistance = 1000000;
distanceMatrixTmp[0] = 0;
for (int j = start + 1; j < end + 1; ++j) {
distanceMatrixTmp[j - start] = max(distanceMatrixTmp[j - start - 1], pointDistance(path2[j - 1], path1[start]));
}
for (int i = 1; i < path1.size() + 1; ++i) {
for (int j = start; j < end + 1; ++j) {
if (j == start) {
distanceMatrix[0] = max(distanceMatrixTmp[0], pointDistance(path1[i-1], path2[start]));
continue;
}
distanceMatrix[j - start] = max(pointDistance(path1[i-1], path2[j-1]), min(distanceMatrixTmp[j - start - 1],
min(distanceMatrix[j - start - 1],
distanceMatrixTmp[j - start])));
}
swap(distanceMatrixTmp, distanceMatrix);
}
for (int i = 1; i < end - start + 1; ++i) {
if (distanceMatrixTmp[i] < AR) MR += 1;
if (distanceMatrixTmp[i] < minDistance) {
tmpend = i - 1;
minDistance = distanceMatrixTmp[i];
}
}
delete[] distanceMatrixTmp;
delete[] distanceMatrix;
return minDistance;
}
double exactLcssDistance(path path1, path path2, int start, int end) {
auto distanceMatrix = new double[end - start + 1];
auto distanceMatrixTmp = new double[end - start + 1];
double minDistance = 1000000;
distanceMatrixTmp[0] = 0;
for (int j = start + 1; j < end + 1; ++j) {
distanceMatrixTmp[j - start] = distanceMatrixTmp[j - start - 1] + distance(path2[j - 1], nullPoint);
}
for (int i = 1; i < path1.size() + 1; ++i) {
for (int j = start; j < end + 1; ++j) {
if (j == start) {
distanceMatrix[0] = distanceMatrixTmp[0] + distance(path1[i-1], nullPoint);
continue;
}
distanceMatrix[j - start] = min(distanceMatrix[j - start - 1] + distance(nullPoint, path2[j-1]),
distanceMatrixTmp[j - start] + distance(nullPoint, path1[i-1]));
if (abs(i - (j - start + 1)) < minThreshold) {
distanceMatrix[j - start] = min(distanceMatrix[j - start], distanceMatrixTmp[j - start - 1] + distance(path1[i-1], path2[j-1]));
}
}
swap(distanceMatrixTmp, distanceMatrix);
}
for (int i = 1; i < end - start + 1; ++i) {
if (distanceMatrixTmp[i] < minDistance) {
tmpend = i - 1;
minDistance = distanceMatrixTmp[i];
}
}
delete[] distanceMatrixTmp;
delete[] distanceMatrix;
return minDistance;
}
double exactDTW(path path1, path path2, int start, int end) {
auto distanceMatrix = new double[end - start + 1];
auto distanceMatrixTmp = new double[end - start + 1];
double minDistance = 1000000;
distanceMatrixTmp[0] = 0;
for (int j = start + 1; j < end + 1; ++j) {
distanceMatrixTmp[j - start] = distanceMatrixTmp[j - start - 1] + pointDistance(path2[j - 1], path1[start]);
}
for (int i = 1; i < path1.size() + 1; ++i) {
for (int j = start; j < end + 1; ++j) {
if (j == start) {
distanceMatrix[0] = distanceMatrixTmp[0] + pointDistance(path1[i-1], path2[start]);
continue;
}
distanceMatrix[j - start] = min(distanceMatrixTmp[j - start - 1] + pointDistance(path1[i-1], path2[j-1]),
min(distanceMatrix[j - start - 1] + pointDistance(path1[i-1], path2[j-1]),
distanceMatrixTmp[j - start] + pointDistance(path1[i-1], path2[j-1])));
}
swap(distanceMatrixTmp, distanceMatrix);
}
for (int i = 1; i < end - start + 1; ++i) {
if (distanceMatrixTmp[i] < AR) MR += 1;
if (distanceMatrixTmp[i] < minDistance) {
tmpend = i - 1;
minDistance = distanceMatrixTmp[i];
}
}
delete[] distanceMatrixTmp;
delete[] distanceMatrix;
return minDistance;
}
subResult exactS(const path& p1, const path& p2) {
double minDistance = 1000000;
for (int i = 0; i < p2.size() - 1; ++i) {
double distance;
if (matricsType == "dtw") {
distance = exactDTW(p1, p2, i, p2.size());
} else if (matricsType == "lcss") {
distance = exactLcssDistance(p1, p2, i, p2.size());
} else if (matricsType == "FC"){
distance = exactFC(p1, p2, i, p2.size());
} else {
distance = exactWedDistance(p1, p2, i, p2.size());
}
if (minDistance > distance) {
pathstart = i;
pathend = pathstart + tmpend;
minDistance = distance;
}
}
subResult r;
r.first.first = pathstart;
r.first.second = pathend;
r.second = minDistance;
return r;
}
void calScore(const path& p1, const path& p2, int start, int end) {
MR = 1, RR = 0;
for (int i = 0; i < p2.size() - 1; ++i) {
if (matricsType == "dtw") {
exactDTW(p1, p2, i, p2.size());
} else if (matricsType == "FC"){
exactFC(p1, p2, i, p2.size());
} else if (matricsType == "lcss") {
exactLcssDistance(p1, p2, i, p2.size());
} else {
exactWedDistance(p1, p2, i, p2.size());
}
}
RR = MR / (p2.size() * (p2.size() + 1) / 2);
}