-
Notifications
You must be signed in to change notification settings - Fork 1.5k
/
maxsmt.cpp
420 lines (362 loc) · 12.9 KB
/
maxsmt.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
/*++
Copyright (c) 2013 Microsoft Corporation
Module Name:
maxsmt.cpp
Abstract:
MaxSMT optimization context.
Author:
Nikolaj Bjorner (nbjorner) 2013-11-7
Notes:
--*/
#include <typeinfo>
#include "util/uint_set.h"
#include "ast/ast_pp.h"
#include "ast/ast_util.h"
#include "ast/pb_decl_plugin.h"
#include "opt/maxsmt.h"
#include "opt/maxcore.h"
#include "opt/maxlex.h"
#include "opt/wmax.h"
#include "opt/opt_params.hpp"
#include "opt/opt_context.h"
#include "opt/opt_preprocess.h"
#include "smt/theory_wmaxsat.h"
#include "smt/theory_pb.h"
namespace opt {
maxsmt_solver_base::maxsmt_solver_base(maxsat_context& c, vector<soft>& s, unsigned index):
m(c.get_manager()),
m_c(c),
m_index(index),
m_soft(s),
m_assertions(m),
m_trail(m) {
c.get_base_model(m_model);
SASSERT(m_model);
updt_params(c.params());
}
void maxsmt_solver_base::updt_params(params_ref& p) {
m_params.copy(p);
}
void maxsmt_solver_base::reset_upper() {
m_upper = m_lower;
for (soft& s : m_soft)
m_upper += s.weight;
}
solver& maxsmt_solver_base::s() {
return m_c.get_solver();
}
void maxsmt_solver_base::commit_assignment() {
expr_ref tmp(m);
expr_ref_vector fmls(m);
rational k(0), cost(0);
vector<rational> weights;
for (soft const& s : m_soft) {
if (s.is_true()) {
k += s.weight;
}
else {
cost += s.weight;
}
weights.push_back(s.weight);
fmls.push_back(s.s);
}
pb_util pb(m);
tmp = pb.mk_ge(weights.size(), weights.data(), fmls.data(), k);
TRACE("opt", tout << "cost: " << cost << "\n" << tmp << "\n";);
s().assert_expr(tmp);
}
bool maxsmt_solver_base::init() {
m_lower.reset();
m_upper.reset();
for (soft& s : m_soft) {
s.set_value(m.is_true(s.s));
if (!s.is_true())
m_upper += s.weight;
}
// return true;
preprocess pp(s());
rational lower(0);
bool r = pp(m_soft, lower);
m_c.add_offset(m_index, lower);
m_upper -= lower;
TRACE("opt",
tout << "lower " << lower << " upper: " << m_upper << " assignments: ";
for (soft& s : m_soft) tout << (s.is_true()?"T":"F");
tout << "\n";);
return r;
}
void maxsmt_solver_base::set_mus(bool f) {
params_ref p;
p.set_bool("minimize_core", f);
// p.set_bool("minimize_core_partial", f);
s().updt_params(p);
}
void maxsmt_solver_base::enable_sls(bool force) {
m_c.enable_sls(force);
}
app* maxsmt_solver_base::mk_fresh_bool(char const* name) {
app* result = m.mk_fresh_const(name, m.mk_bool_sort());
m_c.fm().hide(result);
return result;
}
smt::theory_wmaxsat* maxsmt_solver_base::get_wmax_theory() const {
smt::theory_id th_id = m.get_family_id("weighted_maxsat");
smt::theory* th = m_c.smt_context().get_theory(th_id);
if (th) {
return dynamic_cast<smt::theory_wmaxsat*>(th);
}
else {
return nullptr;
}
}
smt::theory_wmaxsat* maxsmt_solver_base::ensure_wmax_theory() {
smt::theory_wmaxsat* wth = get_wmax_theory();
if (wth) {
wth->reset_local();
}
else {
wth = alloc(smt::theory_wmaxsat, m_c.smt_context(), m, m_c.fm());
m_c.smt_context().register_plugin(wth);
}
smt::theory_id th_pb = m.get_family_id("pb");
smt::theory_pb* pb = dynamic_cast<smt::theory_pb*>(m_c.smt_context().get_theory(th_pb));
if (!pb) {
theory_pb_params params;
pb = alloc(smt::theory_pb, m_c.smt_context());
m_c.smt_context().register_plugin(pb);
}
return wth;
}
maxsmt_solver_base::scoped_ensure_theory::scoped_ensure_theory(maxsmt_solver_base& s) {
m_wth = s.ensure_wmax_theory();
}
maxsmt_solver_base::scoped_ensure_theory::~scoped_ensure_theory() {
if (m_wth) {
m_wth->reset_local();
}
}
smt::theory_wmaxsat& maxsmt_solver_base::scoped_ensure_theory::operator()() { return *m_wth; }
void maxsmt_solver_base::trace_bounds(char const * solver) {
IF_VERBOSE(1,
rational l = m_c.adjust(m_index, m_lower);
rational u = m_c.adjust(m_index, m_upper);
if (l > u) std::swap(l, u);
verbose_stream() << "(opt." << solver << " [" << l << ":" << u << "])\n";);
}
maxsmt::maxsmt(maxsat_context& c, unsigned index):
m(c.get_manager()), m_c(c), m_index(index), m_answer(m) {}
lbool maxsmt::operator()(bool committed) {
lbool is_sat = l_undef;
m_msolver = nullptr;
opt_params optp(m_params);
symbol const& maxsat_engine = m_c.maxsat_engine();
IF_VERBOSE(1, verbose_stream() << "(maxsmt)\n";);
TRACE("opt_verbose", s().display(tout << "maxsmt\n") << "\n";);
if (!committed && optp.maxlex_enable() && is_maxlex(m_soft))
m_msolver = mk_maxlex(m_c, m_index, m_soft);
else if (m_soft.empty() || maxsat_engine == symbol("maxres") || maxsat_engine == symbol::null)
m_msolver = mk_maxres(m_c, m_index, m_soft);
else if (maxsat_engine == symbol("maxres-bin"))
m_msolver = mk_maxres_binary(m_c, m_index, m_soft);
else if (maxsat_engine == symbol("rc2"))
m_msolver = mk_rc2(m_c, m_index, m_soft);
else if (maxsat_engine == symbol("rc2bin"))
m_msolver = mk_rc2bin(m_c, m_index, m_soft);
else if (maxsat_engine == symbol("pd-maxres"))
m_msolver = mk_primal_dual_maxres(m_c, m_index, m_soft);
else if (maxsat_engine == symbol("wmax"))
m_msolver = mk_wmax(m_c, m_soft, m_index);
else if (maxsat_engine == symbol("sortmax"))
m_msolver = mk_sortmax(m_c, m_soft, m_index);
else {
auto str = maxsat_engine.str();
warning_msg("solver %s is not recognized, using default 'maxres'", str.c_str());
m_msolver = mk_maxres(m_c, m_index, m_soft);
}
if (m_msolver) {
m_msolver->updt_params(m_params);
is_sat = l_undef;
try {
is_sat = (*m_msolver)();
}
catch (z3_exception& ex) {
IF_VERBOSE(1, verbose_stream() << ex.what() << "\n");
is_sat = l_undef;
}
if (is_sat != l_false) {
m_msolver->get_model(m_model, m_labels);
}
}
IF_VERBOSE(5, verbose_stream() << "is-sat: " << is_sat << "\n";
if (is_sat == l_true) {
verbose_stream() << "Satisfying soft constraints\n";
display_answer(verbose_stream());
});
DEBUG_CODE(if (is_sat == l_true) verify_assignment(););
return is_sat;
}
void maxsmt::reset_upper() {
if (m_msolver) {
m_msolver->reset_upper();
m_upper = m_msolver->get_upper();
}
}
void maxsmt::verify_assignment() {
// TBD: have to use a different solver
// because we don't push local scope any longer.
return;
}
bool maxsmt::get_assignment(unsigned idx) const {
if (m_msolver) {
return m_msolver->get_assignment(idx);
}
else {
return true;
}
}
rational maxsmt::get_lower() const {
rational r = m_lower;
if (m_msolver) {
rational q = m_msolver->get_lower();
if (q > r) r = q;
}
return m_c.adjust(m_index, r);
}
rational maxsmt::get_upper() const {
rational r = m_upper;
if (m_msolver) {
rational q = m_msolver->get_upper();
if (q < r) r = q;
}
return m_c.adjust(m_index, r);
}
void maxsmt::update_lower(rational const& r) {
m_lower = r;
}
void maxsmt::update_upper(rational const& r) {
m_upper = r;
}
void maxsmt::get_model(model_ref& mdl, svector<symbol>& labels) {
mdl = m_model.get();
labels = m_labels;
}
void maxsmt::commit_assignment() {
if (m_msolver) {
m_msolver->commit_assignment();
}
}
void maxsmt::add(expr* f, rational const& w) {
TRACE("opt", tout << mk_pp(f, m) << " weight: " << w << "\n";);
SASSERT(m.is_bool(f));
SASSERT(w.is_pos());
unsigned index = 0;
if (m_soft_constraint_index.find(f, index)) {
m_soft[index].weight += w;
}
else {
m_soft_constraint_index.insert(f, m_soft.size());
m_soft.push_back(soft(expr_ref(f, m), w, false));
}
m_upper += w;
}
struct cmp_first {
bool operator()(std::pair<unsigned, rational> const& x, std::pair<unsigned, rational> const& y) const {
return x.second < y.second;
}
};
void maxsmt::display_answer(std::ostream& out) const {
unsigned idx = 0;
for (auto const & [_e, w, t] : m_soft) {
expr* e = _e.get();
bool is_not = m.is_not(e, e);
out << w << ": " << mk_pp(e, m)
<< ((is_not != get_assignment(idx))?" |-> true ":" |-> false ")
<< "\n";
++idx;
}
}
bool maxsmt::is_maxsat_problem(vector<rational> const& ws) const {
for (unsigned i = 0; i < ws.size(); ++i) {
if (!ws[i].is_one()) {
return false;
}
}
return true;
}
void maxsmt::updt_params(params_ref& p) {
m_params.append(p);
if (m_msolver)
m_msolver->updt_params(p);
}
void maxsmt::collect_statistics(statistics& st) const {
if (m_msolver)
m_msolver->collect_statistics(st);
}
solver& maxsmt::s() {
return m_c.get_solver();
}
void maxsmt::model_updated(model* mdl) {
m_c.model_updated(mdl);
}
class solver_maxsat_context : public maxsat_context {
params_ref m_params;
solver_ref m_solver;
model_ref m_model;
ref<generic_model_converter> m_fm;
symbol m_maxsat_engine;
vector<rational> m_offsets;
public:
solver_maxsat_context(params_ref& p, solver* s, model * m):
m_params(p),
m_solver(s),
m_model(m),
m_fm(alloc(generic_model_converter, s->get_manager(), "maxsmt")) {
opt_params _p(p);
m_maxsat_engine = _p.maxsat_engine();
}
generic_model_converter& fm() override { return *m_fm.get(); }
bool sat_enabled() const override { return false; }
solver& get_solver() override { return *m_solver.get(); }
ast_manager& get_manager() const override { return m_solver->get_manager(); }
params_ref& params() override { return m_params; }
void enable_sls(bool force) override { } // no op
symbol const& maxsat_engine() const override { return m_maxsat_engine; }
void get_base_model(model_ref& _m) override { _m = m_model; };
smt::context& smt_context() override {
throw default_exception("stand-alone maxsat context does not support wmax");
}
unsigned num_objectives() override { return 1; }
bool verify_model(unsigned id, model* mdl, rational const& v) override { return true; };
void set_model(model_ref& _m) override { m_model = _m; }
void model_updated(model* mdl) override { } // no-op
rational adjust(unsigned id, rational const& r) override {
m_offsets.reserve(id+1);
return r + m_offsets[id];
}
void add_offset(unsigned id, rational const& r) override {
m_offsets.reserve(id+1);
m_offsets[id] += r;
}
};
lbool maxsmt_wrapper::operator()(vector<std::pair<expr*,rational>>& soft) {
solver_maxsat_context ctx(m_params, m_solver.get(), m_model.get());
maxsmt maxsmt(ctx, 0);
for (auto const& p : soft) {
maxsmt.add(p.first, p.second);
}
lbool r = maxsmt(true);
if (r == l_true) {
svector<symbol> labels;
maxsmt.get_model(m_model, labels);
// TBD: is m_fm applied or not?
unsigned j = 0;
for (auto const& p : soft) {
if (m_model->is_true(p.first)) {
soft[j++] = p;
}
}
soft.shrink(j);
}
return r;
}
};