-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCluster.cpp
139 lines (123 loc) · 3.31 KB
/
Cluster.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
#include <fstream>
#include "Cluster.h"
using namespace std;
int Find(vector<int> &a, int index)
{
int ret;
if( a[index] < 0)
ret = index;
else
ret = Find(a, a[index]);
return ret;
}
void Union(vector<int> &a, int c1, int c2)
{
if( a[c1] < a[c2] ){
a[c1] += a[c2];
a[c2] = c1;
}
else{
a[c2] += a[c1];
a[c1] = c2;
}
}
// 聚类函数,输入phase的向量以及聚类目标数k,返回聚类结果的并查集
std::vector<Cluster> AHCluster(vector<Phase> v_phase, int k){
vector<int> Set;
vector<dist> v_dist;
int cur_k = v_phase.size();
// initialize the disjoint set
for (int i = 0; i <= cur_k; ++i)
Set.push_back(-1);
// initialize the vector of distances
for (int i = 0; i < cur_k; i++)
for (int j = 0; j < i; ++j)
{
double delta_d = getDeltaDistance(v_phase[i], v_phase[j]);
dist d(delta_d, i + 1, j + 1);
v_dist.push_back(d);
}
// clustering using the distance and the disjoint set
sort(v_dist.begin(), v_dist.end());
for (std::vector<dist>::iterator ptr = v_dist.begin(); ptr != v_dist.end(); ++ptr)
{
if(cur_k <= k)
break;
if( Find(Set, ptr->i1) == Find(Set, ptr->i2) )
continue;
else{
Union(Set, Find(Set, ptr->i1), Find(Set, ptr->i2));
cur_k --;
}
}
// genertaing clusters and counting shot numbers using the results above
vector<Cluster> v_cluster;
cout << Set.size() << endl;
for (int i = 1; i < Set.size(); ++i)
{
bool flag = insertPhase(v_cluster ,v_phase[i-1], Find(Set, i) );
if(!flag){
int root = Find(Set, i);
Cluster c(root);
v_cluster.push_back(c);
flag = insertPhase(v_cluster ,v_phase[i-1], Find(Set, i) );
if(!flag)
cout << "Phase classification error at index: " << i << endl;
}
}
cout << "Cluster over" << endl;
// sort according to shot number
sort(v_cluster.begin(), v_cluster.end());
return v_cluster;
}
// insert a phase into the cluster it belongs
bool insertPhase(vector<Cluster> &v, Phase p, int r){
for (std::vector<Cluster>::iterator i = v.begin(); i != v.end(); ++i){
if(i->root == r){
i->phases.push_back(p);
//counting shots here
if ( (p.end() - 1)->event_type >= 35 ){
cout << "shot detected" << endl;
i->shot ++;
}
return true;
}
}
return false;
}
void printClusters(std::vector<Cluster> v)
{
cout << "Cluster Number: " << v.size() << endl;
for (std::vector<Cluster>::iterator i = v.begin(); i != v.end(); ++i)
{
cout << "root:" << i->root << " shot:" << i->shot << endl;
for (vector<Phase>::iterator j = i->phases.begin(); j != i->phases.end(); ++j){
for (std::vector<Event>::iterator ie = (*j).begin(); ie != (*j).end(); ++ie)
{
cout << ie->team << " " << ie->event_type << " |";
}
cout << endl;
}
cout << "#########" << endl;
}
}
// write results to files with certain formats
void outputClusters(std::vector<Cluster> v, string team)
{
for (std::vector<Cluster>::iterator i = v.begin(); i != v.end(); ++i)
{
int index = i - v.begin();
string filepath = "Cluster\\" + team + "Cluster" + to_string(index) + ".txt";
ofstream file(filepath);
for (vector<Phase>::iterator j = i->phases.begin(); j != i->phases.end(); ++j){
file << '[' ;
for (std::vector<Event>::iterator ie = (*j).begin(); ie < (*j).end() - 1; ++ie)
{
file << ie->event_type << ",";
}
file << ((*j).end() - 1)->event_type;
file << "]" << endl;
}
file.close();
}
}