-
Notifications
You must be signed in to change notification settings - Fork 0
/
SetCluster.cpp
275 lines (237 loc) · 9.74 KB
/
SetCluster.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
268
269
270
271
272
273
274
275
#include "SetCluster.h"
#include <algorithm> // std::sort
// creates a blank cluster
SetCluster::SetCluster(ColorCustom &clusterColor) {
color = clusterColor;
setsInCluster = std::vector<int>();
minimumValues = std::vector<double>();
meanValues = std::vector<double>();
maximumValues = std::vector<double>();
}
// creates the cluster with the passed sets
SetCluster::SetCluster(ColorCustom &clusterColor, std::vector<int>* newSetsInCluster) {
color = clusterColor;
setsInCluster = std::vector<int>();
minimumValues = std::vector<double>();
meanValues = std::vector<double>();
maximumValues = std::vector<double>();
for (int i = 0; i < newSetsInCluster->size(); i++) {
setsInCluster.push_back((*newSetsInCluster)[i]);
}
std::sort(setsInCluster.begin(), setsInCluster.end());
}
// creates the cluster with the passed sets and values from the passed dimensions
SetCluster::SetCluster(ColorCustom &clusterColor, std::vector<int>* newSetsInCluster, std::vector<Dimension>* dimensionsToCalculateWith) {
// intialize fields
color = clusterColor;
setsInCluster = std::vector<int>();
minimumValues = std::vector<double>();
meanValues = std::vector<double>();
maximumValues = std::vector<double>();
// put sets into the cluster
for (int i = 0; i < newSetsInCluster->size(); i++) {
setsInCluster.push_back((*newSetsInCluster)[i]);
}
std::sort(setsInCluster.begin(), setsInCluster.end());
// get the values from the dimensions
// check if there are any dimensions passed
if (dimensionsToCalculateWith->size() <= 0) {
// do nothing dimensions cannot be used for this cluster
}
// check if then largest set index is is within the dimensions passed
if (setsInCluster[setsInCluster.size() - 1] >= (*dimensionsToCalculateWith)[0].size()) {
// do nothing dimensions cannot be used for this cluster
}
else {
// there are enough sets in the dimensions to use the passed dimensions
// so get the data for this cluster
for (int i = 0; i < dimensionsToCalculateWith->size(); i++) {
double min = getMinimumValue(&(*dimensionsToCalculateWith)[i], &setsInCluster);
double mean = getMeanValue(&(*dimensionsToCalculateWith)[i], &setsInCluster);
double max = getMaximumValue(&(*dimensionsToCalculateWith)[i], &setsInCluster);
minimumValues.push_back(min);
meanValues.push_back(mean);
maximumValues.push_back(max);
}
}
}
// deletes object
SetCluster::~SetCluster() {
}
// gets the calculates the minimum value of dimension for the sets whose indexes are passed(setIndexes)
double SetCluster::getMinimumValue(Dimension* dimension, std::vector<int>* setIndexes) {
double minimum = 0.0;
std::sort(setIndexes->begin(), setIndexes->end());
// check if the dimension has enough sets for the sets in the passed vector(setIndexes)
if ((*setIndexes)[setIndexes->size() - 1] >= dimension->size()) {
return minimum;
}
// calculate the minimum
for (int i = 0; i < setIndexes->size(); i++) {
double dataValue = dimension->getCalibratedData((*setIndexes)[i]);
if (dataValue < minimum) {
minimum = dataValue;
}
}
return minimum;
}
// gets the calculates the mean value of dimension for the sets whose indexes are passed(setIndexes)
double SetCluster::getMeanValue(Dimension * dimension, std::vector<int>* setIndexes) {
double sum = 0.0;
std::sort(setIndexes->begin(), setIndexes->end());
// check if the dimension has enough sets for the sets in the passed vector(setIndexes)
if ((*setIndexes)[setIndexes->size() - 1] >= dimension->size()) {
return sum;
}
// calculate the minimum
for (int i = 0; i < setIndexes->size(); i++) {
double dataValue = dimension->getCalibratedData((*setIndexes)[i]);
sum += dataValue;
}
return sum / ((double)setIndexes->size());
}
// gets the calculates the maximum value of dimension for the sets whose indexes are passed(setIndexes)
double SetCluster::getMaximumValue(Dimension* dimension, std::vector<int>* setIndexes) {
double maximum = 0.0;
std::sort(setIndexes->begin(), setIndexes->end());
// check if the dimension has enough sets for the sets in the passed vector(setIndexes)
if ((*setIndexes)[setIndexes->size() - 1] >= dimension->size()) {
return maximum;
}
// calculate the minimum
for (int i = 0; i < setIndexes->size(); i++) {
double dataValue = dimension->getCalibratedData((*setIndexes)[i]);
if (dataValue > maximum) {
maximum = dataValue;
}
}
return maximum;
}
// adds the passed set index(setIndex) to the list of sets
// returns true if the set is added and false if the set was already in the cluster
bool SetCluster::addSet(int setIndex) {
for (int i = 0; i < setsInCluster.size(); i++) {
if (setsInCluster[i] == setIndex) {
return false;
}
}
setsInCluster.push_back(setIndex);
std::sort(setsInCluster.begin(), setsInCluster.end());
return true;
}
// removes the set index(setIndex) to the list of sets
// returns true if the set is removed and false if the set not in the cluster
bool SetCluster::removeSet(int setIndex) {
for (int i = 0; i < setsInCluster.size(); i++) {
if (setsInCluster[i] == setIndex) {
setsInCluster.erase(setsInCluster.begin() + i);
return true;
}
}
return false;
}
// gets the number of sets in the cluster
int SetCluster::getSetNumber() {
return setsInCluster.size();
}
// gets the index of the set in this cluster for the set at the passed index(clusterRelativeIndex), which is the index the set index is stored at
int SetCluster::getIndexOfSet(int clusterRelativeIndex) {
if (clusterRelativeIndex >= setsInCluster.size() || clusterRelativeIndex < 0) {
return -1;
}
return setsInCluster[clusterRelativeIndex];
}
// gets the minimum value in the cluster for the dimension at the passed index
double SetCluster::getMinimum(int dimensionIndex) {
if (dimensionIndex >= minimumValues.size() || dimensionIndex < 0) {
return 0.0;
}
return minimumValues[dimensionIndex];
}
// gets the mean value in the cluster for the dimension at the passed index
double SetCluster::getMean(int dimensionIndex) {
if (dimensionIndex >= meanValues.size() || dimensionIndex < 0) {
return 0.0;
}
return meanValues[dimensionIndex];
}
// gets the maximum value in the cluster for the dimension at the passed index
double SetCluster::getMaximum(int dimensionIndex) {
if (dimensionIndex >= maximumValues.size() || dimensionIndex < 0) {
return 0.0;
}
return maximumValues[dimensionIndex];
}
// recalculates the values for the cluster using the passed dimensions
void SetCluster::calculateValues(std::vector<Dimension>* dimensionsToCalculateWith) {
// get the values from the dimensions
// check if there are any dimensions passed
if (dimensionsToCalculateWith->size() <= 0) {
// do nothing dimensions cannot be used for this cluster
}
// check if then largest set index is is within the dimensions passed
if (setsInCluster[setsInCluster.size() - 1] >= (*dimensionsToCalculateWith)[0].size()) {
// do nothing dimensions cannot be used for this cluster
}
else {
// there are enough sets in the dimensions to use the passed dimensions
// so get the data for this cluster
for (int i = 0; i < dimensionsToCalculateWith->size(); i++) {
double min = getMinimumValue(&(*dimensionsToCalculateWith)[i], &setsInCluster);
double mean = getMeanValue(&(*dimensionsToCalculateWith)[i], &setsInCluster);
double max = getMaximumValue(&(*dimensionsToCalculateWith)[i], &setsInCluster);
minimumValues.push_back(min);
meanValues.push_back(mean);
maximumValues.push_back(max);
}
}
}
// inverts the values of the set at the passed index
void SetCluster::invertValues(int dimensionToInvertValuesAt) {
if (dimensionToInvertValuesAt >= minimumValues.size() || dimensionToInvertValuesAt < 0) {
return;
}
minimumValues[dimensionToInvertValuesAt] = 1 - minimumValues[dimensionToInvertValuesAt];
meanValues[dimensionToInvertValuesAt] = 1 - meanValues[dimensionToInvertValuesAt];
maximumValues[dimensionToInvertValuesAt] = 1 - maximumValues[dimensionToInvertValuesAt];
}
// move the position of the values in the set(at originalIndex) to the index after indexBeforeNewIndex
void SetCluster::moveValues(int originalIndex, int indexOfInsertion) {
if (originalIndex >= minimumValues.size() || originalIndex < 0) {
return;
}
if (indexOfInsertion > minimumValues.size()) {
indexOfInsertion = minimumValues.size();
}
if (indexOfInsertion < 0) {
indexOfInsertion = 0;
}
if (originalIndex == indexOfInsertion) {
return;
}
// check if the insertion at the end
if (indexOfInsertion == minimumValues.size()) {
minimumValues.push_back(minimumValues[originalIndex]);
meanValues.push_back(meanValues[originalIndex]);
maximumValues.push_back(maximumValues[originalIndex]);
minimumValues.erase(minimumValues.begin() + originalIndex);
meanValues.erase(meanValues.begin() + originalIndex);
maximumValues.erase(maximumValues.begin() + originalIndex);
return;
}
// place the dimension in the new position
minimumValues.insert(minimumValues.begin() + indexOfInsertion, minimumValues[originalIndex]);
meanValues.insert(meanValues.begin() + indexOfInsertion, meanValues[originalIndex]);
maximumValues.insert(maximumValues.begin() + indexOfInsertion, maximumValues[originalIndex]);
// remove the dimension from the old position
if (originalIndex <= indexOfInsertion) {
minimumValues.erase(minimumValues.begin() + originalIndex);
meanValues.erase(meanValues.begin() + originalIndex);
maximumValues.erase(maximumValues.begin() + originalIndex);
}
else {
minimumValues.erase(minimumValues.begin() + originalIndex + 1);
meanValues.erase(meanValues.begin() + originalIndex + 1);
maximumValues.erase(maximumValues.begin() + originalIndex + 1);
}
}