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
/*++
Copyright (c) 2019 Microsoft Corporation
Module Name:
maxlex.cpp
Abstract:
MaxLex solves weighted max-sat problems where weights impose lexicographic order.
MaxSAT is particularly easy for this class:
In order of highest weight, check if soft constraint can be satisfied.
If so, assert it, otherwise assert the negation and record whether the soft
constraint is true or false in the solution.
Author:
Nikolaj Bjorner (nbjorner) 2019-25-1
--*/
#include "ast/ast_pp.h"
#include "opt/opt_context.h"
#include "opt/maxsmt.h"
#include "opt/maxlex.h"
#include <tuple>
namespace opt {
bool is_maxlex(vector<soft> const & _ws) {
vector<soft> ws(_ws);
std::sort(ws.begin(), ws.end(), [&](soft const& s1, soft const& s2) { return s1.weight < s2.weight; });
ws.reverse();
rational sum(0);
for (auto const& [e, w, t] : ws) {
sum += w;
}
for (auto const& [e, w, t] : ws) {
if (sum > w + w) return false;
sum -= w;
}
return true;
}
class maxlex : public maxsmt_solver_base {
struct cmp_soft {
bool operator()(soft const& s1, soft const& s2) const {
return std::tuple<rational const&, unsigned>{s1.weight, s1.s->get_id()} >
std::tuple<rational const&, unsigned>{s2.weight, s2.s->get_id()};
}
};
ast_manager& m;
maxsat_context& m_c;
bool update_assignment() {
model_ref mdl;
s().get_model(mdl);
if (mdl) {
m_model = mdl;
m_c.model_updated(mdl.get());
update_assignment(mdl);
}
return mdl.get() != nullptr;
}
void assert_value(soft& soft) {
switch (soft.value) {
case l_true:
s().assert_expr(soft.s);
break;
case l_false:
s().assert_expr(expr_ref(m.mk_not(soft.s), m));
break;
default:
break;
}
}
void update_assignment(model_ref & mdl) {
mdl->set_model_completion(true);
bool first_undef = true, second_undef = false;
for (auto & soft : m_soft) {
if (first_undef && soft.value != l_undef) {
continue;
}
else if (first_undef) {
SASSERT(soft.value == l_undef);
soft.set_value(l_true);
assert_value(soft);
first_undef = false;
}
else if (soft.value != l_false) {
lbool v = mdl->is_true(soft.s) ? l_true : l_undef;
if (v == l_undef) {
second_undef = true;
}
if (second_undef) {
soft.set_value(v);
}
else {
SASSERT(v == l_true);
soft.set_value(v);
assert_value(soft);
}
}
}
update_bounds();
}
void update_bounds() {
m_lower.reset();
m_upper.reset();
for (auto & soft : m_soft) {
switch (soft.value) {
case l_undef:
m_upper += soft.weight;
break;
case l_true:
break;
case l_false:
m_lower += soft.weight;
m_upper += soft.weight;
break;
}
}
trace_bounds("maxlex");
}
void init() {
for (auto & soft : m_soft) {
soft.set_value(l_undef);
}
model_ref mdl;
s().get_model(mdl);
if (mdl) {
TRACE(opt, tout << *mdl << "\n";);
for (auto & soft : m_soft) {
if (!mdl->is_true(soft.s)) {
update_bounds();
return;
}
soft.set_value(l_true);
assert_value(soft);
}
update_assignment();
}
}
//
// include soft constraints that are known to be assignable to true after failed literal.
//
lbool maxlexN() {
unsigned sz = m_soft.size();
for (unsigned i = 0; i < sz; ++i) {
auto& soft = m_soft[i];
if (soft.value != l_undef)
continue;
expr_ref_vector asms(m);
asms.push_back(soft.s);
lbool is_sat = s().check_sat(asms);
switch (is_sat) {
case l_true:
if (!update_assignment())
return l_undef;
SASSERT(soft.value == l_true || m.limit().is_canceled());
break;
case l_false:
soft.set_value(l_false);
assert_value(soft);
for (unsigned j = i + 1; j < sz; ++j) {
auto& soft2 = m_soft[j];
if (soft2.value != l_true) {
break;
}
assert_value(soft2);
}
update_bounds();
break;
case l_undef:
return l_undef;
}
}
return l_true;
}
public:
maxlex(maxsat_context& c, unsigned id, vector<soft>& s):
maxsmt_solver_base(c, s, id),
m(c.get_manager()),
m_c(c) {
// ensure that soft constraints are sorted with largest soft constraints first.
cmp_soft cmp;
std::sort(m_soft.begin(), m_soft.end(), cmp);
}
lbool operator()() override {
init();
return maxlexN();
}
void commit_assignment() override {
for (auto & soft : m_soft) {
if (soft.value == l_undef) {
return;
}
assert_value(soft);
}
}
};
maxsmt_solver_base* mk_maxlex(maxsat_context& c, unsigned id, vector<soft>& soft) {
return alloc(maxlex, c, id, soft);
}
}