-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTiDbscanRef.cpp
267 lines (182 loc) · 6.78 KB
/
TiDbscanRef.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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
/*
============================================================================
Name : TiDbscanRef.cpp
Author : Bart³omiej Jañczak
Date : 2012-09-21
Version : 1.0
Copyright : Your copyright notice
Description : Implementation of TiDbscanRef.h
============================================================================
*/
#include "TiDbscanRef.h"
#include <algorithm>
#include <cmath>
#include <ctime>
TiDbscanRef::TiDbscanRef():TiDbscanBase(){
this->algorithmName = "TiDbscanRef";
}
TiDbscanRef::TiDbscanRef(const TiDbscanRef& object):TiDbscanBase(object){
this->algorithmName = "TiDbscanRef";
}
TimeReport TiDbscanRef::run(const Properties& properties, Dataset& dataset){
TimeReport timeReport;
const vector<Point> *referencePoints = &properties.referencePoints;
vector<Point>::const_iterator referencePointsIt;
vector<Point>::const_iterator referencePointsEnd = referencePoints->end();
vector<DbscanPoint> *tempDataset = &dataset.datasetDbscanPoint;
vector<DbscanPoint>::iterator it;
vector<DbscanPoint>::iterator end = tempDataset->end();
list<vector<DbscanPoint>::iterator> datasetIterators;
list<vector<DbscanPoint>::iterator>::iterator datasetIteratorsIt;
unsigned long clusterId = 1;
clock_t distanceCalculationStart;
clock_t distanceCalculationFinish;
clock_t sortingStart;
clock_t sortingFinish;
clock_t clusteringStart;
clock_t clusteringFinish;
this->eps = properties.eps;
this->minPts = properties.minPts;
/*
* Distance to reference points calculation.
*/
distanceCalculationStart = clock();
for(it = tempDataset->begin(); it != end; it++){
for(referencePointsIt = referencePoints->begin(); referencePointsIt!= referencePointsEnd; referencePointsIt++){
(*it).distance.push_back(Point::minkowskiDistance(*referencePointsIt, (*it), 2));
}
(*it).neighborsNr = 1;
}
distanceCalculationFinish = clock();
if(properties.isUseDatasetIndexAcess){
/*
* Build working index.
*/
for(it = tempDataset->begin(); it != end; it++){
datasetIterators.push_back(it);
}
/*
* Sorting points by distance to reference point.
*/
sortingStart = clock();
datasetIterators.sort(DbscanPoint::distanceComparatorIterator);
sortingFinish = clock();
}
else{
/*
* Sorting points by distance to reference point.
*/
sortingStart = clock();
sort(tempDataset->begin(), tempDataset->end(), DbscanPoint::distanceComparator);
sortingFinish = clock();
/*
* Build working index.
*/
for(it = tempDataset->begin(); it != end; it++){
datasetIterators.push_back(it);
}
}
/*
* Clustering.
*/
clusteringStart = clock();
while(datasetIterators.size() > 0){
datasetIteratorsIt = datasetIterators.begin();
if(indexExpandCluster(datasetIterators, datasetIteratorsIt, clusterId, TiDbscanRef::indexTiNeighborhood)){
clusterId++;
}
}
clusteringFinish = clock();
timeReport.clusteringExecutionTime = ((double)(clusteringFinish - clusteringStart))/CLOCKS_PER_SEC;
timeReport.distanceCalculationExecutionTime = ((double)(distanceCalculationFinish - distanceCalculationStart))/CLOCKS_PER_SEC;
timeReport.sortingPointsExecutionTime = ((double)(sortingFinish - sortingStart))/CLOCKS_PER_SEC;
timeReport.algorithmExecutionTime = timeReport.clusteringExecutionTime + timeReport.distanceCalculationExecutionTime + timeReport.sortingPointsExecutionTime;
return timeReport;
}
list<list<vector<DbscanPoint>::iterator>::iterator> TiDbscanRef::indexTiNeighborhood(list<vector<DbscanPoint>::iterator>& setOfPoints, list<vector<DbscanPoint>::iterator>::iterator pointIt, const double eps){
list<list<vector<DbscanPoint>::iterator>::iterator> forwardNeighborhood = TiDbscanRef::indexTiForwardNeighborhood(setOfPoints, pointIt, eps);
list<list<vector<DbscanPoint>::iterator>::iterator> backwardNeighborhood = TiDbscanRef::indexTiBackwardNeighborhood(setOfPoints, pointIt, eps);
list<list<vector<DbscanPoint>::iterator>::iterator> result;
list<list<vector<DbscanPoint>::iterator>::iterator>::iterator it;
list<list<vector<DbscanPoint>::iterator>::iterator>::iterator end;
list<vector<DbscanPoint>::iterator>::iterator datasetEnd = setOfPoints.end();
list<vector<DbscanPoint>::iterator>::iterator datasetIt;
end = forwardNeighborhood.end();
for(it = forwardNeighborhood.begin(); it != end; it++){
result.push_back(*it);
}
end = backwardNeighborhood.end();
for(it = backwardNeighborhood.begin(); it != end; it++){
result.push_back(*it);
}
return result;
}
list<list<vector<DbscanPoint>::iterator>::iterator> TiDbscanRef::indexTiForwardNeighborhood(list<vector<DbscanPoint>::iterator>& setOfPoints, list<vector<DbscanPoint>::iterator>::iterator pointIt, const double eps){
list<list<vector<DbscanPoint>::iterator>::iterator> result;
if(pointIt == setOfPoints.end()){
return result;
}
else{
double forwardTreshold = (*pointIt)->distance[0] + eps;
list<vector<DbscanPoint>::iterator>::iterator it = pointIt;
it++;
if(it == setOfPoints.end()){
return result;
}
else{
list<vector<DbscanPoint>::iterator>::iterator end = setOfPoints.end();
for(it; it != end; it++){
if((*it)->distance[0] > forwardTreshold){
break;
}
bool candidateNeighbour = indexIsCandidateNeighborByAdditionalReferencePoints(pointIt, it, eps);
if(candidateNeighbour && (Point::minkowskiDistance(**it, **pointIt, 2) <= eps)){
result.push_back(it);
}
}
return result;
}
}
}
list<list<vector<DbscanPoint>::iterator>::iterator> TiDbscanRef::indexTiBackwardNeighborhood(list<vector<DbscanPoint>::iterator>& setOfPoints, list<vector<DbscanPoint>::iterator>::iterator pointIt, const double eps){
list<list<vector<DbscanPoint>::iterator>::iterator> result;
if(pointIt == setOfPoints.begin()){
return result;
}
else{
double backwardTreshold = (*pointIt)->distance[0] - eps;
list<vector<DbscanPoint>::iterator>::iterator it = pointIt;
list<vector<DbscanPoint>::iterator>::iterator begin = setOfPoints.begin();
it--;
while(true){
if((*it)->distance[0] < backwardTreshold){
break;
}
bool candidateNeighbour = indexIsCandidateNeighborByAdditionalReferencePoints(pointIt, it, eps);
if(candidateNeighbour && (Point::minkowskiDistance(*(*it), *(*pointIt), 2) <= eps)){
result.push_back(it);
}
if(it == begin){
break;
}
it--;
}
return result;
}
}
bool TiDbscanRef::indexIsCandidateNeighborByAdditionalReferencePoints(
list<vector<DbscanPoint>::iterator>::iterator pointIt
, list<vector<DbscanPoint>::iterator>::iterator queryPointIt
, double eps){
bool isCandidateNeighbor = true;
unsigned long i = 1;
while (isCandidateNeighbor && (i < (*pointIt)->distance.size())){
if(abs((*queryPointIt)->distance[i] - (*pointIt)->distance[i]) > eps ){
isCandidateNeighbor = false;
}
else{
i++;
}
}
return isCandidateNeighbor;
}