-
Notifications
You must be signed in to change notification settings - Fork 1.5k
/
opt_parse.cpp
892 lines (796 loc) · 24.3 KB
/
opt_parse.cpp
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
/*++
Copyright (c) 2017 Microsoft Corporation
Module Name:
opt_parse.cpp
Abstract:
Parse utilities for optimization.
Author:
Nikolaj Bjorner (nbjorner) 2017-11-19
Revision History:
--*/
#include "opt/opt_context.h"
#include "opt/opt_parse.h"
#include <iostream>
class opt_stream_buffer {
std::istream & m_stream;
int m_val;
unsigned m_line;
public:
opt_stream_buffer(std::istream & s):
m_stream(s),
m_line(0) {
m_val = m_stream.get();
}
int operator *() const { return m_val;}
void operator ++() { m_val = m_stream.get(); }
int ch() const { return m_val; }
void next() { m_val = m_stream.get(); }
bool eof() const { return ch() == EOF; }
unsigned line() const { return m_line; }
void skip_whitespace() {
while ((ch() >= 9 && ch() <= 13) || ch() == 32) {
if (ch() == 10) ++m_line;
next();
}
}
void skip_space() {
while (ch() != 10 && ((ch() >= 9 && ch() <= 13) || ch() == 32))
next();
}
void skip_line() {
while(true) {
if (eof()) {
return;
}
if (ch() == '\n') {
++m_line;
next();
return;
}
next();
}
}
bool parse_token(char const* token);
int parse_int();
unsigned parse_unsigned();
};
bool opt_stream_buffer::parse_token(char const* token) {
skip_whitespace();
char const* t = token;
while (*t && ch() && ch() == *t) {
next();
++t;
}
return 0 == *t;
}
unsigned opt_stream_buffer::parse_unsigned() {
skip_space();
if (ch() == '\n') {
return UINT_MAX;
}
unsigned val = 0;
while (ch() >= '0' && ch() <= '9') {
val = val*10 + (ch() - '0');
next();
}
return val;
}
int opt_stream_buffer::parse_int() {
int val = 0;
bool neg = false;
skip_whitespace();
if (ch() == '-') {
neg = true;
next();
}
else if (ch() == '+') {
next();
}
if (ch() < '0' || ch() > '9') {
std::cerr << "(error line " << line() << " \"unexpected char: " << ((char)ch()) << "\" )\n";
exit(3);
}
while (ch() >= '0' && ch() <= '9') {
val = val*10 + (ch() - '0');
next();
}
return neg ? -val : val;
}
class wcnf {
opt::context& opt;
ast_manager& m;
opt_stream_buffer& in;
unsigned_vector& m_handles;
app_ref read_clause(unsigned& weight) {
int parsed_lit;
int var;
weight = in.parse_unsigned();
app_ref result(m), p(m);
expr_ref_vector ors(m);
while (true) {
parsed_lit = in.parse_int();
if (parsed_lit == 0)
break;
var = abs(parsed_lit);
p = m.mk_const(symbol((unsigned)var), m.mk_bool_sort());
if (parsed_lit < 0) p = m.mk_not(p);
ors.push_back(p);
}
result = to_app(mk_or(m, ors.size(), ors.data()));
return result;
}
void parse_spec(unsigned& num_vars, unsigned& num_clauses, unsigned& max_weight) {
in.parse_token("wcnf");
num_vars = in.parse_unsigned();
num_clauses = in.parse_unsigned();
max_weight = in.parse_unsigned();
}
public:
wcnf(opt::context& opt, opt_stream_buffer& in, unsigned_vector& h): opt(opt), m(opt.get_manager()), in(in), m_handles(h) {
opt.set_clausal(true);
}
void parse() {
unsigned num_vars = 0, num_clauses = 0, max_weight = 0;
while (true) {
in.skip_whitespace();
if (in.eof()) {
break;
}
else if (*in == 'c') {
in.skip_line();
}
else if (*in == 'p') {
++in;
parse_spec(num_vars, num_clauses, max_weight);
}
else {
unsigned weight = 0;
app_ref cls = read_clause(weight);
if (weight >= max_weight) {
opt.add_hard_constraint(cls);
}
else {
unsigned id = opt.add_soft_constraint(cls, rational(weight), symbol::null);
if (m_handles.empty()) {
m_handles.push_back(id);
}
}
}
}
}
};
class opb {
opt::context& opt;
ast_manager& m;
opt_stream_buffer& in;
unsigned_vector& m_handles;
arith_util arith;
app_ref parse_id() {
bool negated = in.parse_token("~");
if (!in.parse_token("x")) {
std::cerr << "(error line " << in.line() << " \"unexpected char: " << ((char)in.ch()) << "\" expected \"x\")\n";
exit(3);
}
app_ref p(m);
int id = in.parse_int();
p = m.mk_const(symbol((unsigned)id), m.mk_bool_sort());
if (negated) p = m.mk_not(p);
in.skip_whitespace();
return p;
}
app_ref parse_ids() {
app_ref result = parse_id();
while (*in == '~' || *in == 'x') {
result = m.mk_and(result, parse_id());
}
return result;
}
rational parse_coeff_r() {
in.skip_whitespace();
svector<char> num;
bool pos = true;
if (*in == '-') pos = false, ++in;
if (*in == '+') ++in;
if (!pos) num.push_back('-');
in.skip_whitespace();
while ('0' <= *in && *in <='9') num.push_back(*in), ++in;
num.push_back(0);
return rational(num.data());
}
app_ref parse_coeff() {
return app_ref(arith.mk_numeral(parse_coeff_r(), true), m);
}
app_ref parse_term() {
app_ref c = parse_coeff();
app_ref e = parse_ids();
return app_ref(m.mk_ite(e, c, arith.mk_numeral(rational(0), true)), m);
}
void parse_objective(bool is_min) {
app_ref t = parse_term();
while (!in.parse_token(";") && !in.eof()) {
if (is_min) {
t = arith.mk_add(t, parse_term());
}
else {
t = arith.mk_sub(t, parse_term());
}
}
m_handles.push_back(opt.add_objective(t, false));
}
void parse_constraint() {
app_ref t = parse_term();
while (!in.eof()) {
if (in.parse_token(">=")) {
t = arith.mk_ge(t, parse_coeff());
in.parse_token(";");
break;
}
if (in.parse_token("=")) {
t = m.mk_eq(t, parse_coeff());
in.parse_token(";");
break;
}
if (in.parse_token("<=")) {
t = arith.mk_le(t, parse_coeff());
in.parse_token(";");
break;
}
t = arith.mk_add(t, parse_term());
}
opt.add_hard_constraint(t);
}
public:
opb(opt::context& opt, opt_stream_buffer& in, unsigned_vector& h):
opt(opt), m(opt.get_manager()),
in(in), m_handles(h), arith(m) {}
void parse() {
while (true) {
in.skip_whitespace();
if (in.eof()) {
break;
}
else if (*in == '*') {
in.skip_line();
}
else if (in.parse_token("min:")) {
parse_objective(true);
}
else if (in.parse_token("max:")) {
parse_objective(false);
}
else {
parse_constraint();
}
}
}
};
void parse_wcnf(opt::context& opt, std::istream& is, unsigned_vector& h) {
opt_stream_buffer _is(is);
wcnf w(opt, _is, h);
w.parse();
}
void parse_opb(opt::context& opt, std::istream& is, unsigned_vector& h) {
opt_stream_buffer _is(is);
opb opb(opt, _is, h);
opb.parse();
}
/**
* \brief Parser for a modest subset of the CPLEX LP format.
* Reference: http://eaton.math.rpi.edu/cplex90html/reffileformatscplex/reffileformatscplex5.html
*/
struct asymbol {
bool m_is_num;
symbol m_sym;
rational m_num;
unsigned m_line;
asymbol(symbol const& s, unsigned l): m_is_num(false), m_sym(s), m_line(l) {}
asymbol(rational const& r, unsigned l): m_is_num(true), m_num(r), m_line(l) {}
};
std::ostream& operator<<(std::ostream& out, asymbol const& c) {
if (c.m_is_num) {
return out << c.m_num;
}
else {
return out << c.m_sym;
}
}
class lp_tokenizer {
vector<asymbol> m_tokens;
unsigned m_pos;
svector<char> m_buffer;
public:
lp_tokenizer(opt_stream_buffer& in):
m_pos(0)
{
parse_all(in);
}
symbol const& peek(unsigned i) {
if (i + m_pos >= m_tokens.size()) {
return symbol::null;
}
return m_tokens[i + m_pos].m_sym;
}
bool peek_num(unsigned i) {
if (i + m_pos >= m_tokens.size()) {
return false;
}
return m_tokens[i + m_pos].m_is_num;
}
rational const& get_num(unsigned i) {
return m_tokens[i + m_pos].m_num;
}
void next(unsigned delta = 1) {
m_pos += delta;
}
bool eof() const {
return m_pos == m_tokens.size();
}
unsigned line() const {
if (m_pos < m_tokens.size()) return m_tokens[m_pos].m_line;
return 0;
}
private:
bool is_separator(char c) {
return c == '\n' || c == '\\' || c == '*' || c == '+';
}
char lower(char c) {
if ('A' <= c && c <= 'Z')
return c - ('A' - 'a');
return c;
}
void parse_all(opt_stream_buffer& in) {
while (!in.eof()) {
in.skip_whitespace();
char c = in.ch();
if (c == '\\') {
in.skip_line();
continue;
}
bool neg = false;
if (c == '-') {
in.next();
c = in.ch();
m_buffer.reset();
m_buffer.push_back('-');
if (is_num(c)) {
neg = true;
}
else {
while (!is_ws(c) && !in.eof()) {
m_buffer.push_back(c);
in.next();
c = in.ch();
}
m_buffer.push_back(0);
m_tokens.push_back(asymbol(symbol(m_buffer.data()), in.line()));
IF_VERBOSE(10, verbose_stream() << "tok: " << m_tokens.back() << "\n");
continue;
}
}
if (is_num(c)) {
rational n(0);
rational div(1);
while (is_num(c) && !in.eof()) {
n = n*rational(10) + rational(c - '0');
in.next();
c = in.ch();
}
if (c == '.') {
in.next();
c = in.ch();
while (is_num(c) && !in.eof()) {
n = n*rational(10) + rational(c - '0');
in.next();
div *= rational(10);
c = in.ch();
}
}
if (div > rational(1)) n = n / div;
if (neg) n.neg();
m_tokens.push_back(asymbol(n, in.line()));
IF_VERBOSE(10, verbose_stream() << "num: " << m_tokens.back() << "\n");
continue;
}
m_buffer.reset();
if (is_alpha(c)) {
while (is_sym(c) && !in.eof()) {
m_buffer.push_back(lower(c));
in.next();
c = in.ch();
}
}
else {
while (!is_ws(c) && !in.eof()) {
m_buffer.push_back(c);
in.next();
c = in.ch();
}
}
m_buffer.push_back(0);
m_tokens.push_back(asymbol(symbol(m_buffer.data()), in.line()));
IF_VERBOSE(10, verbose_stream() << "tok: " << m_tokens.back() << "\n");
}
}
bool is_alpha(char c) const {
return ('a' <= c && c <= 'z') || ('A' <= c && c <= 'Z');
}
bool is_num(char c) const {
return '0' <= c && c <= '9';
}
bool is_ws(char c) const {
return c == ' ' || c == '\n' || c == '\t';
}
bool is_sym(char c) const {
return
is_alpha(c) ||
is_num(c) ||
c == '!' ||
c == '"' ||
c == '-' ||
c == '#' ||
c == '$' ||
c == '%' ||
c == '&' ||
c == '{' ||
c == '}' ||
c == ',' ||
c == '_' ||
c == '.' ||
c == ';' ||
c == '?' ||
c == '@' ||
c == '`' ||
c == '\'' ||
c == '(' ||
c == ')' ||
c == '~';
}
};
class lp_parse {
typedef vector<std::pair<rational, symbol> > lin_term;
struct objective {
bool m_is_max;
symbol m_name;
lin_term m_expr;
};
enum rel_op {
le, ge, eq
};
struct constraint {
symbol m_name;
symbol m_bvar;
rational m_bval;
lin_term m_expr;
rel_op m_rel;
rational m_bound;
constraint(symbol const& name, symbol const& v, rational const& val, lin_term& terms, rel_op r, rational const& bound):
m_name(name), m_bvar(v), m_bval(val), m_expr(terms), m_rel(r), m_bound(bound) {}
};
struct bound {
optional<rational> m_lo, m_hi;
bool m_int;
bound() : m_int(false) {}
};
opt::context& opt;
unsigned_vector& m_h;
lp_tokenizer tok;
objective m_objective;
vector<constraint> m_constraints;
map<symbol, bound, symbol_hash_proc, symbol_eq_proc> m_bounds;
public:
lp_parse(opt::context& opt, opt_stream_buffer& in, unsigned_vector& h) :
opt(opt), m_h(h), tok(in) {}
void parse() {
parse_objective();
if (!try_subject_to()) {
error("subject to section expected");
return;
}
while (!is_section()) {
parse_constraint();
}
while (true) {
if (is_bounds()) {
tok.next();
while (!is_section()) {
parse_bound();
}
}
else if (is_binary()) {
tok.next();
while (!is_section()) {
parse_binary();
}
}
else if (is_general()) {
tok.next();
while (!is_section()) {
parse_general();
}
}
else {
break;
}
}
post_process();
}
private:
void error(char const* msg) {
std::ostringstream ous;
ous << tok.line() << ": " << msg << " got: " << peek(0) << "\n";
throw default_exception(ous.str());
}
symbol const& peek(unsigned i) { return tok.peek(i); }
bool try_accept(char const * token) {
if (peek(0) == token) {
tok.next();
return true;
}
return false;
}
void parse_objective() {
m_objective.m_is_max = minmax();
if (peek(1) == ":") {
m_objective.m_name = peek(0);
tok.next(2);
}
parse_expr(m_objective.m_expr);
}
bool minmax() {
if (try_accept("minimize"))
return false;
if (try_accept("min"))
return false;
if (try_accept("maximize"))
return true;
if (try_accept("max"))
return true;
error("expected min or max objective");
return false;
}
void parse_constraint() {
symbol name;
if (peek(1) == ":") {
name = peek(0);
tok.next(2);
}
IF_VERBOSE(10, verbose_stream() << name << "\n");
rational val(0);
symbol var;
parse_indicator(var, val);
lin_term terms;
parse_expr(terms);
rel_op op = parse_relation();
rational rhs = tok.get_num(0);
tok.next();
m_constraints.push_back(constraint(name, var, val, terms, op, rhs));
}
void parse_expr(lin_term& terms) {
if (is_relation()) {
return;
}
bool pos = true;
if (peek(0) == "-") {
pos = false;
tok.next();
}
while (peek(0) == "+") {
tok.next();
}
terms.push_back(parse_term());
if (!pos) terms.back().first = -terms.back().first;
while (peek(0) == "+" || peek(0) == "-") {
bool pos = peek(0) == "+";
tok.next();
terms.push_back(parse_term());
if (!pos) terms.back().first = -terms.back().first;
}
}
std::pair<rational, symbol> parse_term() {
std::pair<rational, symbol> r(rational::one(), peek(0));
if (tok.peek_num(0)) {
r.first = tok.get_num(0);
r.second = peek(1);
tok.next(2);
}
else {
tok.next(1);
}
return r;
}
rel_op parse_relation() {
if (try_accept("<=")) return le;
if (try_accept("=<")) return le;
if (try_accept(">=")) return ge;
if (try_accept("=>")) return ge;
if (try_accept("=")) return eq;
error("expected relation");
return eq;
}
bool peek_le(unsigned pos) {
return peek(pos) == "<=" || peek(pos) == "=<";
}
bool peek_minus_infty_long(unsigned pos) {
return peek(pos) == "-" && (peek(pos+1) == "inf" || peek(pos+1) == "infinity");
}
bool peek_minus_infty_short(unsigned pos) {
return peek(pos) == "-inf" || peek(pos) == "-infinity";
}
bool peek_plus_infty_long(unsigned pos) {
return peek(pos) == "+" && (peek(pos+1) == "inf" || peek(pos+1) == "infinity");
}
bool peek_plus_infty_short(unsigned pos) {
return peek(pos) == "+inf" || peek(pos) == "+infinity";
}
void parse_indicator(symbol& var, rational& val) {
if (peek(1) == "=" && tok.peek_num(2) && peek(3) == "->") {
var = peek(0);
val = tok.get_num(2);
tok.next(4);
}
}
bool try_subject_to() {
if (try_accept("subject") && try_accept("to")) return true;
if (try_accept("such") && try_accept("that")) return true;
if (try_accept("st")) return true;
if (try_accept("s.t.")) return true;
return false;
}
bool is_relation() { return peek(0) == "=" || peek(0) == "=<" || peek(0) == ">=" || peek(0) == "=>" || peek(0) == "<="; }
bool is_section() { return is_general() || is_binary() || is_bounds() || is_end();}
bool is_bounds() { return peek(0) == "bounds"; }
bool is_general() { return peek(0) == "general" || peek(0) == "gen" || peek(0) == "generals"; }
bool is_binary() { return peek(0) == "binary" || peek(0) == "binaries" || peek(0) == "bin"; }
bool is_end() { return peek(0) == "end" || tok.eof(); }
// lo <= x
// x <= hi
// lo <= x <= hi
//
void parse_bound() {
symbol v;
if (peek_le(1) && tok.peek_num(0)) {
rational lhs = tok.get_num(0);
v = peek(2);
update_lower(lhs, v);
tok.next(3);
parse_upper(v);
}
else if (peek_minus_infty_long(0) && peek_le(2)) {
v = peek(3);
tok.next(4);
parse_upper(v);
}
else if (peek_minus_infty_short(0) && peek_le(1)) {
v = peek(2);
tok.next(3);
parse_upper(v);
}
else if (peek_plus_infty_long(2) && peek_le(1)) {
tok.next(4);
}
else if (peek_plus_infty_short(2) && peek_le(1)) {
tok.next(3);
}
else if (peek_le(1) && tok.peek_num(2)) {
v = peek(0);
tok.next(2);
rational rhs = tok.get_num(0);
update_upper(v, rhs);
tok.next(1);
}
else {
error("bound expected");
}
}
void parse_upper(symbol const& v) {
if (peek_le(0) && tok.peek_num(1)) {
rational rhs = tok.get_num(1);
update_upper(v, rhs);
tok.next(2);
}
else if (peek_le(0) && peek_plus_infty_long(1)) {
tok.next(3);
}
else if (peek_le(0) && peek_plus_infty_short(1)) {
tok.next(2); }
}
void update_lower(rational const& r, symbol const& v) {
bound b;
m_bounds.find(v, b);
b.m_lo = r;
m_bounds.insert(v, b);
}
void update_upper(symbol const& v, rational const& r) {
bound b;
if (!m_bounds.find(v, b)) {
// set the lower bound to default 0
b.m_lo = rational::zero();
}
b.m_hi = r;
m_bounds.insert(v, b);
}
void parse_binary() {
symbol const& v = peek(0);
update_lower(rational::zero(), v);
update_upper(v, rational::one());
m_bounds[v].m_int = true;
tok.next();
}
void parse_general() {
if (peek(1) == ":" && peek(3) == "=") {
symbol const& v = peek(2);
std::cout << "TBD: " << v << "\n";
return;
}
symbol const& v = peek(0);
bound b;
m_bounds.find(v, b);
b.m_int = true;
m_bounds.insert(v, b);
tok.next();
}
void post_process() {
ast_manager& m = opt.get_manager();
arith_util a(m);
for (constraint const& c : m_constraints) {
expr_ref fml(m);
expr_ref term = process_terms(c.m_expr);
bool is_int = a.is_int(term) && c.m_bound.is_int();
switch (c.m_rel) {
case le: fml = a.mk_le(term, a.mk_numeral(c.m_bound, is_int)); break;
case ge: fml = a.mk_ge(term, a.mk_numeral(c.m_bound, is_int)); break;
case eq: fml = m.mk_eq(term, a.mk_numeral(c.m_bound, is_int)); break;
}
if (c.m_bvar != symbol::null) {
term = mk_var(c.m_bvar);
bool is_int = c.m_bval.is_int() && a.is_int(term);
term = m.mk_eq(mk_var(c.m_bvar), a.mk_numeral(c.m_bval, is_int));
fml = m.mk_implies(term, fml);
}
opt.add_hard_constraint(fml);
}
for (auto const& kv : m_bounds) {
bound const& b = kv.m_value;
expr_ref term = mk_var(kv.m_key);
if (b.m_lo) {
bool is_int = b.m_lo->is_int() && a.is_int(term);
opt.add_hard_constraint(a.mk_le(a.mk_numeral(*b.m_lo, is_int), term));
}
if (b.m_hi) {
bool is_int = b.m_hi->is_int() && a.is_int(term);
opt.add_hard_constraint(a.mk_le(term, a.mk_numeral(*b.m_hi, is_int)));
}
}
expr_ref term = process_terms(m_objective.m_expr);
m_h.push_back(opt.add_objective(to_app(term), m_objective.m_is_max));
}
expr_ref process_terms(lin_term const& terms) {
ast_manager& m = opt.get_manager();
arith_util a(m);
expr_ref_vector result(m);
for (auto const& kv : terms) {
expr_ref term = mk_var(kv.second);
if (!kv.first.is_one()) {
bool is_int = kv.first.is_int() && a.is_int(term);
term = a.mk_mul(a.mk_numeral(kv.first, is_int), term);
}
result.push_back(term);
}
return expr_ref(a.mk_add(result.size(), result.data()), m);
}
expr_ref mk_var(symbol const& v) {
ast_manager& m = opt.get_manager();
arith_util a(m);
bound b;
if (!m_bounds.find(v, b)) {
b.m_lo = rational::zero();
m_bounds.insert(v, b);
}
return expr_ref(m.mk_const(v, b.m_int ? a.mk_int() : a.mk_real()), m);
}
};
void parse_lp(opt::context& opt, std::istream& is, unsigned_vector& h) {
opt_stream_buffer _is(is);
lp_parse lp(opt, _is, h);
lp.parse();
}