/
ibex_Newton.cpp
373 lines (291 loc) · 9.67 KB
/
ibex_Newton.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
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
//============================================================================
// I B E X
// File : ibex_Newton.cpp
// Author : Gilles Chabert
// Copyright : Ecole des Mines de Nantes (France)
// License : See the LICENSE file
// Created : May 24, 2012
// Last Update : May 24, 2012
//============================================================================
#include "ibex_Newton.h"
#include "ibex_Linear.h"
#include "ibex_LinearException.h"
#include <cassert>
using namespace std;
namespace ibex {
double default_newton_prec=1e-07;
double default_gauss_seidel_ratio=1e-04;
namespace {
//
//inline bool newton_step(const Fnc& f, IntervalVector& box,
// IntervalVector& mid, IntervalVector& Fmid, IntervalMatrix& J) {
//
// f.hansen_matrix(box,J);
// if (J.is_empty()) { return false; }
// mid = box.mid();
// Fmid=f.eval_vector(mid);
//
}
bool newton(const Fnc& f, const VarSet* vars, IntervalVector& full_box, double prec, double ratio_gauss_seidel) {
int n=vars? vars->nb_var : f.nb_var();
int m=f.image_dim();
assert(full_box.size()==f.nb_var());
IntervalMatrix J(m, n);
IntervalVector* p=NULL; // Parameter box
IntervalVector* midp=NULL; // Parameter box midpoint
IntervalMatrix* Jp=NULL; // Jacobian % parameters
if (vars) {
p=new IntervalVector(vars->param_box(full_box));
midp=new IntervalVector(p->mid());
Jp=new IntervalMatrix(m,vars->nb_param);
}
IntervalVector y(n);
IntervalVector y1(n);
IntervalVector mid(n);
IntervalVector Fmid(m);
bool reducted=false;
double gain;
IntervalVector& box = vars ? *new IntervalVector(vars->var_box(full_box)) : full_box;
IntervalVector& full_mid = vars ? *new IntervalVector(full_box) : mid;
y1 = box.mid();
do {
if (vars)
f.hansen_matrix(full_box,J,*Jp,*vars);
else
f.hansen_matrix(full_box,J);
// f.jacobian(box,J);
if (J.is_empty() || (vars && Jp->is_empty())) break;
/* remove this block
*
for (int i=0; i<m; i++)
for (int j=0; j<n; j++)
if (J[i][j].is_unbounded()) return false;
*/
mid = box.mid();
if (vars) vars->set_var_box(full_mid, mid);
Fmid = f.eval_vector(full_mid);
// Use the jacobian % parameters to calculate
// a mean-value form for Fmid
if (vars) {
Fmid &= f.eval_vector(vars->full_box(mid,*midp))+(*Jp)*(*p-*midp);
}
y = mid-box;
if (y==y1) break;
y1=y;
try {
precond(J, Fmid);
gauss_seidel(J, Fmid, y, ratio_gauss_seidel);
if (y.is_empty()) {
reducted=true;
if (vars) full_box.set_empty();
else box.set_empty();
break;
}
} catch (LinearException& ) {
assert(!reducted);
break;
}
IntervalVector box2=mid-y;
if ((box2 &= box).is_empty()) {
reducted=true;
if (vars) full_box.set_empty();
else box.set_empty();
break;
}
gain = box.maxdelta(box2);
if (gain >= prec) reducted = true;
box=box2;
if (vars) vars->set_var_box(full_box, box);
}
while (gain >= prec);
if (vars) {
delete p;
delete midp;
delete Jp;
delete &box;
delete &full_mid;
}
return reducted;
}
bool newton(const Fnc& f, IntervalVector& box, double prec, double ratio_gauss_seidel) {
return newton(f,NULL,box,prec,ratio_gauss_seidel);
}
bool newton(const Fnc& f, const VarSet& vars, IntervalVector& full_box, double prec, double ratio_gauss_seidel) {
return newton(f,&vars,full_box,prec,ratio_gauss_seidel);
}
bool inflating_newton(const Fnc& f, const VarSet* vars, const IntervalVector& full_box, IntervalVector& box_existence, IntervalVector& box_unicity, int k_max, double mu_max, double delta, double chi) {
int n=vars ? vars->nb_var : f.nb_var();
assert(f.image_dim()==n);
assert(full_box.size()==f.nb_var());
if (full_box.is_empty()) {
box_existence.set_empty();
box_unicity.set_empty();
return false;
}
int k=0;
bool success=false;
IntervalVector mid(n); // Midpoint of the current box
IntervalVector Fmid(n); // Evaluation of f at the midpoint
IntervalMatrix J(n, n); // Hansen matrix of f % variables
// Following variables are introduced just to use a
// centered-form on parameters when evaluating Fmid
IntervalVector* p=NULL; // Parameter box
IntervalVector* midp=NULL; // Parameter box midpoint
// -------------------------------------------------
IntervalMatrix* Jp=NULL; // Jacobian % parameters
//
if (vars) {
p=new IntervalVector(vars->param_box(full_box));
midp=new IntervalVector(p->mid());
Jp=new IntervalMatrix(n,vars->nb_param);
}
IntervalVector y(n);
IntervalVector y1(n);
IntervalVector box = vars ? vars->var_box(full_box) : full_box;
IntervalVector& full_mid = vars ? *new IntervalVector(full_box) : mid;
// Warning: box_existence is used to store the full box of the
// current iteration (that is, param_box x box)
// It will eventually (at return) be the
// existence box in case of success. Nothing is proven inside
// box_existence until success==true in the loop (note: inflation
// stops when success is true and existence is thus preserved
// until the end)
box_existence = full_box;
// Just to quickly initialize the domains of parameters
box_unicity = full_box;
y1 = box.mid();
while (k<k_max) {
//cout << "current box=" << box << endl << endl;
if (vars)
f.hansen_matrix(box_existence, J, *Jp, *vars);
else
f.hansen_matrix(box_existence, J);
if (J.is_empty()) break;
mid = box.mid();
if (vars) vars->set_var_box(full_mid, mid);
Fmid=f.eval_vector(full_mid);
// Use the jacobian % parameters to calculate
// a mean-value form for Fmid
if (vars) {
Fmid &= f.eval_vector(vars->full_box(mid,*midp))+(*Jp)*(*p-*midp);
}
y = mid-box;
//if (y==y1) break; <--- allowed in Newton inflation
y1=y;
try {
precond(J, Fmid);
} catch(LinearException&) {
break; // should be false
}
// Note: giving mu_max to gauss-seidel (GS) is slightly different from checking the condition "mu<mu_max" in the
// Newton procedure itself. If GS transforms x0 to x1 in n iterations, and then x1 to x2 in n other iterations
// it is possible that each of these 2n iterations satisfies mu<mu_max, whereas the two global Newton iterations
// do not, i.e., d(x2,x1) > mu_max d(x1,x0).
if (!inflating_gauss_seidel(J, Fmid, y, 1e-12, mu_max)) {// TODO: replace hardcoded value 1e-12
// when k~kmax, "divergence" may also mean "cannot contract more" (d/dold~1)
break;
}
IntervalVector box2=mid-y;
if (box2.is_subset(box)) {
assert(!box2.is_empty());
if (!success) { // to get the largest unicity box, we do this
// only when the first contraction occurs
if (vars) vars->set_var_box(box_unicity,box);
else box_unicity = box;
//=================================================
// We now try to enlarge the unicity box as possible
// =================================================
// IntervalVector box2copy=box2;
//
// bool inflate_ok=true;
//
// while (inflate_ok) {
//
// box2copy.inflate(delta,0.0);
//
// // box_existence is also used inside this iteration
// // to store the "full box"
// if (vars) vars->set_var_box(box_existence,box2copy);
// else box_existence = box2copy;
//
// newton(f,vars,box_existence,0.0,default_gauss_seidel_ratio);
//
// if (vars) {
// if (vars->var_box(box_existence).is_interior_subset(box2))
// vars->set_var_box(box_unicity,box2copy);
// else inflate_ok=false;
// } else {
// if (box_existence.is_interior_subset(box2))
// box_unicity = box2copy;
// else inflate_ok=false;
// }
// }
}
success=true; // we don't return now, to let the box being contracted more
}
box = success? box2 : box2.inflate(delta,chi);
k++;
// we update box_existence inside the loop because
// the Jacobian has to be recalculated on the current
// full box at each iteration
if (vars) vars->set_var_box(box_existence,box);
else box_existence = box;
}
if (vars) {
delete p;
delete midp;
delete Jp;
delete &full_mid;
}
if (!success) {
box_existence.set_empty();
box_unicity.set_empty();
}
return success;
}
bool inflating_newton(const Fnc& f, const IntervalVector& full_box, IntervalVector& box_existence, IntervalVector& box_unicity, int k_max, double mu_max, double delta, double chi) {
return inflating_newton(f,NULL,full_box,box_existence,box_unicity,k_max,mu_max,delta,chi);
}
bool inflating_newton(const Fnc& f, const VarSet& vars, const IntervalVector& full_box, IntervalVector& box_existence, IntervalVector& box_unicity, int k_max, double mu_max, double delta, double chi) {
return inflating_newton(f,&vars,full_box,box_existence,box_unicity,k_max,mu_max,delta,chi);
}
VarSet get_newton_vars(const Fnc& f, const Vector& pt, const VarSet& forced_params) {
int n=f.nb_var();
int m=f.image_dim();
if (forced_params.nb_param==n-m)
// no need to find parameters: they are given
return VarSet(forced_params);
Matrix A=f.jacobian(pt).mid();
Matrix LU(m,n);
int *pr = new int[m];
int *pc = new int[n]; // the interesting output: the variables permutation
// To force the Gauss elimination not to choose
// the "forced" parameters, we fill their respective
// column with zeros
for (int i=0; i<n; i++) {
if (!forced_params.is_var[i]) {
A.set_col(i,Vector::zeros(m));
}
}
try {
real_LU(A,LU,pr,pc);
} catch(SingularMatrixException& e) {
// means in particular that we could not extract an
// invertible m*m submatrix
delete [] pr;
delete [] pc;
throw e;
}
// ==============================================================
BitSet _vars=BitSet::empty(n);
for (int i=0; i<m; i++) {
_vars.add(pc[i]);
}
for (int j=0; j<n; j++) {
assert(!(!forced_params.is_var[j] && _vars[j]));
}
delete [] pr;
delete [] pc;
return VarSet(f.nb_var(),_vars);
}
} // end namespace ibex