/
FOREL.cpp
250 lines (215 loc) · 6.68 KB
/
FOREL.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
#include "FOREL.h"
#include <stdlib.h>
#include <string>
#include <string.h>
#include <fstream>
#include <math.h>
#include "time.h"
int num_of_col(char* fname)
{
ifstream file(fname);
if (!file)
{
cerr<<"Ошибка открытия файла: "<<fname<<endl;
exit(3);
}
else
{
int numofcol = 0;
char* tmp = new char[255]; //first string is taken as example
file.getline(tmp, 255, '\n');
file.close();
int j = strlen(tmp);
for(int i = 0; i<j-1; ++i) //counting columns
{
while (tmp[i] == ' ')
{
++i;
}
++numofcol;
while ( (tmp[i] != ' ') && (i<j-1) )
{
++i;
}
}
return numofcol;
}
}
float Euclidean(vector<float> from, vector<float> to, int propnum) //euclidean distance
{
float sum = 0;
for (int i = 0; i < propnum; ++i)
{
sum += pow(from[i] - to[i] ,2);
}
return sqrt(sum);
}
//Claster
void Claster::add(vector<float>& object) //adding object
{
this->objects.push_back(object);
}
Claster::Claster(char* fname)
{
char tmp[15];
propnum = num_of_col(fname); //number of properties
ifstream file(fname);
if(!file)
{
cerr<<"Ошибка открытия файла: "<<fname<<endl;
exit(3);
}
vector<float> newobjpropert;
while ( !file.eof() )
{
for (int i=0; i < propnum; ++i)
{
file >> tmp;
newobjpropert.push_back(atof(tmp));
}
objects.push_back(newobjpropert);
newobjpropert.clear();
}
objects.pop_back(); //end of file is shifted by one row
}
void Claster::standartization()
{
float tmp = 0;
vector<float> xj_, Sj;
// filling in vector of mean value for every property of all objects and filling in vector Sj
for (int alongpr = 0; alongpr < propnum; ++alongpr) //looping by properties
{
xj_.push_back(0);
for (int alongobj = 0; alongobj < objects.size(); ++alongobj) //loop by objects
{
xj_[xj_.size() - 1] += objects[alongobj][alongpr]; //sum of к-th property of all objects
}
xj_[xj_.size() - 1] = xj_[xj_.size() - 1] / objects.size();
Sj.push_back(0);
for (int alongobj = 0; alongobj < objects.size(); ++alongobj) //sum of square (xij - xj_)^2 proper by all of objects
{
tmp += pow( ( objects[alongobj][alongpr] - xj_[xj_.size() - 1] ), 2 );
}
Sj[Sj.size() - 1] = sqrt( tmp / (objects.size()-1) );
tmp = 0;
}
//changing of values of matrix
for (int i = 0; i < objects.size(); ++i) //by objects
{
for (int k = 0; k < propnum; ++k) //by properties
{
objects[i][k] = ( (objects[i][k] - xj_[k]) / Sj[k] );
}
}
}
vector<float> Claster::count_center() //counting center of gravity of sphere
{
vector<float> newcenter;
float sum = 0;
for (int k=0; k<this->propnum; ++k) //loop by each property
{
sum = 0;
for (int i=0; i<this->objects.size(); ++i)//loop by rows
{
sum += objects[i][k]; //summing properties
}
newcenter.push_back(sum/objects.size()); //mean value of property
}
return newcenter;
}
void Claster::show()
{
for (int i=0; i<this->objects.size();++i)
{
for (int j=0;j<this->propnum; ++j)
{
cout<<this->objects[i][j]<<" ";
}
cout<<endl;
}
}
//Forel
Forel::Forel()
{
this->claster = new Claster();
}
Forel::Forel(char* fname)
{
this->claster = new Claster(fname);
}
Forel::~Forel()
{
delete(this->claster);
}
vector< Claster* > Forel::clustering(float R)
{
int M = rand() % this->claster->objects.size();
vector< Claster* > after_clustering; //vector of clustering that will be got after clustering
Claster* new_claster = NULL, *ptr = NULL;
vector<float> center_prew;
vector<float> center_new;
vector<vector<float> >::iterator del_vec;
int abs_val = 0;
bool is_same;
while (!this->claster->objects.empty())
{
center_prew = this->claster->objects[M];
do
{
if(new_claster != NULL)
{
delete(new_claster);
new_claster = NULL;
}
new_claster = new Claster(); //new sphere
new_claster->propnum = this->claster->propnum;
for(int i=0;i<this->claster->objects.size(); ++i) //forming a sphere(new claster)
{
if ( Euclidean(center_prew, this->claster->objects[i], center_prew.size() ) <= R) //if distance from center
{ //of a shphere to curent object <= R than add object to new sphere
new_claster->add(this->claster->objects[i]);
}
}
center_new = new_claster->count_center(); //counting new center of a shpere
abs_val = 0;
for (int i=0;i<this->claster->propnum;++i)
{
abs_val += fabs(center_new[i] - center_prew[i]); //the difference between new and previous centers
}
center_prew = center_new;
// }while(abs_val > 0.005);
}while(abs_val!=0);
ptr = new Claster();
*ptr = *new_claster;
after_clustering.push_back(ptr); //saving new claster
//deleting ready claster from input claster
for (int k=0; k<new_claster->objects.size(); ++k) //by number of objects
{
del_vec = this->claster->objects.begin();
for (int i=0; i<this->claster->objects.size(); ++i) //by number of objects
{
is_same = true;
for (int j=0;j<this->claster->objects[i].size();++j) //by number of properties
{
if (this->claster->objects[i][j] != new_claster->objects[k][j])
{
is_same = false;
}
}
if (is_same == true)
{
this->claster->objects.erase(del_vec);
break;
}
++del_vec;
}
} //end deleting
if (!this->claster->objects.empty())
M = rand() % this->claster->objects.size(); //new center of new claster(sphere)
}
return after_clustering;
}
void Forel::show()
{
this->claster->show();
}