LINE Solver (C++)
Templated C++ port of the LINE queueing solver
Loading...
Searching...
No Matches
mamap22_repair.h
Go to the documentation of this file.
1/*
2 * Copyright (c) 2012-2026, QORE Lab, Imperial College London
3 * All rights reserved.
4 */
5#ifndef LINE_API_MAM_MAMAP22_REPAIR_H
6#define LINE_API_MAM_MAMAP22_REPAIR_H
7
8/**
9 * @file
10 * @ingroup api_mam
11 * The exact global repair of the MAMAP(2,2) moment-plus-sigma inverse, shared by
12 * `mamap22_fit_fs.h` and `mamap22_fit_bs.h`.
13 *
14 * Port of `_real_roots`, `_polymul`, `_polysub`, `_bilinear_repair` and
15 * `_hybrid_repair` in python/line_solver/lib/m3a/mamap22.py.
16 *
17 * When the closed-form inverse lands outside the unit box, the MATLAB reference
18 * repairs it by solving (`mamap22_bilinear_repair.m`; YALMIP `bmibnb` until
19 * 2026-11-29) the NONCONVEX program
20 *
21 * min wx (x/X - 1)^2 + ws (s/St - 1)^2
22 * s.t. q (d1 x + d0) = n2 x^2 + n1 x + n0 + c s
23 * 0 <= alpha_i + beta_i x + gamma_i q <= 1 (i = 1, 2)
24 * 0 <= q <= 1, 0 <= s <= 1, xlo <= x <= xhi
25 *
26 * over x (the forward or backward moment), q (the free marking probability) and
27 * s (the sigma entry S11), on the two sides of the map mean, and keeps the better
28 * side or the trivial marking. This is solved EXACTLY here, without a branch and
29 * bound: for fixed x the equality makes s affine in q, so the inner problem is a
30 * convex quadratic in q over an interval, minimized by clamping the unconstrained
31 * minimizer (s = St). Every interval end is a ratio P(x)/Q(x) with deg P <= 2,
32 * deg Q <= 1, so the outer function of x is continuous and piecewise smooth, its
33 * pieces delimited by real roots of P_i Q_j - P_j Q_i (degree <= 3). On a piece
34 * where q sits at an affine bound the objective is a quartic in x with
35 * stationary points at the roots of a cubic; elsewhere it is wx (x/X - 1)^2 plus a
36 * constant, minimized at X. The global minimum is attained at one of finitely
37 * many candidates, all of which are enumerated and evaluated.
38 *
39 * ARITHMETIC: double. The repair selects (x, s); the callers then evaluate the
40 * closed-form inverse at that point in their own scalar type, as the other
41 * one-variable repairs of these fitters do.
42 */
43
44#include <algorithm>
45#include <cmath>
46#include <cstddef>
47#include <limits>
48#include <vector>
49
50namespace line {
51namespace mam {
52namespace mamap22detail {
53
54/** The bilinear program's data: (q_a, q_b) = alpha + beta x + gamma q_free, and
55 * q_free (d1 x + d0) = n2 x^2 + n1 x + n0 + c s. */
57 double alpha[2];
58 double beta[2];
59 double gamma[2];
60 double d0, d1, n0, n1, n2, c;
61};
62
63/** The optimum of one side of the repair. */
65 bool feasible = false;
66 double obj = std::numeric_limits<double>::quiet_NaN();
67 double x = std::numeric_limits<double>::quiet_NaN();
68 double s = std::numeric_limits<double>::quiet_NaN();
69};
70
71/** What the hybrid selector chose: the trivial marking q1 = q2 = q3 = p, or the
72 * closed-form inverse evaluated at (x, s). */
74 bool trivial = true;
75 double x = std::numeric_limits<double>::quiet_NaN();
76 double s = std::numeric_limits<double>::quiet_NaN();
77};
78
79/**
80 * Real roots of c[0] + c[1] x + c[2] x^2 + c[3] x^3 (degree <= 3; shorter inputs
81 * are zero padded). Leading coefficients negligible against the largest one are
82 * dropped; the cubic is solved by Cardano/Viete and every root polished by two
83 * Newton steps. Empty for the zero (or constant) polynomial.
84 */
85inline std::vector<double> real_roots(const std::vector<double>& cin) {
86 double c[4] = {0.0, 0.0, 0.0, 0.0};
87 for (std::size_t i = 0; i < cin.size() && i < 4; ++i) c[i] = cin[i];
88 double scale = 0.0;
89 for (int i = 0; i < 4; ++i) scale = std::max(scale, std::fabs(c[i]));
90 std::vector<double> roots;
91 if (scale == 0.0) return roots;
92 int d = 3;
93 while (d > 0 && std::fabs(c[d]) <= 1e-14 * scale) --d;
94 if (d == 0) return roots;
95 if (d == 1) {
96 roots.push_back(-c[0] / c[1]);
97 } else if (d == 2) {
98 const double a = c[2], b = c[1], k = c[0];
99 double disc = b * b - 4.0 * a * k;
100 if (disc < 0.0) {
101 if (disc < -1e-12 * b * b) return roots;
102 disc = 0.0;
103 }
104 const double sq = std::sqrt(disc);
105 const double qq = -0.5 * (b + (b >= 0.0 ? sq : -sq));
106 roots.push_back(qq / a);
107 if (qq != 0.0) roots.push_back(k / qq);
108 } else {
109 const double a = c[2] / c[3], b = c[1] / c[3], k = c[0] / c[3];
110 // depressed cubic t^3 + P t + Q with x = t - a/3
111 const double P = b - a * a / 3.0;
112 const double Q = 2.0 * a * a * a / 27.0 - a * b / 3.0 + k;
113 const double disc = (Q / 2.0) * (Q / 2.0) + (P / 3.0) * (P / 3.0) * (P / 3.0);
114 if (disc > 0.0) {
115 const double sq = std::sqrt(disc);
116 roots.push_back(std::cbrt(-Q / 2.0 + sq) + std::cbrt(-Q / 2.0 - sq) - a / 3.0);
117 } else if (P == 0.0) {
118 roots.push_back(-a / 3.0);
119 } else {
120 const double pi = 3.14159265358979323846;
121 const double m = 2.0 * std::sqrt(-P / 3.0);
122 const double arg = 3.0 * Q / (P * m);
123 const double th = std::acos(std::min(1.0, std::max(-1.0, arg))) / 3.0;
124 for (int j = 0; j < 3; ++j) roots.push_back(m * std::cos(th - 2.0 * pi * j / 3.0) - a / 3.0);
125 }
126 }
127 for (std::size_t r = 0; r < roots.size(); ++r) {
128 double x = roots[r];
129 for (int it = 0; it < 2; ++it) {
130 const double f = c[0] + x * (c[1] + x * (c[2] + x * c[3]));
131 const double df = c[1] + x * (2.0 * c[2] + x * 3.0 * c[3]);
132 if (df == 0.0) break;
133 x -= f / df;
134 }
135 roots[r] = x;
136 }
137 return roots;
138}
139
140/** Product of two coefficient vectors (lowest degree first). */
141inline std::vector<double> polymul(const std::vector<double>& a, const std::vector<double>& b) {
142 std::vector<double> out(a.size() + b.size() - 1, 0.0);
143 for (std::size_t i = 0; i < a.size(); ++i)
144 for (std::size_t j = 0; j < b.size(); ++j) out[i + j] += a[i] * b[j];
145 return out;
146}
147
148/** Difference of two coefficient vectors (lowest degree first). */
149inline std::vector<double> polysub(const std::vector<double>& a, const std::vector<double>& b) {
150 const std::size_t n = std::max(a.size(), b.size());
151 std::vector<double> out(n, 0.0);
152 for (std::size_t i = 0; i < n; ++i)
153 out[i] = (i < a.size() ? a[i] : 0.0) - (i < b.size() ? b[i] : 0.0);
154 return out;
155}
156
157/** Inner problem of the repair at a fixed x; false when x is infeasible. */
158inline bool repair_evaluate(const Mamap22RepairCoef& k, double X, double St, double wx, double ws,
159 double xlo, double xhi, double xin, double* obj, double* xout,
160 double* sout) {
161 const double tiny = 1e-14, feastol_inner = 1e-10;
162 if (!(xin >= xlo - feastol_inner * std::max(1.0, std::fabs(xlo)) &&
163 xin <= xhi + feastol_inner * std::max(1.0, std::fabs(xhi))))
164 return false;
165 const double x = std::min(std::max(xin, xlo), xhi);
166 const double Dx = k.d0 + k.d1 * x;
167 const double Nx = k.n0 + x * (k.n1 + x * k.n2);
168 double lo = 0.0, hi = 1.0;
169 for (int i = 0; i < 2; ++i) {
170 const double a = k.alpha[i] + k.beta[i] * x;
171 if (std::fabs(k.gamma[i]) <= tiny) {
172 if (a < -feastol_inner || a > 1.0 + feastol_inner) return false;
173 continue;
174 }
175 const double u = -a / k.gamma[i], v = (1.0 - a) / k.gamma[i];
176 lo = std::max(lo, std::min(u, v));
177 hi = std::min(hi, std::max(u, v));
178 }
179 double q = 0.0, s = 0.0;
180 if (std::fabs(k.c) <= tiny) {
181 // s does not enter the equality: q is pinned, s is free
182 if (std::fabs(Dx) <= tiny) return false;
183 q = Nx / Dx;
184 if (q < lo - feastol_inner || q > hi + feastol_inner) return false;
185 s = std::min(std::max(St, 0.0), 1.0);
186 } else if (std::fabs(Dx) <= tiny) {
187 // q drops out of the equality: s is pinned, q is free in [lo, hi]
188 s = -Nx / k.c;
189 if (lo > hi + feastol_inner || s < -feastol_inner || s > 1.0 + feastol_inner) return false;
190 s = std::min(std::max(s, 0.0), 1.0);
191 q = std::min(std::max(0.5 * (lo + hi), lo), hi); // unused: q is free here
192 } else {
193 const double u = Nx / Dx, v = (Nx + k.c) / Dx; // q at s = 0 and at s = 1
194 lo = std::max(lo, std::min(u, v));
195 hi = std::min(hi, std::max(u, v));
196 if (lo > hi + feastol_inner) return false;
197 q = std::min(std::max((Nx + k.c * St) / Dx, lo), hi);
198 s = (q * Dx - Nx) / k.c;
199 }
200 *obj = wx * (x / X - 1.0) * (x / X - 1.0) + ws * (s / St - 1.0) * (s / St - 1.0);
201 *xout = x;
202 *sout = s;
203 return true;
204}
205
206/**
207 * Global optimum of the hybrid-space repair on xlo <= x <= xhi (xhi may be
208 * +infinity). See the file comment for the program and why the candidate set
209 * below contains its minimizer.
210 */
211inline Mamap22RepairSide bilinear_repair(const Mamap22RepairCoef& k, double X, double St,
212 double wx, double ws, double xlo, double xhi) {
213 const double tiny = 1e-14;
214 const std::vector<double> D = {k.d0, k.d1};
215 const std::vector<double> N = {k.n0, k.n1, k.n2};
216
217 // the rational functions of x that q is compared against: (P, Q)
218 std::vector<std::vector<double>> fP, fQ;
219 std::vector<double> affA, affB; // q = a + b x for the box ends
220 fP.push_back({0.0}); fQ.push_back({1.0});
221 fP.push_back({1.0}); fQ.push_back({1.0});
222 affA.push_back(0.0); affB.push_back(0.0);
223 affA.push_back(1.0); affB.push_back(0.0);
224 for (int i = 0; i < 2; ++i) {
225 if (std::fabs(k.gamma[i]) > tiny) {
226 for (int kk = 0; kk < 2; ++kk) {
227 const double bnd = kk;
228 const double a = (bnd - k.alpha[i]) / k.gamma[i], b = -k.beta[i] / k.gamma[i];
229 fP.push_back({a, b}); fQ.push_back({1.0});
230 affA.push_back(a); affB.push_back(b);
231 }
232 }
233 }
234 const double svals[3] = {0.0, 1.0, St};
235 for (int j = 0; j < 3; ++j) {
236 fP.push_back({k.n0 + k.c * svals[j], k.n1, k.n2});
237 fQ.push_back(D);
238 }
239
240 std::vector<double> cand = {X, xlo};
241 auto append = [&](const std::vector<double>& r) { cand.insert(cand.end(), r.begin(), r.end()); };
242 if (std::isfinite(xhi)) cand.push_back(xhi);
243 if (std::fabs(k.d1) > tiny) cand.push_back(-k.d0 / k.d1);
244 for (int i = 0; i < 2; ++i) {
245 if (std::fabs(k.beta[i]) > tiny) {
246 cand.push_back(-k.alpha[i] / k.beta[i]);
247 cand.push_back((1.0 - k.alpha[i]) / k.beta[i]);
248 }
249 }
250 for (int kk = 0; kk < 2; ++kk) append(real_roots({k.n0 + k.c * kk, k.n1, k.n2}));
251 for (std::size_t i = 0; i < fP.size(); ++i)
252 for (std::size_t j = i + 1; j < fP.size(); ++j)
253 append(real_roots(polysub(polymul(fP[i], fQ[j]), polymul(fP[j], fQ[i]))));
254 if (std::fabs(k.c) <= tiny) {
255 for (int i = 0; i < 2; ++i) {
256 for (int kk = 0; kk < 2; ++kk) {
257 // alpha + beta x + gamma N/D = k <=> (alpha - k + beta x) D + gamma N = 0
258 std::vector<double> gN(3);
259 for (int t = 0; t < 3; ++t) gN[t] = -k.gamma[i] * N[t];
260 append(real_roots(polysub(polymul({k.alpha[i] - kk, k.beta[i]}, D), gN)));
261 }
262 }
263 } else {
264 const double A = ws / (St * St);
265 const double Bw = wx / (X * X);
266 for (std::size_t t = 0; t < affA.size(); ++t) {
267 const double a = affA[t], b = affB[t];
268 const double s2 = (b * k.d1 - k.n2) / k.c;
269 const double s1 = (a * k.d1 + b * k.d0 - k.n1) / k.c;
270 const double s0 = (a * k.d0 - k.n0) / k.c - St;
271 append(real_roots({A * s1 * s0 - Bw * X, A * (s1 * s1 + 2.0 * s2 * s0) + Bw,
272 A * 3.0 * s1 * s2, A * 2.0 * s2 * s2}));
273 }
274 }
275
277 for (std::size_t i = 0; i < cand.size(); ++i) {
278 if (!std::isfinite(cand[i])) continue;
279 double obj, x, s;
280 if (repair_evaluate(k, X, St, wx, ws, xlo, xhi, cand[i], &obj, &x, &s) &&
281 (!best.feasible || obj < best.obj)) {
282 best.feasible = true;
283 best.obj = obj;
284 best.x = x;
285 best.s = s;
286 }
287 }
288 return best;
289}
290
291/**
292 * The reference's `solve_nonlinear('hybrid')`: trivial vs left vs right. The left
293 * side keeps the moment in [0, M1 - tol], the right side in [M1 + tol, +inf), and
294 * the trivial marking (all p0: moment M1, sigma p0^2) wins ties. An infeasible
295 * side scores +infinity, so a feasible side is never preferred over a better
296 * trivial marking merely because the other side is infeasible.
297 */
298inline Mamap22RepairChoice hybrid_repair(const Mamap22RepairCoef& k, double X, double St,
299 double wx, double ws, double M1, double p0) {
300 const double tol = 1e-6, inf = std::numeric_limits<double>::infinity();
301 const double eobj = wx * (M1 / X - 1.0) * (M1 / X - 1.0) +
302 ws * (p0 * p0 / St - 1.0) * (p0 * p0 / St - 1.0);
303 const Mamap22RepairSide l = bilinear_repair(k, X, St, wx, ws, 0.0, M1 - tol);
304 const Mamap22RepairSide r = bilinear_repair(k, X, St, wx, ws, M1 + tol, inf);
305 const double lobj = l.feasible ? l.obj : inf;
306 const double robj = r.feasible ? r.obj : inf;
308 if (eobj <= lobj && eobj <= robj) return out;
309 out.trivial = false;
310 if (lobj <= robj) {
311 out.x = l.x;
312 out.s = l.s;
313 } else {
314 out.x = r.x;
315 out.s = r.s;
316 }
317 return out;
318}
319
320} // namespace mamap22detail
321} // namespace mam
322} // namespace line
323
324#endif // LINE_API_MAM_MAMAP22_REPAIR_H
std::vector< double > polysub(const std::vector< double > &a, const std::vector< double > &b)
Difference of two coefficient vectors (lowest degree first).
Mamap22RepairSide bilinear_repair(const Mamap22RepairCoef &k, double X, double St, double wx, double ws, double xlo, double xhi)
Global optimum of the hybrid-space repair on xlo <= x <= xhi (xhi may be +infinity).
Mamap22RepairChoice hybrid_repair(const Mamap22RepairCoef &k, double X, double St, double wx, double ws, double M1, double p0)
The reference's solve_nonlinear('hybrid'): trivial vs left vs right.
std::vector< double > real_roots(const std::vector< double > &cin)
Real roots of c[0] + c[1] x + c[2] x^2 + c[3] x^3 (degree <= 3; shorter inputs are zero padded).
bool repair_evaluate(const Mamap22RepairCoef &k, double X, double St, double wx, double ws, double xlo, double xhi, double xin, double *obj, double *xout, double *sout)
Inner problem of the repair at a fixed x; false when x is infeasible.
std::vector< double > polymul(const std::vector< double > &a, const std::vector< double > &b)
Product of two coefficient vectors (lowest degree first).
Conservation laws of a layered queueing network, enumerated from its structure.
Definition aoi_dist2ph.h:51
What the hybrid selector chose: the trivial marking q1 = q2 = q3 = p, or the closed-form inverse eval...
The bilinear program's data: (q_a, q_b) = alpha + beta x + gamma q_free, and q_free (d1 x + d0) = n2 ...
The optimum of one side of the repair.