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
Binary file
@@ -0,0 +1,5 @@
1
+ #include "glbopts.h"
2
+
3
+ const char *SCS(version)(void) { return SCS_VERSION; }
4
+ size_t SCS(sizeof_int)(void) { return sizeof(scs_int); }
5
+ size_t SCS(sizeof_float)(void) { return sizeof(scs_float); }
Binary file
@@ -0,0 +1,196 @@
1
+ #include "util.h"
2
+
3
+ #include "glbopts.h"
4
+ #include "linsys.h"
5
+
6
+ /* return milli-seconds */
7
+ #if (defined NOTIMER)
8
+
9
+ void SCS(tic)(SCS(timer) * t) {}
10
+ scs_float SCS(tocq)(SCS(timer) * t) { return NAN; }
11
+
12
+ #elif (defined _WIN32 || _WIN64 || defined _WINDLL)
13
+
14
+ void SCS(tic)(SCS(timer) * t) {
15
+ QueryPerformanceFrequency(&t->freq);
16
+ QueryPerformanceCounter(&t->tic);
17
+ }
18
+
19
+ scs_float SCS(tocq)(SCS(timer) * t) {
20
+ QueryPerformanceCounter(&t->toc);
21
+ return (1e3 * (t->toc.QuadPart - t->tic.QuadPart) /
22
+ (scs_float)t->freq.QuadPart);
23
+ }
24
+
25
+ #elif (defined __APPLE__)
26
+ #include <stdint.h>
27
+
28
+ void SCS(tic)(SCS(timer) * t) {
29
+ /* read current clock cycles */
30
+ t->tic = mach_absolute_time();
31
+ }
32
+
33
+ scs_float SCS(tocq)(SCS(timer) * t) {
34
+ uint64_t duration; /* elapsed time in clock cycles*/
35
+
36
+ t->toc = mach_absolute_time();
37
+ duration = t->toc - t->tic;
38
+
39
+ /*conversion from clock cycles to nanoseconds*/
40
+ mach_timebase_info(&(t->tinfo));
41
+ duration *= t->tinfo.numer;
42
+ duration /= t->tinfo.denom;
43
+
44
+ return (scs_float)duration / 1e6;
45
+ }
46
+
47
+ #else
48
+
49
+ void SCS(tic)(SCS(timer) * t) { clock_gettime(CLOCK_MONOTONIC, &t->tic); }
50
+
51
+ scs_float SCS(tocq)(SCS(timer) * t) {
52
+ struct timespec temp;
53
+
54
+ clock_gettime(CLOCK_MONOTONIC, &t->toc);
55
+
56
+ if ((t->toc.tv_nsec - t->tic.tv_nsec) < 0) {
57
+ temp.tv_sec = t->toc.tv_sec - t->tic.tv_sec - 1;
58
+ temp.tv_nsec = 1e9 + t->toc.tv_nsec - t->tic.tv_nsec;
59
+ } else {
60
+ temp.tv_sec = t->toc.tv_sec - t->tic.tv_sec;
61
+ temp.tv_nsec = t->toc.tv_nsec - t->tic.tv_nsec;
62
+ }
63
+ return (scs_float)temp.tv_sec * 1e3 + (scs_float)temp.tv_nsec / 1e6;
64
+ }
65
+
66
+ #endif
67
+
68
+ scs_float SCS(toc)(SCS(timer) * t) {
69
+ scs_float time = SCS(tocq)(t);
70
+ scs_printf("time: %8.4f milli-seconds.\n", time);
71
+ return time;
72
+ }
73
+
74
+ scs_float SCS(str_toc)(char *str, SCS(timer) * t) {
75
+ scs_float time = SCS(tocq)(t);
76
+ scs_printf("%s - time: %8.4f milli-seconds.\n", str, time);
77
+ return time;
78
+ }
79
+
80
+ void SCS(print_cone_data)(const ScsCone *k) {
81
+ scs_int i;
82
+ scs_printf("num zeros = %i\n", (int)k->f);
83
+ scs_printf("num LP = %i\n", (int)k->l);
84
+ scs_printf("num SOCs = %i\n", (int)k->qsize);
85
+ scs_printf("soc array:\n");
86
+ for (i = 0; i < k->qsize; i++) {
87
+ scs_printf("%i\n", (int)k->q[i]);
88
+ }
89
+ scs_printf("num SDCs = %i\n", (int)k->ssize);
90
+ scs_printf("sdc array:\n");
91
+ for (i = 0; i < k->ssize; i++) {
92
+ scs_printf("%i\n", (int)k->s[i]);
93
+ }
94
+ scs_printf("num ep = %i\n", (int)k->ep);
95
+ scs_printf("num ed = %i\n", (int)k->ed);
96
+ scs_printf("num PCs = %i\n", (int)k->psize);
97
+ scs_printf("pow array:\n");
98
+ for (i = 0; i < k->psize; i++) {
99
+ scs_printf("%4f\n", (double)k->p[i]);
100
+ }
101
+ }
102
+
103
+ void SCS(print_work)(const ScsWork *w) {
104
+ scs_int i, l = w->n + w->m;
105
+ scs_printf("\n u_t is \n");
106
+ for (i = 0; i < l; i++) {
107
+ scs_printf("%f\n", w->u_t[i]);
108
+ }
109
+ scs_printf("\n u is \n");
110
+ for (i = 0; i < l; i++) {
111
+ scs_printf("%f\n", w->u[i]);
112
+ }
113
+ scs_printf("\n v is \n");
114
+ for (i = 0; i < l; i++) {
115
+ scs_printf("%f\n", w->v[i]);
116
+ }
117
+ }
118
+
119
+ void SCS(print_data)(const ScsData *d) {
120
+ scs_printf("m = %i\n", (int)d->m);
121
+ scs_printf("n = %i\n", (int)d->n);
122
+
123
+ scs_printf("max_iters = %i\n", (int)d->stgs->max_iters);
124
+ scs_printf("verbose = %i\n", (int)d->stgs->verbose);
125
+ scs_printf("normalize = %i\n", (int)d->stgs->normalize);
126
+ scs_printf("warm_start = %i\n", (int)d->stgs->warm_start);
127
+ scs_printf("acceleration_lookback = %i\n",
128
+ (int)d->stgs->acceleration_lookback);
129
+ scs_printf("eps = %4f\n", d->stgs->eps);
130
+ scs_printf("alpha = %4f\n", d->stgs->alpha);
131
+ scs_printf("rho_x = %4f\n", d->stgs->rho_x);
132
+ scs_printf("cg_rate = %4f\n", d->stgs->cg_rate);
133
+ scs_printf("scale = %4f\n", d->stgs->scale);
134
+ scs_printf("write_data_filename = %s\n",
135
+ (char *)d->stgs->write_data_filename);
136
+ }
137
+
138
+ void SCS(print_array)(const scs_float *arr, scs_int n, const char *name) {
139
+ scs_int i, j, k = 0;
140
+ scs_int num_on_one_line = 10;
141
+ scs_printf("\n");
142
+ for (i = 0; i < n / num_on_one_line; ++i) {
143
+ for (j = 0; j < num_on_one_line; ++j) {
144
+ scs_printf("%s[%li] = %4f, ", name, (long)k, arr[k]);
145
+ k++;
146
+ }
147
+ scs_printf("\n");
148
+ }
149
+ for (j = k; j < n; ++j) {
150
+ scs_printf("%s[%li] = %4f, ", name, (long)j, arr[j]);
151
+ }
152
+ scs_printf("\n");
153
+ }
154
+
155
+ void SCS(free_data)(ScsData *d, ScsCone *k) {
156
+ if (d) {
157
+ scs_free(d->b);
158
+ scs_free(d->c);
159
+ scs_free(d->stgs);
160
+ if (d->A) {
161
+ SCS(free_a_matrix)(d->A);
162
+ }
163
+ scs_free(d);
164
+ }
165
+ if (k) {
166
+ scs_free(k->q);
167
+ scs_free(k->s);
168
+ scs_free(k->p);
169
+ scs_free(k);
170
+ }
171
+ }
172
+
173
+ void SCS(free_sol)(ScsSolution *sol) {
174
+ if (sol) {
175
+ scs_free(sol->x);
176
+ scs_free(sol->y);
177
+ scs_free(sol->s);
178
+ scs_free(sol);
179
+ }
180
+ }
181
+
182
+ /* assumes d->stgs already allocated memory */
183
+ void SCS(set_default_settings)(ScsData *d) {
184
+ /* These constants are defined in include/glbopts.h */
185
+ d->stgs->max_iters = MAX_ITERS;
186
+ d->stgs->eps = EPS;
187
+ d->stgs->alpha = ALPHA;
188
+ d->stgs->rho_x = RHO_X;
189
+ d->stgs->scale = SCALE;
190
+ d->stgs->cg_rate = CG_RATE;
191
+ d->stgs->verbose = VERBOSE;
192
+ d->stgs->normalize = NORMALIZE;
193
+ d->stgs->warm_start = WARM_START;
194
+ d->stgs->acceleration_lookback = ACCELERATION_LOOKBACK;
195
+ d->stgs->write_data_filename = WRITE_DATA_FILENAME;
196
+ }
Binary file
@@ -0,0 +1,13 @@
1
+ /* Taken from http://www.jera.com/techinfo/jtns/jtn002.html */
2
+
3
+ /* Simple Macros for testing */
4
+ #define mu_assert(message, test) \
5
+ do { \
6
+ if (!(test)) return message; \
7
+ } while (0)
8
+ #define mu_run_test(test) \
9
+ do { \
10
+ const char *message = test(); \
11
+ tests_run++; \
12
+ if (message) return message; \
13
+ } while (0)
@@ -0,0 +1,93 @@
1
+ #ifndef PUTILS_H_GUARD
2
+ #define PUTILS_H_GUARD
3
+
4
+ #include "amatrix.h"
5
+ #include "cones.h"
6
+ #include "linalg.h"
7
+ #include "scs.h"
8
+ #include "util.h"
9
+
10
+ #define PI (3.141592654)
11
+ #ifdef DLONG
12
+ #ifdef _WIN64
13
+ /* this is a Microsoft extension, but also works with min_g_w-w64 */
14
+ #define INTRW "%I64d"
15
+ #else
16
+ #define INTRW "%ld"
17
+ #endif
18
+ #else
19
+ #define INTRW "%i"
20
+ #endif
21
+
22
+ void gen_random_prob_data(scs_int nnz, scs_int col_nnz, ScsData *d, ScsCone *k,
23
+ ScsSolution *opt_sol);
24
+
25
+ /* uniform random number in [-1,1] */
26
+ static scs_float rand_scs_float(void) {
27
+ return 2 * (((scs_float)rand()) / RAND_MAX) - 1;
28
+ }
29
+
30
+ void gen_random_prob_data(scs_int nnz, scs_int col_nnz, ScsData *d, ScsCone *k,
31
+ ScsSolution *opt_sol) {
32
+ scs_int n = d->n;
33
+ scs_int m = d->m;
34
+ ScsMatrix *A = d->A = (ScsMatrix *)scs_calloc(1, sizeof(ScsMatrix));
35
+ scs_float *b = d->b = (scs_float *)scs_calloc(m, sizeof(scs_float));
36
+ scs_float *c = d->c = (scs_float *)scs_calloc(n, sizeof(scs_float));
37
+ scs_float *x = opt_sol->x = (scs_float *)scs_calloc(n, sizeof(scs_float));
38
+ scs_float *y = opt_sol->y = (scs_float *)scs_calloc(m, sizeof(scs_float));
39
+ scs_float *s = opt_sol->s = (scs_float *)scs_calloc(m, sizeof(scs_float));
40
+ /* temporary variables */
41
+ scs_float *z = (scs_float *)scs_calloc(m, sizeof(scs_float));
42
+ scs_int i, j, r, rn, rm;
43
+
44
+ A->i = (scs_int *)scs_calloc(nnz, sizeof(scs_int));
45
+ A->p = (scs_int *)scs_calloc((n + 1), sizeof(scs_int));
46
+ A->x = (scs_float *)scs_calloc(nnz, sizeof(scs_float));
47
+ A->n = d->n;
48
+ A->m = d->m;
49
+ /* y, s >= 0 and y'*s = 0 */
50
+ for (i = 0; i < m; i++) {
51
+ y[i] = z[i] = rand_scs_float();
52
+ }
53
+
54
+ SCS(proj_dual_cone)(y, k, SCS_NULL, SCS_NULL, -1);
55
+
56
+ for (i = 0; i < m; i++) {
57
+ b[i] = s[i] = y[i] - z[i];
58
+ }
59
+
60
+ for (i = 0; i < n; i++) {
61
+ x[i] = rand_scs_float();
62
+ }
63
+
64
+ /*
65
+ c = -A'*y
66
+ b = A*x + s
67
+ */
68
+ A->p[0] = 0;
69
+ scs_printf("Generating random matrix:\n");
70
+ for (j = 0; j < n; j++) { /* column */
71
+ if (j * 100 % n == 0 && (j * 100 / n) % 10 == 0) {
72
+ scs_printf("%ld%%\n", (long)(j * 100 / n));
73
+ }
74
+ r = 0;
75
+ for (i = 0; i < m && r < col_nnz; ++i) {
76
+ /* generate a unique sorted array via Knuths alg */
77
+ rn = m - i;
78
+ rm = col_nnz - r;
79
+ if ((rand() % rn) < rm) {
80
+ A->x[r + j * col_nnz] = rand_scs_float();
81
+ A->i[r + j * col_nnz] = i;
82
+ b[i] += A->x[r + j * col_nnz] * x[j];
83
+ c[j] -= A->x[r + j * col_nnz] * y[i];
84
+ r++;
85
+ }
86
+ }
87
+ A->p[j + 1] = (j + 1) * col_nnz;
88
+ }
89
+ scs_printf("done\n");
90
+ scs_free(z);
91
+ }
92
+
93
+ #endif
@@ -0,0 +1,85 @@
1
+ #include "amatrix.h"
2
+ #include "glbopts.h"
3
+ #include "linalg.h"
4
+ #include "minunit.h"
5
+ #include "scs.h"
6
+ #include "util.h"
7
+
8
+ static scs_float Ax[] = {-1.0, -0.25035003149, -0.214684983694, -0.0013696512491, -1.0, -1.0, 0.0941450010301, -0.0929840802212, 0.0143905024814, -1.0, -1.41421356237, -1.0, 0.0941450010301, -0.0929840802212, 0.0143905024814, 1.0, -1.0, -0.0354035554388, -0.0402731435884, -0.151196563215, -1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, -1.0, -1.0, -1.0, -1.0, -1.0, -1.0, -1.0, -1.0, -1.0, -1.0, 1.0, -1.0, 1.0, 1.0, -1.41421356237, 1.0, -1.41421356237, 1.0, -1.41421356237, 1.0, -1.0, 1.0, -1.41421356237, 1.0, -1.41421356237, 1.0, -1.0, 1.0, 1.0, -1.41421356237, 1.0, -1.0, -1.0, -1.0, 1.0, -1.0, -1.0, -1.0, -1.0, 1.0, -1.0, -1.0, 1.0, -1.0, -1.0, -1.0, -1.0, -1.0, -1.0};
9
+ static scs_int Ai[] = {11, 15, 16, 17, 36, 12, 15, 16, 17, 18, 37, 13, 15, 16, 17, 18, 14, 15, 16, 17, 38, 15, 19, 16, 20, 17, 21, 19, 20, 21, 25, 19, 20, 21, 19, 22, 20, 23, 21, 24, 3, 26, 4, 5, 27, 7, 28, 9, 29, 6, 30, 8, 31, 10, 32, 11, 33, 12, 13, 34, 14, 35, 1, 7, 0, 8, 9, 2, 10, 1, 3, 41, 2, 6, 44, 25, 39, 25, 42};
10
+ static scs_int Ap[] = {0, 5, 11, 16, 21, 23, 25, 27, 31, 34, 36, 38, 40, 42, 45, 47, 49, 51, 53, 55, 57, 60, 62, 64, 66, 67, 69, 72, 75, 77, 79};
11
+ static scs_float b[] = {-0.0, -0.0, -0.0, -0.0, -0.0, -0.0, -0.0, -0.0, -0.0, -0.0, -0.0, -0.0, -0.0, -0.0, -0.0, -0.0, -0.0, -0.0, -0.0, -0.0, -0.0, -0.0, -0.0, -0.0, -0.0, -0.0, -0.0, -0.0, -0.0, -0.0, -0.0, -0.0, -0.0, -0.0, -0.0, -0.0, -0.0, -0.0, -0.0, -0.0, 1.0, -0.0, -0.0, 1.0, -0.0};
12
+ static scs_float c[] ={0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 2.0, 1.0, 1.0, 1.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0};
13
+
14
+ static scs_int m = 45;
15
+ static scs_int n = 30;
16
+
17
+ static scs_int f = 19;
18
+ static scs_int l = 7;
19
+ static scs_int *q = SCS_NULL;
20
+ static scs_int qsize = 0;
21
+ static scs_int s[] = {4, 2};
22
+ static scs_int ssize = 2;
23
+ static scs_int ep = 2;
24
+ static scs_int ed = 0;
25
+ static scs_float *p = SCS_NULL;
26
+ static scs_int psize = 0;
27
+
28
+ static scs_float opt = -4.8912;
29
+
30
+ static const char *rob_gauss_cov_est(void) {
31
+ ScsCone *k = (ScsCone *)scs_calloc(1, sizeof(ScsCone));
32
+ ScsData *d = (ScsData *)scs_calloc(1, sizeof(ScsData));
33
+ ScsSolution *sol = (ScsSolution *)scs_calloc(1, sizeof(ScsSolution));
34
+ ScsInfo info = {0};
35
+ scs_int exitflag;
36
+ scs_float perr, derr;
37
+ scs_int success;
38
+
39
+ d->m = m;
40
+ d->n = n;
41
+ d->b = b;
42
+ d->c = c;
43
+
44
+ d->stgs = (ScsSettings *)scs_calloc(1, sizeof(ScsSettings));
45
+ d->A = (ScsMatrix *)scs_calloc(1, sizeof(ScsMatrix));
46
+
47
+ d->A->m = m;
48
+ d->A->n = n;
49
+ d->A->x = Ax;
50
+ d->A->i = Ai;
51
+ d->A->p = Ap;
52
+
53
+ k->f = f;
54
+ k->l = l;
55
+ k->q = q;
56
+ k->qsize = qsize;
57
+ k->s = s;
58
+ k->ssize = ssize;
59
+ k->ep = ep;
60
+ k->ed = ed;
61
+ k->p = p;
62
+ k->psize = psize;
63
+
64
+ SCS(set_default_settings)(d);
65
+
66
+ exitflag = scs(d, k, sol, &info);
67
+
68
+ perr = SCS(dot)(d->c, sol->x, d->n) - opt;
69
+ derr = -SCS(dot)(d->b, sol->y, d->m) - opt;
70
+
71
+ scs_printf("primal obj error %4e\n", perr);
72
+ scs_printf("dual obj error %4e\n", derr);
73
+
74
+ success = ABS(perr) < 1e-4 && ABS(derr) < 1e-4 && exitflag == SCS_SOLVED;
75
+
76
+ SCS(free_sol)(sol);
77
+ scs_free(d->A);
78
+ scs_free(k);
79
+ scs_free(d->stgs);
80
+ scs_free(d);
81
+
82
+ mu_assert("rob_gauss_cov_est: SCS failed to produce outputflag SCS_SOLVED",
83
+ success);
84
+ return 0;
85
+ }
@@ -0,0 +1,50 @@
1
+ #include "glbopts.h"
2
+ #include "minunit.h"
3
+ #include "problem_utils.h"
4
+ #include "scs.h"
5
+ #include "util.h"
6
+
7
+ static const char *small_lp(void) {
8
+ ScsCone *k = (ScsCone *)scs_calloc(1, sizeof(ScsCone));
9
+ ScsData *d = (ScsData *)scs_calloc(1, sizeof(ScsData));
10
+ ScsSolution *sol = (ScsSolution *)scs_calloc(1, sizeof(ScsSolution));
11
+ ScsSolution *opt_sol = (ScsSolution *)scs_calloc(1, sizeof(ScsSolution));
12
+ ScsInfo info = {0};
13
+ scs_float p_f = 0.1;
14
+ int seed = 1234;
15
+ scs_int n = 100;
16
+ scs_int m = 300;
17
+ scs_int col_nnz = (scs_int)ceil(sqrt(n));
18
+ scs_int nnz = n * col_nnz;
19
+ scs_int exitflag;
20
+ scs_float perr, derr;
21
+ scs_int success;
22
+
23
+ d->stgs = (ScsSettings *)scs_calloc(1, sizeof(ScsSettings));
24
+ k->f = (scs_int)floor(m * p_f);
25
+ k->l = m - k->f;
26
+
27
+ srand(seed);
28
+
29
+ d->m = m;
30
+ d->n = n;
31
+ gen_random_prob_data(nnz, col_nnz, d, k, opt_sol);
32
+ SCS(set_default_settings)(d);
33
+
34
+ /* this test fails with the default choice of 10 */
35
+ d->stgs->acceleration_lookback = 20;
36
+
37
+ exitflag = scs(d, k, sol, &info);
38
+ perr = SCS(dot)(d->c, sol->x, d->n) - SCS(dot)(d->c, opt_sol->x, d->n);
39
+ derr = -SCS(dot)(d->b, sol->y, d->m) + SCS(dot)(d->b, opt_sol->y, d->m);
40
+ scs_printf("primal obj error %4e\n", perr);
41
+ scs_printf("dual obj error %4e\n", derr);
42
+
43
+ success = ABS(perr) < 1e-4 && ABS(derr) < 1e-4 && exitflag == SCS_SOLVED;
44
+
45
+ SCS(free_data)(d, k);
46
+ SCS(free_sol)(sol);
47
+ SCS(free_sol)(opt_sol);
48
+ mu_assert("small_lp: SCS failed to produce outputflag SCS_SOLVED", success);
49
+ return 0;
50
+ }
@@ -0,0 +1,33 @@
1
+ #include "rw.h"
2
+ #include "scs.h"
3
+ #include "util.h"
4
+
5
+ #define OPT (1.530897)
6
+
7
+ static const char *small_random_socp(void) {
8
+ ScsData *d;
9
+ ScsCone *k;
10
+ ScsSolution *sol;
11
+ ScsInfo info = {0};
12
+ scs_int exitflag, success;
13
+ scs_float perr, derr;
14
+ const char *filename = "test/data/small_random_socp";
15
+
16
+ SCS(read_data)(filename, &d, &k);
17
+ sol = scs_calloc(1, sizeof(ScsSolution));
18
+ exitflag = scs(d, k, sol, &info);
19
+
20
+ perr = SCS(dot)(d->c, sol->x, d->n) - OPT;
21
+ derr = -SCS(dot)(d->b, sol->y, d->m) - OPT;
22
+
23
+ scs_printf("primal obj error %4e\n", perr);
24
+ scs_printf("dual obj error %4e\n", derr);
25
+
26
+ success = ABS(perr) < 1e-4 && ABS(derr) < 1e-4 && exitflag == SCS_SOLVED;
27
+
28
+ SCS(free_data)(d, k);
29
+ SCS(free_sol)(sol);
30
+ mu_assert("small_random_socp: SCS failed to produce outputflag SCS_SOLVED",
31
+ success);
32
+ return 0;
33
+ }
@@ -0,0 +1,171 @@
1
+ #include <time.h> /* to seed random */
2
+ #include "amatrix.h"
3
+ #include "problem_utils.h"
4
+ #include "scs.h"
5
+
6
+ /*
7
+ create data for problem:
8
+
9
+ minimize c'*x
10
+ subject to Ax <=_K b
11
+
12
+ where K is a product of zero, linear, and second-order cones. A is a sparse
13
+ matrix in
14
+ CSC format. A is 3n by n with about sqrt(n) nonzeros per column.
15
+
16
+ Construct data in such a way that the problem is primal and dual
17
+ feasible and thus bounded.
18
+ */
19
+
20
+ int main(int argc, char **argv) {
21
+ scs_int n, m, col_nnz, nnz, i, q_total, q_num_rows, max_q;
22
+ ScsCone *k;
23
+ ScsData *d;
24
+ ScsSolution *sol, *opt_sol;
25
+ ScsInfo info = {0};
26
+ scs_float p_f, p_l;
27
+ int seed = 0;
28
+
29
+ /* default parameters */
30
+ p_f = 0.1;
31
+ p_l = 0.3;
32
+ seed = time(SCS_NULL);
33
+
34
+ switch (argc) {
35
+ case 5:
36
+ seed = atoi(argv[4]);
37
+ /* no break */
38
+ case 4:
39
+ p_f = atof(argv[2]);
40
+ p_l = atof(argv[3]);
41
+ /* no break */
42
+ case 2:
43
+ n = atoi(argv[1]);
44
+ break;
45
+ default:
46
+ scs_printf(
47
+ "usage:\t%s n p_f p_l s\n"
48
+ "\tcreates an SOCP with n variables where p_f fraction of "
49
+ "rows correspond\n"
50
+ "\tto equality constraints, p_l fraction of rows correspond "
51
+ "to LP constraints,\n"
52
+ "\tand the remaining percentage of rows are involved in "
53
+ "second-order\n"
54
+ "\tcone constraints. the random number generator is seeded "
55
+ "with s.\n"
56
+ "\tnote that p_f + p_l should be less than or equal to 1, "
57
+ "and that\n"
58
+ "\tp_f should be less than .33, since that corresponds to "
59
+ "as many equality\n"
60
+ "\tconstraints as variables.\n",
61
+ argv[0]);
62
+ scs_printf(
63
+ "\nusage:\t%s n p_f p_l\n"
64
+ "\tdefaults the seed to the system time\n",
65
+ argv[0]);
66
+ scs_printf(
67
+ "\nusage:\t%s n\n"
68
+ "\tdefaults to using p_f = 0.1 and p_l = 0.3\n",
69
+ argv[0]);
70
+ return 0;
71
+ }
72
+ srand(seed);
73
+ scs_printf("seed : %i\n", seed);
74
+
75
+ k = (ScsCone *)scs_calloc(1, sizeof(ScsCone));
76
+ d = (ScsData *)scs_calloc(1, sizeof(ScsData));
77
+ d->stgs = (ScsSettings *)scs_calloc(1, sizeof(ScsSettings));
78
+ sol = (ScsSolution *)scs_calloc(1, sizeof(ScsSolution));
79
+ opt_sol = (ScsSolution *)scs_calloc(1, sizeof(ScsSolution));
80
+
81
+ m = 3 * n;
82
+ col_nnz = (int)ceil(sqrt(n));
83
+ nnz = n * col_nnz;
84
+
85
+ max_q = (scs_int)ceil(3 * n / log(3 * n));
86
+
87
+ if (p_f + p_l > 1.0) {
88
+ printf("error: p_f + p_l > 1.0!\n");
89
+ return 1;
90
+ }
91
+
92
+ k->f = (scs_int)floor(3 * n * p_f);
93
+ k->l = (scs_int)floor(3 * n * p_l);
94
+
95
+ k->qsize = 0;
96
+ q_num_rows = 3 * n - k->f - k->l;
97
+ k->q = (scs_int *)scs_malloc(q_num_rows * sizeof(scs_int));
98
+
99
+ while (q_num_rows > max_q) {
100
+ int size;
101
+ size = (rand() % max_q) + 1;
102
+ k->q[k->qsize] = size;
103
+ k->qsize++;
104
+ q_num_rows -= size;
105
+ }
106
+ if (q_num_rows > 0) {
107
+ k->q[k->qsize] = q_num_rows;
108
+ k->qsize++;
109
+ }
110
+
111
+ q_total = 0;
112
+ for (i = 0; i < k->qsize; i++) {
113
+ q_total += k->q[i];
114
+ }
115
+
116
+ k->s = SCS_NULL;
117
+ k->ssize = 0;
118
+ k->ep = 0;
119
+ k->ed = 0;
120
+
121
+ scs_printf("\nA is %ld by %ld, with %ld nonzeros per column.\n", (long)m,
122
+ (long)n, (long)col_nnz);
123
+ scs_printf("A has %ld nonzeros (%f%% dense).\n", (long)nnz,
124
+ 100 * (scs_float)col_nnz / m);
125
+ scs_printf("Nonzeros of A take %f GB of storage.\n",
126
+ ((scs_float)nnz * sizeof(scs_float)) / POWF(2, 30));
127
+ scs_printf("Row idxs of A take %f GB of storage.\n",
128
+ ((scs_float)nnz * sizeof(scs_int)) / POWF(2, 30));
129
+ scs_printf("Col ptrs of A take %f GB of storage.\n\n",
130
+ ((scs_float)n * sizeof(scs_int)) / POWF(2, 30));
131
+
132
+ printf("ScsCone information:\n");
133
+ printf("Zero cone rows: %ld\n", (long)k->f);
134
+ printf("LP cone rows: %ld\n", (long)k->l);
135
+ printf("Number of second-order cones: %ld, covering %ld rows, with sizes\n[",
136
+ (long)k->qsize, (long)q_total);
137
+ for (i = 0; i < k->qsize; i++) {
138
+ printf("%ld, ", (long)k->q[i]);
139
+ }
140
+ printf("]\n");
141
+ printf("Number of rows covered is %ld out of %ld.\n\n",
142
+ (long)(q_total + k->f + k->l), (long)m);
143
+
144
+ /* set up SCS structures */
145
+ d->m = m;
146
+ d->n = n;
147
+ gen_random_prob_data(nnz, col_nnz, d, k, opt_sol);
148
+ SCS(set_default_settings)(d);
149
+
150
+ /* d->stgs->write_data_filename = "random_socp_prob"; */
151
+
152
+ scs_printf("true pri opt = %4f\n", SCS(dot)(d->c, opt_sol->x, d->n));
153
+ scs_printf("true dua opt = %4f\n", -SCS(dot)(d->b, opt_sol->y, d->m));
154
+ /* solve! */
155
+ scs(d, k, sol, &info);
156
+ scs_printf("true pri opt = %4f\n", SCS(dot)(d->c, opt_sol->x, d->n));
157
+ scs_printf("true dua opt = %4f\n", -SCS(dot)(d->b, opt_sol->y, d->m));
158
+
159
+ if (sol->x) {
160
+ scs_printf("scs pri obj= %4f\n", SCS(dot)(d->c, sol->x, d->n));
161
+ }
162
+ if (sol->y) {
163
+ scs_printf("scs dua obj = %4f\n", -SCS(dot)(d->b, sol->y, d->m));
164
+ }
165
+
166
+ SCS(free_data)(d, k);
167
+ SCS(free_sol)(sol);
168
+ SCS(free_sol)(opt_sol);
169
+
170
+ return 0;
171
+ }