-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmfcm.cpp
195 lines (187 loc) · 4.93 KB
/
mfcm.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
#include <bits/stdc++.h>
using namespace std;
using namespace std::chrono;
double** myImg;
double* oldMeans;
double* newMeans;
double*** membershipMatix;
double*** spatialMatix;
ifstream fin("sample.txt");
int w,h,p,q,K,C;
double square(double x){
return x*x;
}
bool checkConv(double eps){ // checks if the convergence is reached
bool temp = true;
for(int i = 0; i < K; i++){
if(abs(newMeans[i] - oldMeans[i]) > eps)
temp = false;
oldMeans[i] = newMeans[i];
newMeans[i] = 0;
}
return temp;
}
void fcm(int x_start, int x_end, int y_start, int y_end){ //calculates initial membership Matrix without considering spatial information
for(int i = x_start; i <= x_end; i++){
for(int j = y_start; j <= y_end; j++){
for(int k = 0; k < C; k++){
double temp = 0;
for(int l = 0; l < C; l++)
temp += square((myImg[i][j] - oldMeans[k])/(myImg[i][j] - oldMeans[l] + 1e-5));
membershipMatix[i][j][k] = 1.0/(temp + 1e-5);
}
}
}
}
void sfcm(int x_start, int x_end, int y_start, int y_end){ //calculates membership Matrix after considering the spatial information using
for(int i = x_start; i <= x_end; i++){ //using previously calculated membership matrix
for(int j = y_start; j <= y_end; j++){
for(int k = 0; k < C; k++){
double temp = 0;
for(int b1 = max(i-2,0); b1 < min(i+3, h); b1++){
for(int b2 = max(j-2, 0); b2 < min(j+3,w); b2++)
temp += membershipMatix[b1][b2][k];
}
spatialMatix[i][j][k] = temp;
}
}
}
for(int i = x_start; i <= x_end; i++){
for(int j = y_start; j <= y_end; j++){
double sum = 0;
for(int k = 0; k < C; k++)
sum += pow(membershipMatix[i][j][k],p) * pow(spatialMatix[i][j][k],q);
for(int k = 0; k < K; k++)
membershipMatix[i][j][k] = (pow(membershipMatix[i][j][k],p) * pow(spatialMatix[i][j][k],q))/(sum + 1e-5);
}
}
}
void update(int idx){
double num = 0;
double den = 0;
for(int i = 0; i < h; i++){
for(int j = 0; j < w; j++){
num += square(membershipMatix[i][j][idx]) * myImg[i][j];
den += square(membershipMatix[i][j][idx]);
}
}
newMeans[idx] = num/(den+1e-5);
}
int main(){
double epsilon;
srand(time(NULL));
cout << "Width and Height: ";
cin >> w >> h;
cout << "No. of clusters: ";
cin >> C;
cout << "No. of threads: ";
cin >> K;
cout << "Threshold for convergence: ";
cin >> epsilon;
cout << "p and q: ";
cin >> p >> q;
oldMeans = new double[C];
newMeans = new double[C];
myImg = new double*[h];
membershipMatix = new double**[h];
spatialMatix = new double**[h];
for(int i = 0; i < h; i++){
myImg[i] = new double[w];
//cout << "Hello" << endl;
string params;
getline(fin, params);
istringstream ss(params);
membershipMatix[i] = new double*[w];
spatialMatix[i] = new double*[w];
for(int j = 0; j < w; j++){
ss >> myImg[i][j];
membershipMatix[i][j] = new double[C];
spatialMatix[i][j] = new double[C];
for(int k = 0; k < C; k++){
membershipMatix[i][j][k] = 0;
spatialMatix[i][j][k] = 0;
}
}
}
int randrows[h] = {0};
int randcols[w] = {0};
iota(randrows,randrows+h,0);
iota(randcols,randcols+w,0);
random_shuffle(randrows,randrows+h);
random_shuffle(randcols,randcols+w);
cout << "Initial means:" << endl;
for(int i = 0; i < C; i++){ //randomly select K distinct datapoints as initial means
oldMeans[i] = myImg[randrows[i]][randcols[i]];
cout << oldMeans[i] << endl;
newMeans[i] = 0;
}
int iter = 0;
auto tstart = high_resolution_clock::now();
while(!checkConv(epsilon)){
thread tids[K];
int wstep = w/K;
int hstep = h/K;
int wleft = w%K;
int hleft = h%K;
int xstart = 0;
int ystart = 0;
int xend = 0;
int yend = 0;
//cout << hstep << " " << wstep << endl;
for(int k = 0; k < K; k++){
xend = xstart + hstep;
yend = ystart + wstep;
if(hleft <= 0){
xend--;
}
if(wleft <= 0){
yend--;
}
hleft--;
wleft--;
tids[k] = thread(fcm, xstart, xend, ystart, yend);
//cout << xstart << " " << xend << " " << ystart << " " << yend << endl;
xstart = xend+1;
ystart = yend+1;
}
for(auto& t : tids)
t.join();
xstart = 0;
ystart = 0;
xend = 0;
yend = 0;
for(int k = 0; k < K; k++){
xend = xstart + hstep;
yend = ystart + wstep;
if(hleft <= 0){
xend--;
}
if(wleft <= 0){
yend--;
}
hleft--;
wleft--;
tids[k] = thread(sfcm, xstart, xend, ystart, yend);
//cout << xstart << " " << xend << " " << ystart << " " << yend << endl;
xstart = xend+1;
ystart = yend+1;
}
for(auto& t : tids)
t.join();
thread tid2[C];
for(int k = 0; k < C; k++)
tid2[k] = thread(update, k);
for(auto& t : tid2)
t.join();
cout << iter++ << endl;
}
auto tend = high_resolution_clock::now();
auto duration = duration_cast<microseconds>(tend-tstart);
cout<<"time : "<<duration.count()<<endl;
cout << iter << endl;
cout << "New means:" << endl;
for(int i = 0; i < C; i++)
cout << oldMeans[i] << endl;
fin.close();
return 0;
}