-
Notifications
You must be signed in to change notification settings - Fork 1
/
readParetoSets.hpp
executable file
·305 lines (245 loc) · 8.83 KB
/
readParetoSets.hpp
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
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
#ifndef READPARETOSETS_HPP_
#define READPARETOSETS_HPP_
#include "OptFrame/MultiObjSearch.hpp"
#include "OptFrame/RandGen.hpp"
#include <unistd.h>
using namespace std;
using namespace optframe;
class readParetoSets
{
public:
MOMETRICS<int> moMetrics;
int nOptObj;
public:
readParetoSets(int _nOptObj, MOMETRICS<int> _moMetrics) :
nOptObj(_nOptObj), moMetrics(_moMetrics)
{
}
~readParetoSets()
{
}
vector<vector<double> > readParetoFronts(vector<string>& vInputModel, vector<vector<vector<double> > >& vParetoSet)
{
vector<vector<double> > paretoSetRef;
for (int iM = 0; iM < vInputModel.size(); iM++)
{
cout << "Reading file: " << vInputModel[iM] << "....." << endl;
Scanner scanner(new File(vInputModel[iM]));
vector<vector<double> > paretoSet;
while (scanner.hasNextDouble())
{
vector<double> fo;
bool add = true;
for (int o = 0; o < nOptObj; o++)
{
if (scanner.hasNextDouble())
{
fo.push_back(scanner.nextDouble());
}
else
{
add = false;
break;
}
}
if (add)
paretoSet.push_back(fo);
}
moMetrics.unionSetsReturnEvaluations(paretoSetRef, paretoSet);
vParetoSet.push_back(paretoSet);
}
return paretoSetRef;
}
vector<vector<double> > readParetoFrontsFrom1ToObj(vector<string>& vInputModel, vector<vector<vector<double> > >& vParetoSet)
{
vector<vector<double> > paretoSetRef;
for (int iM = 0; iM < vInputModel.size(); iM++)
{
cout << "Reading file: " << vInputModel[iM] << "....." << endl;
Scanner scanner(new File(vInputModel[iM]));
vector<vector<double> > paretoSet;
while (scanner.hasNextDouble())
{
string nextLine = scanner.nextLine();
Scanner scannerLine(nextLine.c_str());
vector<double> fo;
bool add = true;
for (int o = 0; o < nOptObj; o++)
{
if (scannerLine.hasNextDouble())
{
fo.push_back(scannerLine.nextDouble());
}
else
{
add = false;
break;
}
}
if (add)
paretoSet.push_back(fo);
}
moMetrics.unionSetsReturnEvaluations(paretoSetRef, paretoSet);
vParetoSet.push_back(paretoSet);
}
return paretoSetRef;
}
void exec(vector<string> vInputModel, vector<double> utopicSol, vector<double> referencePointsHV, string paretoOutput)
{
cout << "Exec Math read Pareto Sets ! \n Be sure to insert an UtopicSol and References points" << endl;
vector<vector<vector<double> > > vParetoSet;
vector<vector<double> > paretoSetRef = readParetoFrontsFrom1ToObj(vInputModel, vParetoSet);
vParetoSet.push_back(paretoSetRef);
vector<vector<double> > vIndicadoresQualidade;
cout << "===================================" << endl;
for (int nPS = 0; nPS < vParetoSet.size(); nPS++)
{
vector<vector<double> > paretoSet = vParetoSet[nPS];
vector<double> indicadores;
indicadores.push_back(paretoSet.size());
//printVectorPareto(paretoSetRef);
//cout << "conjuntoParetoAtual:" << endl;
//printVectorPareto(paretoSet);
double cardinalidade = moMetrics.cardinalite(paretoSet, paretoSetRef);
double setCover = moMetrics.setCoverage(paretoSet, paretoSetRef);
double spacing = moMetrics.spacing(paretoSet);
indicadores.push_back(cardinalidade);
indicadores.push_back(setCover);
indicadores.push_back(spacing);
double hv = -1;
double delta = -1;
if (utopicSol.size() > 0)
delta = moMetrics.deltaMetric(paretoSet, utopicSol, true);
if ((referencePointsHV.size() > 0) && (nPS < (vParetoSet.size() - 1)))
hv = moMetrics.hipervolumeWithExecRequested(paretoSet, referencePointsHV, true);
indicadores.push_back(delta);
indicadores.push_back(hv);
vIndicadoresQualidade.push_back(indicadores);
if (nPS < (vParetoSet.size() - 1))
cout << "instance: " << vInputModel[nPS] << endl;
else
cout << "instance: Pareto Reference" << endl;
cout << "size: " << paretoSet.size() << "/" << paretoSetRef.size() << endl;
cout << "card: " << cardinalidade << endl;
cout << "CS: " << setCover << endl;
cout << "spacing: " << spacing << endl;
cout << "delta: " << delta << endl;
cout << "hv: " << hv << "\n" << endl;
}
cout << "printing pareto REF" << endl;
cout << paretoSetRef << endl;
cout << "printing Indicadores" << endl;
cout.precision(10);
cout << vIndicadoresQualidade << endl;
cout << "writing Pareto at file:" << paretoOutput << endl;
FILE* fFronteiraPareto = fopen(paretoOutput.c_str(), "w");
for (int nS = 0; nS < paretoSetRef.size(); nS++)
{
for (int nE = 0; nE < nOptObj; nE++)
{
fprintf(fFronteiraPareto, "%.5f\t", paretoSetRef[nS][nE]);
}
fprintf(fFronteiraPareto, "\n");
}
fclose(fFronteiraPareto);
cout << "File wrote with success!" << endl;
getchar();
}
vector<double> getParetoFrontIndicators(string instanceName, vector<vector<double> > paretoSet, vector<vector<double> > paretoSetRef, vector<double> utopicSol, vector<double> referencePointsHV)
{
vector<double> metricValues;
double cardinalidade = moMetrics.cardinalite(paretoSet, paretoSetRef);
double setCover = moMetrics.setCoverage(paretoSet, paretoSetRef);
double spacing = moMetrics.spacing(paretoSet);
double hv = -1;
double delta = -1;
if (utopicSol.size() > 0)
delta = moMetrics.deltaMetric(paretoSet, utopicSol, true);
if (referencePointsHV.size() > 0)
hv = moMetrics.hipervolumeWithExecRequested(paretoSet, referencePointsHV, true);
metricValues.push_back(paretoSet.size());
metricValues.push_back(cardinalidade);
metricValues.push_back(setCover);
metricValues.push_back(spacing);
metricValues.push_back(delta);
metricValues.push_back(hv);
cout << "instance: " << instanceName << endl;
cout << "size: " << metricValues[0] << "/" << paretoSetRef.size() << endl;
cout << "card: " << metricValues[1] << endl;
cout << "CS: " << metricValues[2] << endl;
cout << "spacing: " << metricValues[3] << endl;
cout << "delta: " << metricValues[4] << endl;
cout << "hv: " << metricValues[5] << "\n" << endl;
return metricValues;
}
void execForBatches(vector<string> vInputModel, vector<double> utopicSol, vector<double> referencePointsHV, string paretoOutput, int nBatches, int nDiffConfigurations)
{
cout << "Exec Math read Pareto Sets ! \n Be sure to insert an UtopicSol and References points" << endl;
vector<vector<vector<double> > > vParetoSet;
vector<vector<double> > paretoSetRef = readParetoFrontsFrom1ToObj(vInputModel, vParetoSet);
vParetoSet.push_back(paretoSetRef);
int nIndicators = 6;
vector<vector<double> > vIndicadoresQualidade(nDiffConfigurations);
for (int dC = 0; dC < nDiffConfigurations; dC++)
for (int m = 0; m < nIndicators; m++)
vIndicadoresQualidade[dC].push_back(0);
cout << "===================================" << endl;
for (int nB = 0; nB < nBatches; nB++)
for (int dC = 0; dC < nDiffConfigurations; dC++)
{
int currentPareto = dC + nB * nDiffConfigurations;
vector<vector<double> > paretoSet = vParetoSet[currentPareto];
vector<double> indicadores = getParetoFrontIndicators(vInputModel[currentPareto], paretoSet, paretoSetRef, utopicSol, referencePointsHV);
if (indicadores.size() != nIndicators)
{
cout << "errors on number of indicators" << endl;
exit(1);
}
for (int m = 0; m < nIndicators; m++)
vIndicadoresQualidade[dC][m] += indicadores[m];
}
for (int dC = 0; dC < nDiffConfigurations; dC++)
for (int m = 0; m < nIndicators; m++)
vIndicadoresQualidade[dC][m] /= nBatches;
cout << "===================================" << endl;
cout << "Printing Pareto Reference Set...." << endl;
vector<double> indicadores = getParetoFrontIndicators("PARETO SET", paretoSetRef, paretoSetRef, utopicSol, referencePointsHV);
cout << "===================================" << endl;
cout << paretoSetRef << endl;
cout << "Printing average values for each configuration" << endl;
cout.precision(10);
cout << vIndicadoresQualidade << endl;
stringstream ss;
ss << paretoOutput << "_Pareto";
cout << "Writing Pareto Reference at file " << ss.str().c_str() << endl;
FILE* fFronteiraPareto = fopen(ss.str().c_str(), "w");
for (int nS = 0; nS < paretoSetRef.size(); nS++)
{
for (int nE = 0; nE < nOptObj; nE++)
{
fprintf(fFronteiraPareto, "%.5f\t", paretoSetRef[nS][nE]);
}
fprintf(fFronteiraPareto, "\n");
}
fclose(fFronteiraPareto);
ss.clear();
ss.str("");
ss << paretoOutput << "_AVGResults";
cout << "Writing average values for each configuration at " << ss.str().c_str() << endl;
FILE* fAVGResults = fopen(ss.str().c_str(), "w");
fprintf(fAVGResults, "PFsize\tcard\tSC\tSpacing\tDelta\tHV\n");
for (int dc = 0; dc < vIndicadoresQualidade.size(); dc++)
{
for (int m = 0; m < nIndicators; m++)
{
fprintf(fAVGResults, "%.5f\t", vIndicadoresQualidade[dc][m]);
}
fprintf(fAVGResults, "\n");
}
fclose(fAVGResults);
cout << "File wrote with success!" << endl;
getchar();
}
// 2
};
#endif /* READPARETOSETS_HPP_ */