-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathipm.cc
702 lines (655 loc) · 23.1 KB
/
ipm.cc
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
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
// Copyright (c) 2018 ERGO-Code. See license.txt for license.
#include "ipm.h"
#include <algorithm>
#include <cmath>
#include <cassert>
#include <limits>
#include "timer.h"
#include "utils.h"
namespace ipx {
struct IPM::Step {
Step(Int m, Int n) : x(n+m), xl(n+m), xu(n+m), y(m), zl(n+m), zu(n+m) {}
Vector x, xl, xu, y, zl, zu;
Step& operator+=(const Step& rhs) {
x += rhs.x; xl += rhs.xl; xu += rhs.xu;
y += rhs.y; zl += rhs.zl; zu += rhs.zu;
return *this;
}
};
IPM::IPM(const Control& control) : control_(control) {}
void IPM::ComputeStartingPoint(KKTSolver* kkt, Iterate* iterate, Info* info) {
kkt_ = kkt;
iterate_ = iterate;
info_ = info;
PrintHeader();
ComputeStartingPoint();
if (info->errflag == 0)
PrintOutput();
// Set status_ipm.
if (info->errflag == IPX_ERROR_interrupt_time) {
info->errflag = 0;
info->status_ipm = IPX_STATUS_time_limit;
} else if (info->errflag) {
info->status_ipm = IPX_STATUS_failed;
} else {
info->status_ipm = IPX_STATUS_not_run;
}
}
void IPM::LoadStartingPoint(const Vector& x, const Vector& xl, const Vector& xu,
const Vector& y, const Vector& zl, const Vector& zu,
Iterate* iterate, Info* info) {
kkt_ = nullptr;
iterate_ = iterate;
info_ = info;
PrintHeader();
LoadStartingPoint(x, xl, xu, y, zl, zu);
PrintOutput();
info->status_ipm = IPX_STATUS_not_run;
}
void IPM::Driver(KKTSolver* kkt, Iterate* iterate, Info* info) {
const Model& model = iterate->model();
const Int m = model.rows();
const Int n = model.cols();
Step step(m, n);
kkt_ = kkt;
iterate_ = iterate;
info_ = info;
num_bad_iter_ = 0;
while (true) {
if (iterate->term_crit_reached()) {
info->status_ipm = IPX_STATUS_optimal;
break;
}
if (num_bad_iter_ >= 5 ||
iterate_->complementarity() > kDivergeTol * best_complementarity_) {
// No progress in reducing the complementarity gap.
// Check if model seems to be primal or dual infeasible.
bool dualized = iterate_->model().dualized();
double pobjective = iterate_->pobjective_after_postproc();
double dobjective = iterate_->dobjective_after_postproc();
if (dobjective > std::max(10.0 * std::abs(pobjective), 1.0)) {
// Dual objective tends to positive infinity. Looks like the
// model is dual unbounded, i.e. primal infeasible.
info->status_ipm = dualized ?
IPX_STATUS_dual_infeas : IPX_STATUS_primal_infeas;
} else if (pobjective < -std::max(10.0 * std::abs(dobjective), 1.0)) {
// Primal objective tends to negative infinity. Looks like the
// model is primal unbounded, i.e. dual infeasible.
info->status_ipm = dualized ?
IPX_STATUS_primal_infeas : IPX_STATUS_dual_infeas;
}
else {
info->status_ipm = IPX_STATUS_no_progress;
}
break;
}
if (info->iter >= maxiter_) {
info->status_ipm = IPX_STATUS_iter_limit;
break;
}
if ((info->errflag = control_.InterruptCheck()) != 0)
break;
kkt->Factorize(iterate, info);
if (info->errflag)
break;
Predictor(step);
if (info->errflag)
break;
AddCorrector(step);
if (info->errflag)
break;
MakeStep(step);
info->iter++;
PrintOutput();
}
// Set status_ipm if errflag terminated IPM.
if (info->errflag) {
if (info->errflag == IPX_ERROR_interrupt_time) {
info->errflag = 0;
info->status_ipm = IPX_STATUS_time_limit;
} else {
info->status_ipm = IPX_STATUS_failed;
}
}
}
void IPM::ComputeStartingPoint() {
const Model& model = iterate_->model();
const Int m = model.rows();
const Int n = model.cols();
const SparseMatrix& AI = model.AI();
const Vector& b = model.b();
const Vector& c = model.c();
const Vector& lb = model.lb();
const Vector& ub = model.ub();
Vector x(n+m), xl(n+m), xu(n+m), y(m), zl(n+m), zu(n+m);
Vector rb(m); // workspace
// Factorize the KKT matrix with the identity matrix in the (1,1) block.
kkt_->Factorize(nullptr, info_);
if (info_->errflag)
return;
// Set x within its bounds and compute the minimum norm solution dx to
// AI*dx = (b-AI*x). Then update x := x + dx to obtain a feasible point.
rb = b;
for (Int j = 0; j < n+m; j++) {
double xj = 0.0;
if (xj < lb[j])
xj = lb[j];
if (xj > ub[j])
xj = ub[j];
x[j] = xj;
if (xj != 0.0)
ScatterColumn(AI, j, -xj, rb);
}
double tol = 0.1 * Infnorm(rb);
zl = 0.0;
kkt_->Solve(zl, rb, tol, xl, y, info_);
if (info_->errflag)
return;
x += xl;
// Compute xl, xu and shift to become positive.
double xinfeas = 0.0;
for (Int j = 0; j < n+m; j++) {
xl[j] = x[j]-lb[j];
xinfeas = std::max(xinfeas, -xl[j]);
xu[j] = ub[j]-x[j];
xinfeas = std::max(xinfeas, -xu[j]);
}
double xshift1 = 1.0 + 1.5*xinfeas;
xl += xshift1;
xu += xshift1;
const double cnorm = Twonorm(c);
if (cnorm == 0.0) {
// Special treatment for zero objective.
for (Int j = 0; j < n+m; j++) {
zl[j] = std::isfinite(lb[j]) ? 1.0 : 0.0;
zu[j] = std::isfinite(ub[j]) ? 1.0 : 0.0;
}
} else {
// Compute y as the least-squares solution to AI'*y=c.
// Recompute zl = c-AI'*y because the KKT system is solved
// approximately with a residual in the first block equation.
rb = 0.0;
double tol = 0.1 * Infnorm(c);
kkt_->Solve(c, rb, tol, zl, y, info_);
if (info_->errflag)
return;
zl = c;
MultiplyAdd(AI, y, -1.0, zl, 'T');
// When c lies in range(AI'), then the dual slack variables are (close
// to) zero, and the initial point would be almost complementary but
// ususally not primal feasible. To prevent this from happening, add
// a fraction of the objective to zl and adjust y. In exact computation
// this does not affect dual feasiblity.
const double znorm = Twonorm(zl);
const double rho = 0.05;
if (znorm < rho*cnorm) {
zl += rho * c;
y *= (1.0-rho);
}
// Split dual slack solution into zl, zu and shift to become positive.
double zinfeas = 0.0;
for (Int j = 0; j < n+m; j++) {
double zval = zl[j];
zl[j] = 0.0;
zu[j] = 0.0;
if (std::isfinite(lb[j]) && std::isfinite(ub[j])) {
zl[j] = 0.5*zval;
zu[j] = -0.5*zval;
}
else if (std::isfinite(lb[j]))
zl[j] = zval;
else if (std::isfinite(ub[j]))
zu[j] = -zval;
zinfeas = std::max(zinfeas, -zl[j]);
zinfeas = std::max(zinfeas, -zu[j]);
}
double zshift1 = 1.0 + 1.5*zinfeas;
for (Int j = 0; j < n+m; j++) {
if (std::isfinite(lb[j]))
zl[j] += zshift1;
if (std::isfinite(ub[j]))
zu[j] += zshift1;
}
}
// Level pairwise complementarity products.
double xsum = 1.0;
double zsum = 1.0;
double mu = 1.0;
for (Int j = 0; j < n+m; j++) {
if (std::isfinite(lb[j])) {
xsum += xl[j];
zsum += zl[j];
mu += xl[j]*zl[j];
}
if (std::isfinite(ub[j])) {
xsum += xu[j];
zsum += zu[j];
mu += xu[j]*zu[j];
}
}
double xshift2 = 0.5*mu/zsum;
double zshift2 = 0.5*mu/xsum;
xl += xshift2;
xu += xshift2;
for (Int j = 0; j < n+m; j++) {
if (std::isfinite(lb[j]))
zl[j] += zshift2;
if (std::isfinite(ub[j]))
zu[j] += zshift2;
}
iterate_->Initialize(x, xl, xu, y, zl, zu);
best_complementarity_ = iterate_->complementarity();
}
void IPM::LoadStartingPoint(Vector x, Vector xl, Vector xu, Vector y, Vector zl,
Vector zu) {
const Model& model = iterate_->model();
const Int m = model.rows();
const Int n = model.cols();
const Vector& lb = model.lb();
const Vector& ub = model.ub();
// Make starting point valid.
Int numComplementarityProducts = 0;
double sumComplementarityProducts = 0.0;
for (Int j = 0; j < n+m; ++j) {
if (xl[j] > 0.0 && zl[j] > 0.0) {
sumComplementarityProducts += xl[j] * zl[j];
numComplementarityProducts++;
}
if (xu[j] > 0.0 && zu[j] > 0.0) {
sumComplementarityProducts += xu[j] * zu[j];
numComplementarityProducts++;
}
}
const double mu = numComplementarityProducts ?
sumComplementarityProducts / numComplementarityProducts : 1.0;
for (Int j = 0; j < n+m; ++j) {
if (std::isfinite(lb[j])) {
assert(std::isfinite(xl[j]) && xl[j] >= 0.0);
assert(std::isfinite(zl[j]) && zl[j] >= 0.0);
if (xl[j] == 0.0 && zl[j] == 0.0)
xl[j] = zl[j] = std::sqrt(mu);
else if (xl[j] == 0.0)
xl[j] = mu / zl[j];
else if (zl[j] == 0.0)
zl[j] = mu / xl[j];
} else {
assert(xl[j] == INFINITY);
assert(zl[j] == 0.0);
}
if (std::isfinite(ub[j])) {
assert(std::isfinite(xu[j]) && xu[j] >= 0.0);
assert(std::isfinite(zu[j]) && zu[j] >= 0.0);
if (xu[j] == 0.0 && zu[j] == 0.0)
xu[j] = zu[j] = std::sqrt(mu);
else if (xu[j] == 0.0)
xu[j] = mu / zu[j];
else if (zu[j] == 0.0)
zu[j] = mu / xu[j];
} else {
assert(xu[j] == INFINITY);
assert(zu[j] == 0.0);
}
}
iterate_->Initialize(x, xl, xu, y, zl, zu);
best_complementarity_ = iterate_->complementarity();
}
// Computes maximum alpha such that x + alpha*dx >= 0.
// The blocking index is returned in blocking_index if not NULL.
static double StepToBoundary(const Vector& x, const Vector& dx,
Int* blocking_index, double alpha = 1.0) {
const Int n = x.size();
const double damp = 1.0 - std::numeric_limits<double>::epsilon();
assert(damp < 1.0);
Int iblock = -1;
for (Int i = 0; i < n; i++) {
assert(x[i] >= 0.0);
if (x[i]+alpha*dx[i] < 0.0) {
alpha = -(x[i]*damp) / dx[i];
assert(x[i]+alpha*dx[i] >= 0.0);
iblock = i;
}
}
assert(alpha >= 0.0);
if (blocking_index)
*blocking_index = iblock;
return alpha;
}
void IPM::Predictor(Step& step) {
const Model& model = iterate_->model();
const Int m = model.rows();
const Int n = model.cols();
const Vector& xl = iterate_->xl();
const Vector& xu = iterate_->xu();
const Vector& zl = iterate_->zl();
const Vector& zu = iterate_->zu();
// sl = -xl.*zl
Vector sl(n+m);
for (Int j = 0; j < n+m; j++)
if (iterate_->has_barrier_lb(j))
sl[j] = -xl[j]*zl[j];
else
sl[j] = 0.0;
assert(AllFinite(sl));
// su = -xu.*zu
Vector su(n+m);
for (Int j = 0; j < n+m; j++)
if (iterate_->has_barrier_ub(j))
su[j] = -xu[j]*zu[j];
else
su[j] = 0.0;
assert(AllFinite(su));
SolveNewtonSystem(&iterate_->rb()[0], &iterate_->rc()[0],
&iterate_->rl()[0], &iterate_->ru()[0], &sl[0], &su[0],
step);
}
void IPM::AddCorrector(Step& step) {
const Model& model = iterate_->model();
const Int m = model.rows();
const Int n = model.cols();
const Vector& xl = iterate_->xl();
const Vector& xu = iterate_->xu();
const Vector& zl = iterate_->zl();
const Vector& zu = iterate_->zu();
const Vector& dxl = step.xl;
const Vector& dxu = step.xu;
const Vector& dzl = step.zl;
const Vector& dzu = step.zu;
const double mu = iterate_->mu();
// Choose centering parameter.
double step_xl = StepToBoundary(xl, dxl, nullptr);
double step_xu = StepToBoundary(xu, dxu, nullptr);
double step_zl = StepToBoundary(zl, dzl, nullptr);
double step_zu = StepToBoundary(zu, dzu, nullptr);
double maxp = std::min(step_xl, step_xu);
double maxd = std::min(step_zl, step_zu);
double muaff = 0.0;
Int num_finite = 0;
for (Int j = 0; j < n+m; j++) {
if (iterate_->has_barrier_lb(j)) {
assert(std::isfinite(xl[j]));
assert(xl[j] != 0.0);
muaff += (xl[j]+maxp*dxl[j]) * (zl[j]+maxd*dzl[j]);
num_finite++;
}
if (iterate_->has_barrier_ub(j)) {
assert(std::isfinite(xu[j]));
assert(xu[j] != 0.0);
muaff += (xu[j]+maxp*dxu[j]) * (zu[j]+maxd*dzu[j]);
num_finite++;
}
}
assert(std::isfinite(muaff));
muaff /= num_finite;
double ratio = muaff / mu;
double sigma = ratio * ratio * ratio;
// sl = -xl.*zl + sigma*mu - dxl.*dzl
Vector sl(n+m);
for (Int j = 0; j < n+m; j++)
if (iterate_->has_barrier_lb(j))
sl[j] = -xl[j]*zl[j] + sigma*mu - dxl[j]*dzl[j];
else
sl[j] = 0.0;
assert(AllFinite(sl));
// su = -xu.*zu + sigma*mu - dxu.*dzu
Vector su(n+m);
for (Int j = 0; j < n+m; j++)
if (iterate_->has_barrier_ub(j))
su[j] = -xu[j]*zu[j] + sigma*mu - dxu[j]*dzu[j];
else
su[j] = 0.0;
assert(AllFinite(su));
SolveNewtonSystem(&iterate_->rb()[0], &iterate_->rc()[0],
&iterate_->rl()[0], &iterate_->ru()[0], &sl[0], &su[0],
step);
}
void IPM::StepSizes(const Step& step) {
const Model& model = iterate_->model();
const Int m = model.rows();
const Int n = model.cols();
const Vector& xl = iterate_->xl();
const Vector& xu = iterate_->xu();
const Vector& zl = iterate_->zl();
const Vector& zu = iterate_->zu();
const Vector& dxl = step.xl;
const Vector& dxu = step.xu;
const Vector& dzl = step.zl;
const Vector& dzu = step.zu;
const double gammaf = 0.9;
const double gammaa = 1.0 / (1.0-gammaf);
Int block_xl, block_xu, block_zl, block_zu;
double step_xl = StepToBoundary(xl, dxl, &block_xl);
double step_xu = StepToBoundary(xu, dxu, &block_xu);
double step_zl = StepToBoundary(zl, dzl, &block_zl);
double step_zu = StepToBoundary(zu, dzu, &block_zu);
double maxp = std::fmin(step_xl, step_xu);
double maxd = std::fmin(step_zl, step_zu);
double mufull = 0.0;
Int num_finite = 0;
for (Int j = 0; j < n+m; j++) {
if (iterate_->has_barrier_lb(j)) {
assert(std::isfinite(xl[j]));
assert(xl[j] != 0.0);
mufull += (xl[j]+maxp*dxl[j]) * (zl[j]+maxd*dzl[j]);
num_finite++;
}
if (iterate_->has_barrier_ub(j)) {
assert(std::isfinite(xu[j]));
assert(xu[j] != 0.0);
mufull += (xu[j]+maxp*dxu[j]) * (zu[j]+maxd*dzu[j]);
num_finite++;
}
}
assert(std::isfinite(mufull));
mufull /= num_finite;
mufull /= gammaa;
double alphap = 1.0;
double alphad = 1.0;
Int blockp = -1;
Int blockd = -1;
if (maxp < 1.0) {
if (step_xl <= step_xu) {
double buffer;
blockp = block_xl;
buffer = mufull / (zl[blockp] + maxd*dzl[blockp]);
alphap = (xl[blockp]-buffer) / (-dxl[blockp]);
} else {
double buffer;
blockp = block_xu;
buffer = mufull / (zu[blockp] + maxd*dzu[blockp]);
alphap = (xu[blockp]-buffer) / (-dxu[blockp]);
}
alphap = std::max(alphap, gammaf*maxp);
alphap = std::min(alphap, 1.0);
assert(blockp >= 0.0);
}
if (maxd < 1.0) {
if (step_zl <= step_zu) {
double buffer;
blockd = block_zl;
buffer = mufull / (xl[blockd] + maxp*dxl[blockd]);
alphad = (zl[blockd]-buffer) / (-dzl[blockd]);
} else {
double buffer;
blockd = block_zu;
buffer = mufull / (xu[blockd] + maxp*dxu[blockd]);
alphad = (zu[blockd]-buffer) / (-dzu[blockd]);
}
alphad = std::max(alphad, gammaf*maxd);
alphad = std::min(alphad, 1.0);
assert(blockd >= 0.0);
}
step_primal_ = std::min(alphap, 1.0-1e-6);
step_dual_ = std::min(alphad, 1.0-1e-6);
}
void IPM::MakeStep(const Step& step) {
StepSizes(step);
iterate_->Update(step_primal_, &step.x[0], &step.xl[0], &step.xu[0],
step_dual_, &step.y[0], &step.zl[0], &step.zu[0]);
if (std::min(step_primal_, step_dual_) < 0.05)
num_bad_iter_++;
else
num_bad_iter_ = 0;
best_complementarity_ =
std::min(best_complementarity_, iterate_->complementarity());
}
void IPM::SolveNewtonSystem(const double* rb, const double* rc,
const double* rl, const double* ru,
const double* sl, const double* su,
Step& step) {
const Model& model = iterate_->model();
const Int m = model.rows();
const Int n = model.cols();
const SparseMatrix& AI = model.AI();
const Vector& xl = iterate_->xl();
const Vector& xu = iterate_->xu();
const Vector& zl = iterate_->zl();
const Vector& zu = iterate_->zu();
Vector& dx = step.x;
Vector& dxl = step.xl;
Vector& dxu = step.xu;
Vector& dy = step.y;
Vector& dzl = step.zl;
Vector& dzu = step.zu;
// Build RHS for KKT system.
Vector rhs1(n+m), rhs2(m);
if (rc) {
for (Int j = 0; j < n+m; j++)
rhs1[j] = -rc[j];
}
for (Int j = 0; j < n+m; j++) {
double rlj = rl ? rl[j] : 0.0;
double ruj = ru ? ru[j] : 0.0;
if (iterate_->has_barrier_lb(j))
rhs1[j] += (sl[j] + zl[j]*rlj) / xl[j];
if (iterate_->has_barrier_ub(j))
rhs1[j] -= (su[j] - zu[j]*ruj) / xu[j];
if (iterate_->StateOf(j) == Iterate::State::fixed)
rhs1[j] = 0.0;
}
assert(AllFinite(rhs1));
if (rb)
std::copy(rb, rb+m, std::begin(rhs2));
// Solve KKT system.
double tol = control_.kkt_tol() * std::sqrt(iterate_->mu());
kkt_->Solve(rhs1, rhs2, tol, dx, dy, info_);
if (info_->errflag)
return;
// Recover solution to Newton system.
dy *= -1.0;
for (Int j = 0; j < n+m; j++) {
if (iterate_->StateOf(j) == Iterate::State::fixed) {
assert(dx[j] == 0.0);
dxl[j] = 0.0;
dzl[j] = 0.0;
} else if (iterate_->StateOf(j) == Iterate::State::free) {
assert(!rl || rl[j] == 0.0);
dxl[j] = 0.0;
dzl[j] = 0.0;
} else {
double rlj = rl ? rl[j] : 0.0;
dxl[j] = dx[j] - rlj;
dzl[j] = (sl[j] - zl[j]*dxl[j]) / xl[j];
}
}
for (Int j = 0; j < n+m; j++) {
if (iterate_->StateOf(j) == Iterate::State::fixed) {
assert(dx[j] == 0.0);
dxu[j] = 0.0;
dzu[j] = 0.0;
} else if (iterate_->StateOf(j) == Iterate::State::free) {
assert(!ru || ru[j] == 0.0);
dxu[j] = 0.0;
dzu[j] = 0.0;
} else {
double ruj = ru ? ru[j] : 0.0;
dxu[j] = ruj - dx[j];
dzu[j] = (su[j] - zu[j]*dxu[j]) / xu[j];
}
}
assert(AllFinite(dxl));
assert(AllFinite(dxu));
assert(AllFinite(dzl));
assert(AllFinite(dzu));
// Shift residual to the last two block equations.
for (Int j = 0; j < n+m; j++) {
if (iterate_->StateOf(j) == Iterate::State::barrier) {
assert(std::isfinite(xl[j]) || std::isfinite(xu[j]));
double atdy = DotColumn(AI, j, dy);
double rcj = rc ? rc[j] : 0.0;
if (std::isfinite(xl[j]) && std::isfinite(xu[j])) {
if (zl[j]*xu[j] >= zu[j]*xl[j])
dzl[j] = rcj + dzu[j] - atdy;
else
dzu[j] = -rcj + dzl[j] + atdy;
} else if (std::isfinite(xl[j])) {
dzl[j] = rcj + dzu[j] - atdy;
} else {
dzu[j] = -rcj + dzl[j] + atdy;
}
}
}
#ifndef NDEBUG
// Check solution for free and fixed variables.
for (Int j = 0; j < n+m; j++) {
if (iterate_->StateOf(j) == Iterate::State::fixed) {
assert(dx[j] == 0.0);
assert(dxl[j] == 0.0 && dxu[j] == 0.0);
assert(dzl[j] == 0.0 && dzu[j] == 0.0);
}
if (iterate_->StateOf(j) == Iterate::State::free)
assert(dzl[j] == 0.0 && dzu[j] == 0.0);
}
#endif
}
void IPM::PrintHeader() {
control_.Log()
<< " " << Format("Iter", 4)
<< " " << Format("P.res", 8) << " " << Format("D.res", 8)
<< " " << Format("P.obj", 15) << " " << Format("D.obj", 15)
<< " " << Format("mu", 8)
<< " " << Format("Time", 7);
control_.Debug()
<< " " << Format("stepsizes", 9)
<< " " << Format("pivots", 7) << " " << Format("kktiter", 7)
<< " " << Format("P.fixed", 7) << " " << Format("D.fixed", 7);
control_.Debug(4) << " " << Format("svdmin(B)", 9);
control_.Debug(4) << " " << Format("density", 8);
control_.Log() << '\n';
}
void IPM::PrintOutput() {
const bool ipm_optimal = iterate_->feasible() && iterate_->optimal();
const Int num_pivots = kkt_ ? kkt_->basis_changes() : Int(0);
const Int num_kktiter = kkt_ ? kkt_->iter() : Int(0);
control_.Log()
<< " " << Format(info_->iter, 3)
<< (ipm_optimal ? "*" : " ")
<< " " << Scientific(iterate_->presidual(), 8, 2)
<< " " << Scientific(iterate_->dresidual(), 8, 2)
<< " " << Scientific(iterate_->pobjective_after_postproc(), 15, 8)
<< " " << Scientific(iterate_->dobjective_after_postproc(), 15, 8)
<< " " << Scientific(iterate_->mu(), 8, 2)
<< " " << Fixed(control_.Elapsed(), 6, 0) << "s";
control_.Debug()
<< " " << Fixed(step_primal_, 4, 2) << " " << Fixed(step_dual_, 4, 2)
<< " " << Format(num_pivots, 7)
<< " " << Format(num_kktiter, 7);
control_.Debug()
<< " " << Format(info_->dual_dropped, 7)
<< " " << Format(info_->primal_dropped, 7);
if (kkt_ && kkt_->basis()) {
const Basis* basis = kkt_->basis();
if (control_.Debug(4)) {
control_.Debug(4) << " "
<< Scientific(basis->MinSingularValue(), 9, 2);
Timer timer;
double density = basis->DensityInverse();
info_->time_symb_invert += timer.Elapsed();
control_.Debug(4) << " " << Scientific(density, 8, 2);
}
} else {
control_.Debug(4) << " " << Format("-", 9);
control_.Debug(4) << " " << Format("-", 8);
}
control_.Log() << '\n';
}
} // namespace ipx