-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpixie.c
More file actions
2950 lines (2723 loc) · 83.2 KB
/
pixie.c
File metadata and controls
2950 lines (2723 loc) · 83.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
Description:
PIXIE is a compact mixed-integer linear programming solver implemented in one ISO C17 file.
It includes an LP/MPS reader, a two-phase primal simplex core, and a deterministic branch-and-bound layer for integer and binary variables.
This port preserves the original solver behavior while removing non-standard dependencies and keeping the implementation portable and self-contained.
Date: 2026-02-06
Author: Riveros / ASPIE / SATX
Copyright: © Oscar Riveros. All rights reserved.
gcc -O3 -std=c17 -Wall -Wextra -pedantic pixie.c -o pixie
*/
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <stdbool.h>
#include <string.h>
#include <math.h>
#include <ctype.h>
#include <errno.h>
#include <limits.h>
#include <time.h>
#define PIXIE_FEAS_TOL 1e-8
#define PIXIE_INT_TOL 1e-7
#define PIXIE_PIV_TOL 1e-10
#define PIXIE_EPS 1e-12
typedef enum {
PIXIE_CMP_LE = -1,
PIXIE_CMP_EQ = 0,
PIXIE_CMP_GE = 1
} PixieCompare;
typedef enum {
PIXIE_VAR_CONT = 0,
PIXIE_VAR_INT = 1,
PIXIE_VAR_BIN = 2
} PixieVarType;
typedef enum {
PIXIE_STATUS_OPTIMAL = 0,
PIXIE_STATUS_INFEASIBLE = 1,
PIXIE_STATUS_UNBOUNDED = 2,
PIXIE_STATUS_UNKNOWN = 3,
PIXIE_STATUS_ERROR = 4
} PixieStatus;
typedef enum {
PIXIE_FORMAT_AUTO = 0,
PIXIE_FORMAT_LP = 1,
PIXIE_FORMAT_MPS = 2
} PixieFormat;
typedef struct {
int *idx;
double *val;
int len;
int cap;
} PixieSparse;
typedef struct {
PixieSparse a;
PixieCompare cmp;
double rhs;
} PixieConstraint;
typedef struct {
char *name;
double obj;
double lb;
double ub;
PixieVarType type;
} PixieVar;
typedef struct {
PixieVar *vars;
int n_vars;
int cap_vars;
PixieConstraint *cons;
int n_cons;
int cap_cons;
int obj_sense; /* +1 minimize, -1 maximize */
} PixieModel;
typedef struct {
PixieFormat format;
const char *file;
double time_limit_sec; /* <= 0 means unlimited */
long long node_limit; /* <= 0 means unlimited */
double gap_limit; /* < 0 means disabled */
uint64_t seed;
bool seed_set;
int verbose; /* 0..3 */
bool pure_lp;
bool selftest;
} PixieOptions;
typedef struct {
PixieStatus status;
double obj_min;
double obj_out;
double *x;
int n;
bool has_primal;
long long nodes_processed;
bool stopped_time;
bool stopped_nodes;
bool stopped_gap;
} PixieSolution;
typedef struct {
double **table;
double *raw;
int *v_idx;
bool *a_flg;
int v_cnt;
int e_cnt;
int s_cnt;
int a_cnt;
} PixieSimplex;
typedef enum {
PIXIE_SIMPLEX_OPTIMAL = 0,
PIXIE_SIMPLEX_UNBOUNDED = 1,
PIXIE_SIMPLEX_NUMERIC = 2
} PixieSimplexStatus;
typedef struct {
PixieConstraint *data;
int len;
int cap;
} PixieConVec;
typedef struct {
double *lb;
double *ub;
double bound;
int depth;
} PixieNode;
typedef struct {
PixieNode *data;
int len;
int cap;
} PixieNodeStack;
typedef struct {
char **data;
int len;
int cap;
} PixieTokVec;
typedef enum {
PIXIE_MAP_SHIFT_LB = 0,
PIXIE_MAP_SHIFT_UB = 1,
PIXIE_MAP_FREE = 2
} PixieVarMapKind;
typedef struct {
PixieVarMapKind kind;
int y0;
int y1;
double constant;
} PixieVarMap;
static void pixie_set_error(char *err, size_t errsz, const char *msg) {
if (err != NULL && errsz > 0) {
size_t n = strlen(msg);
if (n >= errsz) {
n = errsz - 1;
}
memcpy(err, msg, n);
err[n] = '\0';
}
}
static void *pixie_xmalloc(size_t n) {
void *p = malloc(n == 0 ? 1 : n);
if (p == NULL) {
fprintf(stderr, "fatal: out of memory\n");
exit(EXIT_FAILURE);
}
return p;
}
static void *pixie_xcalloc(size_t count, size_t size) {
void *p = calloc(count == 0 ? 1 : count, size == 0 ? 1 : size);
if (p == NULL) {
fprintf(stderr, "fatal: out of memory\n");
exit(EXIT_FAILURE);
}
return p;
}
static void *pixie_xrealloc(void *ptr, size_t n) {
void *p = realloc(ptr, n == 0 ? 1 : n);
if (p == NULL) {
fprintf(stderr, "fatal: out of memory\n");
exit(EXIT_FAILURE);
}
return p;
}
static char *pixie_strdup_c(const char *s) {
size_t n = strlen(s);
char *d = (char *)pixie_xmalloc(n + 1);
memcpy(d, s, n + 1);
return d;
}
static int pixie_stricmp_c(const char *a, const char *b) {
unsigned char ca;
unsigned char cb;
while (*a != '\0' && *b != '\0') {
ca = (unsigned char)tolower((unsigned char)*a);
cb = (unsigned char)tolower((unsigned char)*b);
if (ca != cb) {
return (int)ca - (int)cb;
}
++a;
++b;
}
ca = (unsigned char)tolower((unsigned char)*a);
cb = (unsigned char)tolower((unsigned char)*b);
return (int)ca - (int)cb;
}
static bool pixie_starts_with_ci(const char *s, const char *prefix) {
while (*prefix != '\0') {
if (*s == '\0') {
return false;
}
if (tolower((unsigned char)*s) != tolower((unsigned char)*prefix)) {
return false;
}
++s;
++prefix;
}
return true;
}
static bool pixie_ends_with_ci(const char *s, const char *suffix) {
size_t n = strlen(s);
size_t m = strlen(suffix);
if (m > n) {
return false;
}
return pixie_stricmp_c(s + (n - m), suffix) == 0;
}
static char *pixie_ltrim(char *s) {
while (*s != '\0' && isspace((unsigned char)*s)) {
++s;
}
return s;
}
static void pixie_rtrim(char *s) {
size_t n = strlen(s);
while (n > 0) {
if (!isspace((unsigned char)s[n - 1])) {
break;
}
s[n - 1] = '\0';
--n;
}
}
static char *pixie_trim(char *s) {
char *t = pixie_ltrim(s);
pixie_rtrim(t);
return t;
}
static bool pixie_is_finite(double x) {
return isfinite(x) != 0;
}
static double pixie_max(double a, double b) {
return (a > b) ? a : b;
}
static double pixie_min(double a, double b) {
return (a < b) ? a : b;
}
static bool pixie_parse_double_token(const char *s, double *out) {
char *end = NULL;
double v;
errno = 0;
v = strtod(s, &end);
if (end == s) {
return false;
}
while (*end != '\0' && isspace((unsigned char)*end)) {
++end;
}
if (*end != '\0') {
return false;
}
if (errno == ERANGE) {
return false;
}
*out = v;
return true;
}
static bool pixie_parse_ll_token(const char *s, long long *out) {
char *end = NULL;
long long v;
errno = 0;
v = strtoll(s, &end, 10);
if (end == s) {
return false;
}
while (*end != '\0' && isspace((unsigned char)*end)) {
++end;
}
if (*end != '\0') {
return false;
}
if (errno == ERANGE) {
return false;
}
*out = v;
return true;
}
static bool pixie_parse_u64_token(const char *s, uint64_t *out) {
char *end = NULL;
unsigned long long v;
errno = 0;
v = strtoull(s, &end, 10);
if (end == s) {
return false;
}
while (*end != '\0' && isspace((unsigned char)*end)) {
++end;
}
if (*end != '\0') {
return false;
}
if (errno == ERANGE) {
return false;
}
*out = (uint64_t)v;
return true;
}
static uint64_t pixie_rng_next(uint64_t *state) {
uint64_t x = *state;
if (x == 0) {
x = UINT64_C(0x9e3779b97f4a7c15);
}
x ^= x >> 12;
x ^= x << 25;
x ^= x >> 27;
*state = x;
return x * UINT64_C(2685821657736338717);
}
static void pixie_sparse_init(PixieSparse *s) {
s->idx = NULL;
s->val = NULL;
s->len = 0;
s->cap = 0;
}
static void pixie_sparse_free(PixieSparse *s) {
free(s->idx);
free(s->val);
s->idx = NULL;
s->val = NULL;
s->len = 0;
s->cap = 0;
}
static void pixie_sparse_reserve(PixieSparse *s, int need) {
int nc;
if (need <= s->cap) {
return;
}
nc = (s->cap <= 0) ? 8 : s->cap;
while (nc < need) {
if (nc > INT_MAX / 2) {
nc = need;
break;
}
nc *= 2;
}
s->idx = (int *)pixie_xrealloc(s->idx, (size_t)nc * sizeof(int));
s->val = (double *)pixie_xrealloc(s->val, (size_t)nc * sizeof(double));
s->cap = nc;
}
static void pixie_sparse_add(PixieSparse *s, int idx, double val) {
int i;
if (fabs(val) <= PIXIE_EPS) {
return;
}
for (i = 0; i < s->len; ++i) {
if (s->idx[i] == idx) {
s->val[i] += val;
return;
}
}
pixie_sparse_reserve(s, s->len + 1);
s->idx[s->len] = idx;
s->val[s->len] = val;
++s->len;
}
static void pixie_sparse_prune(PixieSparse *s, double tol) {
int i;
int w = 0;
for (i = 0; i < s->len; ++i) {
if (fabs(s->val[i]) > tol) {
s->idx[w] = s->idx[i];
s->val[w] = s->val[i];
++w;
}
}
s->len = w;
}
static void pixie_sparse_move(PixieSparse *dst, PixieSparse *src) {
dst->idx = src->idx;
dst->val = src->val;
dst->len = src->len;
dst->cap = src->cap;
src->idx = NULL;
src->val = NULL;
src->len = 0;
src->cap = 0;
}
static void pixie_sparse_clone(PixieSparse *dst, const PixieSparse *src) {
int i;
pixie_sparse_init(dst);
if (src->len <= 0) {
return;
}
pixie_sparse_reserve(dst, src->len);
for (i = 0; i < src->len; ++i) {
dst->idx[i] = src->idx[i];
dst->val[i] = src->val[i];
}
dst->len = src->len;
}
static void pixie_model_init(PixieModel *m) {
m->vars = NULL;
m->n_vars = 0;
m->cap_vars = 0;
m->cons = NULL;
m->n_cons = 0;
m->cap_cons = 0;
m->obj_sense = +1;
}
static void pixie_model_free(PixieModel *m) {
int i;
for (i = 0; i < m->n_vars; ++i) {
free(m->vars[i].name);
}
for (i = 0; i < m->n_cons; ++i) {
pixie_sparse_free(&m->cons[i].a);
}
free(m->vars);
free(m->cons);
m->vars = NULL;
m->cons = NULL;
m->n_vars = 0;
m->cap_vars = 0;
m->n_cons = 0;
m->cap_cons = 0;
m->obj_sense = +1;
}
static int pixie_model_find_var(const PixieModel *m, const char *name) {
int i;
for (i = 0; i < m->n_vars; ++i) {
if (strcmp(m->vars[i].name, name) == 0) {
return i;
}
}
return -1;
}
static int pixie_model_add_var(PixieModel *m, const char *name) {
int nc;
PixieVar *v;
if (m->n_vars >= m->cap_vars) {
nc = (m->cap_vars <= 0) ? 8 : m->cap_vars;
while (nc <= m->n_vars) {
if (nc > INT_MAX / 2) {
nc = m->n_vars + 1;
break;
}
nc *= 2;
}
m->vars = (PixieVar *)pixie_xrealloc(m->vars, (size_t)nc * sizeof(PixieVar));
m->cap_vars = nc;
}
v = &m->vars[m->n_vars];
v->name = pixie_strdup_c(name);
v->obj = 0.0;
v->lb = 0.0;
v->ub = HUGE_VAL;
v->type = PIXIE_VAR_CONT;
++m->n_vars;
return m->n_vars - 1;
}
static int pixie_model_get_var(PixieModel *m, const char *name, bool create) {
int idx = pixie_model_find_var(m, name);
if (idx >= 0) {
return idx;
}
if (!create) {
return -1;
}
return pixie_model_add_var(m, name);
}
static int pixie_model_reserve_cons(PixieModel *m, int need) {
int nc;
if (need <= m->cap_cons) {
return 1;
}
nc = (m->cap_cons <= 0) ? 8 : m->cap_cons;
while (nc < need) {
if (nc > INT_MAX / 2) {
nc = need;
break;
}
nc *= 2;
}
m->cons = (PixieConstraint *)pixie_xrealloc(m->cons, (size_t)nc * sizeof(PixieConstraint));
m->cap_cons = nc;
return 1;
}
static int pixie_model_add_empty_constraint(PixieModel *m, PixieCompare cmp, double rhs) {
if (!pixie_model_reserve_cons(m, m->n_cons + 1)) {
return -1;
}
pixie_sparse_init(&m->cons[m->n_cons].a);
m->cons[m->n_cons].cmp = cmp;
m->cons[m->n_cons].rhs = rhs;
++m->n_cons;
return m->n_cons - 1;
}
static int pixie_model_add_constraint(PixieModel *m, const PixieSparse *a, PixieCompare cmp, double rhs) {
int idx = pixie_model_add_empty_constraint(m, cmp, rhs);
if (idx < 0) {
return 0;
}
pixie_sparse_clone(&m->cons[idx].a, a);
pixie_sparse_prune(&m->cons[idx].a, PIXIE_EPS);
return 1;
}
static void pixie_var_mark_integer(PixieModel *m, int idx) {
if (idx < 0 || idx >= m->n_vars) {
return;
}
if (m->vars[idx].type == PIXIE_VAR_CONT) {
m->vars[idx].type = PIXIE_VAR_INT;
}
}
static void pixie_var_mark_binary(PixieModel *m, int idx) {
if (idx < 0 || idx >= m->n_vars) {
return;
}
m->vars[idx].type = PIXIE_VAR_BIN;
if (m->vars[idx].lb < 0.0) {
m->vars[idx].lb = 0.0;
}
if (m->vars[idx].ub > 1.0) {
m->vars[idx].ub = 1.0;
}
}
static int pixie_count_integer_vars(const PixieModel *m) {
int i;
int cnt = 0;
for (i = 0; i < m->n_vars; ++i) {
if (m->vars[i].type != PIXIE_VAR_CONT) {
++cnt;
}
}
return cnt;
}
static bool pixie_is_name_start_char(char c) {
return isalpha((unsigned char)c) || c == '_' || c == '.' || c == '$';
}
static bool pixie_is_name_char(char c) {
return isalnum((unsigned char)c) || c == '_' || c == '.' || c == '$' || c == '[' || c == ']' || c == '#';
}
static int pixie_find_comparator(const char *s, int *pos, int *len, PixieCompare *cmp) {
int i;
int n = (int)strlen(s);
for (i = 0; i < n; ++i) {
if (s[i] == '<') {
if (i + 1 < n && s[i + 1] == '=') {
*pos = i;
*len = 2;
*cmp = PIXIE_CMP_LE;
return 1;
}
*pos = i;
*len = 1;
*cmp = PIXIE_CMP_LE;
return 1;
}
if (s[i] == '>') {
if (i + 1 < n && s[i + 1] == '=') {
*pos = i;
*len = 2;
*cmp = PIXIE_CMP_GE;
return 1;
}
*pos = i;
*len = 1;
*cmp = PIXIE_CMP_GE;
return 1;
}
if (s[i] == '=') {
*pos = i;
*len = 1;
*cmp = PIXIE_CMP_EQ;
return 1;
}
}
return 0;
}
static int pixie_parse_linear_expr(PixieModel *m, const char *text, bool create_vars, PixieSparse *out_terms, double *out_const, char *err, size_t errsz) {
const char *p = text;
int sign = +1;
pixie_sparse_init(out_terms);
*out_const = 0.0;
while (*p != '\0') {
char *end_num = NULL;
double coef = 0.0;
bool has_num = false;
while (*p != '\0' && isspace((unsigned char)*p)) {
++p;
}
if (*p == '\0') {
break;
}
if (*p == '+') {
sign = +1;
++p;
continue;
}
if (*p == '-') {
sign = -1;
++p;
continue;
}
errno = 0;
coef = strtod(p, &end_num);
if (end_num != p) {
has_num = true;
if (errno == ERANGE) {
pixie_sparse_free(out_terms);
pixie_set_error(err, errsz, "numeric overflow while parsing expression");
return 0;
}
p = end_num;
while (*p != '\0' && isspace((unsigned char)*p)) {
++p;
}
if (*p == '*') {
++p;
while (*p != '\0' && isspace((unsigned char)*p)) {
++p;
}
}
}
if (pixie_is_name_start_char(*p)) {
const char *start = p;
char name_buf[256];
size_t name_len = 0;
int vidx;
while (*p != '\0' && pixie_is_name_char(*p)) {
++p;
}
name_len = (size_t)(p - start);
if (name_len == 0 || name_len >= sizeof(name_buf)) {
pixie_sparse_free(out_terms);
pixie_set_error(err, errsz, "invalid variable name in expression");
return 0;
}
memcpy(name_buf, start, name_len);
name_buf[name_len] = '\0';
vidx = pixie_model_get_var(m, name_buf, create_vars);
if (vidx < 0) {
pixie_sparse_free(out_terms);
pixie_set_error(err, errsz, "unknown variable in expression");
return 0;
}
if (!has_num) {
coef = 1.0;
}
pixie_sparse_add(out_terms, vidx, (double)sign * coef);
sign = +1;
continue;
}
if (has_num) {
*out_const += (double)sign * coef;
sign = +1;
continue;
}
{
char msg[256];
snprintf(msg, sizeof(msg), "cannot parse linear expression near '%.80s'", p);
pixie_sparse_free(out_terms);
pixie_set_error(err, errsz, msg);
}
return 0;
}
pixie_sparse_prune(out_terms, PIXIE_EPS);
return 1;
}
/* Portable fallback for systems without strtok_s. */
#ifndef _WIN32
#define strtok_s strtok_r
#endif
static int pixie_parse_lp_objective(PixieModel *m, char *stmt, char *err, size_t errsz) {
PixieSparse terms;
double cst = 0.0;
char *p = stmt;
char *colon = NULL;
int i;
p = pixie_trim(p);
colon = strchr(p, ':');
if (colon != NULL) {
*colon = '\0';
p = pixie_trim(colon + 1);
}
if (!pixie_parse_linear_expr(m, p, true, &terms, &cst, err, errsz)) {
return 0;
}
(void)cst;
for (i = 0; i < terms.len; ++i) {
int v = terms.idx[i];
m->vars[v].obj += terms.val[i];
}
pixie_sparse_free(&terms);
return 1;
}
static int pixie_parse_lp_constraint(PixieModel *m, char *stmt, char *err, size_t errsz) {
PixieCompare cmp = PIXIE_CMP_EQ;
char *buf = pixie_strdup_c(stmt);
char *lhs = NULL;
char *rhs = NULL;
char *p = NULL;
char *colon = NULL;
int clen = 0;
PixieSparse lterms;
PixieSparse rterms;
PixieSparse comb;
double lc = 0.0;
double rc = 0.0;
double nrhs = 0.0;
int i;
lhs = buf;
p = buf;
while (*p != '\0') {
if (*p == '<') {
cmp = PIXIE_CMP_LE;
clen = (p[1] == '=') ? 2 : 1;
break;
}
if (*p == '>') {
cmp = PIXIE_CMP_GE;
clen = (p[1] == '=') ? 2 : 1;
break;
}
if (*p == '=') {
cmp = PIXIE_CMP_EQ;
clen = 1;
break;
}
++p;
}
if (*p == '\0') {
free(buf);
pixie_set_error(err, errsz, "constraint has no comparator");
return 0;
}
*p = '\0';
rhs = p + clen;
lhs = pixie_trim(lhs);
rhs = pixie_trim(rhs);
colon = strchr(lhs, ':');
if (colon != NULL) {
lhs = pixie_trim(colon + 1);
}
if (strchr(lhs, '<') != NULL || strchr(lhs, '>') != NULL || strchr(lhs, '=') != NULL) {
char msg[256];
snprintf(msg, sizeof(msg), "internal split error lhs='%.120s'", lhs);
free(buf);
pixie_set_error(err, errsz, msg);
return 0;
}
if (!pixie_parse_linear_expr(m, lhs, true, <erms, &lc, err, errsz)) {
free(buf);
return 0;
}
if (!pixie_parse_linear_expr(m, rhs, true, &rterms, &rc, err, errsz)) {
pixie_sparse_free(<erms);
free(buf);
return 0;
}
pixie_sparse_init(&comb);
for (i = 0; i < lterms.len; ++i) {
pixie_sparse_add(&comb, lterms.idx[i], lterms.val[i]);
}
for (i = 0; i < rterms.len; ++i) {
pixie_sparse_add(&comb, rterms.idx[i], -rterms.val[i]);
}
nrhs = rc - lc;
pixie_sparse_prune(&comb, PIXIE_EPS);
if (!pixie_model_add_constraint(m, &comb, cmp, nrhs)) {
pixie_sparse_free(&comb);
pixie_sparse_free(<erms);
pixie_sparse_free(&rterms);
free(buf);
pixie_set_error(err, errsz, "failed to add LP constraint");
return 0;
}
pixie_sparse_free(&comb);
pixie_sparse_free(<erms);
pixie_sparse_free(&rterms);
free(buf);
return 1;
}
static void pixie_tokvec_init(PixieTokVec *tv) {
tv->data = NULL;
tv->len = 0;
tv->cap = 0;
}
static void pixie_tokvec_free(PixieTokVec *tv) {
int i;
for (i = 0; i < tv->len; ++i) {
free(tv->data[i]);
}
free(tv->data);
tv->data = NULL;
tv->len = 0;
tv->cap = 0;
}
static void pixie_tokvec_push(PixieTokVec *tv, const char *tok) {
int nc;
if (tv->len >= tv->cap) {
nc = (tv->cap <= 0) ? 8 : tv->cap;
while (nc <= tv->len) {
if (nc > INT_MAX / 2) {
nc = tv->len + 1;
break;
}
nc *= 2;
}
tv->data = (char **)pixie_xrealloc(tv->data, (size_t)nc * sizeof(char *));
tv->cap = nc;
}
tv->data[tv->len] = pixie_strdup_c(tok);
++tv->len;
}
static void pixie_tokenize_bounds(const char *stmt, PixieTokVec *tv) {
const char *p = stmt;
char buf[512];
pixie_tokvec_init(tv);
while (*p != '\0') {
size_t n = 0;
while (*p != '\0' && isspace((unsigned char)*p)) {
++p;
}
if (*p == '\0') {
break;
}
if (*p == '<' || *p == '>' || *p == '=') {
buf[n++] = *p;
if ((p[0] == '<' || p[0] == '>') && p[1] == '=') {
++p;
buf[n++] = *p;
}
++p;
buf[n] = '\0';
pixie_tokvec_push(tv, buf);
continue;
}
while (*p != '\0' && !isspace((unsigned char)*p) && *p != '<' && *p != '>' && *p != '=') {
if (n + 1 < sizeof(buf)) {
buf[n++] = *p;
}
++p;
}
buf[n] = '\0';
if (n > 0) {
pixie_tokvec_push(tv, buf);
}
}
}
static int pixie_lp_apply_bound_rel(PixieModel *m, const char *left, const char *op, const char *right, char *err, size_t errsz) {
double lv = 0.0;
double rv = 0.0;
bool left_num = pixie_parse_double_token(left, &lv);
bool right_num = pixie_parse_double_token(right, &rv);
int vidx = -1;
if (!left_num && right_num) {
vidx = pixie_model_get_var(m, left, true);
if (vidx < 0) {
pixie_set_error(err, errsz, "invalid bound variable");
return 0;
}
if (strcmp(op, "<=") == 0 || strcmp(op, "<") == 0) {
m->vars[vidx].ub = pixie_min(m->vars[vidx].ub, rv);
return 1;
}
if (strcmp(op, ">=") == 0 || strcmp(op, ">") == 0) {
m->vars[vidx].lb = pixie_max(m->vars[vidx].lb, rv);
return 1;
}
if (strcmp(op, "=") == 0) {
m->vars[vidx].lb = rv;
m->vars[vidx].ub = rv;
return 1;
}
pixie_set_error(err, errsz, "unsupported bounds operator");
return 0;
}
if (left_num && !right_num) {
vidx = pixie_model_get_var(m, right, true);
if (vidx < 0) {
pixie_set_error(err, errsz, "invalid bound variable");
return 0;
}
if (strcmp(op, "<=") == 0 || strcmp(op, "<") == 0) {
m->vars[vidx].lb = pixie_max(m->vars[vidx].lb, lv);
return 1;
}
if (strcmp(op, ">=") == 0 || strcmp(op, ">") == 0) {
m->vars[vidx].ub = pixie_min(m->vars[vidx].ub, lv);
return 1;
}
if (strcmp(op, "=") == 0) {
m->vars[vidx].lb = lv;
m->vars[vidx].ub = lv;
return 1;
}
pixie_set_error(err, errsz, "unsupported bounds operator");
return 0;
}
pixie_set_error(err, errsz, "bounds relation must be between one variable and one number");
return 0;
}