-
Notifications
You must be signed in to change notification settings - Fork 1
/
Constraint.h
executable file
·306 lines (220 loc) · 6.79 KB
/
Constraint.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
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
/*
* Constraint.h
*
* Created on: Sep 16, 2008
* Author: isil
*/
#ifndef CONSTRAINT_H_
#define CONSTRAINT_H_
#include "ConstraintSolver.h"
#include "Term.h"
#include <set>
#include <iostream>
#include <unordered_map>
#include <unordered_set>
#include <string>
#include <functional>
using namespace std;
class CNode;
class Constraint {
friend class boost::serialization::access;
private:
friend struct std::hash<Constraint>;
friend class ConstraintSolver;
friend class Term;
int id;
template<class Archive>
void save(Archive & ar, const unsigned int version) const
{
CNode* nc = cs.get_nc((int&)id);
CNode* sc = cs.get_sc((int&)id);
ar & nc;
ar & sc;
}
template<class Archive>
void load(Archive & ar, const unsigned int version)
{
CNode* nc;
CNode* sc;
ar & nc;
ar & sc;
nc = CNode::uniquify_cnode(nc);
sc = CNode::uniquify_cnode(sc);
pair<CNode*, CNode*> key(nc, sc);
id = cs.get_id(key);
}
BOOST_SERIALIZATION_SPLIT_MEMBER()
void get_msa_assignment(set<VariableTerm*>&
msa_vars, map<Term*, SatValue>& msa);
public:
static ConstraintSolver cs;
Constraint();
/**
* Constructs boolean constant true/false
*/
Constraint(bool val);
Constraint(const Constraint& nc, const Constraint& sc);
Constraint(Term* t1, Term* t2, atom_op_type op);
Constraint(const Constraint & other);
Constraint(string s);
Constraint(const char* s);
Constraint(CNode* n);
static void set_geqz_attribute(Term* t);
static void set_gtz_attribute(Term* t);
bool sat() const;
bool unsat() const;
bool valid() const;
bool equivalent(Constraint other) const;
/*
* These methods do not simplify.
*/
bool sat_discard() const;
bool unsat_discard() const;
bool valid_discard() const;
Constraint nc() const;
Constraint sc() const;
int nc_size() const;
int sc_size() const;
/*
* Checks if the constraint is literally the constant true/false
*/
bool is_true() const;
bool is_false() const;
static void clear();
Constraint operator&(const Constraint & other) const;
Constraint operator|(const Constraint & other) const;
/*
* Find a constraint C such that b&C=>this and C is consistent with everything
* in the consistency_constraints set.
*/
Constraint abduce(Constraint b,
const set<Constraint> & consistency_constraints,
map<Term*, int>& costs) const;
Constraint abduce(Constraint b,
const set<Constraint> & consistency_constraints) const;
Constraint abduce(Constraint b) const;
void operator&=(const Constraint & other);
void operator|=(const Constraint & other);
Constraint operator!() const ;
void operator=(const Constraint & other);
bool operator==(const Constraint & other) const;
bool operator!=(const Constraint & other) const;
bool implies(const Constraint & other) const;
bool operator<(const Constraint & other) const;
/*
* Returns true if NC==SC, i.e. the current constraint has
* no imprecision.
*/
bool is_precise() const;
/*
* Functions for eliminating existentially quantified variables
*/
void eliminate_evar(VariableTerm* var);
void eliminate_evars(set<VariableTerm*>& vars);
/*
* Functions for eliminating universally quantified variables
*/
void eliminate_uvar(VariableTerm* var);
void eliminate_uvars(set<VariableTerm*>& vars);
/*
* Does this constraint contain a <, <=, >, >=?
*/
bool contains_inequality();
void fresh_id();
/*
* Functions for eliminating free variables
*/
void eliminate_free_var(VariableTerm* var);
void eliminate_free_vars(set<VariableTerm*>& vars);
/*
* Yields the set of all terms used in this constraint
*/
void get_terms(set<Term*>& terms, bool include_nested_terms);
/*
* Methods to manage background knowledge, i.e.
* assumptions on the left side of the turnstile.
*/
static void add_ground_axiom(Constraint key, Constraint c);
static void add_quantified_axiom(Constraint key, Constraint c);
static void set_background_knowledge(Constraint c);
static void replace_term_in_axioms(Term* old_t, Term* new_t);
static string background_knowledge_to_string();
static Constraint get_general_background();
/*
* The return value R is a term t' such that (t=t') is
* implied by this constraint. Returns NULL if no
* such equality is implied by the constraint.
*/
Term* find_equality(Term* t) const;
void find_equalities(Term* t, set<Term*> & eqs) const;
void replace_term(Term* to_replace, Term* replacement);
void replace_terms(map<Term*, Term*> & replacements);
void replace_constraint(Constraint to_replace, Constraint replacement);
void replace_terms(Term* (*sub_func)(Term* t, void* data), void* my_data)
{
c_id old_id = id;
id = cs.replace_terms(old_id, sub_func, my_data);
}
void get_free_variables(set<Term*>& vars);
bool contains_term(Term* var);
// Does this constraint contain any of these terms?
bool contains_term(set<Term*>& terms);
static void clear_background();
/*
* If this method is called, background knowledge is not
* taken into account when determining satisfiability/validity.
*/
static void disable_background();
/*
* If this constraint contains artificial variables used to enforce,
* for example, existence and uniqueness, this function
* eliminates these fake variables and replaces them by
* what they stand for.
*/
void propagate_background();
/*
* Assume that c holds, i.e. set all leaves from c that occur inside you
* to true.
*/
void assume(Constraint c);
/*
* Are t1 and t2 related by an equality in this constraint?
*/
bool has_equality_relation(Term* t1, Term* t2);
void get_disjunctive_equalities(Term* var,
map<Term*, Constraint> & equalities);
void divide(long int c, Term* t);
/*
* Gives a satisfying assignment to all terms in the constraint.
* e.g. <drf(a), 3>,...
*/
bool get_assignment(set<pair<string, string> > & assignments);
bool get_assignment(map<Term*, SatValue> & assignments);
void replace_terms(Term* (*sub_func)(Term* t));
string to_string() const;
string debug_string();
int msa(map<Term*, SatValue>& msa);
int msa(set<VariableTerm*> & msa) const;
int msa(set<VariableTerm*> & msa, set<Constraint>& bg) const;
int msa(map<Term*, SatValue> & msa, set<Constraint>& bg);
int msa(set<VariableTerm*> & msa,
map<VariableTerm*, int>& costs) const;
int msa(set<VariableTerm*> & msa, set<Constraint>& bg,
map<VariableTerm*, int>& costs) const;
int msa(map<Term*, SatValue> & msa, set<Constraint>& bg,
map<VariableTerm*, int>& costs);
pair<CNode*, CNode*> get_cnodes();
void to_dnf(set<Constraint> & dnf);
void to_cnf(set<Constraint> & cnf);
};
ostream& operator <<(ostream &os,const Constraint &_obj);
namespace std {
template <>
struct hash<Constraint> {
size_t operator() (const Constraint & x) const {
Constraint & c = (Constraint &)x;
return hash<int>()(c.id);
}
};
}
#endif /* CONSTRAINT_H_ */