scs 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (106) hide show
  1. checksums.yaml +7 -0
  2. data/CHANGELOG.md +3 -0
  3. data/LICENSE.txt +22 -0
  4. data/README.md +84 -0
  5. data/ext/scs/Rakefile +11 -0
  6. data/lib/scs/ffi.rb +117 -0
  7. data/lib/scs/solver.rb +178 -0
  8. data/lib/scs/version.rb +3 -0
  9. data/lib/scs.rb +17 -0
  10. data/vendor/scs/LICENSE.txt +21 -0
  11. data/vendor/scs/Makefile +164 -0
  12. data/vendor/scs/README.md +220 -0
  13. data/vendor/scs/include/aa.h +56 -0
  14. data/vendor/scs/include/cones.h +46 -0
  15. data/vendor/scs/include/ctrlc.h +33 -0
  16. data/vendor/scs/include/glbopts.h +177 -0
  17. data/vendor/scs/include/linalg.h +26 -0
  18. data/vendor/scs/include/linsys.h +64 -0
  19. data/vendor/scs/include/normalize.h +18 -0
  20. data/vendor/scs/include/rw.h +17 -0
  21. data/vendor/scs/include/scs.h +161 -0
  22. data/vendor/scs/include/scs_blas.h +51 -0
  23. data/vendor/scs/include/util.h +65 -0
  24. data/vendor/scs/linsys/amatrix.c +305 -0
  25. data/vendor/scs/linsys/amatrix.h +36 -0
  26. data/vendor/scs/linsys/amatrix.o +0 -0
  27. data/vendor/scs/linsys/cpu/direct/private.c +366 -0
  28. data/vendor/scs/linsys/cpu/direct/private.h +26 -0
  29. data/vendor/scs/linsys/cpu/direct/private.o +0 -0
  30. data/vendor/scs/linsys/cpu/indirect/private.c +256 -0
  31. data/vendor/scs/linsys/cpu/indirect/private.h +31 -0
  32. data/vendor/scs/linsys/cpu/indirect/private.o +0 -0
  33. data/vendor/scs/linsys/external/amd/LICENSE.txt +934 -0
  34. data/vendor/scs/linsys/external/amd/SuiteSparse_config.c +469 -0
  35. data/vendor/scs/linsys/external/amd/SuiteSparse_config.h +254 -0
  36. data/vendor/scs/linsys/external/amd/SuiteSparse_config.o +0 -0
  37. data/vendor/scs/linsys/external/amd/amd.h +400 -0
  38. data/vendor/scs/linsys/external/amd/amd_1.c +180 -0
  39. data/vendor/scs/linsys/external/amd/amd_1.o +0 -0
  40. data/vendor/scs/linsys/external/amd/amd_2.c +1842 -0
  41. data/vendor/scs/linsys/external/amd/amd_2.o +0 -0
  42. data/vendor/scs/linsys/external/amd/amd_aat.c +184 -0
  43. data/vendor/scs/linsys/external/amd/amd_aat.o +0 -0
  44. data/vendor/scs/linsys/external/amd/amd_control.c +64 -0
  45. data/vendor/scs/linsys/external/amd/amd_control.o +0 -0
  46. data/vendor/scs/linsys/external/amd/amd_defaults.c +37 -0
  47. data/vendor/scs/linsys/external/amd/amd_defaults.o +0 -0
  48. data/vendor/scs/linsys/external/amd/amd_dump.c +179 -0
  49. data/vendor/scs/linsys/external/amd/amd_dump.o +0 -0
  50. data/vendor/scs/linsys/external/amd/amd_global.c +16 -0
  51. data/vendor/scs/linsys/external/amd/amd_global.o +0 -0
  52. data/vendor/scs/linsys/external/amd/amd_info.c +119 -0
  53. data/vendor/scs/linsys/external/amd/amd_info.o +0 -0
  54. data/vendor/scs/linsys/external/amd/amd_internal.h +304 -0
  55. data/vendor/scs/linsys/external/amd/amd_order.c +199 -0
  56. data/vendor/scs/linsys/external/amd/amd_order.o +0 -0
  57. data/vendor/scs/linsys/external/amd/amd_post_tree.c +120 -0
  58. data/vendor/scs/linsys/external/amd/amd_post_tree.o +0 -0
  59. data/vendor/scs/linsys/external/amd/amd_postorder.c +206 -0
  60. data/vendor/scs/linsys/external/amd/amd_postorder.o +0 -0
  61. data/vendor/scs/linsys/external/amd/amd_preprocess.c +118 -0
  62. data/vendor/scs/linsys/external/amd/amd_preprocess.o +0 -0
  63. data/vendor/scs/linsys/external/amd/amd_valid.c +92 -0
  64. data/vendor/scs/linsys/external/amd/amd_valid.o +0 -0
  65. data/vendor/scs/linsys/external/amd/changes +11 -0
  66. data/vendor/scs/linsys/external/qdldl/LICENSE +201 -0
  67. data/vendor/scs/linsys/external/qdldl/README.md +120 -0
  68. data/vendor/scs/linsys/external/qdldl/changes +4 -0
  69. data/vendor/scs/linsys/external/qdldl/qdldl.c +298 -0
  70. data/vendor/scs/linsys/external/qdldl/qdldl.h +177 -0
  71. data/vendor/scs/linsys/external/qdldl/qdldl.o +0 -0
  72. data/vendor/scs/linsys/external/qdldl/qdldl_types.h +21 -0
  73. data/vendor/scs/linsys/gpu/gpu.c +41 -0
  74. data/vendor/scs/linsys/gpu/gpu.h +85 -0
  75. data/vendor/scs/linsys/gpu/indirect/private.c +304 -0
  76. data/vendor/scs/linsys/gpu/indirect/private.h +36 -0
  77. data/vendor/scs/scs.mk +181 -0
  78. data/vendor/scs/src/aa.c +224 -0
  79. data/vendor/scs/src/aa.o +0 -0
  80. data/vendor/scs/src/cones.c +802 -0
  81. data/vendor/scs/src/cones.o +0 -0
  82. data/vendor/scs/src/ctrlc.c +77 -0
  83. data/vendor/scs/src/ctrlc.o +0 -0
  84. data/vendor/scs/src/linalg.c +84 -0
  85. data/vendor/scs/src/linalg.o +0 -0
  86. data/vendor/scs/src/normalize.c +93 -0
  87. data/vendor/scs/src/normalize.o +0 -0
  88. data/vendor/scs/src/rw.c +167 -0
  89. data/vendor/scs/src/rw.o +0 -0
  90. data/vendor/scs/src/scs.c +975 -0
  91. data/vendor/scs/src/scs.o +0 -0
  92. data/vendor/scs/src/scs_version.c +5 -0
  93. data/vendor/scs/src/scs_version.o +0 -0
  94. data/vendor/scs/src/util.c +196 -0
  95. data/vendor/scs/src/util.o +0 -0
  96. data/vendor/scs/test/data/small_random_socp +0 -0
  97. data/vendor/scs/test/minunit.h +13 -0
  98. data/vendor/scs/test/problem_utils.h +93 -0
  99. data/vendor/scs/test/problems/rob_gauss_cov_est.h +85 -0
  100. data/vendor/scs/test/problems/small_lp.h +50 -0
  101. data/vendor/scs/test/problems/small_random_socp.h +33 -0
  102. data/vendor/scs/test/random_socp_prob.c +171 -0
  103. data/vendor/scs/test/run_from_file.c +69 -0
  104. data/vendor/scs/test/run_tests +2 -0
  105. data/vendor/scs/test/run_tests.c +32 -0
  106. metadata +203 -0
@@ -0,0 +1,975 @@
1
+ #include "scs.h"
2
+
3
+ #include "aa.h"
4
+ #include "ctrlc.h"
5
+ #include "glbopts.h"
6
+ #include "linalg.h"
7
+ #include "linsys.h"
8
+ #include "normalize.h"
9
+ #include "rw.h"
10
+ #include "util.h"
11
+
12
+ SCS(timer) global_timer;
13
+
14
+ /* printing header */
15
+ static const char *HEADER[] = {
16
+ " Iter ", " pri res ", " dua res ", " rel gap ",
17
+ " pri obj ", " dua obj ", " kap/tau ", " time (s)",
18
+ };
19
+ static const scs_int HSPACE = 9;
20
+ static const scs_int HEADER_LEN = 8;
21
+ static const scs_int LINE_LEN = 76;
22
+
23
+ static scs_int scs_isnan(scs_float x) { return (x == NAN || x != x); }
24
+
25
+ static void free_work(ScsWork *w) {
26
+ if (w) {
27
+ scs_free(w->u);
28
+ scs_free(w->u_best);
29
+ scs_free(w->u_t);
30
+ scs_free(w->u_prev);
31
+ /* Don't need these because u*, v* are contiguous in mem
32
+ scs_free(w->v);
33
+ scs_free(w->v_best);
34
+ scs_free(w->v_prev);
35
+ */
36
+ scs_free(w->h);
37
+ scs_free(w->g);
38
+ scs_free(w->b);
39
+ scs_free(w->c);
40
+ scs_free(w->pr);
41
+ scs_free(w->dr);
42
+ if (w->scal) {
43
+ scs_free(w->scal->D);
44
+ scs_free(w->scal->E);
45
+ scs_free(w->scal);
46
+ }
47
+ scs_free(w);
48
+ }
49
+ }
50
+
51
+ static void print_init_header(const ScsData *d, const ScsCone *k) {
52
+ scs_int i;
53
+ ScsSettings *stgs = d->stgs;
54
+ char *cone_str = SCS(get_cone_header)(k);
55
+ char *lin_sys_method = SCS(get_lin_sys_method)(d->A, d->stgs);
56
+ #ifdef USE_LAPACK
57
+ scs_int acceleration_lookback = stgs->acceleration_lookback;
58
+ #else
59
+ scs_int acceleration_lookback = 0;
60
+ #endif
61
+ for (i = 0; i < LINE_LEN; ++i) {
62
+ scs_printf("-");
63
+ }
64
+ scs_printf(
65
+ "\n\tSCS v%s - Splitting Conic Solver\n\t(c) Brendan "
66
+ "O'Donoghue, Stanford University, 2012\n",
67
+ SCS(version)());
68
+ for (i = 0; i < LINE_LEN; ++i) {
69
+ scs_printf("-");
70
+ }
71
+ scs_printf("\n");
72
+ if (lin_sys_method) {
73
+ scs_printf("Lin-sys: %s\n", lin_sys_method);
74
+ scs_free(lin_sys_method);
75
+ }
76
+ if (stgs->normalize) {
77
+ scs_printf(
78
+ "eps = %.2e, alpha = %.2f, max_iters = %i, normalize = %i, "
79
+ "scale = %2.2f\nacceleration_lookback = %i, rho_x = %.2e\n",
80
+ stgs->eps, stgs->alpha, (int)stgs->max_iters, (int)stgs->normalize,
81
+ stgs->scale, (int)acceleration_lookback, stgs->rho_x);
82
+ } else {
83
+ scs_printf(
84
+ "eps = %.2e, alpha = %.2f, max_iters = %i, normalize = %i\n"
85
+ "acceleration_lookback = %i, rho_x = %.2e\n",
86
+ stgs->eps, stgs->alpha, (int)stgs->max_iters, (int)stgs->normalize,
87
+ (int)acceleration_lookback, stgs->rho_x);
88
+ }
89
+ scs_printf("Variables n = %i, constraints m = %i\n", (int)d->n, (int)d->m);
90
+ scs_printf("%s", cone_str);
91
+ scs_free(cone_str);
92
+ #ifdef MATLAB_MEX_FILE
93
+ mexEvalString("drawnow;");
94
+ #endif
95
+ }
96
+
97
+ static void populate_on_failure(scs_int m, scs_int n, ScsSolution *sol,
98
+ ScsInfo *info, scs_int status_val,
99
+ const char *msg) {
100
+ if (info) {
101
+ info->rel_gap = NAN;
102
+ info->res_pri = NAN;
103
+ info->res_dual = NAN;
104
+ info->pobj = NAN;
105
+ info->dobj = NAN;
106
+ info->iter = -1;
107
+ info->status_val = status_val;
108
+ info->solve_time = NAN;
109
+ strcpy(info->status, msg);
110
+ }
111
+ if (sol) {
112
+ if (n > 0) {
113
+ if (!sol->x) {
114
+ sol->x = (scs_float *)scs_malloc(sizeof(scs_float) * n);
115
+ }
116
+ SCS(scale_array)(sol->x, NAN, n);
117
+ }
118
+ if (m > 0) {
119
+ if (!sol->y) {
120
+ sol->y = (scs_float *)scs_malloc(sizeof(scs_float) * m);
121
+ }
122
+ SCS(scale_array)(sol->y, NAN, m);
123
+ if (!sol->s) {
124
+ sol->s = (scs_float *)scs_malloc(sizeof(scs_float) * m);
125
+ }
126
+ SCS(scale_array)(sol->s, NAN, m);
127
+ }
128
+ }
129
+ }
130
+
131
+ static scs_int failure(ScsWork *w, scs_int m, scs_int n, ScsSolution *sol,
132
+ ScsInfo *info, scs_int stint, const char *msg,
133
+ const char *ststr) {
134
+ scs_int status = stint;
135
+ populate_on_failure(m, n, sol, info, status, ststr);
136
+ scs_printf("Failure:%s\n", msg);
137
+ scs_end_interrupt_listener();
138
+ return status;
139
+ }
140
+
141
+ static void warm_start_vars(ScsWork *w, const ScsSolution *sol) {
142
+ scs_int i, n = w->n, m = w->m;
143
+ memset(w->v, 0, n * sizeof(scs_float));
144
+ memcpy(w->u, sol->x, n * sizeof(scs_float));
145
+ memcpy(&(w->u[n]), sol->y, m * sizeof(scs_float));
146
+ memcpy(&(w->v[n]), sol->s, m * sizeof(scs_float));
147
+ w->u[n + m] = 1.0;
148
+ w->v[n + m] = 0.0;
149
+ #ifndef NOVALIDATE
150
+ for (i = 0; i < n + m + 1; ++i) {
151
+ if (scs_isnan(w->u[i])) {
152
+ w->u[i] = 0;
153
+ }
154
+ if (scs_isnan(w->v[i])) {
155
+ w->v[i] = 0;
156
+ }
157
+ }
158
+ #endif
159
+ if (w->stgs->normalize) {
160
+ SCS(normalize_warm_start)(w);
161
+ }
162
+ }
163
+
164
+ static scs_float calc_primal_resid(ScsWork *w, const scs_float *x,
165
+ const scs_float *s, const scs_float tau,
166
+ scs_float *nm_axs) {
167
+ scs_int i;
168
+ scs_float pres = 0, scale, *pr = w->pr;
169
+ *nm_axs = 0;
170
+ memset(pr, 0, w->m * sizeof(scs_float));
171
+ SCS(accum_by_a)(w->A, w->p, x, pr);
172
+ SCS(add_scaled_array)(pr, s, w->m, 1.0); /* pr = Ax + s */
173
+ for (i = 0; i < w->m; ++i) {
174
+ scale = w->stgs->normalize ? w->scal->D[i] / (w->sc_b * w->stgs->scale) : 1;
175
+ scale = scale * scale;
176
+ *nm_axs += (pr[i] * pr[i]) * scale;
177
+ pres += (pr[i] - w->b[i] * tau) * (pr[i] - w->b[i] * tau) * scale;
178
+ }
179
+ *nm_axs = SQRTF(*nm_axs);
180
+ return SQRTF(pres); /* SCS(norm)(Ax + s - b * tau) */
181
+ }
182
+
183
+ static scs_float calc_dual_resid(ScsWork *w, const scs_float *y,
184
+ const scs_float tau, scs_float *nm_a_ty) {
185
+ scs_int i;
186
+ scs_float dres = 0, scale, *dr = w->dr;
187
+ *nm_a_ty = 0;
188
+ memset(dr, 0, w->n * sizeof(scs_float));
189
+ SCS(accum_by_atrans)(w->A, w->p, y, dr); /* dr = A'y */
190
+ for (i = 0; i < w->n; ++i) {
191
+ scale = w->stgs->normalize ? w->scal->E[i] / (w->sc_c * w->stgs->scale) : 1;
192
+ scale = scale * scale;
193
+ *nm_a_ty += (dr[i] * dr[i]) * scale;
194
+ dres += (dr[i] + w->c[i] * tau) * (dr[i] + w->c[i] * tau) * scale;
195
+ }
196
+ *nm_a_ty = SQRTF(*nm_a_ty);
197
+ return SQRTF(dres); /* SCS(norm)(A'y + c * tau) */
198
+ }
199
+
200
+ /* calculates un-normalized quantities */
201
+ static void calc_residuals(ScsWork *w, ScsResiduals *r, scs_int iter) {
202
+ scs_float *x = w->u, *y = &(w->u[w->n]), *s = &(w->v[w->n]);
203
+ scs_float nmpr_tau, nmdr_tau, nm_axs_tau, nm_a_ty_tau, ct_x, bt_y;
204
+ scs_int n = w->n, m = w->m;
205
+
206
+ /* checks if the residuals are unchanged by checking iteration */
207
+ if (r->last_iter == iter) {
208
+ return;
209
+ }
210
+ r->last_iter = iter;
211
+
212
+ r->tau = ABS(w->u[n + m]);
213
+ r->kap = ABS(w->v[n + m]) /
214
+ (w->stgs->normalize ? (w->stgs->scale * w->sc_c * w->sc_b) : 1);
215
+
216
+ nmpr_tau = calc_primal_resid(w, x, s, r->tau, &nm_axs_tau);
217
+ nmdr_tau = calc_dual_resid(w, y, r->tau, &nm_a_ty_tau);
218
+
219
+ r->bt_y_by_tau =
220
+ SCS(dot)(y, w->b, m) /
221
+ (w->stgs->normalize ? (w->stgs->scale * w->sc_c * w->sc_b) : 1);
222
+ r->ct_x_by_tau =
223
+ SCS(dot)(x, w->c, n) /
224
+ (w->stgs->normalize ? (w->stgs->scale * w->sc_c * w->sc_b) : 1);
225
+
226
+ r->res_infeas =
227
+ r->bt_y_by_tau < 0 ? w->nm_b * nm_a_ty_tau / -r->bt_y_by_tau : NAN;
228
+ r->res_unbdd =
229
+ r->ct_x_by_tau < 0 ? w->nm_c * nm_axs_tau / -r->ct_x_by_tau : NAN;
230
+
231
+ bt_y = SAFEDIV_POS(r->bt_y_by_tau, r->tau);
232
+ ct_x = SAFEDIV_POS(r->ct_x_by_tau, r->tau);
233
+
234
+ r->res_pri = SAFEDIV_POS(nmpr_tau / (1 + w->nm_b), r->tau);
235
+ r->res_dual = SAFEDIV_POS(nmdr_tau / (1 + w->nm_c), r->tau);
236
+ r->rel_gap = ABS(ct_x + bt_y) / (1 + ABS(ct_x) + ABS(bt_y));
237
+ }
238
+
239
+ static void cold_start_vars(ScsWork *w) {
240
+ scs_int l = w->n + w->m + 1;
241
+ memset(w->u, 0, l * sizeof(scs_float));
242
+ memset(w->v, 0, l * sizeof(scs_float));
243
+ w->u[l - 1] = SQRTF((scs_float)l);
244
+ w->v[l - 1] = SQRTF((scs_float)l);
245
+ }
246
+
247
+ /* status < 0 indicates failure */
248
+ static scs_int project_lin_sys(ScsWork *w, scs_int iter) {
249
+ /* ut = u + v */
250
+
251
+ scs_int n = w->n, m = w->m, l = n + m + 1, status;
252
+ memcpy(w->u_t, w->u, l * sizeof(scs_float));
253
+ SCS(add_scaled_array)(w->u_t, w->v, l, 1.0);
254
+
255
+ SCS(scale_array)(w->u_t, w->stgs->rho_x, n);
256
+
257
+ SCS(add_scaled_array)(w->u_t, w->h, l - 1, -w->u_t[l - 1]);
258
+ SCS(add_scaled_array)
259
+ (w->u_t, w->h, l - 1, -SCS(dot)(w->u_t, w->g, l - 1) / (w->g_th + 1));
260
+ SCS(scale_array)(&(w->u_t[n]), -1, m);
261
+
262
+ status = SCS(solve_lin_sys)(w->A, w->stgs, w->p, w->u_t, w->u, iter);
263
+
264
+ w->u_t[l - 1] += SCS(dot)(w->u_t, w->h, l - 1);
265
+
266
+ return status;
267
+ }
268
+
269
+ static void update_dual_vars(ScsWork *w) {
270
+ scs_int i, n = w->n, l = n + w->m + 1;
271
+ /* this does not relax 'x' variable */
272
+ for (i = n; i < l; ++i) {
273
+ w->v[i] += (w->u[i] - w->stgs->alpha * w->u_t[i] -
274
+ (1.0 - w->stgs->alpha) * w->u_prev[i]);
275
+ }
276
+ }
277
+
278
+ /* status < 0 indicates failure */
279
+ static scs_int project_cones(ScsWork *w, const ScsCone *k, scs_int iter) {
280
+ scs_int i, n = w->n, l = n + w->m + 1, status;
281
+ /* this does not relax 'x' variable */
282
+ for (i = 0; i < n; ++i) {
283
+ w->u[i] = w->u_t[i] - w->v[i];
284
+ }
285
+ for (i = n; i < l; ++i) {
286
+ w->u[i] = w->stgs->alpha * w->u_t[i] + (1 - w->stgs->alpha) * w->u_prev[i] -
287
+ w->v[i];
288
+ }
289
+ /* u = [x;y;tau] */
290
+ status =
291
+ SCS(proj_dual_cone)(&(w->u[n]), k, w->cone_work, &(w->u_prev[n]), iter);
292
+ if (w->u[l - 1] < 0.0) {
293
+ w->u[l - 1] = 0.0;
294
+ }
295
+
296
+ return status;
297
+ }
298
+
299
+ static scs_int indeterminate(ScsWork *w, ScsSolution *sol, ScsInfo *info) {
300
+ strcpy(info->status, "Indeterminate");
301
+ SCS(scale_array)(sol->x, NAN, w->n);
302
+ SCS(scale_array)(sol->y, NAN, w->m);
303
+ SCS(scale_array)(sol->s, NAN, w->m);
304
+ return SCS_INDETERMINATE;
305
+ }
306
+
307
+ static void sety(ScsWork *w, ScsSolution *sol) {
308
+ if (!sol->y) {
309
+ sol->y = (scs_float *)scs_malloc(sizeof(scs_float) * w->m);
310
+ }
311
+ memcpy(sol->y, &(w->u[w->n]), w->m * sizeof(scs_float));
312
+ }
313
+
314
+ static void sets(ScsWork *w, ScsSolution *sol) {
315
+ if (!sol->s) {
316
+ sol->s = (scs_float *)scs_malloc(sizeof(scs_float) * w->m);
317
+ }
318
+ memcpy(sol->s, &(w->v[w->n]), w->m * sizeof(scs_float));
319
+ }
320
+
321
+ static void setx(ScsWork *w, ScsSolution *sol) {
322
+ if (!sol->x) {
323
+ sol->x = (scs_float *)scs_malloc(sizeof(scs_float) * w->n);
324
+ }
325
+ memcpy(sol->x, w->u, w->n * sizeof(scs_float));
326
+ }
327
+
328
+ static scs_float get_max_residual(ScsResiduals *r) {
329
+ return MAX(r->rel_gap, MAX(r->res_pri, r->res_dual));
330
+ }
331
+
332
+ static void copy_from_best_iterate(ScsWork *w) {
333
+ memcpy(w->u, w->u_best, (w->m + w->n + 1) * sizeof(scs_float));
334
+ memcpy(w->v, w->v_best, (w->m + w->n + 1) * sizeof(scs_float));
335
+ }
336
+
337
+ static scs_int solved(ScsWork *w, ScsSolution *sol, ScsInfo *info,
338
+ ScsResiduals *r, scs_int iter) {
339
+ if (w->best_max_residual < get_max_residual(r)) {
340
+ r->last_iter = -1; /* Forces residual recomputation. */
341
+ copy_from_best_iterate(w);
342
+ calc_residuals(w, r, iter);
343
+ setx(w, sol);
344
+ sety(w, sol);
345
+ sets(w, sol);
346
+ }
347
+ SCS(scale_array)(sol->x, SAFEDIV_POS(1.0, r->tau), w->n);
348
+ SCS(scale_array)(sol->y, SAFEDIV_POS(1.0, r->tau), w->m);
349
+ SCS(scale_array)(sol->s, SAFEDIV_POS(1.0, r->tau), w->m);
350
+ if (info->status_val == 0) {
351
+ strcpy(info->status, "Solved/Inaccurate");
352
+ return SCS_SOLVED_INACCURATE;
353
+ }
354
+ strcpy(info->status, "Solved");
355
+ return SCS_SOLVED;
356
+ }
357
+
358
+ static scs_int infeasible(ScsWork *w, ScsSolution *sol, ScsInfo *info,
359
+ scs_float bt_y) {
360
+ SCS(scale_array)(sol->y, -1 / bt_y, w->m);
361
+ SCS(scale_array)(sol->x, NAN, w->n);
362
+ SCS(scale_array)(sol->s, NAN, w->m);
363
+ if (info->status_val == 0) {
364
+ strcpy(info->status, "Infeasible/Inaccurate");
365
+ return SCS_INFEASIBLE_INACCURATE;
366
+ }
367
+ strcpy(info->status, "Infeasible");
368
+ return SCS_INFEASIBLE;
369
+ }
370
+
371
+ static scs_int unbounded(ScsWork *w, ScsSolution *sol, ScsInfo *info,
372
+ scs_float ct_x) {
373
+ SCS(scale_array)(sol->x, -1 / ct_x, w->n);
374
+ SCS(scale_array)(sol->s, -1 / ct_x, w->m);
375
+ SCS(scale_array)(sol->y, NAN, w->m);
376
+ if (info->status_val == 0) {
377
+ strcpy(info->status, "Unbounded/Inaccurate");
378
+ return SCS_UNBOUNDED_INACCURATE;
379
+ }
380
+ strcpy(info->status, "Unbounded");
381
+ return SCS_UNBOUNDED;
382
+ }
383
+
384
+ static scs_int is_solved_status(scs_int status) {
385
+ return status == SCS_SOLVED || status == SCS_SOLVED_INACCURATE;
386
+ }
387
+
388
+ static scs_int is_infeasible_status(scs_int status) {
389
+ return status == SCS_INFEASIBLE || status == SCS_INFEASIBLE_INACCURATE;
390
+ }
391
+
392
+ static scs_int is_unbounded_status(scs_int status) {
393
+ return status == SCS_UNBOUNDED || status == SCS_UNBOUNDED_INACCURATE;
394
+ }
395
+
396
+ static void get_info(ScsWork *w, ScsSolution *sol, ScsInfo *info,
397
+ ScsResiduals *r, scs_int iter) {
398
+ info->iter = iter;
399
+ info->res_infeas = r->res_infeas;
400
+ info->res_unbdd = r->res_unbdd;
401
+ if (is_solved_status(info->status_val)) {
402
+ info->rel_gap = r->rel_gap;
403
+ info->res_pri = r->res_pri;
404
+ info->res_dual = r->res_dual;
405
+ info->pobj = r->ct_x_by_tau / r->tau;
406
+ info->dobj = -r->bt_y_by_tau / r->tau;
407
+ } else if (is_unbounded_status(info->status_val)) {
408
+ info->rel_gap = NAN;
409
+ info->res_pri = NAN;
410
+ info->res_dual = NAN;
411
+ info->pobj = -INFINITY;
412
+ info->dobj = -INFINITY;
413
+ } else if (is_infeasible_status(info->status_val)) {
414
+ info->rel_gap = NAN;
415
+ info->res_pri = NAN;
416
+ info->res_dual = NAN;
417
+ info->pobj = INFINITY;
418
+ info->dobj = INFINITY;
419
+ }
420
+ }
421
+
422
+ /* sets solutions, re-scales by inner prods if infeasible or unbounded */
423
+ static void get_solution(ScsWork *w, ScsSolution *sol, ScsInfo *info,
424
+ ScsResiduals *r, scs_int iter) {
425
+ scs_int l = w->n + w->m + 1;
426
+ calc_residuals(w, r, iter);
427
+ setx(w, sol);
428
+ sety(w, sol);
429
+ sets(w, sol);
430
+ if (info->status_val == SCS_UNFINISHED) {
431
+ /* not yet converged, take best guess */
432
+ if (r->tau > INDETERMINATE_TOL && r->tau > r->kap) {
433
+ info->status_val = solved(w, sol, info, r, iter);
434
+ } else if (SCS(norm)(w->u, l) < INDETERMINATE_TOL * SQRTF((scs_float)l)) {
435
+ info->status_val = indeterminate(w, sol, info);
436
+ } else if (r->bt_y_by_tau < r->ct_x_by_tau) {
437
+ info->status_val = infeasible(w, sol, info, r->bt_y_by_tau);
438
+ } else {
439
+ info->status_val = unbounded(w, sol, info, r->ct_x_by_tau);
440
+ }
441
+ } else if (is_solved_status(info->status_val)) {
442
+ info->status_val = solved(w, sol, info, r, iter);
443
+ } else if (is_infeasible_status(info->status_val)) {
444
+ info->status_val = infeasible(w, sol, info, r->bt_y_by_tau);
445
+ } else {
446
+ info->status_val = unbounded(w, sol, info, r->ct_x_by_tau);
447
+ }
448
+ if (w->stgs->normalize) {
449
+ SCS(un_normalize_sol)(w, sol);
450
+ }
451
+ get_info(w, sol, info, r, iter);
452
+ }
453
+
454
+ static void print_summary(ScsWork *w, scs_int i, ScsResiduals *r,
455
+ SCS(timer) * solve_timer) {
456
+ scs_printf("%*i|", (int)strlen(HEADER[0]), (int)i);
457
+ scs_printf("%*.2e ", (int)HSPACE, r->res_pri);
458
+ scs_printf("%*.2e ", (int)HSPACE, r->res_dual);
459
+ scs_printf("%*.2e ", (int)HSPACE, r->rel_gap);
460
+ scs_printf("%*.2e ", (int)HSPACE, SAFEDIV_POS(r->ct_x_by_tau, r->tau));
461
+ scs_printf("%*.2e ", (int)HSPACE, SAFEDIV_POS(-r->bt_y_by_tau, r->tau));
462
+ scs_printf("%*.2e ", (int)HSPACE, SAFEDIV_POS(r->kap, r->tau));
463
+ scs_printf("%*.2e ", (int)HSPACE, SCS(tocq)(solve_timer) / 1e3);
464
+ scs_printf("\n");
465
+
466
+ #if EXTRA_VERBOSE > 0
467
+ scs_printf("Norm u = %4f, ", SCS(norm)(w->u, w->n + w->m + 1));
468
+ scs_printf("Norm u_t = %4f, ", SCS(norm)(w->u_t, w->n + w->m + 1));
469
+ scs_printf("Norm v = %4f, ", SCS(norm)(w->v, w->n + w->m + 1));
470
+ scs_printf("tau = %4f, ", w->u[w->n + w->m]);
471
+ scs_printf("kappa = %4f, ", w->v[w->n + w->m]);
472
+ scs_printf("|u - u_prev| = %1.2e, ",
473
+ SCS(norm_diff)(w->u, w->u_prev, w->n + w->m + 1));
474
+ scs_printf("|u - u_t| = %1.2e, ",
475
+ SCS(norm_diff)(w->u, w->u_t, w->n + w->m + 1));
476
+ scs_printf("res_infeas = %1.2e, ", r->res_infeas);
477
+ scs_printf("res_unbdd = %1.2e\n", r->res_unbdd);
478
+ #endif
479
+
480
+ #ifdef MATLAB_MEX_FILE
481
+ mexEvalString("drawnow;");
482
+ #endif
483
+ }
484
+
485
+ static void print_header(ScsWork *w, const ScsCone *k) {
486
+ scs_int i;
487
+ if (w->stgs->warm_start) {
488
+ scs_printf("SCS using variable warm-starting\n");
489
+ }
490
+ for (i = 0; i < LINE_LEN; ++i) {
491
+ scs_printf("-");
492
+ }
493
+ scs_printf("\n");
494
+ for (i = 0; i < HEADER_LEN - 1; ++i) {
495
+ scs_printf("%s|", HEADER[i]);
496
+ }
497
+ scs_printf("%s\n", HEADER[HEADER_LEN - 1]);
498
+ for (i = 0; i < LINE_LEN; ++i) {
499
+ scs_printf("-");
500
+ }
501
+ scs_printf("\n");
502
+ #ifdef MATLAB_MEX_FILE
503
+ mexEvalString("drawnow;");
504
+ #endif
505
+ }
506
+
507
+ static scs_float get_dual_cone_dist(const scs_float *y, const ScsCone *k,
508
+ ScsConeWork *c, scs_int m) {
509
+ scs_float dist;
510
+ scs_float *t = (scs_float *)scs_malloc(sizeof(scs_float) * m);
511
+ memcpy(t, y, m * sizeof(scs_float));
512
+ SCS(proj_dual_cone)(t, k, c, SCS_NULL, -1);
513
+ dist = SCS(norm_inf_diff)(t, y, m);
514
+ #if EXTRA_VERBOSE > 0
515
+ SCS(print_array)(y, m, "y");
516
+ SCS(print_array)(t, m, "proj_y");
517
+ scs_printf("dist = %4f\n", dist);
518
+ #endif
519
+ scs_free(t);
520
+ return dist;
521
+ }
522
+
523
+ /* via moreau */
524
+ static scs_float get_pri_cone_dist(const scs_float *s, const ScsCone *k,
525
+ ScsConeWork *c, scs_int m) {
526
+ scs_float dist;
527
+ scs_float *t = (scs_float *)scs_malloc(sizeof(scs_float) * m);
528
+ memcpy(t, s, m * sizeof(scs_float));
529
+ SCS(scale_array)(t, -1.0, m);
530
+ SCS(proj_dual_cone)(t, k, c, SCS_NULL, -1);
531
+ dist = SCS(norm_inf)(t, m); /* ||s - Pi_c(s)|| = ||Pi_c*(-s)|| */
532
+ #if EXTRA_VERBOSE > 0
533
+ SCS(print_array)(s, m, "s");
534
+ SCS(print_array)(t, m, "(s - proj_s)");
535
+ scs_printf("dist = %4f\n", dist);
536
+ #endif
537
+ scs_free(t);
538
+ return dist;
539
+ }
540
+
541
+ static char *get_accel_summary(ScsInfo *info, scs_float total_accel_time) {
542
+ char *str = (char *)scs_malloc(sizeof(char) * 64);
543
+ sprintf(str, "\tAcceleration: avg step time: %1.2es\n",
544
+ total_accel_time / (info->iter + 1) / 1e3);
545
+ return str;
546
+ }
547
+
548
+ static void print_footer(const ScsData *d, const ScsCone *k, ScsSolution *sol,
549
+ ScsWork *w, ScsInfo *info,
550
+ scs_float total_accel_time) {
551
+ scs_int i;
552
+ char *lin_sys_str = SCS(get_lin_sys_summary)(w->p, info);
553
+ char *cone_str = SCS(get_cone_summary)(info, w->cone_work);
554
+ char *accel_str = get_accel_summary(info, total_accel_time);
555
+ for (i = 0; i < LINE_LEN; ++i) {
556
+ scs_printf("-");
557
+ }
558
+ scs_printf("\nStatus: %s\n", info->status);
559
+ if (info->iter == w->stgs->max_iters) {
560
+ scs_printf(
561
+ "Hit max_iters, solution may be inaccurate, returning best found "
562
+ "solution.\n");
563
+ }
564
+ scs_printf("Timing: Solve time: %1.2es\n", info->solve_time / 1e3);
565
+
566
+ if (lin_sys_str) {
567
+ scs_printf("%s", lin_sys_str);
568
+ scs_free(lin_sys_str);
569
+ }
570
+
571
+ if (cone_str) {
572
+ scs_printf("%s", cone_str);
573
+ scs_free(cone_str);
574
+ }
575
+
576
+ if (accel_str) {
577
+ scs_printf("%s", accel_str);
578
+ scs_free(accel_str);
579
+ }
580
+
581
+ for (i = 0; i < LINE_LEN; ++i) {
582
+ scs_printf("-");
583
+ }
584
+ scs_printf("\n");
585
+
586
+ if (is_infeasible_status(info->status_val)) {
587
+ scs_printf("Certificate of primal infeasibility:\n");
588
+ scs_printf("dist(y, K*) = %.4e\n",
589
+ get_dual_cone_dist(sol->y, k, w->cone_work, d->m));
590
+ scs_printf("|A'y|_2 * |b|_2 = %.4e\n", info->res_infeas);
591
+ scs_printf("b'y = %.4f\n", SCS(dot)(d->b, sol->y, d->m));
592
+ } else if (is_unbounded_status(info->status_val)) {
593
+ scs_printf("Certificate of dual infeasibility:\n");
594
+ scs_printf("dist(s, K) = %.4e\n",
595
+ get_pri_cone_dist(sol->s, k, w->cone_work, d->m));
596
+ scs_printf("|Ax + s|_2 * |c|_2 = %.4e\n", info->res_unbdd);
597
+ scs_printf("c'x = %.4f\n", SCS(dot)(d->c, sol->x, d->n));
598
+ } else {
599
+ scs_printf("Error metrics:\n");
600
+ scs_printf("dist(s, K) = %.4e, dist(y, K*) = %.4e, s'y/|s||y| = %.4e\n",
601
+ get_pri_cone_dist(sol->s, k, w->cone_work, d->m),
602
+ get_dual_cone_dist(sol->y, k, w->cone_work, d->m),
603
+ SCS(dot)(sol->s, sol->y, d->m) / SCS(norm)(sol->s, d->m) /
604
+ SCS(norm)(sol->y, d->m));
605
+ scs_printf("primal res: |Ax + s - b|_2 / (1 + |b|_2) = %.4e\n",
606
+ info->res_pri);
607
+ scs_printf("dual res: |A'y + c|_2 / (1 + |c|_2) = %.4e\n",
608
+ info->res_dual);
609
+ scs_printf("rel gap: |c'x + b'y| / (1 + |c'x| + |b'y|) = %.4e\n",
610
+ info->rel_gap);
611
+ for (i = 0; i < LINE_LEN; ++i) {
612
+ scs_printf("-");
613
+ }
614
+ scs_printf("\n");
615
+ scs_printf("c'x = %.4f, -b'y = %.4f\n", info->pobj, info->dobj);
616
+ }
617
+ for (i = 0; i < LINE_LEN; ++i) {
618
+ scs_printf("=");
619
+ }
620
+ scs_printf("\n");
621
+ #ifdef MATLAB_MEX_FILE
622
+ mexEvalString("drawnow;");
623
+ #endif
624
+ }
625
+
626
+ static scs_int has_converged(ScsWork *w, ScsResiduals *r, scs_int iter) {
627
+ scs_float eps = w->stgs->eps;
628
+ if (r->res_pri < eps && r->res_dual < eps && r->rel_gap < eps) {
629
+ return SCS_SOLVED;
630
+ }
631
+ /* Add iter > 0 to avoid strange edge case where infeasible point found
632
+ * right at start of run `out/demo_SOCP_indirect 2 0.1 0.3 1506264403` */
633
+ if (r->res_unbdd < eps && iter > 0) {
634
+ return SCS_UNBOUNDED;
635
+ }
636
+ if (r->res_infeas < eps && iter > 0) {
637
+ return SCS_INFEASIBLE;
638
+ }
639
+ return 0;
640
+ }
641
+
642
+ static scs_int validate(const ScsData *d, const ScsCone *k) {
643
+ ScsSettings *stgs = d->stgs;
644
+ if (d->m <= 0 || d->n <= 0) {
645
+ scs_printf("m and n must both be greater than 0; m = %li, n = %li\n",
646
+ (long)d->m, (long)d->n);
647
+ return -1;
648
+ }
649
+ if (d->m < d->n) {
650
+ scs_printf("WARN: m less than n, problem likely degenerate\n");
651
+ /* return -1; */
652
+ }
653
+ if (SCS(validate_lin_sys)(d->A) < 0) {
654
+ scs_printf("invalid linear system input data\n");
655
+ return -1;
656
+ }
657
+ if (SCS(validate_cones)(d, k) < 0) {
658
+ scs_printf("cone validation error\n");
659
+ return -1;
660
+ }
661
+ if (stgs->max_iters <= 0) {
662
+ scs_printf("max_iters must be positive\n");
663
+ return -1;
664
+ }
665
+ if (stgs->eps <= 0) {
666
+ scs_printf("eps tolerance must be positive\n");
667
+ return -1;
668
+ }
669
+ if (stgs->alpha <= 0 || stgs->alpha >= 2) {
670
+ scs_printf("alpha must be in (0,2)\n");
671
+ return -1;
672
+ }
673
+ if (stgs->rho_x <= 0) {
674
+ scs_printf("rho_x must be positive (1e-3 works well).\n");
675
+ return -1;
676
+ }
677
+ if (stgs->scale <= 0) {
678
+ scs_printf("scale must be positive (1 works well).\n");
679
+ return -1;
680
+ }
681
+ return 0;
682
+ }
683
+
684
+ static ScsWork *init_work(const ScsData *d, const ScsCone *k) {
685
+ ScsWork *w = (ScsWork *)scs_calloc(1, sizeof(ScsWork));
686
+ scs_int l = d->n + d->m + 1;
687
+ if (d->stgs->verbose) {
688
+ print_init_header(d, k);
689
+ }
690
+ if (!w) {
691
+ scs_printf("ERROR: allocating work failure\n");
692
+ return SCS_NULL;
693
+ }
694
+ /* get settings and dims from data struct */
695
+ w->stgs = d->stgs;
696
+ w->m = d->m;
697
+ w->n = d->n;
698
+ w->best_max_residual = INFINITY;
699
+ /* allocate workspace: */
700
+ /* u* include v* values */
701
+ w->u = (scs_float *)scs_malloc(2 * l * sizeof(scs_float));
702
+ w->u_best = (scs_float *)scs_malloc(2 * l * sizeof(scs_float));
703
+ w->u_t = (scs_float *)scs_malloc(l * sizeof(scs_float));
704
+ w->u_prev = (scs_float *)scs_malloc(2 * l * sizeof(scs_float));
705
+ w->h = (scs_float *)scs_malloc((l - 1) * sizeof(scs_float));
706
+ w->g = (scs_float *)scs_malloc((l - 1) * sizeof(scs_float));
707
+ w->pr = (scs_float *)scs_malloc(d->m * sizeof(scs_float));
708
+ w->dr = (scs_float *)scs_malloc(d->n * sizeof(scs_float));
709
+ w->b = (scs_float *)scs_malloc(d->m * sizeof(scs_float));
710
+ w->c = (scs_float *)scs_malloc(d->n * sizeof(scs_float));
711
+ if (!w->u || !w->u_t || !w->u_prev || !w->h || !w->g || !w->pr || !w->dr ||
712
+ !w->b || !w->c) {
713
+ scs_printf("ERROR: work memory allocation failure\n");
714
+ return SCS_NULL;
715
+ }
716
+ /* make u,v and u_prev,v_prev contiguous in memory */
717
+ w->v = &(w->u[l]);
718
+ w->v_best = &(w->u_best[l]);
719
+ w->v_prev = &(w->u_prev[l]);
720
+ w->A = d->A;
721
+ if (w->stgs->normalize) {
722
+ #ifdef COPYAMATRIX
723
+ if (!SCS(copy_a_matrix)(&(w->A), d->A)) {
724
+ scs_printf("ERROR: copy A matrix failed\n");
725
+ return SCS_NULL;
726
+ }
727
+ #endif
728
+ w->scal = (ScsScaling *)scs_malloc(sizeof(ScsScaling));
729
+ SCS(normalize_a)(w->A, w->stgs, k, w->scal);
730
+ #if EXTRA_VERBOSE > 0
731
+ SCS(print_array)(w->scal->D, d->m, "D");
732
+ scs_printf("SCS(norm) D = %4f\n", SCS(norm)(w->scal->D, d->m));
733
+ SCS(print_array)(w->scal->E, d->n, "E");
734
+ scs_printf("SCS(norm) E = %4f\n", SCS(norm)(w->scal->E, d->n));
735
+ #endif
736
+ } else {
737
+ w->scal = SCS_NULL;
738
+ }
739
+ if (!(w->cone_work = SCS(init_cone)(k))) {
740
+ scs_printf("ERROR: init_cone failure\n");
741
+ return SCS_NULL;
742
+ }
743
+ if (!(w->p = SCS(init_lin_sys_work)(w->A, w->stgs))) {
744
+ scs_printf("ERROR: init_lin_sys_work failure\n");
745
+ return SCS_NULL;
746
+ }
747
+ if (!(w->accel =
748
+ aa_init(2 * (w->m + w->n + 1), ABS(w->stgs->acceleration_lookback),
749
+ w->stgs->acceleration_lookback >= 0))) {
750
+ scs_printf("WARN: aa_init returned NULL, no acceleration applied.\n");
751
+ }
752
+ return w;
753
+ }
754
+
755
+ static scs_int update_work(const ScsData *d, ScsWork *w,
756
+ const ScsSolution *sol) {
757
+ /* before normalization */
758
+ scs_int n = d->n;
759
+ scs_int m = d->m;
760
+
761
+ w->nm_b = SCS(norm)(d->b, m);
762
+ w->nm_c = SCS(norm)(d->c, n);
763
+ memcpy(w->b, d->b, d->m * sizeof(scs_float));
764
+ memcpy(w->c, d->c, d->n * sizeof(scs_float));
765
+
766
+ #if EXTRA_VERBOSE > 0
767
+ SCS(print_array)(w->b, m, "b");
768
+ scs_printf("pre-normalized norm b = %4f\n", SCS(norm)(w->b, m));
769
+ SCS(print_array)(w->c, n, "c");
770
+ scs_printf("pre-normalized norm c = %4f\n", SCS(norm)(w->c, n));
771
+ #endif
772
+ if (w->stgs->normalize) {
773
+ SCS(normalize_b_c)(w);
774
+ #if EXTRA_VERBOSE > 0
775
+ SCS(print_array)(w->b, m, "bn");
776
+ scs_printf("sc_b = %4f\n", w->sc_b);
777
+ scs_printf("post-normalized norm b = %4f\n", SCS(norm)(w->b, m));
778
+ SCS(print_array)(w->c, n, "cn");
779
+ scs_printf("sc_c = %4f\n", w->sc_c);
780
+ scs_printf("post-normalized norm c = %4f\n", SCS(norm)(w->c, n));
781
+ #endif
782
+ }
783
+ if (w->stgs->warm_start) {
784
+ warm_start_vars(w, sol);
785
+ } else {
786
+ cold_start_vars(w);
787
+ }
788
+ memcpy(w->h, w->c, n * sizeof(scs_float));
789
+ memcpy(&(w->h[n]), w->b, m * sizeof(scs_float));
790
+ memcpy(w->g, w->h, (n + m) * sizeof(scs_float));
791
+ SCS(solve_lin_sys)(w->A, w->stgs, w->p, w->g, SCS_NULL, -1);
792
+ SCS(scale_array)(&(w->g[n]), -1, m);
793
+ w->g_th = SCS(dot)(w->h, w->g, n + m);
794
+ return 0;
795
+ }
796
+
797
+ static scs_float iterate_norm_diff(ScsWork *w) {
798
+ scs_int l = w->m + w->n + 1;
799
+ scs_float u_norm_difference = SCS(norm_diff)(w->u, w->u_prev, l);
800
+ scs_float v_norm_difference = SCS(norm_diff)(w->v, w->v_prev, l);
801
+ scs_float norm = SQRTF(SCS(norm_sq)(w->u, l) + SCS(norm_sq)(w->v, l));
802
+ scs_float norm_diff = SQRTF(u_norm_difference * u_norm_difference +
803
+ v_norm_difference * v_norm_difference);
804
+ return norm_diff / norm;
805
+ }
806
+
807
+ static void update_best_iterate(ScsWork *w, ScsResiduals *r) {
808
+ scs_float max_residual = get_max_residual(r);
809
+ if (w->best_max_residual > max_residual) {
810
+ w->best_max_residual = max_residual;
811
+ memcpy(w->u_best, w->u, (w->m + w->n + 1) * sizeof(scs_float));
812
+ memcpy(w->v_best, w->v, (w->m + w->n + 1) * sizeof(scs_float));
813
+ }
814
+ }
815
+
816
+ scs_int SCS(solve)(ScsWork *w, const ScsData *d, const ScsCone *k,
817
+ ScsSolution *sol, ScsInfo *info) {
818
+ scs_int i;
819
+ SCS(timer) solve_timer, accel_timer;
820
+ scs_float total_accel_time = 0.0, total_norm;
821
+ ScsResiduals r;
822
+ scs_int l = w->m + w->n + 1;
823
+ if (!d || !k || !sol || !info || !w || !d->b || !d->c) {
824
+ scs_printf("ERROR: SCS_NULL input\n");
825
+ return SCS_FAILED;
826
+ }
827
+ /* initialize ctrl-c support */
828
+ scs_start_interrupt_listener();
829
+ SCS(tic)(&solve_timer);
830
+ info->status_val = SCS_UNFINISHED; /* not yet converged */
831
+ r.last_iter = -1;
832
+ update_work(d, w, sol);
833
+
834
+ if (w->stgs->verbose) {
835
+ print_header(w, k);
836
+ }
837
+ /* scs: */
838
+ for (i = 0; i < w->stgs->max_iters; ++i) {
839
+ /* accelerate here so that last step always projection onto cone */
840
+ /* this ensures the returned iterates always satisfy conic constraints */
841
+ /* this relies on the fact that u and v are contiguous in memory */
842
+ SCS(tic)(&accel_timer);
843
+ if (i > 0 && aa_apply(w->u, w->u_prev, w->accel) != 0) {
844
+ /*
845
+ return failure(w, w->m, w->n, sol, info, SCS_FAILED,
846
+ "error in accelerate", "Failure");
847
+ */
848
+ }
849
+ total_accel_time += SCS(tocq)(&accel_timer);
850
+
851
+ /* scs is homogeneous so scale the iterates to keep norm reasonable */
852
+ total_norm = SQRTF(SCS(norm_sq)(w->u, l) + SCS(norm_sq)(w->v, l));
853
+ SCS(scale_array)(w->u, SQRTF((scs_float)l) * ITERATE_NORM / total_norm, l);
854
+ SCS(scale_array)(w->v, SQRTF((scs_float)l) * ITERATE_NORM / total_norm, l);
855
+
856
+ memcpy(w->u_prev, w->u, l * sizeof(scs_float));
857
+ memcpy(w->v_prev, w->v, l * sizeof(scs_float));
858
+
859
+ if (project_lin_sys(w, i) < 0) {
860
+ return failure(w, w->m, w->n, sol, info, SCS_FAILED,
861
+ "error in project_lin_sys", "Failure");
862
+ }
863
+ if (project_cones(w, k, i) < 0) {
864
+ return failure(w, w->m, w->n, sol, info, SCS_FAILED,
865
+ "error in project_cones", "Failure");
866
+ }
867
+
868
+ update_dual_vars(w);
869
+
870
+ if (scs_is_interrupted()) {
871
+ return failure(w, w->m, w->n, sol, info, SCS_SIGINT, "Interrupted",
872
+ "Interrupted");
873
+ }
874
+ if (i % CONVERGED_INTERVAL == 0 || iterate_norm_diff(w) < 1e-10) {
875
+ calc_residuals(w, &r, i);
876
+ if ((info->status_val = has_converged(w, &r, i)) != 0) {
877
+ break;
878
+ }
879
+ update_best_iterate(w, &r);
880
+ }
881
+
882
+ if (w->stgs->verbose && i % PRINT_INTERVAL == 0) {
883
+ calc_residuals(w, &r, i);
884
+ update_best_iterate(w, &r);
885
+ print_summary(w, i, &r, &solve_timer);
886
+ }
887
+ }
888
+ if (w->stgs->verbose) {
889
+ calc_residuals(w, &r, i);
890
+ print_summary(w, i, &r, &solve_timer);
891
+ }
892
+ /* populate solution vectors (unnormalized) and info */
893
+ get_solution(w, sol, info, &r, i);
894
+ info->solve_time = SCS(tocq)(&solve_timer);
895
+
896
+ if (w->stgs->verbose) {
897
+ print_footer(d, k, sol, w, info, total_accel_time);
898
+ }
899
+ scs_end_interrupt_listener();
900
+ return info->status_val;
901
+ }
902
+
903
+ void SCS(finish)(ScsWork *w) {
904
+ if (w) {
905
+ SCS(finish_cone)(w->cone_work);
906
+ if (w->stgs && w->stgs->normalize) {
907
+ #ifndef COPYAMATRIX
908
+ SCS(un_normalize_a)(w->A, w->stgs, w->scal);
909
+ #else
910
+ SCS(free_a_matrix)(w->A);
911
+ #endif
912
+ }
913
+ if (w->p) {
914
+ SCS(free_lin_sys_work)(w->p);
915
+ }
916
+ if (w->accel) {
917
+ aa_finish(w->accel);
918
+ }
919
+ free_work(w);
920
+ }
921
+ }
922
+
923
+ ScsWork *SCS(init)(const ScsData *d, const ScsCone *k, ScsInfo *info) {
924
+ #if EXTRA_VERBOSE > 1
925
+ SCS(tic)(&global_timer);
926
+ #endif
927
+ ScsWork *w;
928
+ SCS(timer) init_timer;
929
+ scs_start_interrupt_listener();
930
+ if (!d || !k || !info) {
931
+ scs_printf("ERROR: Missing ScsData, ScsCone or ScsInfo input\n");
932
+ return SCS_NULL;
933
+ }
934
+ #if EXTRA_VERBOSE > 0
935
+ SCS(print_data)(d);
936
+ SCS(print_cone_data)(k);
937
+ #endif
938
+ #ifndef NOVALIDATE
939
+ if (validate(d, k) < 0) {
940
+ scs_printf("ERROR: Validation returned failure\n");
941
+ return SCS_NULL;
942
+ }
943
+ #endif
944
+ SCS(tic)(&init_timer);
945
+ if (d->stgs->write_data_filename) {
946
+ SCS(write_data)(d, k);
947
+ }
948
+ w = init_work(d, k);
949
+ info->setup_time = SCS(tocq)(&init_timer);
950
+ if (d->stgs->verbose) {
951
+ scs_printf("Setup time: %1.2es\n", info->setup_time / 1e3);
952
+ }
953
+ scs_end_interrupt_listener();
954
+ return w;
955
+ }
956
+
957
+ /* this just calls SCS(init), SCS(solve), and SCS(finish) */
958
+ scs_int scs(const ScsData *d, const ScsCone *k, ScsSolution *sol,
959
+ ScsInfo *info) {
960
+ scs_int status;
961
+ ScsWork *w = SCS(init)(d, k, info);
962
+ #if EXTRA_VERBOSE > 0
963
+ scs_printf("size of scs_int = %lu, size of scs_float = %lu\n",
964
+ sizeof(scs_int), sizeof(scs_float));
965
+ #endif
966
+ if (w) {
967
+ SCS(solve)(w, d, k, sol, info);
968
+ status = info->status_val;
969
+ } else {
970
+ status = failure(SCS_NULL, d ? d->m : -1, d ? d->n : -1, sol, info,
971
+ SCS_FAILED, "could not initialize work", "Failure");
972
+ }
973
+ SCS(finish)(w);
974
+ return status;
975
+ }