-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathedmstream.hpp
More file actions
158 lines (135 loc) · 4.13 KB
/
Copy pathedmstream.hpp
File metadata and controls
158 lines (135 loc) · 4.13 KB
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
/*
* Copyright 2024 IntelliStream team (https://github.com/intellistream)
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#ifndef PDSC_EDMSTREAM_HPP
#define PDSC_EDMSTREAM_HPP
#include "algorithm.hpp"
#include "common.hpp"
// const int MAX_CLUSTERS = 100;
const double DECAY_RATE = 0.01;
const double DENSITY_THRESHOLD = 0.9;
struct ClusterCell {
Point seed;
double density = 0.0;
const double dependent_distance = 500000.0;
double creation_time = 0.0;
ClusterCell(int dimensions) : seed(dimensions) {}
void addPoint(const Point &point) {
for (int i = 0; i < point.features.size(); i++) {
seed.features[i] += point.features[i];
}
density += 1.0; // Simplified for illustration
seed.timestamp = point.timestamp;
}
void decayDensity() { density *= exp(-DECAY_RATE); }
double calcDistance(const Point &point) const {
double dist = 0.0;
for (int i = 0; i < point.features.size(); i++) {
dist += (point.features[i] - seed.features[i]) *
(point.features[i] - seed.features[i]);
}
return sqrt(dist);
}
};
class DPNode {
public:
ClusterCell cell;
std::vector<DPNode *> children;
DPNode(const ClusterCell &c) : cell(c) {}
};
class DPTree {
public:
DPNode *root;
DPTree() : root(nullptr) {}
void addClusterCell(const ClusterCell &cell) {
if (!root) {
root = new DPNode(cell);
} else {
addClusterCellRecursive(root, cell);
}
}
void decayClusters(double current_time) {
decayClustersRecursive(root, current_time);
}
private:
void addClusterCellRecursive(DPNode *node, const ClusterCell &cell) {
double dist = node->cell.calcDistance(cell.seed);
if (dist < node->cell.dependent_distance) {
DPNode *child = new DPNode(cell);
node->children.push_back(child);
} else {
for (auto &child : node->children) {
addClusterCellRecursive(child, cell);
}
}
}
void decayClustersRecursive(DPNode *node, double current_time) {
if (!node)
return;
node->cell.decayDensity();
if (node->cell.density < DENSITY_THRESHOLD) {
node->children.clear();
}
for (auto &child : node->children) {
decayClustersRecursive(child, current_time);
}
}
};
class EDMStream : public Algorithm {
public:
EDMStream(int dimensions) : dimensions(dimensions), dp_tree(new DPTree()) {}
int point_count = 0;
void insert(const Point &point) {
// Decay existing clusters
this->point_count++;
if (this->point_count % 200 == 0)
dp_tree->decayClusters(point.timestamp);
// dp_tree->decayClusters(point.timestamp);
// Create a new cluster cell
ClusterCell newCell(dimensions);
newCell.addPoint(point);
newCell.creation_time = point.timestamp;
// Insert into DP-Tree
dp_tree->addClusterCell(newCell);
}
void cluster(const std::vector<Point> &points) {
for (const auto &point : points) {
insert(point);
}
}
std::vector<Point> output_centers() {
std::vector<Point> centers;
output_centers_recursive(dp_tree->root, centers);
return centers;
}
void output_centers_recursive(DPNode *node, std::vector<Point> ¢ers) {
if (!node)
return;
// if (node->cell.density > DENSITY_THRESHOLD * 10^(-29)) {
// std::cout << "density: " << node->cell.density << std::endl;
Point center(node->cell.seed.features);
center /= node->cell.density;
centers.push_back(center);
// }
// std::cout << "children size: " << node->children.size() << std::endl;
for (auto &child : node->children) {
output_centers_recursive(child, centers);
}
}
private:
int dimensions;
DPTree *dp_tree;
};
#endif // PDSC_EDMSTREAM_HPP