Serialize ID API
[satune.git] / src / csolver.h
1 #ifndef CSOLVER_H
2 #define CSOLVER_H
3 #include "classes.h"
4 #include "ops.h"
5 #include "corestructs.h"
6 #include "asthash.h"
7 #include "solver_interface.h"
8 #include "common.h"
9
10 class CSolver {
11 public:
12         CSolver();
13         ~CSolver();
14         void resetSolver();
15         /** This function creates a set containing the elements passed in the array. */
16         Set *createSet(VarType type, uint64_t *elements, uint num);
17
18         /** This function creates a set from lowrange to highrange (inclusive). */
19
20         Set *createRangeSet(VarType type, uint64_t lowrange, uint64_t highrange);
21
22         bool itemExistInSet(Set *set, uint64_t item);
23
24         VarType getSetVarType(Set *set);
25
26         Element *createRangeVar(VarType type, uint64_t lowrange, uint64_t highrange);
27
28         /** This function creates a mutable set.
29          * Note: You should use addItem for adding new item to Mutable sets, and
30          * at the end, you should call finalizeMutableSet!
31          */
32
33         MutableSet *createMutableSet(VarType type);
34
35         /** This function adds a new item to a set. */
36
37         //Deprecating this unless we need it...
38         void addItem(MutableSet *set, uint64_t element);
39
40         /** This function adds a new unique item to the set and returns it.
41             This function cannot be used in conjunction with manually adding
42             items to the set. */
43
44         uint64_t createUniqueItem(MutableSet *set);
45
46         /**
47          * Freeze and finalize the mutableSet ...
48          */
49         void finalizeMutableSet(MutableSet *set);
50
51         /** This function creates an element variable over a set. */
52
53         Element *getElementVar(Set *set);
54
55         /** This function creates an element constrant. */
56         Element *getElementConst(VarType type, uint64_t value);
57
58         Set *getElementRange (Element *element);
59
60         void mustHaveValue(Element *element);
61
62         BooleanEdge getBooleanTrue();
63
64         BooleanEdge getBooleanFalse();
65
66         /** This function creates a boolean variable. */
67
68         BooleanEdge getBooleanVar(VarType type);
69
70         /** This function creates a function operator. */
71
72         Function *createFunctionOperator(ArithOp op, Set *range,
73                                                                                                                                          OverFlowBehavior overflowbehavior);
74
75         /** This function creates a predicate operator. */
76
77         Predicate *createPredicateOperator(CompOp op);
78
79         Predicate *createPredicateTable(Table *table, UndefinedBehavior behavior);
80
81         /** This function creates an empty instance table.*/
82
83         Table *createTable(Set *range);
84
85         Table *createTableForPredicate();
86         /** This function adds an input output relation to a table. */
87
88         void addTableEntry(Table *table, uint64_t *inputs, uint inputSize, uint64_t result);
89
90         /** This function converts a completed table into a function. */
91
92         Function *completeTable(Table *, UndefinedBehavior behavior);
93
94         /** This function applies a function to the Elements in its input. */
95
96         Element *applyFunction(Function *function, Element **array, uint numArrays, BooleanEdge overflowstatus);
97
98         /** This function applies a predicate to the Elements in its input. */
99
100         BooleanEdge applyPredicateTable(Predicate *predicate, Element **inputs, uint numInputs, BooleanEdge undefinedStatus);
101
102         BooleanEdge applyPredicate(Predicate *predicate, Element **inputs, uint numInputs);
103
104         /** This exactly one element of array can be true! (i.e. a1 + a2 + a3 + ... + an = 1)*/
105         BooleanEdge applyExactlyOneConstraint (BooleanEdge *array, uint asize);
106         
107         /** This exactly one element of array can be true! (i.e. a1 => !a2 & !a3 & ... & !an)*/
108         BooleanEdge applyAtMostOneConstraint (BooleanEdge *array, uint asize);
109
110         /** This function applies a logical operation to the Booleans in its input. */
111
112         BooleanEdge applyLogicalOperation(LogicOp op, BooleanEdge *array, uint asize);
113
114         /** This function applies a logical operation to the Booleans in its input. */
115
116         BooleanEdge applyLogicalOperation(LogicOp op, BooleanEdge arg1, BooleanEdge arg2);
117
118         /** This function applies a logical operation to the Booleans in its input. */
119
120         BooleanEdge applyLogicalOperation(LogicOp op, BooleanEdge arg);
121
122         /** This function adds a boolean constraint to the set of constraints
123             to be satisfied */
124
125         void addConstraint(BooleanEdge constraint);
126
127         /** This function instantiates an order of type type over the set set. */
128         Order *createOrder(OrderType type, Set *set);
129
130         /** This function instantiates a boolean on two items in an order. */
131         BooleanEdge orderConstraint(Order *order, uint64_t first, uint64_t second);
132
133         /** When everything is done, the client calls this function and then csolver starts to encode*/
134         int solve();
135         /**
136          * Incremental Solving for SATUNE.
137          * It only supports incremental solving for elements!
138          * No support for BooleanVar, BooleanOrder or using interpreters
139          * @return
140          */
141         int solveIncremental();
142
143         /** After getting the solution from the SAT solver, client can get the value of an element via this function*/
144         uint64_t getElementValue(Element *element);
145
146         void freezeElement(Element *e);
147         void turnoffOptimizations(){optimizationsOff = true;}
148         /** After getting the solution from the SAT solver, client can get the value of a boolean via this function*/
149         bool getBooleanValue(BooleanEdge boolean);
150
151         bool getOrderConstraintValue(Order *order, uint64_t first, uint64_t second);
152
153         bool isTrue(BooleanEdge b);
154         bool isFalse(BooleanEdge b);
155
156         void setUnSAT() { model_print("Setting UNSAT %%%%%%\n"); unsat = true; }
157         void setSatSolverTimeout(long seconds) { satsolverTimeout = seconds;}
158         bool isUnSAT() { return unsat; }
159         bool isBooleanVarUsed() {return booleanVarUsed;}
160         void printConstraint(BooleanEdge boolean);
161         void printConstraints();
162
163         Vector<Order *> *getOrders() { return &allOrders;}
164         HashsetOrder *getActiveOrders() { return &activeOrders;}
165
166         Tuner *getTuner() { return tuner; }
167
168         SetIteratorBooleanEdge *getConstraints() { return constraints.iterator(); }
169         bool isConstraintEncoded(BooleanEdge be) { return encodedConstraints.contains(be);}
170         void addEncodedConstraint(BooleanEdge be) {encodedConstraints.add(be);}
171
172         SATEncoder *getSATEncoder() {return satEncoder;}
173
174         void replaceBooleanWithTrue(BooleanEdge bexpr);
175         void replaceBooleanWithTrueNoRemove(BooleanEdge bexpr);
176         void replaceBooleanWithFalseNoRemove(BooleanEdge bexpr);
177         void replaceBooleanWithFalse(BooleanEdge bexpr);
178         void replaceBooleanWithBoolean(BooleanEdge oldb, BooleanEdge newb);
179         CSolver *clone();
180         void serializeID(long long id);
181         void serialize();
182         static CSolver *deserialize(const char *file, InterpreterType itype = SATUNE);
183         void autoTune(uint budget);
184         void inferFixedOrders();
185         void inferFixedOrder(Order *order);
186         void setInterpreter(InterpreterType type);
187         bool useInterpreter() {return interpreter != NULL;}
188         void setTuner(Tuner *_tuner) { tuner = _tuner; }
189         long long getElapsedTime() { return elapsedTime; }
190         long long getEncodeTime();
191         long long getSolveTime();
192         long getSatSolverTimeout() { return satsolverTimeout;}
193         bool isIncrementalMode() {return incrementalMode;}
194         void freezeElementsVariables();
195         CMEMALLOC;
196
197 private:
198         void handleIFFTrue(BooleanLogic *bexpr, BooleanEdge child);
199         void handleANDTrue(BooleanLogic *bexpr, BooleanEdge child);
200         void handleFunction(ElementFunction *ef, BooleanEdge child);
201
202         //These two functions are helpers if the client has a pointer to a
203         //Boolean object that we have since replaced
204         BooleanEdge rewriteLogicalOperation(LogicOp op, BooleanEdge *array, uint asize);
205         BooleanEdge doRewrite(BooleanEdge b);
206         /** This is a vector of constraints that must be satisfied. */
207         HashsetBooleanEdge constraints;
208         HashsetBooleanEdge encodedConstraints;
209
210         /** This is a vector of all boolean structs that we have allocated. */
211         Vector<Boolean *> allBooleans;
212
213         /** This is a vector of all set structs that we have allocated. */
214         Vector<Set *> allSets;
215
216         /** This is a vector of all element structs that we have allocated. */
217         Vector<Element *> allElements;
218
219         /** This is a vector of all predicate structs that we have allocated. */
220         Vector<Predicate *> allPredicates;
221
222         /** This is a vector of all table structs that we have allocated. */
223         Vector<Table *> allTables;
224
225         /** This is a vector of all order structs that we have allocated. */
226         Vector<Order *> allOrders;
227
228         HashsetOrder activeOrders;
229
230         /** This is a vector of all function structs that we have allocated. */
231         Vector<Function *> allFunctions;
232
233         BooleanEdge boolTrue;
234         BooleanEdge boolFalse;
235
236         /** These two tables are used for deduplicating entries. */
237         BooleanMatchMap boolMap;
238         ElementMatchMap elemMap;
239
240         SATEncoder *satEncoder;
241         bool unsat;
242         bool booleanVarUsed;
243         bool incrementalMode;
244         bool optimizationsOff;
245         Tuner *tuner;
246         long long elapsedTime;
247         long satsolverTimeout;
248         Interpreter *interpreter;
249         bool noOptimization;
250         friend class ElementOpt;
251         friend class VarOrderingOpt;
252 };
253
254 inline CompOp flipOp(CompOp op) {
255         switch (op) {
256         case SATC_EQUALS:
257                 return SATC_EQUALS;
258         case SATC_LT:
259                 return SATC_GT;
260         case SATC_GT:
261                 return SATC_LT;
262         case SATC_LTE:
263                 return SATC_GTE;
264         case SATC_GTE:
265                 return SATC_LTE;
266         }
267         ASSERT(0);
268 }
269
270 inline CompOp negateOp(CompOp op) {
271         switch (op) {
272         case SATC_EQUALS:
273                 ASSERT(0);
274         case SATC_LT:
275                 return SATC_GTE;
276         case SATC_GT:
277                 return SATC_LTE;
278         case SATC_LTE:
279                 return SATC_GT;
280         case SATC_GTE:
281                 return SATC_LT;
282         }
283         ASSERT(0);
284 }
285 #endif