Skip to content

Commit bd991fa

Browse files
bodonoBrendan O'Donoghue
and
Brendan O'Donoghue
authored
mpc bug test (#280)
* mpc bug test * explicit memset to 0 * not sure what is failing * fix n * add some prints * better print * test disable res infeas * disable other tests * Revert "disable other tests" This reverts commit b1a41ba. * cleanup * add more tests * rename var --------- Co-authored-by: Brendan O'Donoghue <[email protected]>
1 parent 91a982e commit bd991fa

File tree

8 files changed

+60
-21
lines changed

8 files changed

+60
-21
lines changed

include/glbopts.h

+2-1
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,8 @@ extern "C" {
3939
#define WRITE_DATA_FILENAME (0)
4040
#define LOG_CSV_FILENAME (0)
4141
#define TIME_LIMIT_SECS (0.)
42-
42+
/* Tolerance to check negativity condition for infeasibility */
43+
#define INFEAS_NEGATIVITY_TOL (1e-9)
4344
/* redefine printfs as needed */
4445
#if NO_PRINTING > 0 /* Disable all printing */
4546
#define scs_printf(...) /* No-op */

src/scs.c

+17-17
Original file line numberDiff line numberDiff line change
@@ -207,13 +207,13 @@ static void compute_residuals(ScsResiduals *r, scs_int m, scs_int n) {
207207
r->res_unbdd_a = NAN;
208208
r->res_unbdd_p = NAN;
209209
r->res_infeas = NAN;
210-
if (r->ctx_tau < 0) {
210+
if (r->ctx_tau < -INFEAS_NEGATIVITY_TOL) {
211211
nm_ax_s = NORM(r->ax_s, m);
212212
nm_px = NORM(r->px, n);
213213
r->res_unbdd_a = SAFEDIV_POS(nm_ax_s, -r->ctx_tau);
214214
r->res_unbdd_p = SAFEDIV_POS(nm_px, -r->ctx_tau);
215215
}
216-
if (r->bty_tau < 0) {
216+
if (r->bty_tau < -INFEAS_NEGATIVITY_TOL) {
217217
nm_aty = NORM(r->aty, n);
218218
r->res_infeas = SAFEDIV_POS(nm_aty, -r->bty_tau);
219219
}
@@ -587,22 +587,22 @@ static void print_summary(ScsWork *w, scs_int i, SCS(timer) * solve_timer) {
587587
scs_printf("\n");
588588

589589
#if VERBOSITY > 0
590-
scs_printf("Norm u = %4f, ", SCS(norm_2)(w->u, w->d->n + w->d->m + 1));
591-
scs_printf("Norm u_t = %4f, ", SCS(norm_2)(w->u_t, w->d->n + w->d->m + 1));
592-
scs_printf("Norm v = %4f, ", SCS(norm_2)(w->v, w->d->n + w->d->m + 1));
593-
scs_printf("Norm rsk = %4f, ", SCS(norm_2)(w->rsk, w->d->n + w->d->m + 1));
594-
scs_printf("Norm x = %4f, ", SCS(norm_2)(w->xys_orig->x, w->d->n));
595-
scs_printf("Norm y = %4f, ", SCS(norm_2)(w->xys_orig->y, w->d->m));
596-
scs_printf("Norm s = %4f, ", SCS(norm_2)(w->xys_orig->s, w->d->m));
597-
scs_printf("Norm |Ax + s| = %1.2e, ", SCS(norm_2)(r->ax_s, w->d->m));
598-
scs_printf("tau = %4f, ", w->u[w->d->n + w->d->m]);
599-
scs_printf("kappa = %4f, ", w->rsk[w->d->n + w->d->m]);
600-
scs_printf("|u - u_t| = %1.2e, ",
590+
scs_printf("Norm u = %1.6e, ", SCS(norm_2)(w->u, w->d->n + w->d->m + 1));
591+
scs_printf("Norm u_t = %1.6e, ", SCS(norm_2)(w->u_t, w->d->n + w->d->m + 1));
592+
scs_printf("Norm v = %1.6e, ", SCS(norm_2)(w->v, w->d->n + w->d->m + 1));
593+
scs_printf("Norm rsk = %1.6e, ", SCS(norm_2)(w->rsk, w->d->n + w->d->m + 1));
594+
scs_printf("Norm x = %1.6e, ", SCS(norm_2)(w->xys_orig->x, w->d->n));
595+
scs_printf("Norm y = %1.6e, ", SCS(norm_2)(w->xys_orig->y, w->d->m));
596+
scs_printf("Norm s = %1.6e, ", SCS(norm_2)(w->xys_orig->s, w->d->m));
597+
scs_printf("Norm |Ax + s| = %1.6e, ", SCS(norm_2)(r->ax_s, w->d->m));
598+
scs_printf("tau = %1.6e, ", w->u[w->d->n + w->d->m]);
599+
scs_printf("kappa = %1.6e, ", w->rsk[w->d->n + w->d->m]);
600+
scs_printf("|u - u_t| = %1.6e, ",
601601
SCS(norm_diff)(w->u, w->u_t, w->d->n + w->d->m + 1));
602-
scs_printf("res_infeas = %1.2e, ", r->res_infeas);
603-
scs_printf("res_unbdd_a = %1.2e, ", r->res_unbdd_a);
604-
scs_printf("res_unbdd_p = %1.2e, ", r->res_unbdd_p);
605-
scs_printf("ctx_tau = %1.2e, ", r->ctx_tau);
602+
scs_printf("res_infeas = %1.6e, ", r->res_infeas);
603+
scs_printf("res_unbdd_a = %1.6e, ", r->res_unbdd_a);
604+
scs_printf("res_unbdd_p = %1.6e, ", r->res_unbdd_p);
605+
scs_printf("ctx_tau = %1.6e, ", r->ctx_tau);
606606
scs_printf("bty_tau = %1.2e\n", r->bty_tau);
607607
#endif
608608

test/problems/mpc_bug.h

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
#include "glbopts.h"
2+
#include "problems/test_prob_from_data_file.h"
3+
#include "scs.h"
4+
5+
static const char *mpc_bug(void) {
6+
const char *fail;
7+
scs_float OPT1 = -0.473957794500; /* from scs */
8+
scs_float OPT2 = -0.029336830816; /* from scs */
9+
scs_float OPT3 = -0.002215217478; /* from scs */
10+
fail = _test_prob_from_data("test/problems/mpc_bug1", OPT1);
11+
if (fail) {
12+
return fail;
13+
}
14+
fail = _test_prob_from_data("test/problems/mpc_bug2", OPT2);
15+
if (fail) {
16+
return fail;
17+
}
18+
return _test_prob_from_data("test/problems/mpc_bug3", OPT3);
19+
}

test/problems/mpc_bug1

23.2 KB
Binary file not shown.

test/problems/mpc_bug2

23.2 KB
Binary file not shown.

test/problems/mpc_bug3

23.2 KB
Binary file not shown.

test/problems/test_prob_from_data_file.h

+19-3
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@ static const char *_test_prob_from_data(const char *file, scs_float OPT) {
1919
scs_float perr, derr;
2020
scs_int success;
2121
const char *fail;
22+
scs_float xt_p_x;
23+
scs_float *px = SCS_NULL;
2224

2325
read_status = SCS(read_data)(file, &d, &k, &stgs);
2426

@@ -28,12 +30,24 @@ static const char *_test_prob_from_data(const char *file, scs_float OPT) {
2830

2931
stgs->eps_abs = 1e-6;
3032
stgs->eps_rel = 1e-6;
33+
/* Force verbosity for the test */
34+
stgs->verbose = 1;
3135

3236
sol = (ScsSolution *)scs_calloc(1, sizeof(ScsSolution));
3337
exitflag = scs(d, k, stgs, sol, &info);
3438

35-
perr = SCS(dot)(d->c, sol->x, d->n) - OPT;
36-
derr = -SCS(dot)(d->b, sol->y, d->m) - OPT;
39+
if (d->P) {
40+
/* px = Px */
41+
px = (scs_float *)scs_calloc(d->n, sizeof(scs_float));
42+
memset(px, 0, d->n * sizeof(scs_float));
43+
SCS(accum_by_p)(d->P, sol->x, px);
44+
xt_p_x = SCS(dot)(px, sol->x, d->n);
45+
} else {
46+
xt_p_x = 0.;
47+
}
48+
49+
perr = 0.5 * xt_p_x + SCS(dot)(d->c, sol->x, d->n) - OPT;
50+
derr = -0.5 * xt_p_x - SCS(dot)(d->b, sol->y, d->m) - OPT;
3751
scs_printf("primal obj error %4e\n", perr);
3852
scs_printf("dual obj error %4e\n", derr);
3953

@@ -47,7 +61,9 @@ static const char *_test_prob_from_data(const char *file, scs_float OPT) {
4761
SCS(free_cone)(k);
4862
SCS(free_sol)(sol);
4963
scs_free(stgs);
50-
64+
if (px) {
65+
scs_free(px);
66+
}
5167
if (fail) {
5268
scs_printf("%s: FAILED\n", file);
5369
}

test/run_tests.c

+3
Original file line numberDiff line numberDiff line change
@@ -43,9 +43,11 @@ _SKIP(random_prob)
4343
#if NO_READ_WRITE == 0 /* reads / writes */
4444
#include "problems/hs21_tiny_qp_rw.h"
4545
#include "problems/max_ent.h"
46+
#include "problems/mpc_bug.h"
4647
#else
4748
_SKIP(hs21_tiny_qp_rw)
4849
_SKIP(max_ent)
50+
_SKIP(mpc_bug)
4951
#endif
5052

5153
static const char *all_tests(void) {
@@ -61,6 +63,7 @@ static const char *all_tests(void) {
6163
mu_run_test(unbounded_tiny_qp);
6264
mu_run_test(random_prob);
6365
mu_run_test(max_ent);
66+
mu_run_test(mpc_bug);
6467
mu_run_test(test_exp_cone);
6568
return 0;
6669
}

0 commit comments

Comments
 (0)