-
Notifications
You must be signed in to change notification settings - Fork 298
/
Ranking.cpp
289 lines (239 loc) · 8.64 KB
/
Ranking.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
276
277
278
279
280
281
282
283
284
285
286
287
288
289
#include <Core/ModelicaDefine.h>
#include <Core/Modelica.h>
#include <Core/ReduceDAE/IReduceDAESettings.h>
#include <Core/SimController/ISimController.h>
#include <Core/ReduceDAE/IReduceDAE.h>
#include <Core/ReduceDAE/Ranking.h>
#include <Core/ReduceDAE/IReduceDAESettings.h>
#include <Core/ReduceDAE/Reduction.h>
#include <ctime>
Ranking::Ranking(shared_ptr<IMixedSystem> system, IReduceDAESettings* settings)
:_system(system)
, _settings(settings)
{
}
Ranking::~Ranking(void)
{
}
//choose a ranking method
label_list_type Ranking::DoRanking(ublas::matrix<double>& R, ublas::matrix<double>& dR, ublas::matrix<double>& Re, vector<double>& time_values)
{
if (_settings->getRankingMethod() == IReduceDAESettings::RESIDUEN)
{
return residuenRanking(R, dR, Re, time_values);
}
else
throw std::runtime_error("Ranking method not supported yet");
}
//residue ranking
label_list_type Ranking::residuenRanking(ublas::matrix<double>& R, ublas::matrix<double>& dR, ublas::matrix<double>& Re, vector<double>& time_values)
{
cout << "Start residue ranking:" << std::endl;
//cast modelica system to reduce dae object
shared_ptr<IReduceDAE> reduce_dae = dynamic_pointer_cast<IReduceDAE>(_system);
//cast modelica system to IContinuous object
shared_ptr<IContinuous> continous_system = dynamic_pointer_cast<IContinuous>(_system);
shared_ptr<IEvent> event_system = dynamic_pointer_cast<IEvent>(_system);
shared_ptr<ITime> _timeevent_system = dynamic_pointer_cast<ITime>(_system);
//cast modelica system to initial system object
shared_ptr<ISystemInitialization> _init_system = dynamic_pointer_cast<ISystemInitialization>(_system);
//IHistory* history = reduce_dae->getHistory();
ublas::matrix<double> Ro;
//get the number of residues
//int dimSys = _system->getDimRes();
int dimSys = 0;
//reference solution for variables in column j in R matrix
ublas::vector<double> r_j;
//reference solution for derivatives in column j in dR matrix
ublas::vector<double> dr_j;
//reference solution for residues in column j in Re matrix
ublas::vector<double> re_j;
//vector for current error
ublas::vector<double> e(time_values.size());
//index for current reduction
unsigned int k = 0;
//help array for residues
double *ydHelp;
double norm_2 = 0;
if (reduce_dae)
{
//get label variables
label_list_type labels = reduce_dae->getLabels();
//number of labels
int kDim = labels.size();
//vector for ranks
vector<double> rank_vector(kDim);
//cout << "size labels in ranking:" << kDim << std::endl;
//loop over labels
FOREACH(label_type label, labels)
{
//index for current time entry
int j = 0;
try
{
_timeevent_system->setTime(0);
_init_system->initialize();
_init_system->setInitial(true);
int dim = event_system->getDimZeroFunc();
bool* conditions0 = new bool[dim];
bool* conditions1 = new bool[dim];
//bool restart=true;
/*int iter = 0;
while(restart &&!(iter++>10))
{
continous_system->evaluateAll(IContinuous::ALL);
restart = event_system->checkForDiscreteEvents();
}
*/
event_system->getConditions(conditions0);
event_system->getConditions(conditions1);
event_system->saveAll();
_init_system->setInitial(false);
delete[] conditions0;
delete[] conditions1;
//set label_1 to 0 and label_2 to 1
*(get<1>(label)) = 0;
*(get<2>(label)) = 1;
//loop over time entries
//double mylabel01 = *(std::get<1>($labels[0]));
// double mylabel02 = *(std::get<2>($labels[0]));
// cout << "Ranking for label: " << get<0>($label) << std::endl;
FOREACH(double time, time_values)
{
//cout << "j counter: " << j << std::endl;
//set time
//cout << "time " <<time<< std::endl;
_timeevent_system->setTime(time);
//continous_system->setTime(time);
//variables reference solution for time j
r_j = ublas::column(R, j);
//cout << "r_j:" << r_j << std::endl;
//derivative reference solution for time j
dr_j = ublas::column(dR, j);
//cout << "dr_j:" << dr_j << std::endl;
//residues reference solution for time j
re_j = ublas::column(Re, j);
//cout << "re_j:" << re_j << std::endl;
//set reference solution
reduce_dae->setVariables(r_j, dr_j);
//update system
//continous_system->update(IContinuous::RANKING);
continous_system->evaluateAll(IContinuous::RANKING);
//get current residues
//continous_system->giveRHS(ydHelp,IContinuous::ALL_RESIDUES);
//IContinuous::UPDATETYPE calltype;
//calltype = IContinuous::RANKING;
dimSys = continous_system->getDimRHS();
ydHelp = new double[dimSys];
continous_system->getRHS(ydHelp);
//_system->getResidual(ydHelp);
shared_vector_t yd(dimSys, adaptor_t(dimSys, ydHelp));
//cout << "yd: " << yd << std::endl;
//To do after residual only return vlue from algeabric loop equation
//cout << "yd-dr_j" << yd-dr_j << std::endl;
//cout << "yd-dr_j" << yd-dr_j << std::endl;
//error norm_2 (x)= sqrt (sum |xi|^2 )
//e(j++) = ublas::norm_2(yd-re_j);
norm_2 = ublas::norm_2(yd - dr_j);
e(j++) = norm_2;
delete[] ydHelp;
//cout << "error " << norm_2 << std::endl;
}
}
catch (std::invalid_argument& ex) // division by zero
{
#undef max
e(j++) = std::numeric_limits<double>::max();
cout << "division by zero for j " << j << std::endl;
}
//rank norm_inf (x)= max |xi|
// cout << "error in residual ranking "<< e << std::endl;
rank_vector[k++] = ublas::norm_inf(e);
cout << "rank value for label " << k - 1 << ": " << rank_vector[k - 1] << std::endl;
//reset current label values
*(get<1>(label)) = 1;
*(get<2>(label)) = 0;
}
//sort the label list in the order of the sorted ranking vector
sort(labels.begin(), labels.end(),
boost::lambda::var(rank_vector)[boost::lambda::bind(Li(), boost::lambda::_1)] <
boost::lambda::var(rank_vector)[boost::lambda::bind(Li(), boost::lambda::_2)]
);
return labels;
}
else
throw std::runtime_error("Modelica system is not of type IReduceDAE");
}
label_list_type Ranking::perfectRanking(ublas::matrix<double>& Ro, shared_ptr<IMixedSystem> _system, IReduceDAESettings* _settings, SimSettings simsettings,
string modelKey, vector<string> output_names, double timeout,ISimController* sim_controller)
{
ublas::matrix<double>::size_type n;
n = Ro.size1();
if (n == 0)
throw std::runtime_error("No output variables for ranking!");
cout << "Start perfect ranking:" << std::endl;
//cast modelica system to reduce dae object
shared_ptr<IReduceDAE> reduce_dae = dynamic_pointer_cast<IReduceDAE>(_system);
IHistory* history;
ublas::matrix<double> Rc; //current result
if (reduce_dae)
{
//get label variables
label_list_type labels = reduce_dae->getLabels();
//number of labels
int kDim = labels.size();
//vector for ranks
vector<double> rank_vector(kDim);
double rank_value;
//index for current ranking
unsigned int k = 0;
//loop over labels
FOREACH(label_type label, labels)
{
try{
sim_controller->initialize(simsettings, modelKey, timeout);
//set label_1 to 0 and label_2 to 1
*(get<1>(label)) = 0;
*(get<2>(label)) = 1;
//vector for errors
ublas::vector<double> e(n);
sim_controller->runReducedSimulation();
history = reduce_dae->getHistory();
//query simulation result outputs
Rc.clear();
history->getOutputResults(Rc);
Reduction reduction(_system, _settings);
e = reduction.getError(Rc, Ro, output_names);;
//rank norm_inf (x)= max |xi|
rank_value = ublas::norm_inf(e);
}
catch (ModelicaSimulationError& ex)
{
#undef max
rank_value = std::numeric_limits<double>::max();
if (!ex.isSuppressed())
cout << "removing label " << (get<0>(label)) << "causes error " << ex.what() << std::endl;
// std::cerr << "Simulation stopped with error in " << error_id_string(ex.getErrorID()) << ": " << ex.what();
}
catch (std::invalid_argument& ex) // division by zero
{
#undef max
rank_value = std::numeric_limits<double>::max();
cout << "division by zero for label " << (get<0>(label)) << std::endl;
}
rank_vector[k++] = rank_value;
cout << "rank value for label " << (get<0>(label)) << ": " << rank_value << std::endl;
//reset current label values
*(get<1>(label)) = 1;
*(get<2>(label)) = 0;
}
//sort the label list in the order of the sorted ranking vector
sort(labels.begin(), labels.end(),
boost::lambda::var(rank_vector)[boost::lambda::bind(Li(), boost::lambda::_1)] <
boost::lambda::var(rank_vector)[boost::lambda::bind(Li(), boost::lambda::_2)]
);
return labels;
}
else
throw std::runtime_error("Modelica system is not of type IReduceDAE");
}