-
Notifications
You must be signed in to change notification settings - Fork 1
/
MSSGenerator.cc
179 lines (139 loc) · 4.45 KB
/
MSSGenerator.cc
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
#include "MSSGenerator.hpp"
#include <limits>
using openwbo::MaxSATFormula;
using NSPACE::IntRange;
using NSPACE::IntOption;
using NSPACE::BoolOption;
/**
* Solver options copied from open-wbo.
*/
const IntOption verbosity("Open-WBO", "verbosity",
"Verbosity level (0=minimal, 1=more).\n", 0,
IntRange(0, 1));
const IntOption weight("WBO", "weight-strategy",
"Weight strategy (0=none, 1=weight-based, 2=diversity-based).\n", 2,
IntRange(0, 2));
const BoolOption symmetry("WBO", "symmetry", "Symmetry breaking.\n", true);
const IntOption symmetry_lim("WBO", "symmetry-limit",
"Limit on the number of symmetry breaking clauses.\n", 500000,
IntRange(0, INT32_MAX));
/**
* Add soft clause with the given weight to the formula.
*/
void addSoftClause(MaxSATFormula* formula,
uint64_t weight,
const CNFClause& clause)
{
vec<Lit> lits;
/* Convert literals to Glucose format */
for (BLit lit : clause)
{
BVar var = abs(lit);
while (var >= formula->nVars())
formula->newVar();
lits.push((lit > 0) ? mkLit(var) : ~mkLit(var));
}
/* Add clause to MaxSAT formula and update weights */
formula->setMaximumWeight(weight);
formula->updateSumWeights(weight);
formula->addSoftClause(weight, lits);
}
/**
* Add hard clause to the given formula. Hard clauses must always be satisfied.
*/
void addHardClause(MaxSATFormula* formula,
const CNFClause& clause)
{
vec<Lit> lits;
/* Convert literals to Glucose format */
for (BLit lit : clause)
{
BVar var = abs(lit);
while (var >= formula->nVars())
formula->newVar();
lits.push((lit > 0) ? mkLit(var) : ~mkLit(var));
}
formula->addHardClause(lits); /*< add clause to the formula */
}
/**
* Add hard clause to the given formula conditioned on an indicator variable.
* Example: if indicator is z_1 and clause is (y_1 \/ ~y_2 \/ y_3), the hard
* clause (~z_1 \/ y_1 \/ ~y_2 \/ y_3) is added.
*/
void addHardClauseWithIndicator(MaxSATFormula* formula,
BVar indicator,
const CNFClause& clause)
{
vec<Lit> lits;
/* Convert literals to Glucose format */
for (BLit lit : clause)
{
BVar var = abs(lit);
while (var >= formula->nVars())
formula->newVar();
lits.push((lit > 0) ? mkLit(var) : ~mkLit(var));
}
lits.push(~mkLit(indicator)); /*< add indicator variable to clause*/
formula->addHardClause(lits); /*< add clause to the formula */
}
MSSGenerator::MSSGenerator(const Vector<BVar>& indicators,
const Vector<CNFClause>& clauses)
{
/* Set weight for hard clauses to the maximum possible value */
uint64_t hardWeight = std::numeric_limits<uint64_t>::max();
maxSatFormula.setHardWeight(hardWeight);
for (size_t i = 0; i < clauses.size(); i++)
{
/* Add hard clause (z_i -> Y_i) */
addHardClauseWithIndicator(&maxSatFormula, indicators[i], clauses[i]);
/* Add soft clause (z_i) */
addSoftClause(&maxSatFormula, 1, CNFClause(indicators[i]));
}
maxSatFormula.setProblemType(_UNWEIGHTED_);
maxSatFormula.setFormat(_FORMAT_MAXSAT_);
}
void MSSGenerator::enforceClause(const CNFClause& clause)
{
addHardClause(&maxSatFormula, clause);
}
/* Given an assignment as a boolean vector, return set of variables set to true */
Set<BVar> variablesSetToTrue(const vec<lbool>& model)
{
Vector<BVar> vars;
for (int i = 0; i < model.size(); i++)
if (model[i] == l_True)
vars.push_back(i);
return Set<BVar>(vars.begin(), vars.end());
}
Optional<Set<BVar>> MSSGenerator::generateMSS()
{
openwbo::WBO maxSatSolver(verbosity, weight, symmetry, symmetry_lim);
maxSatSolver.loadFormula(maxSatFormula.copyMaxSATFormula());
if (maxSatSolver.search()) /*< search was successful, return MSS */
{
return variablesSetToTrue(maxSatSolver.getModel());
}
else /*< we ran out of MSS, return null object */
{
return nullopt;
}
}
Optional<Set<BVar>> MSSGenerator::generateMSSCovering(const Set<BVar>& vars)
{
openwbo::WBO maxSatSolver(verbosity, weight, symmetry, symmetry_lim);
/* Create copy of the formula */
MaxSATFormula* copy = maxSatFormula.copyMaxSATFormula();
/* Add constraints enforcing that result covers the given set */
for (BVar var : vars)
addHardClause(copy, CNFClause(var));
maxSatSolver.loadFormula(copy);
bool success = maxSatSolver.search();
if (success) /*< search was successful, return MSS */
{
return variablesSetToTrue(maxSatSolver.getModel());
}
else /*< we ran out of MSS, return null object */
{
return nullopt;
}
}