-
Notifications
You must be signed in to change notification settings - Fork 2
/
MaxSAT.h
214 lines (169 loc) · 6.02 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
/*!
* \author Ruben Martins - [email protected]
*
* @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;
// '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;
}
MaxSAT() {
maxsat_formula = NULL;
// '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;
}
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 void 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; }
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.
// Properties of the MaxSAT formula
//
vec<lbool> model; // Stores the best satisfying model.
// 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.
// 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 printModel(); // Print the best satisfying model.
void printStats(); // Print search statistics.
// Greater than comparator.
bool static greaterThan(uint64_t i, uint64_t j) { return (i > j); }
};
} // namespace openwbo
#endif