Aleph-w 7.10.0
A C++ Library for Data Structures and Algorithms
Loading...
Searching...
No Matches
simplex_exhaustive_test.cc
Go to the documentation of this file.
1/*
2 Aleph_w
3
4 Data structures & Algorithms
5 version 2.0.0b
6 https://github.com/lrleon/Aleph-w
7
8 This file is part of Aleph-w library
9
10 Copyright (c) 2002-2026 Leandro Rabindranath Leon
11
12 Permission is hereby granted, free of charge, to any person obtaining a copy
13 of this software and associated documentation files (the "Software"), to deal
14 in the Software without restriction, including without limitation the rights
15 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
16 copies of the Software, and to permit persons to whom the Software is
17 furnished to do so, subject to the following conditions:
18
19 The above copyright notice and this permission notice shall be included in all
20 copies or substantial portions of the Software.
21
22 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
23 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
24 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
25 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
26 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
27 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
28 SOFTWARE.
29*/
30
31
51#include <gtest/gtest.h>
52#include <Simplex.H>
53#include <algorithm>
54#include <cmath>
55#include <cstdint>
56#include <filesystem>
57#include <limits>
58#include <memory>
59#include <random>
60#include <sstream>
61#include <stdexcept>
62#include <string>
63#include <vector>
64
65using namespace Aleph;
66
67namespace
68{
69 using CT = ConstraintType;
70 using i64 = std::int64_t;
71
72 // A linear program with integer data: optimize c·x subject to
73 // a_i·x (type_i) b_i and x >= 0
74 struct Program
75 {
76 size_t n = 0;
77 bool maximize = true;
78 std::vector<i64> c;
79 std::vector<std::vector<i64>> a;
80 std::vector<CT> type;
81 std::vector<i64> b;
82
83 [[nodiscard]] size_t m() const { return a.size(); }
84
85 [[nodiscard]] std::string str() const
86 {
87 std::ostringstream s;
88 s << (maximize ? "max" : "min");
89 for (size_t j = 0; j < n; ++j)
90 s << ' ' << c[j] << "x" << j;
91 for (size_t i = 0; i < m(); ++i)
92 {
93 s << " |";
94 for (size_t j = 0; j < n; ++j)
95 s << ' ' << a[i][j] << "x" << j;
96 s << (type[i] == CT::LE ? " <= " : type[i] == CT::GE ? " >= " : " = ") << b[i];
97 }
98 return s.str();
99 }
100 };
101
102 // ------------------------------------------------------------------
103 // Exact arithmetic of the oracle, which must not overflow
104 // ------------------------------------------------------------------
105
106 i64 magnitude(const i64 x) { return x < 0 ? -x : x; }
107
108 i64 mul(const i64 x, const i64 y)
109 {
110 if (x != 0 and y != 0
111 and magnitude(x) > std::numeric_limits<i64>::max() / magnitude(y))
112 throw std::overflow_error("the oracle overflows");
113 return x * y;
114 }
115
116 i64 add(const i64 x, const i64 y)
117 {
118 if ((y > 0 and x > std::numeric_limits<i64>::max() - y)
119 or (y < 0 and x < -std::numeric_limits<i64>::max() - y))
120 throw std::overflow_error("the oracle overflows");
121 return x + y;
122 }
123
124 // Whether p1/q1 < p2/q2, with q1, q2 > 0
125 bool less(const i64 p1, const i64 q1, const i64 p2, const i64 q2)
126 {
127 return mul(p1, q2) < mul(p2, q1);
128 }
129
130 // The determinant of M, by Bareiss' fraction-free elimination
131 i64 det(std::vector<std::vector<i64>> M)
132 {
133 const size_t n = M.size();
134 i64 sign = 1, prev = 1;
135 for (size_t k = 0; k + 1 < n; ++k)
136 {
137 if (M[k][k] == 0)
138 {
139 size_t r = k + 1;
140 while (r < n and M[r][k] == 0)
141 ++r;
142 if (r == n)
143 return 0;
144 std::swap(M[k], M[r]);
145 sign = -sign;
146 }
147 for (size_t i = k + 1; i < n; ++i)
148 for (size_t j = k + 1; j < n; ++j)
149 M[i][j] = add(mul(M[i][j], M[k][k]), -mul(M[i][k], M[k][j])) / prev;
150 prev = M[k][k];
151 }
152 return sign * M[n - 1][n - 1];
153 }
154
155 // The optimum that the oracle finds
156 struct Optimum
157 {
158 enum Kind { Infeasible, Unbounded, Optimal } kind = Infeasible;
159 i64 num = 0; // the optimal value, num / den
160 i64 den = 1;
161 std::vector<i64> x; // an optimal vertex, x / x_den
162 i64 x_den = 1;
163
164 [[nodiscard]] double value() const { return double(num) / double(den); }
165 };
166
167 // The best vertex of the program with the box x <= U, if one is feasible
168 bool best_vertex(const Program & p, const i64 U, Optimum & best)
169 {
170 const size_t n = p.n, m = p.m();
171
172 // The hyperplanes: the constraints, x_j = 0 and x_j = U
173 const size_t h = m + 2 * n;
174 auto row = [&] (size_t k, size_t j) -> i64
175 {
176 if (k < m)
177 return p.a[k][j];
178 return (k - m) % n == j ? 1 : 0;
179 };
180 auto rhs = [&] (size_t k) -> i64
181 {
182 if (k < m)
183 return p.b[k];
184 return k < m + n ? 0 : U;
185 };
186
187 bool found = false;
188 std::vector<size_t> pick(n);
189 std::vector<std::vector<i64>> M(n, std::vector<i64>(n));
190 std::vector<i64> N(n);
191
192 // every subset of n hyperplanes, in increasing order
193 for (size_t i = 0; i < n; ++i)
194 pick[i] = i;
195 while (true)
196 {
197 for (size_t r = 0; r < n; ++r)
198 for (size_t j = 0; j < n; ++j)
199 M[r][j] = row(pick[r], j);
200
201 if (i64 D = det(M); D != 0)
202 {
203 for (size_t j = 0; j < n; ++j)
204 {
205 auto Mj = M;
206 for (size_t r = 0; r < n; ++r)
207 Mj[r][j] = rhs(pick[r]);
208 N[j] = det(Mj);
209 }
210 if (D < 0)
211 {
212 D = -D;
213 for (auto & v : N)
214 v = -v;
215 }
216
217 bool feasible = true;
218 for (size_t j = 0; j < n and feasible; ++j)
219 feasible = N[j] >= 0 and N[j] <= mul(U, D);
220 for (size_t i = 0; i < m and feasible; ++i)
221 {
222 i64 lhs = 0;
223 for (size_t j = 0; j < n; ++j)
224 lhs = add(lhs, mul(p.a[i][j], N[j]));
225 const i64 bd = mul(p.b[i], D);
226 feasible = p.type[i] == CT::LE ? lhs <= bd
227 : p.type[i] == CT::GE ? lhs >= bd : lhs == bd;
228 }
229
230 if (feasible)
231 {
232 i64 value = 0;
233 for (size_t j = 0; j < n; ++j)
234 value = add(value, mul(p.c[j], N[j]));
235 if (not found
236 or (p.maximize ? less(best.num, best.den, value, D)
237 : less(value, D, best.num, best.den)))
238 {
239 best.num = value;
240 best.den = D;
241 best.x = N;
242 best.x_den = D;
243 }
244 found = true;
245 }
246 }
247
248 // the next subset
249 size_t i = n;
250 while (i > 0 and pick[i - 1] == h - n + i - 1)
251 --i;
252 if (i == 0)
253 break;
254 ++pick[i - 1];
255 for (size_t k = i; k < n; ++k)
256 pick[k] = pick[k - 1] + 1;
257 }
258
259 return found;
260 }
261
262 // A bound of the coordinates of the vertices of the program, with a
263 // right-hand side up to extra_b too: by Cramer's rule,
264 // n! * amax^(n-1) * bmax, at least 1. Scaling a row does not change the
265 // vertices: the bound of the unscaled program holds.
266 i64 vertex_bound(const Program & p, const i64 extra_b = 0)
267 {
268 i64 amax = 1, bmax = std::max<i64>(1, extra_b), f = 1;
269 for (size_t i = 0; i < p.m(); ++i)
270 {
271 bmax = std::max(bmax, magnitude(p.b[i]));
272 for (size_t j = 0; j < p.n; ++j)
273 amax = std::max(amax, magnitude(p.a[i][j]));
274 }
275 for (size_t k = 2; k <= p.n; ++k)
276 f = mul(f, i64(k));
277 i64 bound = mul(f, bmax);
278 for (size_t k = 1; k < p.n; ++k)
279 bound = mul(bound, amax);
280 return bound;
281 }
282
283 // The optimum of p, with the box U, above the coordinates of its
284 // vertices; by default 2 * vertex_bound(p) + 1
285 Optimum solve_exactly(const Program & p, i64 U = 0)
286 {
287 if (U == 0)
288 U = 2 * vertex_bound(p) + 1;
289 Optimum small, large;
290 if (not best_vertex(p, U, small))
291 return small; // Infeasible
292
293 (void) best_vertex(p, 2 * U, large);
294 if (small.num != large.num or small.den != large.den)
295 if (mul(small.num, large.den) != mul(large.num, small.den))
296 {
297 large.kind = Optimum::Unbounded;
298 return large;
299 }
300
301 small.kind = Optimum::Optimal;
302 return small;
303 }
304
305 // ------------------------------------------------------------------
306 // Rational approximation of a bound that the solver gives
307 // ------------------------------------------------------------------
308
309 // p/q, with q in [1, max_den], within tol of v, by continued fractions
310 bool to_rational(const double v, i64 & p, i64 & q, const i64 max_den = 20000)
311 {
312 if (not std::isfinite(v) or std::abs(v) > 1e9)
313 return false;
314
315 double x = v;
316 i64 p0 = 0, q0 = 1, p1 = 1, q1 = 0;
317 for (int k = 0; k < 64; ++k)
318 {
319 const double f = std::floor(x);
320 const i64 a = i64(f);
321 const i64 p2 = add(mul(a, p1), p0), q2 = add(mul(a, q1), q0);
322 if (q2 > max_den)
323 break;
324 p0 = p1; q0 = q1; p1 = p2; q1 = q2;
325 if (std::abs(double(p1) / double(q1) - v) <= 1e-9 * std::max(1.0, std::abs(v)))
326 {
327 p = p1;
328 q = q1;
329 return true;
330 }
331 if (x - f < 1e-12)
332 break;
333 x = 1.0 / (x - f);
334 }
335 return false;
336 }
337
338 // ------------------------------------------------------------------
339 // The solvers on a program
340 // ------------------------------------------------------------------
341
342 template <typename T>
343 std::unique_ptr<Simplex<T>> make_simplex(const Program & p, const bool bland = false)
344 {
345 auto lp = std::make_unique<Simplex<T>>(p.n, p.maximize ? OptimizationType::Maximize
346 : OptimizationType::Minimize);
347 for (size_t j = 0; j < p.n; ++j)
348 lp->put_objetive_function_coef(j, T(p.c[j]));
349 for (size_t i = 0; i < p.m(); ++i)
350 {
351 std::vector<T> row(p.n + 1);
352 for (size_t j = 0; j < p.n; ++j)
353 row[j] = T(p.a[i][j]);
354 row[p.n] = T(p.b[i]);
355 lp->put_restriction(row.data(), p.type[i]);
356 }
357 if (bland)
358 lp->enable_bland_rule();
359 lp->prepare_linear_program();
360 return lp;
361 }
362
363 template <typename T>
364 Optimum::Kind kind_of(const typename Simplex<T>::State s)
365 {
366 switch (s)
367 {
368 case Simplex<T>::Solved: return Optimum::Optimal;
369 case Simplex<T>::Unbounded: return Optimum::Unbounded;
370 default: return Optimum::Infeasible;
371 }
372 }
373
374 const char * name(const Optimum::Kind k)
375 {
376 return k == Optimum::Optimal ? "Optimal" : k == Optimum::Unbounded ? "Unbounded"
377 : "Infeasible";
378 }
379
380 // The tolerance for values of the size of v
381 double tol(const double v, const double base = 1e-8)
382 {
383 return base * std::max(1.0, std::abs(v));
384 }
385
386 // What the checks have seen, so that a test can require that its programs
387 // cover every case
388 struct Coverage
389 {
390 size_t programs = 0;
391 size_t optimal = 0;
392 size_t infeasible = 0;
393 size_t unbounded = 0;
394 size_t degenerate = 0; // optimal at a degenerate vertex
395 size_t with_eq = 0;
396 size_t with_negative_rhs = 0;
397 size_t range_points = 0; // sensitivity points checked
398 size_t range_tight = 0; // points beyond a range checked
399 size_t reoptimized = 0;
400 };
401
402 // Of a solved program: the solution, its value, the shadow prices as an
403 // optimal dual solution, and the reduced costs. Empty if all is right.
404 std::string check_optimum(const Program & p, Simplex<double> & lp, const Optimum & want)
405 {
406 std::ostringstream err;
407 const size_t n = p.n, m = p.m();
408 lp.load_solution();
409 std::vector<double> x(n);
410 double value = 0;
411 for (size_t j = 0; j < n; ++j)
412 {
413 x[j] = lp.get_solution(j);
414 value += double(p.c[j]) * x[j];
415 if (x[j] < -tol(x[j]))
416 err << " x" << j << " = " << x[j] << " < 0;";
417 }
418
419 if (std::abs(value - want.value()) > tol(want.value()))
420 err << " value " << value << ", want " << want.value() << ';';
421 if (std::abs(lp.objetive_value() - value) > tol(value))
422 err << " objetive_value() " << lp.objetive_value() << " != " << value << ';';
423
424 for (size_t i = 0; i < m; ++i)
425 {
426 double lhs = 0;
427 for (size_t j = 0; j < n; ++j)
428 lhs += double(p.a[i][j]) * x[j];
429 const double b = double(p.b[i]);
430 const bool ok = p.type[i] == CT::LE ? lhs <= b + tol(b)
431 : p.type[i] == CT::GE ? lhs >= b - tol(b) : std::abs(lhs - b) <= tol(b);
432 if (not ok)
433 err << " constraint " << i << " violated: " << lhs << " vs " << b << ';';
434 }
435 if (not lp.verify_solution())
436 err << " verify_solution() is false;";
437
438 // The shadow prices: signs, dual feasibility and strong duality
439 const auto prices = lp.get_all_shadow_prices();
440 double dual_value = 0;
441 for (size_t i = 0; i < m; ++i)
442 {
443 const double y = lp.shadow_price(i);
444 if (y != prices[i])
445 err << " get_all_shadow_prices()[" << i << "] differs;";
446 dual_value += y * double(p.b[i]);
447 const bool up = (p.type[i] == CT::LE) == p.maximize; // y >= 0
448 if (p.type[i] != CT::EQ and (up ? y < -tol(y) : y > tol(y)))
449 err << " shadow price " << i << " = " << y << " of the wrong sign;";
450 }
451 if (std::abs(dual_value - want.value()) > tol(want.value(), 1e-7))
452 err << " dual value " << dual_value << " != " << want.value() << ';';
453
454 const auto costs = lp.get_all_reduced_costs();
455 for (size_t j = 0; j < n; ++j)
456 {
457 double s = double(p.c[j]);
458 for (size_t i = 0; i < m; ++i)
459 s -= prices[i] * double(p.a[i][j]);
460 const double rc = lp.reduced_cost(j);
461 if (rc != costs[j])
462 err << " get_all_reduced_costs()[" << j << "] differs;";
463 if (std::abs(rc - s) > tol(s, 1e-7))
464 err << " reduced cost " << j << " = " << rc << ", c - y A = " << s << ';';
465 if (p.maximize ? rc > tol(rc, 1e-7) : rc < -tol(rc, 1e-7))
466 err << " reduced cost " << j << " = " << rc << " of the wrong sign;";
467 if ((x[j] > 1e-7 or lp.is_basic_variable(j)) and std::abs(rc) > tol(rc, 1e-7))
468 err << " basic x" << j << " has reduced cost " << rc << ';';
469 }
470
471 return err.str();
472 }
473
474 // Whether the optimum of the solver is degenerate: fewer positive values
475 // among the variables and the slacks of the inequalities than rows
476 bool degenerate(const Program & p, const Simplex<double> & lp)
477 {
478 size_t positive = 0;
479 for (size_t j = 0; j < p.n; ++j)
480 if (lp.get_solution(j) > 1e-7)
481 ++positive;
482 for (size_t i = 0; i < p.m(); ++i)
483 {
484 if (p.type[i] == CT::EQ)
485 continue;
486 double lhs = 0;
487 for (size_t j = 0; j < p.n; ++j)
488 lhs += double(p.a[i][j]) * lp.get_solution(j);
489 if (std::abs(lhs - double(p.b[i])) > 1e-7)
490 ++positive;
491 }
492 return positive < p.m();
493 }
494
495 // Whether some non-basic reduced cost is zero: the dual is degenerate
496 bool dual_degenerate(const Program & p, const Simplex<double> & lp)
497 {
498 for (size_t j = 0; j < p.n; ++j)
499 if (lp.get_solution(j) <= 1e-7 and std::abs(lp.reduced_cost(j)) <= 1e-7)
500 return true;
501 for (size_t i = 0; i < p.m(); ++i)
502 {
503 if (p.type[i] == CT::EQ)
504 continue;
505 double lhs = 0;
506 for (size_t j = 0; j < p.n; ++j)
507 lhs += double(p.a[i][j]) * lp.get_solution(j);
508 if (std::abs(lhs - double(p.b[i])) <= 1e-7 and std::abs(lp.shadow_price(i)) <= 1e-7)
509 return true;
510 }
511 return false;
512 }
513
514 // The program with c_j = num / den: the whole objective scaled by den
515 Program with_cost(const Program & p, const size_t j, const i64 num, const i64 den)
516 {
517 Program q = p;
518 for (auto & v : q.c)
519 v = mul(v, den);
520 q.c[j] = num;
521 return q;
522 }
523
524 // The program with b_i = num / den: the row i scaled by den
525 Program with_rhs(const Program & p, const size_t i, const i64 num, const i64 den)
526 {
527 Program q = p;
528 for (auto & v : q.a[i])
529 v = mul(v, den);
530 q.b[i] = num;
531 return q;
532 }
533
534 // The ranges of objective_sensitivity(): at each finite bound and in the
535 // middle, the solution stays optimal; just beyond a bound, of a
536 // nondegenerate unique optimum, it is not
537 std::string check_cost_ranges(const Program & p, Simplex<double> & lp, Coverage & cov)
538 {
539 std::ostringstream err;
540 const bool tight = not degenerate(p, lp) and not dual_degenerate(p, lp);
541 const i64 U = 2 * vertex_bound(p) + 1; // the vertices do not change
542 std::vector<double> x(p.n);
543 for (size_t j = 0; j < p.n; ++j)
544 x[j] = lp.get_solution(j);
545
546 for (size_t j = 0; j < p.n; ++j)
547 {
548 const auto r = lp.objective_sensitivity(j);
549 if (r.current_value != double(p.c[j]))
550 err << " cost range " << j << ": current " << r.current_value << ';';
551 if (r.lower_bound > double(p.c[j]) + 1e-9 or r.upper_bound < double(p.c[j]) - 1e-9)
552 err << " cost range " << j << " [" << r.lower_bound << ", " << r.upper_bound
553 << "] misses " << p.c[j] << ';';
554
555 // points: the bounds (or c_j -+ 5 if infinite), and beyond them
556 struct Point { double v; };
557 std::vector<Point> points;
558 const double lo = r.is_unbounded_below() ? double(p.c[j]) - 5 : r.lower_bound;
559 const double hi = r.is_unbounded_above() ? double(p.c[j]) + 5 : r.upper_bound;
560 points.push_back({lo});
561 points.push_back({hi});
562 points.push_back({(lo + hi) / 2});
563
564 for (const Point & pt : points)
565 {
566 i64 num, den;
567 if (not to_rational(pt.v, num, den, 4000))
568 continue;
569 const Program q = with_cost(p, j, num, den);
570 const Optimum o = solve_exactly(q, U);
571 double value = 0;
572 for (size_t k = 0; k < p.n; ++k)
573 value += double(q.c[k]) * x[k];
574 ++cov.range_points;
575 if (o.kind != Optimum::Optimal or std::abs(o.value() - value) > tol(value, 1e-7))
576 err << " cost " << j << " = " << pt.v << " inside its range: the solution gives "
577 << value << ", the optimum is " << name(o.kind) << ' ' << o.value() << ';';
578
579 // just beyond a finite bound
580 const bool is_lo = &pt == &points[0];
581 const bool is_hi = &pt == &points[1];
582 if (not tight or not ((is_lo and not r.is_unbounded_below())
583 or (is_hi and not r.is_unbounded_above())))
584 continue;
585
586 const i64 out_num = mul(num, 2) + (is_lo ? -1 : 1), out_den = mul(den, 2);
587 const Program qo = with_cost(p, j, out_num, out_den);
588 const Optimum oo = solve_exactly(qo, U);
589 double out_value = 0;
590 for (size_t k = 0; k < p.n; ++k)
591 out_value += double(qo.c[k]) * x[k];
592 ++cov.range_tight;
593 const bool still = oo.kind == Optimum::Optimal
594 and std::abs(oo.value() - out_value) <= tol(out_value, 1e-9);
595 if (still)
596 err << " cost " << j << " just beyond " << pt.v << " keeps the solution optimal;";
597 }
598 }
599 return err.str();
600 }
601
602 // The ranges of rhs_sensitivity(): at each finite bound and in the
603 // middle, the optimum changes by the shadow price per unit, and
604 // update_rhs_and_reoptimize() gives it without a pivot; just beyond a
605 // bound, update_rhs_and_reoptimize() gives the optimum of the oracle, with
606 // a pivot or as infeasible, and of a nondegenerate unique optimum the
607 // shadow price no longer gives it
608 std::string check_rhs_ranges(const Program & p, Simplex<double> & lp, const Optimum & want,
609 Coverage & cov)
610 {
611 std::ostringstream err;
612 const bool tight = not degenerate(p, lp) and not dual_degenerate(p, lp);
613
614 for (size_t i = 0; i < p.m(); ++i)
615 {
616 const auto r = lp.rhs_sensitivity(i);
617 const double y = lp.shadow_price(i);
618 const double b = double(p.b[i]);
619 if (r.current_value != b)
620 err << " rhs range " << i << ": current " << r.current_value << ';';
621 if (r.lower_bound > b + 1e-9 or r.upper_bound < b - 1e-9)
622 err << " rhs range " << i << " [" << r.lower_bound << ", " << r.upper_bound
623 << "] misses " << b << ';';
624
625 const double lo = r.is_unbounded_below() ? b - 5 : r.lower_bound;
626 const double hi = r.is_unbounded_above() ? b + 5 : r.upper_bound;
627 const double points[] = {lo, hi, (lo + hi) / 2};
628 for (int k = 0; k < 3; ++k)
629 {
630 const double v = points[k];
631 i64 num, den;
632 if (not to_rational(v, num, den, 4000))
633 continue;
634
635 // the vertices of the program with b_i = v
636 const i64 U = 2 * vertex_bound(p, i64(std::ceil(std::abs(v))) + 1) + 1;
637 const Optimum o = solve_exactly(with_rhs(p, i, num, den), U);
638 const double predicted = want.value() + y * (v - b);
639 ++cov.range_points;
640 if (o.kind != Optimum::Optimal or std::abs(o.value() - predicted) > tol(predicted, 1e-7))
641 err << " rhs " << i << " = " << v << " inside its range: the optimum is "
642 << name(o.kind) << ' ' << o.value() << ", the shadow price gives "
643 << predicted << ';';
644
645 auto again = make_simplex<double>(p);
646 (void) again->solve();
647 const auto st = again->update_rhs_and_reoptimize(i, v);
648 again->load_solution();
649 ++cov.reoptimized;
650 if (st != Simplex<double>::Solved or again->get_pivot_count() != 0
651 or std::abs(again->objetive_value() - predicted) > tol(predicted, 1e-7))
652 err << " update_rhs_and_reoptimize(" << i << ", " << v << ") inside the range gives "
653 << name(kind_of<double>(st)) << ' ' << again->objetive_value() << " with "
654 << again->get_pivot_count() << " pivots;";
655
656 const bool finite = k == 0 ? not r.is_unbounded_below()
657 : k == 1 ? not r.is_unbounded_above() : false;
658 if (not finite)
659 continue;
660
661 // just beyond the bound
662 const i64 out_num = mul(num, 2) + (k == 0 ? -1 : 1), out_den = mul(den, 2);
663 const double out = double(out_num) / double(out_den);
664 const Program qo = with_rhs(p, i, out_num, out_den);
665 const Optimum oo = solve_exactly(qo, U);
666 auto beyond = make_simplex<double>(p);
667 (void) beyond->solve();
668 const auto sto = beyond->update_rhs_and_reoptimize(i, out);
669 beyond->load_solution();
670 ++cov.reoptimized;
671 ++cov.range_tight;
672 if (kind_of<double>(sto) != oo.kind
673 or (oo.kind == Optimum::Optimal
674 and (std::abs(beyond->objetive_value() - oo.value()) > tol(oo.value(), 1e-7)
675 or beyond->get_pivot_count() == 0
676 or not beyond->verify_solution())))
677 err << " update_rhs_and_reoptimize(" << i << ", " << out << ") beyond the range gives "
678 << name(kind_of<double>(sto)) << ' ' << beyond->objetive_value() << " with "
679 << beyond->get_pivot_count() << " pivots, the oracle " << name(oo.kind) << ' '
680 << oo.value() << ';';
681
682 const double out_predicted = want.value() + y * (out - b);
683 if (tight and oo.kind == Optimum::Optimal
684 and std::abs(oo.value() - out_predicted) <= tol(out_predicted, 1e-9))
685 err << " rhs " << i << " just beyond " << v << " keeps the shadow price;";
686 }
687 }
688 return err.str();
689 }
690
691 // Checks the program; the deep checks (sensitivity, float, Bland) only
692 // when asked. Empty if all is right.
693 std::string check_program(const Program & p, Coverage & cov, const bool deep)
694 {
695 ++cov.programs;
696 for (size_t i = 0; i < p.m(); ++i)
697 {
698 cov.with_eq += p.type[i] == CT::EQ;
699 cov.with_negative_rhs += p.b[i] < 0;
700 }
701
702 const Optimum want = solve_exactly(p);
703 auto lp = make_simplex<double>(p);
704 const auto st = lp->solve();
705 std::string err;
706 if (kind_of<double>(st) != want.kind)
707 return std::string(" state ") + name(kind_of<double>(st)) + ", want " + name(want.kind);
708
709 switch (want.kind)
710 {
711 case Optimum::Infeasible:
712 ++cov.infeasible;
713 break;
714 case Optimum::Unbounded:
715 ++cov.unbounded;
716 lp->load_solution();
717 if (not lp->verify_solution())
718 err += " the vertex of an unbounded program is not feasible;";
719 break;
720 case Optimum::Optimal:
721 ++cov.optimal;
722 err += check_optimum(p, *lp, want);
723 if (degenerate(p, *lp))
724 ++cov.degenerate;
725 if (deep)
726 {
727 err += check_cost_ranges(p, *lp, cov);
728 err += check_rhs_ranges(p, *lp, want, cov);
729 }
730 break;
731 }
732
733 // Bland's rule gives the same
734 auto bland = make_simplex<double>(p, true);
735 const auto stb = bland->solve();
736 if (stb != st)
737 err += std::string(" with Bland's rule: ") + name(kind_of<double>(stb));
738 else if (st == Simplex<double>::Solved)
739 {
740 bland->load_solution();
741 if (std::abs(bland->objetive_value() - want.value()) > tol(want.value()))
742 err += " with Bland's rule the value is " + std::to_string(bland->objetive_value());
743 if (not bland->get_stats().bland_rule_used and bland->get_pivot_count() > 0)
744 err += " Bland's rule is not reported as used;";
745 }
746
747 if (deep)
748 {
749 auto lf = make_simplex<float>(p);
750 const auto stf = lf->solve();
751 if (kind_of<float>(stf) != want.kind)
752 err += std::string(" with float: ") + name(kind_of<float>(stf));
753 else if (stf == Simplex<float>::Solved)
754 {
755 lf->load_solution();
756 if (std::abs(double(lf->objetive_value()) - want.value()) > tol(want.value(), 1e-3))
757 err += " with float the value is " + std::to_string(lf->objetive_value());
758 }
759 }
760
761 return err;
762 }
763
764 // RevisedSimplex on a program of <= constraints to maximize
765 std::string check_revised(const Program & p, const size_t refactor_frequency)
766 {
767 RevisedSimplex<double> rs(p.n, p.m());
768 for (size_t j = 0; j < p.n; ++j)
769 rs.set_objective(j, double(p.c[j]));
770 for (size_t i = 0; i < p.m(); ++i)
771 {
772 std::vector<double> row(p.n);
773 for (size_t j = 0; j < p.n; ++j)
774 row[j] = double(p.a[i][j]);
775 rs.set_constraint_row(i, row.data(), double(p.b[i]));
776 }
777 rs.set_refactorization_frequency(refactor_frequency);
778
779 const Optimum want = solve_exactly(p);
780 const auto st = rs.solve();
781 const Optimum::Kind got = st == RevisedSimplex<double>::Solved ? Optimum::Optimal
782 : st == RevisedSimplex<double>::Unbounded ? Optimum::Unbounded : Optimum::Infeasible;
783 if (got != want.kind)
784 return std::string(" RevisedSimplex: ") + name(got) + ", want " + name(want.kind);
785 if (got == Optimum::Optimal)
786 {
787 if (std::abs(rs.objective_value() - want.value()) > tol(want.value()))
788 return " RevisedSimplex value " + std::to_string(rs.objective_value()) + ", want "
789 + std::to_string(want.value());
790 if (not rs.verify_solution())
791 return " RevisedSimplex: verify_solution() is false";
792 }
793 return "";
794 }
795
796 // A random program: n in [1, max_n], m in [1, max_m], coefficients in
797 // [-amax, amax], a third of them zero. In a third of the programs the
798 // right-hand sides are in [-bmax, bmax], which makes most of them
799 // infeasible; in the others they are such that a point x0 of
800 // coordinates in [0, 3] is feasible, by up to 3 in an inequality.
801 Program random_program(std::mt19937_64 & rng, const size_t max_n, const size_t max_m,
802 const i64 amax, const i64 bmax, const bool only_le = false)
803 {
804 // the raw numbers of std::mt19937_64, which the standard fixes, so that
805 // every standard library makes the same programs
806 auto pick = [&] (i64 lo, i64 hi) { return lo + i64(rng() % std::uint64_t(hi - lo + 1)); };
807 Program p;
808 p.n = size_t(pick(1, i64(max_n)));
809 const size_t m = size_t(pick(1, i64(max_m)));
810 p.maximize = only_le or pick(0, 1) == 1;
811 p.c.resize(p.n);
812 for (auto & v : p.c)
813 v = pick(-amax, amax);
814
815 const bool around_x0 = pick(0, 2) != 0;
816 std::vector<i64> x0(p.n);
817 for (auto & v : x0)
818 v = pick(0, 3);
819
820 for (size_t i = 0; i < m; ++i)
821 {
822 std::vector<i64> row(p.n);
823 for (auto & v : row)
824 v = pick(0, 2) == 0 ? 0 : pick(-amax, amax);
825 const i64 t = pick(0, 9);
826 const CT type = only_le or t < 5 ? CT::LE : t < 8 ? CT::GE : CT::EQ;
827 i64 b = pick(-bmax, bmax);
828 if (around_x0)
829 {
830 i64 ax = 0;
831 for (size_t j = 0; j < p.n; ++j)
832 ax += row[j] * x0[j];
833 const i64 room = pick(0, 3);
834 b = type == CT::LE ? ax + room : type == CT::GE ? ax - room : ax;
835 }
836 p.a.push_back(row);
837 p.type.push_back(type);
838 p.b.push_back(b);
839 }
840 return p;
841 }
842} // namespace
843
844// ==========================================================================
845// The oracle itself, on programs whose answer is known
846// ==========================================================================
847
848TEST(SimplexOracle, KnowsTheTextbookPrograms)
849{
850 Program p;
851 p.n = 2;
852 p.c = {40, 30};
853 p.a = {{1, 1}, {2, 1}};
854 p.type = {CT::LE, CT::LE};
855 p.b = {40, 60};
856 auto o = solve_exactly(p);
857 ASSERT_EQ(o.kind, Optimum::Optimal);
858 EXPECT_DOUBLE_EQ(o.value(), 1400.0);
859
860 p.a = {{1, 0}, {1, 0}};
861 p.type = {CT::LE, CT::GE};
862 p.b = {0, 1};
863 EXPECT_EQ(solve_exactly(p).kind, Optimum::Infeasible);
864
865 p.a = {{1, -1}};
866 p.type = {CT::LE};
867 p.b = {1};
868 EXPECT_EQ(solve_exactly(p).kind, Optimum::Unbounded);
869
870 p.maximize = false;
871 p.c = {1, 1};
872 p.a = {{1, 1}, {1, -1}};
873 p.type = {CT::EQ, CT::EQ};
874 p.b = {3, 1};
875 o = solve_exactly(p);
876 ASSERT_EQ(o.kind, Optimum::Optimal);
877 EXPECT_DOUBLE_EQ(o.value(), 3.0);
878}
879
880TEST(SimplexOracle, RationalApproximation)
881{
882 i64 p, q;
883 ASSERT_TRUE(to_rational(2.5, p, q));
884 EXPECT_EQ(p, 5);
885 EXPECT_EQ(q, 2);
886 ASSERT_TRUE(to_rational(-1.0 / 3.0, p, q));
887 EXPECT_EQ(p, -1);
888 EXPECT_EQ(q, 3);
889 ASSERT_TRUE(to_rational(7.0, p, q));
890 EXPECT_EQ(p, 7);
891 EXPECT_EQ(q, 1);
892 EXPECT_FALSE(to_rational(std::numeric_limits<double>::infinity(), p, q));
893}
894
895// ==========================================================================
896// Every program of a small domain
897// ==========================================================================
898
899// n and m of 1 and 2, coefficients and right-hand sides in {-1, 0, 1},
900// every type, every objective in {-1, 0, 1}^n, maximizing and minimizing
901TEST(SimplexExhaustive, EveryProgramOfTheSmallDomain)
902{
903 Coverage cov;
904 size_t failures = 0;
905 const CT types[] = {CT::LE, CT::GE, CT::EQ};
906
907 for (size_t n = 1; n <= 2; ++n)
908 {
909 // every row: n coefficients, a right-hand side and a type
910 std::vector<std::pair<std::vector<i64>, std::pair<i64, CT>>> rows;
911 const size_t combos = n == 1 ? 3 : 9;
912 for (size_t code = 0; code < combos; ++code)
913 {
914 std::vector<i64> a(n);
915 size_t rest = code;
916 for (size_t j = 0; j < n; ++j, rest /= 3)
917 a[j] = i64(rest % 3) - 1;
918 for (i64 b = -1; b <= 1; ++b)
919 for (CT t : types)
920 rows.push_back({a, {b, t}});
921 }
922
923 std::vector<std::vector<i64>> objectives;
924 for (size_t code = 0; code < combos; ++code)
925 {
926 std::vector<i64> c(n);
927 size_t rest = code;
928 for (size_t j = 0; j < n; ++j, rest /= 3)
929 c[j] = i64(rest % 3) - 1;
930 objectives.push_back(c);
931 }
932
933 for (size_t m = 1; m <= 2; ++m)
934 {
935 std::vector<size_t> idx(m, 0);
936 while (true)
937 {
938 for (const auto & c : objectives)
939 for (bool maximize : {true, false})
940 {
941 Program p;
942 p.n = n;
943 p.maximize = maximize;
944 p.c = c;
945 for (size_t k : idx)
946 {
947 p.a.push_back(rows[k].first);
948 p.b.push_back(rows[k].second.first);
949 p.type.push_back(rows[k].second.second);
950 }
951 const std::string e = check_program(p, cov, false);
952 if (not e.empty() and ++failures <= 20)
953 ADD_FAILURE() << p.str() << ":" << e;
954 }
955
956 size_t k = 0;
957 while (k < m and ++idx[k] == rows.size())
958 idx[k++] = 0;
959 if (k == m)
960 break;
961 }
962 }
963 }
964
965 EXPECT_EQ(failures, 0u);
966 EXPECT_GT(cov.programs, 100000u);
967 EXPECT_GT(cov.optimal, 10000u);
968 EXPECT_GT(cov.infeasible, 10000u);
969 EXPECT_GT(cov.unbounded, 10000u);
970 EXPECT_GT(cov.degenerate, 1000u);
971}
972
973// ==========================================================================
974// Random programs, with the deep checks
975// ==========================================================================
976
977TEST(SimplexExhaustive, RandomProgramsWithTheirSensitivity)
978{
979 std::mt19937_64 rng(20261104);
980 Coverage cov;
981 size_t failures = 0;
982 for (int k = 0; k < 1500; ++k)
983 {
984 const Program p = random_program(rng, 3, 4, 5, 10);
985 const std::string e = check_program(p, cov, true);
986 if (not e.empty() and ++failures <= 20)
987 ADD_FAILURE() << p.str() << ":" << e;
988 }
989
990 EXPECT_EQ(failures, 0u);
991 EXPECT_GT(cov.optimal, 400u);
992 EXPECT_GT(cov.infeasible, 150u);
993 EXPECT_GT(cov.unbounded, 200u);
994 EXPECT_GT(cov.degenerate, 150u);
995 EXPECT_GT(cov.with_eq, 400u);
996 EXPECT_GT(cov.with_negative_rhs, 800u);
997 EXPECT_GT(cov.range_points, 5000u);
998 EXPECT_GT(cov.range_tight, 1500u);
999 EXPECT_GT(cov.reoptimized, 4000u);
1000}
1001
1002TEST(SimplexExhaustive, LargerRandomPrograms)
1003{
1004 std::mt19937_64 rng(4111);
1005 Coverage cov;
1006 size_t failures = 0;
1007 for (int k = 0; k < 400; ++k)
1008 {
1009 const Program p = random_program(rng, 4, 5, 5, 10);
1010 const std::string e = check_program(p, cov, false);
1011 if (not e.empty() and ++failures <= 20)
1012 ADD_FAILURE() << p.str() << ":" << e;
1013 }
1014
1015 EXPECT_EQ(failures, 0u);
1016 EXPECT_GT(cov.optimal, 100u);
1017 EXPECT_GT(cov.infeasible, 35u);
1018 EXPECT_GT(cov.unbounded, 55u);
1019}
1020
1021// Random programs with all their data multiplied by 1e6 and by 1e-6, and
1022// with each row, each column and the objective multiplied by its own power
1023// of ten, in [1e-6, 1e6]: the same states; the optimum scaled as the
1024// objective; a solution feasible in the program as given; and shadow prices
1025// of the right signs whose dual value is the optimum. The tolerances of the
1026// solver must not depend on the units of the data.
1027TEST(SimplexExhaustive, ScaledProgramsGiveScaledResults)
1028{
1029 std::mt19937_64 rng(77);
1030 auto power = [&] { return std::pow(10.0, double(i64(rng() % 13) - 6)); };
1031 size_t failures = 0, solved = 0, programs = 0;
1032 for (int k = 0; k < 400; ++k)
1033 {
1034 const Program p = random_program(rng, 3, 4, 5, 10);
1035 const Optimum want = solve_exactly(p);
1036 for (int how = 0; how < 3; ++how)
1037 {
1038 // the scales: of each row, of each column (x_j = col_j x'_j) and
1039 // of the objective
1040 std::vector<double> row(p.m()), col(p.n);
1041 double obj;
1042 if (how < 2)
1043 {
1044 const double all = how == 0 ? 1e6 : 1e-6;
1045 std::fill(row.begin(), row.end(), all);
1046 std::fill(col.begin(), col.end(), 1.0);
1047 obj = all;
1048 }
1049 else
1050 {
1051 for (auto & v : row)
1052 v = power();
1053 for (auto & v : col)
1054 v = power();
1055 obj = power();
1056 }
1057
1058 Simplex<double> lp(p.n, p.maximize ? OptimizationType::Maximize
1059 : OptimizationType::Minimize);
1060 for (size_t j = 0; j < p.n; ++j)
1061 lp.put_objetive_function_coef(j, double(p.c[j]) * col[j] * obj);
1062 std::vector<std::vector<double>> a(p.m(), std::vector<double>(p.n + 1));
1063 for (size_t i = 0; i < p.m(); ++i)
1064 {
1065 for (size_t j = 0; j < p.n; ++j)
1066 a[i][j] = double(p.a[i][j]) * row[i] * col[j];
1067 a[i][p.n] = double(p.b[i]) * row[i];
1068 lp.put_restriction(a[i].data(), p.type[i]);
1069 }
1071 const auto st = lp.solve();
1072 ++programs;
1073 std::string err;
1074 if (kind_of<double>(st) != want.kind)
1075 err = std::string(name(kind_of<double>(st))) + ", want " + name(want.kind);
1076 else if (st == Simplex<double>::Solved)
1077 {
1078 ++solved;
1079 lp.load_solution();
1080 const double v = want.value() * obj;
1081 if (std::abs(lp.objetive_value() - v) > tol(v, 1e-7))
1082 err = "value " + std::to_string(lp.objetive_value()) + ", want "
1083 + std::to_string(v);
1084 else if (not lp.verify_solution())
1085 err = "verify_solution() is false";
1086
1087 // the shadow prices: strong duality and their signs
1088 double dual = 0, size = std::abs(v);
1089 for (size_t i = 0; i < p.m(); ++i)
1090 {
1091 const double y = lp.shadow_price(i);
1092 dual += y * a[i][p.n];
1093 size += std::abs(y * a[i][p.n]);
1094 const bool up = (p.type[i] == CT::LE) == p.maximize;
1095 if (p.type[i] != CT::EQ and (up ? y < -tol(y) : y > tol(y)))
1096 err += " shadow price " + std::to_string(i) + " of the wrong sign;";
1097 }
1098 if (std::abs(dual - v) > 1e-7 * std::max(1.0, size))
1099 err += " dual value " + std::to_string(dual) + ", want " + std::to_string(v);
1100 }
1101 if (not err.empty() and ++failures <= 20)
1102 ADD_FAILURE() << p.str() << " scaled (" << how << "): " << err;
1103 }
1104 }
1105 EXPECT_EQ(failures, 0u);
1106 EXPECT_EQ(programs, 1200u);
1107 EXPECT_GT(solved, 300u);
1108}
1109
1110// ==========================================================================
1111// latex_solve() gives what solve() gives, and writes its files
1112// ==========================================================================
1113
1114TEST(SimplexExhaustive, LatexSolveGivesWhatSolveGives)
1115{
1116 const auto dir = std::filesystem::temp_directory_path()
1117 / ("aleph_simplex_latex_" + std::to_string(std::random_device{}()));
1118 std::filesystem::create_directories(dir);
1119
1120 std::mt19937_64 rng(5);
1121 size_t failures = 0;
1122 for (int k = 0; k < 60; ++k)
1123 {
1124 const Program p = random_program(rng, 3, 3, 5, 10);
1125 auto a = make_simplex<double>(p);
1126 auto b = make_simplex<double>(p);
1127 const auto sa = a->solve();
1128 const std::string base = (dir / ("p" + std::to_string(k))).string();
1129 const auto sb = b->latex_solve(base.c_str());
1130 bool ok = sa == sb and std::filesystem::exists(base + "-0.tex");
1131 if (ok and sa == Simplex<double>::Solved)
1132 {
1133 a->load_solution();
1134 b->load_solution();
1135 ok = std::abs(a->objetive_value() - b->objetive_value()) <= tol(a->objetive_value())
1136 and a->get_pivot_count() == b->get_pivot_count();
1137 }
1138 if (ok and b->get_pivot_count() > 0)
1139 ok = std::filesystem::exists(base + "-1.tex")
1140 and std::filesystem::exists(base + "-1-after.tex");
1141 if (not ok and ++failures <= 10)
1142 ADD_FAILURE() << p.str();
1143 }
1144 EXPECT_EQ(failures, 0u);
1145
1146 std::filesystem::remove_all(dir);
1147}
1148
1149// ==========================================================================
1150// RevisedSimplex
1151// ==========================================================================
1152
1153TEST(RevisedSimplexExhaustive, RandomProgramsOfLessOrEqualConstraints)
1154{
1155 std::mt19937_64 rng(1234);
1156 size_t failures = 0, optimal = 0, infeasible = 0, unbounded = 0;
1157 for (int k = 0; k < 800; ++k)
1158 {
1159 const Program p = random_program(rng, 4, 5, 5, 10, true);
1160 const Optimum want = solve_exactly(p);
1161 optimal += want.kind == Optimum::Optimal;
1162 infeasible += want.kind == Optimum::Infeasible;
1163 unbounded += want.kind == Optimum::Unbounded;
1164 for (size_t f : {size_t(1), size_t(2), size_t(7), size_t(50)})
1165 if (const std::string e = check_revised(p, f); not e.empty() and ++failures <= 20)
1166 ADD_FAILURE() << p.str() << " refactorization every " << f << ":" << e;
1167 }
1168 EXPECT_EQ(failures, 0u);
1169 EXPECT_GT(optimal, 180u);
1170 EXPECT_GT(infeasible, 60u);
1171 EXPECT_GT(unbounded, 150u);
1172}
1173
1174// RevisedSimplex on random programs of <= constraints with each row, each
1175// column and the objective multiplied by its own power of ten, in
1176// [1e-6, 1e6]: the same states, the optimum scaled as the objective, and a
1177// solution feasible in the program as given
1178TEST(RevisedSimplexExhaustive, ScaledPrograms)
1179{
1180 std::mt19937_64 rng(4321);
1181 auto power = [&] { return std::pow(10.0, double(i64(rng() % 13) - 6)); };
1182 size_t failures = 0, solved = 0;
1183 for (int k = 0; k < 400; ++k)
1184 {
1185 const Program p = random_program(rng, 4, 5, 5, 10, true);
1186 const Optimum want = solve_exactly(p);
1187 std::vector<double> row(p.m()), col(p.n);
1188 for (auto & v : row)
1189 v = power();
1190 for (auto & v : col)
1191 v = power();
1192 const double obj = power();
1193
1194 RevisedSimplex<double> rs(p.n, p.m());
1195 for (size_t j = 0; j < p.n; ++j)
1196 rs.set_objective(j, double(p.c[j]) * col[j] * obj);
1197 for (size_t i = 0; i < p.m(); ++i)
1198 {
1199 std::vector<double> r(p.n);
1200 for (size_t j = 0; j < p.n; ++j)
1201 r[j] = double(p.a[i][j]) * row[i] * col[j];
1202 rs.set_constraint_row(i, r.data(), double(p.b[i]) * row[i]);
1203 }
1204 const auto st = rs.solve();
1205 const Optimum::Kind got = st == RevisedSimplex<double>::Solved ? Optimum::Optimal
1206 : st == RevisedSimplex<double>::Unbounded ? Optimum::Unbounded : Optimum::Infeasible;
1207 bool ok = got == want.kind;
1208 if (ok and got == Optimum::Optimal)
1209 {
1210 ++solved;
1211 const double v = want.value() * obj;
1212 ok = std::abs(rs.objective_value() - v) <= tol(v, 1e-7) and rs.verify_solution();
1213 }
1214 if (not ok and ++failures <= 20)
1215 ADD_FAILURE() << p.str() << ": " << name(got) << ", want " << name(want.kind);
1216 }
1217 EXPECT_EQ(failures, 0u);
1218 EXPECT_GT(solved, 100u);
1219}
1220
1221TEST(RevisedSimplexExhaustive, AgreesWithSimplex)
1222{
1223 std::mt19937_64 rng(99);
1224 size_t failures = 0;
1225 for (int k = 0; k < 300; ++k)
1226 {
1227 const Program p = random_program(rng, 4, 5, 5, 10, true);
1228 auto lp = make_simplex<double>(p);
1229 const auto st = lp->solve();
1230 RevisedSimplex<double> rs(p.n, p.m());
1231 for (size_t j = 0; j < p.n; ++j)
1232 rs.set_objective(j, double(p.c[j]));
1233 for (size_t i = 0; i < p.m(); ++i)
1234 {
1235 std::vector<double> row(p.n);
1236 for (size_t j = 0; j < p.n; ++j)
1237 row[j] = double(p.a[i][j]);
1238 rs.set_constraint_row(i, row.data(), double(p.b[i]));
1239 }
1240 const auto sr = rs.solve();
1241 bool ok = int(st) == int(sr);
1242 if (ok and st == Simplex<double>::Solved)
1243 {
1244 lp->load_solution();
1245 ok = std::abs(lp->objetive_value() - rs.objective_value()) <= tol(rs.objective_value());
1246 }
1247 if (not ok and ++failures <= 10)
1248 ADD_FAILURE() << p.str();
1249 }
1250 EXPECT_EQ(failures, 0u);
1251}
Simplex algorithm for linear programming.
long double h
Definition btreepic.C:154
size_t size_t int32_t value
Definition ca-c-api.h:116
size_t size_t int32_t * out
Definition ca-c-api.h:120
size_t row
Definition ca-c-api.h:115
size_t * rows
Definition ca-c-api.h:112
size_t size_t col
Definition ca-c-api.h:116
Number Theoretic Transform over Z / MOD Z.
Definition ntt.H:115
Represents a point with rectangular coordinates in a 2D plane.
Definition point.H:221
Revised Simplex algorithm for linear programming.
Definition Simplex.H:2176
void set_objective(size_t j, T coef)
Sets objective coefficient.
Definition Simplex.H:2732
void set_constraint_row(size_t i, const T *coefs, T rhs)
Adds a complete constraint.
Definition Simplex.H:2784
T objective_value() const
Computes objective function value.
Definition Simplex.H:2920
State solve()
Solves the linear program.
Definition Simplex.H:2813
bool verify_solution() const
Verifies solution satisfies all constraints.
Definition Simplex.H:2948
Linear program solver using the Simplex method.
Definition Simplex.H:279
SensitivityRange< T > rhs_sensitivity(size_t rest_idx) const
Computes sensitivity range for a RHS value.
Definition Simplex.H:1744
State
Solution states for the linear program.
Definition Simplex.H:290
T objetive_value() const noexcept
Computes and returns the objective function value.
Definition Simplex.H:1374
std::vector< T > get_all_reduced_costs() const
Gets all reduced costs as a vector.
Definition Simplex.H:1895
bool verify_solution() const
Verifies that the solution satisfies all constraints.
Definition Simplex.H:1393
SensitivityRange< T > objective_sensitivity(size_t var_idx) const
Computes sensitivity range for an objective coefficient.
Definition Simplex.H:1677
size_t get_pivot_count() const noexcept
Gets the number of pivot operations.
Definition Simplex.H:1636
T * put_restriction(const T *coefs=nullptr, ConstraintType type=ConstraintType::LE)
Adds a constraint to the linear program.
Definition Simplex.H:1132
void prepare_linear_program()
Prepares the linear program for solving.
Definition Simplex.H:1287
T shadow_price(size_t rest_idx) const
Gets the shadow price (dual value) for a constraint.
Definition Simplex.H:1812
bool is_basic_variable(size_t var_idx) const
Checks if a variable is basic in the optimal solution.
Definition Simplex.H:1865
const T & get_solution(size_t i) const noexcept
Gets the solution value for a specific variable.
Definition Simplex.H:1360
void put_objetive_function_coef(size_t i, const T &coef)
Sets a coefficient in the objective function.
Definition Simplex.H:1072
T reduced_cost(size_t var_idx) const
Gets the reduced cost for a variable.
Definition Simplex.H:1843
void load_solution() noexcept
Loads the solution values into the solution array.
Definition Simplex.H:1337
State solve()
Solves the linear program.
Definition Simplex.H:1310
std::vector< T > get_all_shadow_prices() const
Gets all shadow prices as a vector.
Definition Simplex.H:1880
constexpr size_t size() const noexcept
Returns the number of entries in the table.
Definition hashDry.H:954
#define TEST(name)
static mt19937 rng
#define N
Definition fib.C:294
__gmp_expr< T, __gmp_unary_expr< __gmp_expr< T, U >, __gmp_abs_function > > abs(const __gmp_expr< T, U > &expr)
Definition gmpfrxx.h:4129
static mpfr_t y
Definition mpfr_mul_d.c:3
ca_size_t magnitude(const ca_index_t c) noexcept
Main namespace for Aleph-w library functions.
size_t size(Node *root) noexcept
See compute_cardinality_rec(Node * root).
bool all(Container &container, Operation &operation)
Return true if all elements satisfy a predicate.
std::decay_t< typename HeadC::Item_Type > T
Definition ah-zip.H:152
std::string code(Node *root)
Compute a string with the Lukasiewicz`s word of a tree.
and
Check uniqueness with explicit hash + equality functors.
ConstraintType
Constraint type for linear programming.
Definition Simplex.H:85
double add(double a, double b)
double mul(double a, double b)
Built_Graph_T< Graph > build_graph(const size_t n, const std::vector< Edge_Def > &edges)
int num(Node *p)
The number of a node of a built graph.
STL namespace.
FooMap m(5, fst_unit_pair_hash, snd_unit_pair_hash)
static int * k
Gsl_Compatible_Rng r