-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathcrossover.cc
467 lines (430 loc) · 16.8 KB
/
crossover.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
// Copyright (c) 2018 ERGO-Code. See license.txt for license.
#include "crossover.h"
#include <algorithm>
#include <cassert>
#include <stdexcept>
#include <valarray>
#include "time.h"
#include "utils.h"
namespace ipx {
Crossover::Crossover(const Control& control) : control_(control) {}
void Crossover::PushAll(Basis* basis, SimplexIterate& iterate,
const double* weights, Info* info) {
const Model& model = basis->model();
const Int m = model.rows();
const Int n = model.cols();
const Vector& lb = model.lb();
const Vector& ub = model.ub();
Vector& x = iterate.x;
Vector& y = iterate.y;
Vector& z = iterate.z;
std::vector<Int> perm = Sortperm(n+m, weights, false);
control_.Log()
<< Textline("Primal residual before push phase:")
<< sci2(PrimalResidual(model, x)) << '\n'
<< Textline("Dual residual before push phase:")
<< sci2(DualResidual(model, y, z)) << '\n';
// Run dual push phase.
std::vector<Int> dual_superbasics;
for (Int p = 0; p < perm.size(); p++) {
Int j = perm[p];
if (basis->IsBasic(j) && z[j] != 0.0)
dual_superbasics.push_back(j);
}
control_.Log()
<< Textline("Number of dual pushes required:")
<< dual_superbasics.size() << '\n';
PushDual(basis, y, z, dual_superbasics, x, info);
assert(DualInfeasibility(model, x, z) == 0.0);
if (info->status_crossover != IPX_STATUS_optimal)
return;
// Run primal push phase. Because z[j]==0 for all basic variables, none of
// the primal variables is fixed at its bound.
std::vector<Int> primal_superbasics;
for (Int p = perm.size()-1; p >= 0; p--) {
Int j = perm[p];
if (basis->IsNonbasic(j) && x[j] != lb[j] && x[j] != ub[j] &&
!(std::isinf(lb[j]) && std::isinf(ub[j]) && x[j] == 0.0))
primal_superbasics.push_back(j);
}
control_.Log()
<< Textline("Number of primal pushes required:")
<< primal_superbasics.size() << '\n';
PushPrimal(basis, x, primal_superbasics, nullptr, info);
assert(PrimalInfeasibility(model, x) == 0.0);
if (info->status_crossover != IPX_STATUS_optimal)
return;
control_.Debug()
<< Textline("Primal residual after push phase:")
<< sci2(PrimalResidual(model, x)) << '\n'
<< Textline("Dual residual after push phase:")
<< sci2(DualResidual(model, y, z)) << '\n';
info->status_crossover = IPX_STATUS_optimal;
}
void Crossover::PushPrimal(Basis* basis, Vector& x,
const std::vector<Int>& variables,
const bool* fixed_at_bound, Info* info) {
Timer timer;
const Model& model = basis->model();
const Int m = model.rows();
const Int n = model.cols();
const Vector& lb = model.lb();
const Vector& ub = model.ub();
IndexedVector ftran(m);
const double feastol = model.dualized() ?
control_.dfeasibility_tol() : control_.pfeasibility_tol();
primal_pushes_ = 0;
primal_pivots_ = 0;
// Check that variables are nonbasic and that x satisfies bound condition.
for (Int j : variables) {
if (!basis->IsNonbasic(j))
throw std::logic_error("invalid variable in Crossover::PushPrimal");
}
for (Int j = 0; j < n+m; j++) {
if (x[j] < lb[j] || x[j] > ub[j])
throw std::logic_error(
"bound condition violated in Crossover::PushPrimal");
if (fixed_at_bound && fixed_at_bound[j] &&
x[j] != lb[j] && x[j] != ub[j])
throw std::logic_error(
"bound condition violated in Crossover::PushPrimal");
}
// Maintain a copy of primal basic variables and their bounds for faster
// ratio test. Fixed-at-bound variables are handled by setting their bounds
// equal.
Vector xbasic = CopyBasic(x, *basis);
Vector lbbasic = CopyBasic(lb, *basis);
Vector ubbasic = CopyBasic(ub, *basis);
if (fixed_at_bound) {
for (Int p = 0; p < m; p++) {
Int j = (*basis)[p];
if (fixed_at_bound[j])
lbbasic[p] = ubbasic[p] = x[j];
}
}
control_.ResetPrintInterval();
Int next = 0;
while (next < variables.size()) {
if ((info->errflag = control_.InterruptCheck()) != 0)
break;
const Int jn = variables[next];
if (x[jn] == lb[jn] || x[jn] == ub[jn] ||
(x[jn] == 0.0 && std::isinf(lb[jn]) && std::isinf(ub[jn]))) {
// nothing to do
next++;
continue;
}
// Choose bound to push to. If the variable has two finite bounds, move
// to the nearer. If it has none, move to zero.
double move_to = 0.0;
if (std::isfinite(lb[jn]) && std::isfinite(ub[jn]))
move_to = x[jn]-lb[jn] <= ub[jn]-x[jn] ? lb[jn] : ub[jn];
else if (std::isfinite(lb[jn]))
move_to = lb[jn];
else if (std::isfinite(ub[jn]))
move_to = ub[jn];
// A full step is such that x[jn]-step is at its bound.
double step = x[jn]-move_to;
basis->SolveForUpdate(jn, ftran);
bool block_at_lb;
Int pblock = PrimalRatioTest(xbasic, ftran, lbbasic, ubbasic,
step, feastol, &block_at_lb);
Int jb = pblock >= 0 ? (*basis)[pblock] : -1;
// If step was blocked, update basis and compute step size.
if (pblock >= 0) {
double pivot = ftran[pblock];
assert(pivot != 0.0);
if (std::abs(pivot) < 1e-4)
control_.Debug(3)
<< " |pivot| = " << sci2(std::abs(pivot)) << '\n';
bool exchanged;
info->errflag = basis->ExchangeIfStable(jb, jn, pivot, -1,
&exchanged);
if (info->errflag) {
control_.Debug()
<< Textline("Minimum singular value of basis matrix:")
<< sci2(basis->MinSingularValue()) << '\n';
break;
}
if (!exchanged) // factorization was unstable, try again
continue;
primal_pivots_++;
// We must use lbbasic[pblock] and ubbasic[pblock] (and not lb[jb]
// and ub[jb]) so that step is 0.0 if a fixed-at-bound variable
// blocked.
if (block_at_lb)
step = (lbbasic[pblock]-xbasic[pblock]) / ftran[pblock];
else
step = (ubbasic[pblock]-xbasic[pblock]) / ftran[pblock];
}
// Update solution.
if (step != 0.0) {
auto update = [&](Int p, double pivot) {
xbasic[p] += step * pivot;
xbasic[p] = std::max(xbasic[p], lbbasic[p]);
xbasic[p] = std::min(xbasic[p], ubbasic[p]);
};
for_each_nonzero(ftran, update);
x[jn] -= step;
}
if (pblock >= 0) {
// make clean
x[jb] = block_at_lb ? lbbasic[pblock] : ubbasic[pblock];
assert(std::isfinite(x[jb]));
// Update copy of basic variables and bounds. Note: jn cannot be
// a fixed-at-bound variable since it was pushed to a bound.
xbasic[pblock] = x[jn];
lbbasic[pblock] = lb[jn];
ubbasic[pblock] = ub[jn];
} else {
x[jn] = move_to; // make clean
assert(std::isfinite(x[jn]));
}
primal_pushes_++;
next++;
control_.IntervalLog()
<< " " << Format(static_cast<Int>(variables.size()-next), 8)
<< " primal pushes remaining"
<< " (" << Format(primal_pivots_, 7) << " pivots)\n";
}
for (Int p = 0; p < m; p++)
x[(*basis)[p]] = xbasic[p];
// Set status flag.
if (info->errflag == IPX_ERROR_interrupt_time) {
info->errflag = 0;
info->status_crossover = IPX_STATUS_time_limit;
} else if (info->errflag != 0) {
info->status_crossover = IPX_STATUS_failed;
} else {
info->status_crossover = IPX_STATUS_optimal;
}
time_primal_ = timer.Elapsed();
}
void Crossover::PushPrimal(Basis* basis, Vector& x,
const std::vector<Int>& variables,
const Vector& z, Info* info) {
std::valarray<bool> bound_restrict = z != 0.0;
PushPrimal(basis, x, variables, &bound_restrict[0], info);
}
void Crossover::PushDual(Basis* basis, Vector& y, Vector& z,
const std::vector<Int>& variables,
const int sign_restrict[], Info* info) {
Timer timer;
const Model& model = basis->model();
const Int m = model.rows();
const Int n = model.cols();
IndexedVector btran(m), row(n+m);
const double feastol = model.dualized() ?
control_.pfeasibility_tol() : control_.dfeasibility_tol();
dual_pushes_ = 0;
dual_pivots_ = 0;
// Check that variables are basic and that z satisfies sign condition.
for (Int j : variables) {
if (!basis->IsBasic(j))
throw std::logic_error("invalid variable in Crossover::PushDual");
}
for (Int j = 0; j < n+m; j++) {
if (((sign_restrict[j] & 1) && z[j] < 0.0) ||
((sign_restrict[j] & 2) && z[j] > 0.0))
throw std::logic_error(
"sign condition violated in Crossover::PushDual");
}
control_.ResetPrintInterval();
Int next = 0;
while (next < variables.size()) {
if ((info->errflag = control_.InterruptCheck()) != 0)
break;
const Int jb = variables[next];
if (z[jb] == 0.0) {
// nothing to do
next++;
continue;
}
// The update operation applied below is
// y := y + step*btran, z := z - step*row, z[jb] := z[jb] - step,
// where row is the tableau row for variable jb. In exact arithmetic
// this leaves A'y+z unchanged.
basis->TableauRow(jb, btran, row);
double step = z[jb];
Int jn = DualRatioTest(z, row, sign_restrict, step, feastol);
// If step was blocked, update basis and compute step size.
if (jn >= 0) {
assert(basis->IsNonbasic(jn));
double pivot = row[jn];
assert(pivot);
if (std::abs(pivot) < 1e-4)
control_.Debug(3)
<< " |pivot| = " << sci2(std::abs(pivot)) << '\n';
bool exchanged;
info->errflag = basis->ExchangeIfStable(jb, jn, pivot, 1,
&exchanged);
if (info->errflag) {
control_.Debug()
<< Textline("Minimum singular value of basis matrix:")
<< sci2(basis->MinSingularValue()) << '\n';
break;
}
if (!exchanged) // factorization was unstable, try again
continue;
dual_pivots_++;
step = z[jn]/row[jn];
// Update must move z[jb] toward zero.
if (sign_restrict[jb] & 1)
assert(step >= 0.0);
if (sign_restrict[jb] & 2)
assert(step <= 0.0);
}
// Update solution.
if (step != 0.0) {
auto update_y = [&](Int i, double x) {
y[i] += step*x;
};
for_each_nonzero(btran, update_y);
auto update_z = [&](Int j, double pivot) {
z[j] -= step * pivot;
if (sign_restrict[j] & 1)
z[j] = std::max(z[j], 0.0);
if (sign_restrict[j] & 2)
z[j] = std::min(z[j], 0.0);
};
for_each_nonzero(row, update_z);
z[jb] -= step;
}
if (jn >= 0)
z[jn] = 0.0; // make clean
else
assert(z[jb] == 0.0);
dual_pushes_++;
next++;
control_.IntervalLog()
<< " " << Format(static_cast<Int>(variables.size()-next), 8)
<< " dual pushes remaining"
<< " (" << Format(dual_pivots_, 7) << " pivots)\n";
}
// Set status flag.
if (info->errflag == IPX_ERROR_interrupt_time) {
info->errflag = 0;
info->status_crossover = IPX_STATUS_time_limit;
} else if (info->errflag != 0) {
info->status_crossover = IPX_STATUS_failed;
} else {
info->status_crossover = IPX_STATUS_optimal;
}
time_dual_ = timer.Elapsed();
}
void Crossover::PushDual(Basis* basis, Vector& y, Vector& z,
const std::vector<Int>& variables,
const Vector& x, Info* info) {
const Model& model = basis->model();
const Int m = model.rows();
const Int n = model.cols();
const Vector& lb = model.lb();
const Vector& ub = model.ub();
std::vector<int> sign_restrict(n+m);
for (Int j = 0; j < sign_restrict.size(); j++) {
if (x[j] != ub[j]) sign_restrict[j] |= 1;
if (x[j] != lb[j]) sign_restrict[j] |= 2;
}
PushDual(basis, y, z, variables, sign_restrict.data(), info);
}
Int Crossover::PrimalRatioTest(const Vector& xbasic, const IndexedVector& ftran,
const Vector& lbbasic, const Vector& ubbasic,
double step, double feastol, bool* block_at_lb) {
Int pblock = -1; // return value
*block_at_lb = true;
// First pass: determine maximum step size exploiting feasibility tol.
auto update_step = [&](Int p, double pivot) {
if (std::abs(pivot) > kPivotZeroTol) {
// test block at lower bound
if (xbasic[p] + step*pivot < lbbasic[p]-feastol) {
step = (lbbasic[p]-xbasic[p]-feastol) / pivot;
pblock = p;
*block_at_lb = true;
}
// test block at upper bound
if (xbasic[p] + step*pivot > ubbasic[p]+feastol) {
step = (ubbasic[p]-xbasic[p]+feastol) / pivot;
pblock = p;
*block_at_lb = false;
}
}
};
for_each_nonzero(ftran, update_step);
// If the step was not blocked, we are done.
if (pblock < 0)
return pblock;
// Second pass: choose maximum pivot among all that block within step.
pblock = -1;
double max_pivot = kPivotZeroTol;
auto update_max = [&](Int p, double pivot) {
if (std::abs(pivot) > max_pivot) {
// test block at lower bound
if (step*pivot < 0.0) {
double step_p = (lbbasic[p]-xbasic[p]) / pivot;
if (std::abs(step_p) <= std::abs(step)) {
pblock = p;
*block_at_lb = true;
max_pivot = std::abs(pivot);
}
}
// test block at upper bound
if (step*pivot > 0.0) {
double step_p = (ubbasic[p]-xbasic[p]) / pivot;
if (std::abs(step_p) <= std::abs(step)) {
pblock = p;
*block_at_lb = false;
max_pivot = std::abs(pivot);
}
}
}
};
for_each_nonzero(ftran, update_max);
assert(pblock >= 0);
return pblock;
}
Int Crossover::DualRatioTest(const Vector& z, const IndexedVector& row,
const int sign_restrict[], double step,
double feastol) {
Int jblock = -1; // return value
// First pass: determine maximum step size exploiting feasibility tol.
auto update_step = [&](Int j, double pivot) {
if (std::abs(pivot) > kPivotZeroTol) {
if ((sign_restrict[j] & 1) && z[j]-step*pivot < -feastol) {
step = (z[j]+feastol) / pivot;
jblock = j;
assert(z[j] >= 0.0);
assert(step*pivot > 0.0);
}
if ((sign_restrict[j] & 2) && z[j]-step*pivot > feastol) {
step = (z[j]-feastol) / pivot;
jblock = j;
assert(z[j] <= 0.0);
assert(step*pivot < 0.0);
}
}
};
for_each_nonzero(row, update_step);
// If step was not block, we are done.
if (jblock < 0)
return jblock;
// Second pass: choose maximum pivot among all that block within step.
jblock = -1;
double max_pivot = kPivotZeroTol;
auto update_max = [&](Int j, double pivot) {
if (std::abs(pivot) > max_pivot &&
std::abs(z[j]/pivot) <= std::abs(step)) {
if ((sign_restrict[j] & 1) && step*pivot > 0.0) {
jblock = j;
max_pivot = std::abs(pivot);
}
if ((sign_restrict[j] & 2) && step*pivot < 0.0) {
jblock = j;
max_pivot = std::abs(pivot);
}
}
};
for_each_nonzero(row, update_max);
assert(jblock >= 0);
return jblock;
}
} // namespace ipx