-
Notifications
You must be signed in to change notification settings - Fork 12
/
MaxSAT.h
267 lines (215 loc) · 7.89 KB
/
MaxSAT.h
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
/*!
* \author Ruben Martins - ruben@sat.inesc-id.pt
*
* @section LICENSE
*
* MiniSat, Copyright (c) 2003-2006, Niklas Een, Niklas Sorensson
* Copyright (c) 2007-2010, Niklas Sorensson
* Open-WBO, Copyright (c) 2013-2017, Ruben Martins, Vasco Manquinho, Ines Lynce
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*
*/
#ifndef MaxSAT_h
#define MaxSAT_h
#ifdef SIMP
#include "simp/SimpSolver.h"
#else
#include "core/Solver.h"
#endif
#include "MaxSATFormula.h"
#include "MaxTypes.h"
#include "utils/System.h"
#include <algorithm>
#include <map>
#include <set>
#include <utility>
#include <vector>
using NSPACE::vec;
using NSPACE::Lit;
using NSPACE::lit_Undef;
using NSPACE::mkLit;
using NSPACE::lbool;
using NSPACE::Solver;
using NSPACE::cpuTime;
namespace openwbo {
class MaxSAT {
public:
MaxSAT(MaxSATFormula *mx) {
maxsat_formula = mx;
searchStatus = _UNKNOWN_;
// 'ubCost' will be set to the sum of the weights of soft clauses
// during the parsing of the MaxSAT formula.
ubCost = 0;
lbCost = 0;
off_set = 0;
// Statistics
nbSymmetryClauses = 0;
nbCores = 0;
nbSatisfiable = 0;
sumSizeCores = 0;
print_model = false;
print_soft = false;
print = false;
unsat_soft_file = NULL;
}
MaxSAT() {
maxsat_formula = NULL;
searchStatus = _UNKNOWN_;
// 'ubCost' will be set to the sum of the weights of soft clauses
// during the parsing of the MaxSAT formula.
ubCost = 0;
lbCost = 0;
off_set = 0;
// Statistics
nbSymmetryClauses = 0;
nbCores = 0;
nbSatisfiable = 0;
sumSizeCores = 0;
print_model = false;
print_soft = false;
print = false;
unsat_soft_file = NULL;
}
virtual ~MaxSAT() {
if (maxsat_formula != NULL)
delete maxsat_formula;
}
void setInitialTime(double initial); // Set initial time.
// Print configuration of the MaxSAT solver.
// virtual void printConfiguration();
void printConfiguration();
// Encoding information.
void print_AMO_configuration(int encoding);
void print_PB_configuration(int encoding);
void print_Card_configuration(int encoding);
// Incremental information.
void print_Incremental_configuration(int incremental);
virtual StatusCode search(); // MaxSAT search.
void printAnswer(int type); // Print the answer.
// Tests if a MaxSAT formula has a lexicographical optimization criterion.
bool isBMO(bool cache = true);
void loadFormula(MaxSATFormula *maxsat) {
maxsat_formula = maxsat;
maxsat_formula->setInitialVars(maxsat_formula->nVars());
if (maxsat_formula->getObjFunction() != NULL) {
off_set = maxsat_formula->getObjFunction()->_const;
maxsat_formula->convertPBtoMaxSAT();
}
ubCost = maxsat_formula->getSumWeights();
}
void blockModel(Solver *solver);
// Get bounds methods
uint64_t getUB();
std::pair<uint64_t, int> getLB();
Soft &getSoftClause(int i) { return maxsat_formula->getSoftClause(i); }
Hard &getHardClause(int i) { return maxsat_formula->getHardClause(i); }
Lit getAssumptionLit(int soft) {
return maxsat_formula->getSoftClause(soft).assumption_var;
}
Lit getRelaxationLit(int soft, int i = 0) {
return maxsat_formula->getSoftClause(soft).relaxation_vars[i];
}
int64_t getOffSet() { return off_set; }
MaxSATFormula *getMaxSATFormula() { return maxsat_formula; }
void setPrintModel(bool model) { print_model = model; }
bool getPrintModel() { return print_model; }
void setPrint(bool doPrint) { print = doPrint; }
bool getPrint() { return print; }
void setPrintSoft(const char* file) {
if (file != NULL){
unsat_soft_file = (char*)malloc(sizeof(char) * (sizeof(file)));
strcpy(unsat_soft_file,file) ;
print_soft = true;
}
}
bool isPrintSoft() { return print_soft; }
char * getPrintSoftFilename() { return unsat_soft_file; }
/** return status of current search
*
* This method helps to extract the status in case the solver is used as a
* library object without printing solutions.
*/
StatusCode getStatus() { return searchStatus; }
/** return truth values for variables
*
* This method returns the truth value for a variable in the internal
* format. However, the return value reflects the external format, e.g.
* getValue(0) will return 1 or -1, depending on the sign of the variable
* in the model.
*/
int getValue(const NSPACE::Var v)
{
if (v > model.size()) return 0;
if (model[v] == l_True) return v+1;
return -(int)v - 1;
}
protected:
// Interface with the SAT solver
//
Solver *newSATSolver(); // Creates a SAT solver.
// Solves the formula that is currently loaded in the SAT solver.
lbool searchSATSolver(Solver *S, vec<Lit> &assumptions, bool pre = false);
lbool searchSATSolver(Solver *S, bool pre = false);
void newSATVariable(Solver *S); // Creates a new variable in the SAT solver.
void reserveSATVariables(Solver *S, unsigned maxVariable); // Reserve space for multiple variables in the SAT solver.
// Properties of the MaxSAT formula
//
vec<lbool> model; // Stores the best satisfying model.
StatusCode searchStatus; // Stores the current state of the formula
// Statistics
//
int nbCores; // Number of cores.
int nbSymmetryClauses; // Number of symmetry clauses.
uint64_t sumSizeCores; // Sum of the sizes of cores.
int nbSatisfiable; // Number of satisfiable calls.
// Bound values
//
uint64_t ubCost; // Upper bound value.
uint64_t lbCost; // Lower bound value.
int64_t off_set; // Offset of the objective function for PB solving.
MaxSATFormula *maxsat_formula;
// Others
// int currentWeight; // Initialized to the maximum weight of soft clauses.
double initialTime; // Initial time.
int verbosity; // Controls the verbosity of the solver.
bool print_model; // Controls if the model is printed at the end.
bool print; // Controls if data should be printed at all
bool print_soft; // Controls if the unsatified soft clauses are printed at the end.
char * unsat_soft_file; // Name of the file where the unsatisfied soft clauses will be printed.
// Different weights that corresponds to each function in the BMO algorithm.
std::vector<uint64_t> orderWeights;
// Utils for model management
//
void saveModel(vec<lbool> ¤tModel); // Saves a Model.
// Compute the cost of a model.
uint64_t computeCostModel(vec<lbool> ¤tModel,
uint64_t weight = UINT64_MAX);
// Utils for printing
//
void printBound(int64_t bound); // Print the current bound.
void printModel(); // Print the best satisfying model.
void printStats(); // Print search statistics.
std::string printSoftClause(int id); // Prints a soft clause.
void printUnsatisfiedSoftClauses(); // Prints unsatisfied soft clauses.
// Greater than comparator.
bool static greaterThan(uint64_t i, uint64_t j) { return (i > j); }
};
} // namespace openwbo
#endif