diff --git a/docs/src/algorithm/index.rst b/docs/src/algorithm/index.rst index 1aab9546..17619dc8 100644 --- a/docs/src/algorithm/index.rst +++ b/docs/src/algorithm/index.rst @@ -192,3 +192,12 @@ SCS will return one of the two above certificates, whichever one it finds first. However, in that case the interpretation of infeasibility in one space being equivalent to unboundedness in the dual space does not hold. +Iteration and time limits +------------------------- + +If none of these criteria hold within :code:`max_iters` iterations or +:code:`time_limit_secs` (see :ref:`settings `), SCS stops and returns +a best guess from its last iterate with an inaccurate status, or +:code:`SCS_INDETERMINATE` when that iterate gives no estimate of a solution or +of a certificate; see :ref:`exit flags `. + diff --git a/docs/src/algorithm/warm_start.rst b/docs/src/algorithm/warm_start.rst index 99fe3662..24419ed3 100644 --- a/docs/src/algorithm/warm_start.rst +++ b/docs/src/algorithm/warm_start.rst @@ -41,7 +41,12 @@ by setting the warm-start :ref:`setting ` to :code:`True` when calling y, s` members of the :ref:`ScsSolution` struct, where those members correspond to the guess of the solution in the :ref:`standard form `. SCS will initialize the solver at those points and then overwrite the :ref:`ScsSolution` -struct members with the real solution at termination. +struct members with the real solution at termination. NaN entries of the guess +are read as zero, so a warm start from a solution that SCS returned as NaN +(after :code:`SCS_INDETERMINATE`, for example, see :ref:`exit flags +`) starts from zero, as a cold start does; the +:code:`scale` and the per-row multipliers still carry over as described +above. The settings that only the solve reads, such as the tolerances, :code:`max_iters` and :code:`time_limit_secs`, can also be changed between diff --git a/docs/src/api/exit_flags.rst b/docs/src/api/exit_flags.rst index 68a94c54..72f37a2c 100644 --- a/docs/src/api/exit_flags.rst +++ b/docs/src/api/exit_flags.rst @@ -27,12 +27,15 @@ in the :code:`include/scs.h` file. * - Primal infeasible / Dual unbounded (to desired tolerance) - -2 - :code:`SCS_INFEASIBLE` - * - Indeterminate (numerical errors when recovering solution) DEPRECATED + * - Indeterminate: reached :code:`max_iters` or :code:`time_limit_secs` + at an iterate that gives no estimate of a solution or of a + certificate; x, y and s are NaN (see :ref:`below `) - -3 - :code:`SCS_INDETERMINATE` - * - Failed: a data input error, or an internal error such as a failed + * - Failed: a data input error, an internal error such as a failed linear solve (the printed message says which; an internal error asks - you to open an issue) + you to open an issue), or an iterate that is not finite (NaN or Inf) + at :code:`max_iters` or :code:`time_limit_secs` - -4 - :code:`SCS_FAILED` * - Interrupted (received SIGINT) @@ -45,3 +48,61 @@ in the :code:`include/scs.h` file. - -7 - :code:`SCS_INFEASIBLE_INACCURATE` + +.. _exit_flags_limit: + +At max_iters or time_limit_secs +^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + +When SCS stops at :code:`max_iters` or :code:`time_limit_secs` before its +:ref:`termination criteria ` hold, it reports what the last +iterate supports. SCS works with a homogeneous iterate :math:`(x, y, s, \tau, +\kappa)`: a solution estimate is :math:`(x, y, s) / \tau`, which needs +:math:`\tau > 0`, and a certificate estimate is :math:`y / (-b^\top y)` or +:math:`(x, s) / (-c^\top x)`, which needs :math:`b^\top y < 0` or +:math:`c^\top x < 0`. The tests below are exact comparisons, taken in this +order, so the status depends only on the last iterate: + +.. list-table:: + :widths: 40 15 45 + :header-rows: 1 + + * - Last iterate + - Exit flag + - Returned + * - Not finite (a NaN or an infinite entry, for example after an + overflow) + - -4 (:code:`SCS_FAILED`) + - x, y, s and the info fields listed for -3 NaN; status + :code:`failure` + * - :math:`\kappa > \tau` and :math:`b^\top y < 0` or :math:`c^\top x + < 0` + - -7 or -6 + - The infeasibility certificate :math:`y / (-b^\top y)` (x and s NaN), + or the unboundedness certificate :math:`(x, s) / (-c^\top x)` (y NaN) + * - :math:`\tau > 0` + - 2 + - The solution estimate :math:`(x, y, s) / \tau` and its objective and + residuals + * - Otherwise: :math:`\tau = 0` and :math:`b^\top y \geq 0`, + :math:`c^\top x \geq 0` + - -3 (:code:`SCS_INDETERMINATE`) + - x, y, s NaN, and :code:`pobj`, :code:`dobj`, :code:`gap`, + :code:`res_pri`, :code:`res_dual`, :code:`res_infeas`, + :code:`res_unbdd_a`, :code:`res_unbdd_p` and :code:`comp_slack` NaN + +Each status except :code:`failure` ends in :code:`(inaccurate - reached +max_iters)` or :code:`(inaccurate - reached time_limit_secs)`, for example +:code:`indeterminate (inaccurate - reached max_iters)`. + +The estimates returned with 2, -6 and -7 do not meet the tolerances. A +certificate estimate rests on the sign of :math:`b^\top y` or :math:`c^\top +x` at the last iterate alone, which on a run whose iterates diverged can +change from one iteration to the next. + +SCS returns :code:`SCS_INDETERMINATE` when the cone projection has set +:math:`\tau` to zero and neither :math:`b^\top y` nor :math:`c^\top x` is +negative. This happens in the first few iterations and on runs whose iterates +diverged. As for an inaccurate status, more iterations or more time can help +(see :ref:`help_indeterminate`). A warm start from the NaN solution starts +from zero, as a cold start does (see :ref:`warm_start`). diff --git a/docs/src/api/info.rst b/docs/src/api/info.rst index a945452c..c9c2c77f 100644 --- a/docs/src/api/info.rst +++ b/docs/src/api/info.rst @@ -83,6 +83,12 @@ the following fields. - :code:`scs_float` - Total time (milliseconds) spent in the :ref:`acceleration routine ` +When SCS returns :code:`SCS_INDETERMINATE`, or stops at :code:`max_iters` or +:code:`time_limit_secs` with an iterate that is not finite (see :ref:`exit +flags `), :code:`pobj`, :code:`dobj`, :code:`res_pri`, +:code:`res_dual`, :code:`gap`, :code:`res_infeas`, :code:`res_unbdd_a`, +:code:`res_unbdd_p` and :code:`comp_slack` are NaN, as are x, y and s. + Anderson acceleration statistics ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ diff --git a/docs/src/api/settings.rst b/docs/src/api/settings.rst index 9a02e265..e9212275 100644 --- a/docs/src/api/settings.rst +++ b/docs/src/api/settings.rst @@ -42,7 +42,7 @@ They are set in the :ref:`ScsSettings ` struct. - 1e-6 (1e-2 in single precision builds) * - :code:`max_iters` - :code:`scs_int` - - Maximum number of iterations to run. + - Maximum number of iterations to run. When it is reached SCS returns its best guess with an inaccurate status, or :code:`SCS_INDETERMINATE` when the last iterate gives none, see :ref:`exit flags `. - :math:`\mathbf{N}` - 1e5 * - :code:`eps_abs` @@ -67,7 +67,7 @@ They are set in the :ref:`ScsSettings ` struct. - 1.5 * - :code:`time_limit_secs` - :code:`scs_float` - - Time limit for the solve in seconds (can be fractional). :code:`0` is interpreted as no limit. The clock starts when :code:`scs_solve` is called, so the setup time (e.g., the initial factorization) is not counted, although the time column of the printed output does include it. The limit is only tested when convergence is checked, every 25 iterations (:code:`CONVERGED_INTERVAL` in :code:`include/glbopts.h`), so a solve can overrun it by up to 25 iterations. When the limit is reached SCS returns its best guess with an inaccurate status, see :ref:`exit flags `. It has no effect if SCS is compiled with the :code:`NO_TIMER` :ref:`compile flag `. It can be changed after setup, for example to give the solve what is left of a total time budget, see :ref:`update_settings`. + - Time limit for the solve in seconds (can be fractional). :code:`0` is interpreted as no limit. The clock starts when :code:`scs_solve` is called, so the setup time (e.g., the initial factorization) is not counted, although the time column of the printed output does include it. The limit is only tested when convergence is checked, every 25 iterations (:code:`CONVERGED_INTERVAL` in :code:`include/glbopts.h`), so a solve can overrun it by up to 25 iterations. When the limit is reached SCS returns its best guess with an inaccurate status, or :code:`SCS_INDETERMINATE` when the last iterate gives none, see :ref:`exit flags `. It has no effect if SCS is compiled with the :code:`NO_TIMER` :ref:`compile flag `. It can be changed after setup, for example to give the solve what is left of a total time budget, see :ref:`update_settings`. - :math:`[0, \infty)` - 0 * - :code:`verbose` @@ -224,8 +224,8 @@ How to check it worked ^^^^^^^^^^^^^^^^^^^^^^ A solve stopped by the time limit returns an inaccurate :ref:`exit flag -` and its status ends in :code:`(inaccurate - reached -time_limit_secs)`. With :code:`verbose` on, :code:`scs_update_settings` +`, or :code:`SCS_INDETERMINATE`, and its status ends in +:code:`(inaccurate - reached time_limit_secs)`. With :code:`verbose` on, :code:`scs_update_settings` prints the values now in force, starting :code:`settings updated:`. What it does not do diff --git a/docs/src/help/index.rst b/docs/src/help/index.rst index 7f492408..4ea206d3 100644 --- a/docs/src/help/index.rst +++ b/docs/src/help/index.rst @@ -36,6 +36,20 @@ residuals exceed the requested tolerances. Try: * **Try adaptive scaling**: set :code:`adaptive_scale=1` (the default). This automatically tunes the internal :code:`scale` parameter during the solve. +.. _help_indeterminate: + +SCS returns ``indeterminate`` +^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + +The status :code:`indeterminate (inaccurate - reached max_iters)` (exit flag +:code:`SCS_INDETERMINATE`, -3) means SCS ran out of iterations (or hit the +time limit) at an iterate that gives no estimate of a solution or of a +certificate, so x, y and s are NaN (see :ref:`exit_flags_limit`). It happens +in the first few iterations, so with a very small :code:`max_iters` or time +limit, and on runs whose iterates diverged. Try the steps above for +``solved_inaccurate``, starting with a larger :code:`max_iters` or time +limit. + SCS returns ``infeasible`` or ``unbounded`` unexpectedly ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ diff --git a/include/scs.h b/include/scs.h index e00d71c0..a4fc1911 100644 --- a/include/scs.h +++ b/include/scs.h @@ -33,11 +33,11 @@ typedef struct SCS_WORK ScsWork; #define SCS_INFEASIBLE_INACCURATE (-7) #define SCS_UNBOUNDED_INACCURATE (-6) #define SCS_SIGINT (-5) -#define SCS_FAILED (-4) -#define SCS_INDETERMINATE (-3) -#define SCS_INFEASIBLE (-2) /* primal infeasible, dual unbounded */ -#define SCS_UNBOUNDED (-1) /* primal unbounded, dual infeasible */ -#define SCS_UNFINISHED (0) /* never returned, used as placeholder */ +#define SCS_FAILED (-4) /* data/internal error, or iterate not finite */ +#define SCS_INDETERMINATE (-3) /* limit reached, no estimate: x, y, s NaN */ +#define SCS_INFEASIBLE (-2) /* primal infeasible, dual unbounded */ +#define SCS_UNBOUNDED (-1) /* primal unbounded, dual infeasible */ +#define SCS_UNFINISHED (0) /* never returned, used as placeholder */ #define SCS_SOLVED (1) #define SCS_SOLVED_INACCURATE (2) diff --git a/src/scs.c b/src/scs.c index 28df6b4c..2dc1bc23 100644 --- a/src/scs.c +++ b/src/scs.c @@ -144,6 +144,7 @@ static void update_dual_vars(ScsWork *w); static scs_int project_cones(ScsWork *w, const ScsCone *k, scs_int iter); static void finalize(ScsWork *w, ScsSolution *sol, ScsInfo *info, scs_int iter); +static scs_int all_finite(const scs_float *x, scs_int len); static void set_diag_r(ScsWork *w); static ScsWork *init_work(const ScsData *d, const ScsCone *k, const ScsSettings *stgs); @@ -970,8 +971,63 @@ static void set_unbounded(const ScsWork *w, ScsSolution *sol, ScsInfo *info) { info->status_val = SCS_UNBOUNDED; } -/* not yet converged, take best guess */ +/* No estimate of a solution or of a certificate: x, y, s and every objective + * and residual field are NaN. NaN rather than the last iterate, which is a + * ray with no meaning of its own and can look like a plausible solution. The + * cost: a warm start from this solution starts from zero, as a cold start + * does (warm_start_vars reads NaN as 0); the scale and the per-row + * multipliers still carry over. */ +static void set_no_estimate(const ScsWork *w, ScsSolution *sol, + ScsInfo *info) { + SCS(scale_array)(sol->x, NAN, w->d->n); + SCS(scale_array)(sol->y, NAN, w->d->m); + SCS(scale_array)(sol->s, NAN, w->d->m); + info->gap = NAN; + info->res_pri = NAN; + info->res_dual = NAN; + info->pobj = NAN; + info->dobj = NAN; + info->res_infeas = NAN; + info->res_unbdd_a = NAN; + info->res_unbdd_p = NAN; + info->comp_slack = NAN; +} + +/* Reached max_iters or time_limit_secs: report what the last iterate + * (x, y, s, tau, kappa) supports, tested in this order. + * - Not finite (a NaN or an infinite entry: the iterate overflowed, or a + * linear solve or BLAS call returned one): SCS_FAILED, status "failure", + * x, y, s NaN. Tested first because such an iterate passes the tests + * below: project_cones sets tau = MAX(NaN, 0) = 0, so it would read as no + * estimate, and with tau > 0 (always at iteration 0) as an inaccurate + * solution. + * - kappa > tau with b'y < 0 or c'x < 0: the inaccurate certificate, + * y / -b'y or (x, s) / -c'x. + * - tau > 0: the inaccurate solution (x, y, s) / tau. + * - Otherwise tau = 0 (project_cones clips it at zero) and b'y >= 0 and + * c'x >= 0: a solution needs tau > 0 and a certificate needs b'y < 0 or + * c'x < 0, so the iterate is an estimate of neither, and no threshold or + * older iterate is used to make one. SCS_INDETERMINATE, x, y, s NaN + * (set_no_estimate). This case also takes tau = kappa = 0, which needs the + * unclipped tau to be exactly zero and has not been seen. + * Every test is exact, so the status is a function of the last iterate alone, + * and the iterates are not changed. Measured on master 4ccf2380 (2026-11-25), + * the indeterminate case comes from two regimes: the first iterations (on + * one-variable LPs with 4 rows, 5,480 of 4.7M runs, all at max_iters 2 or 3) + * and runs whose iterates diverged (netlib beaconfd with CG at eps 1e-4: 19 + * of 500 starts at 700 iterations; in the one start examined, iterates of + * size 1e69 to 1e71). Before SCS 3.4.0 it returned SCS_FAILED, the raw + * iterate, and a status of only " (inaccurate - reached max_iters)". */ static void set_unfinished(const ScsWork *w, ScsSolution *sol, ScsInfo *info) { + if (!isfinite(w->r_orig->tau) || !isfinite(w->r_orig->kap) || + !all_finite(sol->x, w->d->n) || !all_finite(sol->y, w->d->m) || + !all_finite(sol->s, w->d->m)) { + set_no_estimate(w, sol, info); + strcpy(info->status, "failure"); + info->status_val = SCS_FAILED; + scs_printf("Failure:the iterate is not finite (NaN or Inf)\n"); + return; /* "failure" takes no "(inaccurate - ...)" suffix */ + } if (w->r_orig->kap > w->r_orig->tau && (w->r_orig->bty_tau < 0 || w->r_orig->ctx_tau < 0)) { if (w->r_orig->bty_tau < 0 && @@ -986,10 +1042,12 @@ static void set_unfinished(const ScsWork *w, ScsSolution *sol, ScsInfo *info) { set_solved(w, sol, info); info->status_val = SCS_SOLVED_INACCURATE; } else { - scs_printf("ERROR: could not determine problem status.\n"); - info->status_val = SCS_FAILED; + set_no_estimate(w, sol, info); + strcpy(info->status, "indeterminate"); + info->status_val = SCS_INDETERMINATE; } - /* Append inaccurate to the status string */ + /* Append inaccurate to the status string. Every branch above starts it with + * strcpy, so this never appends to what the caller left in info->status. */ if (w->time_limit_reached) { strcat(info->status, " (inaccurate - reached time_limit_secs)"); } else if (info->iter >= w->stgs->max_iters) { diff --git a/test/problems/test_inaccurate.h b/test/problems/test_inaccurate.h index 843de2b6..ca1f5565 100644 --- a/test/problems/test_inaccurate.h +++ b/test/problems/test_inaccurate.h @@ -1,3 +1,6 @@ +#include +#include + #include "glbopts.h" #include "minunit.h" #include "scs.h" @@ -207,6 +210,236 @@ static const char *test_max_iters_1(void) { return 0; } +/* + * Box LP for the stops at the limit where the last iterate gives no estimate. + * min 2x s.t. -2 <= x <= 2, as x + s_1 = 2, -x + s_2 = 2, s >= 0. + * n=1, m=2, l=2, A=[[1],[-1]], b=[2,2], c=[2]; solution x = -2, obj -4. + * SCS solves it in 25 iterations. + */ +static void inaccurate_box_lp(ScsData *d, ScsCone *k) { + static scs_float Ax[] = {1.0, -1.0}; + static scs_int Ai[] = {0, 1}; + static scs_int Ap[] = {0, 2}; + static scs_float b[] = {2.0, 2.0}; + static scs_float c[] = {2.0}; + d->m = 2; + d->n = 1; + d->b = b; + d->c = c; + d->A = (ScsMatrix *)scs_calloc(1, sizeof(ScsMatrix)); + d->A->m = 2; + d->A->n = 1; + d->A->x = Ax; + d->A->i = Ai; + d->A->p = Ap; + k->l = 2; +} + +/* 1 if every entry of x, y and s is NaN */ +static int inaccurate_sol_all_nan(const ScsSolution *sol, scs_int n, + scs_int m) { + scs_int i; + for (i = 0; i < n; ++i) { + if (!isnan(sol->x[i])) { + return 0; + } + } + for (i = 0; i < m; ++i) { + if (!isnan(sol->y[i]) || !isnan(sol->s[i])) { + return 0; + } + } + return 1; +} + +/* + * SCS_INDETERMINATE: the box LP stopped at max_iters = 2. After iteration 1 + * the unclipped tau is negative, so project_cones sets tau = 0, and b'y > 0 + * and c'x > 0: the iterate is an estimate of neither a solution (it needs + * tau > 0) nor a certificate (it needs b'y < 0 or c'x < 0). Measured before + * this change (QDLDL and CG; double, DLONG and single; normalize, + * adaptive_scale and acceleration each on or off): unclipped tau -0.023 to + * -0.046, b'y +0.56 to +0.72 and c'x +0.56 to +1.1 at an iterate of norm + * about 2, so rounding cannot move the status in either precision. SCS then + * returned SCS_FAILED, the raw iterate (x = 0.557, which looks like a point) + * and the status " (inaccurate - reached max_iters)", and it never wrote + * pobj, dobj, gap, res_pri and res_dual. + * + * info starts filled with 0x7f bytes, as from a caller that does not zero it + * (scs-matlab): before this change the status had no NUL in its 128 bytes, + * so the suffix was appended past its end (on macOS the fortified strcat + * aborts the test binary there), and pobj etc. came back as 1.38e306. + * + * The other side: max_iters = 3 gives SCS_SOLVED_INACCURATE (tau 0.75, and + * 0.52 in single precision), the default max_iters gives SCS_SOLVED, and a + * warm-started solve on the same workspace after SCS_INDETERMINATE solves the + * problem: the NaN solution is read as zero, a cold start. That solve stops + * at eps 1e-4 (|x + 2| at most 9e-5 on the Mac builds); a NaN carried into + * the iterate would give NaN or another status, which the status check and + * the 1e-2 bound catch. + */ +static const char *test_indeterminate_at_limit(void) { + ScsCone *k = (ScsCone *)scs_calloc(1, sizeof(ScsCone)); + ScsData *d = (ScsData *)scs_calloc(1, sizeof(ScsData)); + ScsSettings *stgs = (ScsSettings *)scs_calloc(1, sizeof(ScsSettings)); + ScsSolution *sol = (ScsSolution *)scs_calloc(1, sizeof(ScsSolution)); + ScsInfo info; + ScsWork *w = SCS_NULL; + scs_int exitflag; + const char *fail = SCS_NULL; + + inaccurate_box_lp(d, k); + scs_set_default_settings(stgs); + stgs->verbose = 1; + + stgs->max_iters = 2; + memset(&info, 0x7f, sizeof(info)); + exitflag = scs(d, k, stgs, sol, &info); + if (exitflag != SCS_INDETERMINATE || info.status_val != SCS_INDETERMINATE) { + fail = "test_indeterminate_at_limit: max_iters 2: expected " + "SCS_INDETERMINATE"; + } else if (memchr(info.status, '\0', sizeof(info.status)) == SCS_NULL || + strcmp(info.status, + "indeterminate (inaccurate - reached max_iters)") != 0) { + fail = "test_indeterminate_at_limit: max_iters 2: wrong status string"; + } else if (info.iter != 2) { + fail = "test_indeterminate_at_limit: max_iters 2: expected 2 iterations"; + } else if (!inaccurate_sol_all_nan(sol, d->n, d->m)) { + fail = "test_indeterminate_at_limit: max_iters 2: x, y, s not all NaN"; + } else if (!isnan(info.pobj) || !isnan(info.dobj) || !isnan(info.gap) || + !isnan(info.res_pri) || !isnan(info.res_dual) || + !isnan(info.res_infeas) || !isnan(info.res_unbdd_a) || + !isnan(info.res_unbdd_p) || !isnan(info.comp_slack)) { + fail = "test_indeterminate_at_limit: max_iters 2: objective or residual " + "fields not NaN"; + } + + if (!fail) { + stgs->max_iters = 3; + memset(&info, 0, sizeof(info)); + exitflag = scs(d, k, stgs, sol, &info); + if (exitflag != SCS_SOLVED_INACCURATE) { + fail = "test_indeterminate_at_limit: max_iters 3: expected " + "SCS_SOLVED_INACCURATE"; + } + } + if (!fail) { + stgs->max_iters = MAX_ITERS; + exitflag = scs(d, k, stgs, sol, &info); + if (exitflag != SCS_SOLVED) { + fail = "test_indeterminate_at_limit: default max_iters: expected " + "SCS_SOLVED"; + } + } + + /* SCS_INDETERMINATE, then a warm-started solve on the same workspace */ + if (!fail) { + stgs->max_iters = 2; + w = scs_init(d, k, stgs); + if (!w) { + fail = "test_indeterminate_at_limit: scs_init failed"; + } + } + if (!fail) { + exitflag = scs_solve(w, sol, &info, 0); + if (exitflag != SCS_INDETERMINATE) { + fail = "test_indeterminate_at_limit: workspace, max_iters 2: expected " + "SCS_INDETERMINATE"; + } + } + if (!fail) { + stgs->max_iters = MAX_ITERS; + if (scs_update_settings(w, stgs) != 0) { + fail = "test_indeterminate_at_limit: scs_update_settings failed"; + } + } + if (!fail) { + exitflag = scs_solve(w, sol, &info, 1); + if (exitflag != SCS_SOLVED || !(fabs(sol->x[0] + 2.0) < 1e-2)) { + fail = "test_indeterminate_at_limit: warm start from the " + "indeterminate result: expected SCS_SOLVED at x = -2"; + } + } + if (fail) { + scs_printf("test_indeterminate_at_limit: status %li '%.*s', iter %li\n", + (long)exitflag, (int)sizeof(info.status) - 1, info.status, + (long)info.iter); + } + + scs_finish(w); + SCS(free_sol)(sol); + scs_free(d->A); + scs_free(k); + scs_free(stgs); + scs_free(d); + return fail; +} + +/* + * SCS_FAILED for an iterate that is not finite at the limit: the box LP + * warm-started at x = y = s = half the largest finite scs_float. The first + * linear solve overflows, and the iterate is NaN from then on. This tests an + * overflow, which every build reaches; a linear solver or BLAS that returns + * a NaN takes the same path. Before this change max_iters = 1 returned + * SCS_SOLVED_INACCURATE with a non-finite solution (tau is fixed at 1 in + * iteration 0) and max_iters = 2 returned SCS_FAILED with the status + * " (inaccurate - reached max_iters)". A linear solver that rejects the + * overflowed system itself stops the solve with SCS_FAILED and "failure" + * too, at another iteration count, so info.iter is not checked. The finite + * runs of test_indeterminate_at_limit are the other side. + */ +static const char *test_nonfinite_at_limit(void) { + ScsCone *k = (ScsCone *)scs_calloc(1, sizeof(ScsCone)); + ScsData *d = (ScsData *)scs_calloc(1, sizeof(ScsData)); + ScsSettings *stgs = (ScsSettings *)scs_calloc(1, sizeof(ScsSettings)); + ScsSolution *sol = (ScsSolution *)scs_calloc(1, sizeof(ScsSolution)); + ScsInfo info = {0}; + scs_int exitflag, max_iters, i; +#ifdef SFLOAT + const scs_float big = FLT_MAX / 2; +#else + const scs_float big = DBL_MAX / 2; +#endif + const char *fail = SCS_NULL; + + inaccurate_box_lp(d, k); + scs_set_default_settings(stgs); + stgs->verbose = 1; + stgs->warm_start = 1; + sol->x = (scs_float *)scs_calloc(d->n, sizeof(scs_float)); + sol->y = (scs_float *)scs_calloc(d->m, sizeof(scs_float)); + sol->s = (scs_float *)scs_calloc(d->m, sizeof(scs_float)); + + for (max_iters = 1; max_iters <= 2 && !fail; ++max_iters) { + for (i = 0; i < d->n; ++i) { + sol->x[i] = big; + } + for (i = 0; i < d->m; ++i) { + sol->y[i] = sol->s[i] = big; + } + stgs->max_iters = max_iters; + exitflag = scs(d, k, stgs, sol, &info); + if (exitflag != SCS_FAILED || info.status_val != SCS_FAILED) { + fail = "test_nonfinite_at_limit: expected SCS_FAILED"; + } else if (strcmp(info.status, "failure") != 0) { + fail = "test_nonfinite_at_limit: expected the status \"failure\""; + } else if (!inaccurate_sol_all_nan(sol, d->n, d->m)) { + fail = "test_nonfinite_at_limit: x, y, s not all NaN"; + } + if (fail) { + scs_printf("test_nonfinite_at_limit: max_iters %li: status %li '%s'\n", + (long)max_iters, (long)exitflag, info.status); + } + } + + SCS(free_sol)(sol); + scs_free(d->A); + scs_free(k); + scs_free(stgs); + scs_free(d); + return fail; +} + #undef _INACCURATE_LP_VARS #undef _INACCURATE_INFEAS_VARS #undef _INACCURATE_UNBDD_VARS diff --git a/test/run_tests.c b/test/run_tests.c index f109e38e..182f9e22 100644 --- a/test/run_tests.c +++ b/test/run_tests.c @@ -57,7 +57,7 @@ const char *const mu_skipped = "skipped"; * as skipped, and main() checks that run + skipped == MU_TOTAL_TESTS. A build * that silently loses tests therefore fails instead of printing ALL TESTS * PASSED. Bump this when adding a test. */ -#define MU_TOTAL_TESTS 121 +#define MU_TOTAL_TESTS 123 #define _SKIP(problem) \ static const char *problem(void) { return mu_skipped; } @@ -261,6 +261,8 @@ static void all_tests(void) { mu_run_test(test_infeasible_inaccurate); mu_run_test(test_unbounded_inaccurate); mu_run_test(test_max_iters_1); + mu_run_test(test_indeterminate_at_limit); + mu_run_test(test_nonfinite_at_limit); mu_run_test(test_adaptive_scale); mu_run_test(test_no_acceleration); mu_run_test(test_type2_acceleration);