Tuner bug fix
[satune.git] / src / csolver.cc
1 #include "csolver.h"
2 #include "set.h"
3 #include "mutableset.h"
4 #include "element.h"
5 #include "boolean.h"
6 #include "predicate.h"
7 #include "order.h"
8 #include "table.h"
9 #include "function.h"
10 #include "satencoder.h"
11 #include "sattranslator.h"
12 #include "tunable.h"
13 #include "polarityassignment.h"
14 #include "analyzer.h"
15 #include "autotuner.h"
16 #include "astops.h"
17
18 CSolver::CSolver() :
19         boolTrue(new BooleanConst(true)),
20         boolFalse(new BooleanConst(false)),
21         unsat(false),
22         tuner(NULL),
23         elapsedTime(0)
24 {
25         satEncoder = new SATEncoder(this);
26 }
27
28 /** This function tears down the solver and the entire AST */
29
30 CSolver::~CSolver() {
31         uint size = allBooleans.getSize();
32         for (uint i = 0; i < size; i++) {
33                 delete allBooleans.get(i);
34         }
35
36         size = allSets.getSize();
37         for (uint i = 0; i < size; i++) {
38                 delete allSets.get(i);
39         }
40
41         size = allElements.getSize();
42         for (uint i = 0; i < size; i++) {
43                 delete allElements.get(i);
44         }
45
46         size = allTables.getSize();
47         for (uint i = 0; i < size; i++) {
48                 delete allTables.get(i);
49         }
50
51         size = allPredicates.getSize();
52         for (uint i = 0; i < size; i++) {
53                 delete allPredicates.get(i);
54         }
55
56         size = allOrders.getSize();
57         for (uint i = 0; i < size; i++) {
58                 delete allOrders.get(i);
59         }
60
61         size = allFunctions.getSize();
62         for (uint i = 0; i < size; i++) {
63                 delete allFunctions.get(i);
64         }
65
66         delete boolTrue;
67         delete boolFalse;
68         delete satEncoder;
69 }
70
71 CSolver *CSolver::clone() {
72         CSolver *copy = new CSolver();
73         CloneMap map;
74         HSIteratorBoolean *it = getConstraints();
75         while (it->hasNext()) {
76                 Boolean *b = it->next();
77                 copy->addConstraint(b->clone(copy, &map));
78         }
79         delete it;
80         return copy;
81 }
82
83 Set *CSolver::createSet(VarType type, uint64_t *elements, uint numelements) {
84         Set *set = new Set(type, elements, numelements);
85         allSets.push(set);
86         return set;
87 }
88
89 Set *CSolver::createRangeSet(VarType type, uint64_t lowrange, uint64_t highrange) {
90         Set *set = new Set(type, lowrange, highrange);
91         allSets.push(set);
92         return set;
93 }
94
95 MutableSet *CSolver::createMutableSet(VarType type) {
96         MutableSet *set = new MutableSet(type);
97         allSets.push(set);
98         return set;
99 }
100
101 void CSolver::addItem(MutableSet *set, uint64_t element) {
102         set->addElementMSet(element);
103 }
104
105 uint64_t CSolver::createUniqueItem(MutableSet *set) {
106         uint64_t element = set->getNewUniqueItem();
107         set->addElementMSet(element);
108         return element;
109 }
110
111 Element *CSolver::getElementVar(Set *set) {
112         Element *element = new ElementSet(set);
113         allElements.push(element);
114         return element;
115 }
116
117 Element *CSolver::getElementConst(VarType type, uint64_t value) {
118         uint64_t array[] = {value};
119         Set *set = new Set(type, array, 1);
120         Element *element = new ElementConst(value, type, set);
121         Element *e = elemMap.get(element);
122         if (e == NULL) {
123                 allSets.push(set);
124                 allElements.push(element);
125                 elemMap.put(element, element);
126                 return element;
127         } else {
128                 delete set;
129                 delete element;
130                 return e;
131         }
132 }
133
134 Element *CSolver::applyFunction(Function *function, Element **array, uint numArrays, Boolean *overflowstatus) {
135         Element *element = new ElementFunction(function,array,numArrays,overflowstatus);
136         Element *e = elemMap.get(element);
137         if (e == NULL) {
138                 allElements.push(element);
139                 elemMap.put(element, element);
140                 return element;
141         } else {
142                 delete element;
143                 return e;
144         }
145 }
146
147 Function *CSolver::createFunctionOperator(ArithOp op, Set **domain, uint numDomain, Set *range,OverFlowBehavior overflowbehavior) {
148         Function *function = new FunctionOperator(op, domain, numDomain, range, overflowbehavior);
149         allFunctions.push(function);
150         return function;
151 }
152
153 Predicate *CSolver::createPredicateOperator(CompOp op, Set **domain, uint numDomain) {
154         Predicate *predicate = new PredicateOperator(op, domain,numDomain);
155         allPredicates.push(predicate);
156         return predicate;
157 }
158
159 Predicate *CSolver::createPredicateTable(Table *table, UndefinedBehavior behavior) {
160         Predicate *predicate = new PredicateTable(table, behavior);
161         allPredicates.push(predicate);
162         return predicate;
163 }
164
165 Table *CSolver::createTable(Set **domains, uint numDomain, Set *range) {
166         Table *table = new Table(domains,numDomain,range);
167         allTables.push(table);
168         return table;
169 }
170
171 Table *CSolver::createTableForPredicate(Set **domains, uint numDomain) {
172         return createTable(domains, numDomain, NULL);
173 }
174
175 void CSolver::addTableEntry(Table *table, uint64_t *inputs, uint inputSize, uint64_t result) {
176         table->addNewTableEntry(inputs, inputSize, result);
177 }
178
179 Function *CSolver::completeTable(Table *table, UndefinedBehavior behavior) {
180         Function *function = new FunctionTable(table, behavior);
181         allFunctions.push(function);
182         return function;
183 }
184
185 Boolean *CSolver::getBooleanVar(VarType type) {
186         Boolean *boolean = new BooleanVar(type);
187         allBooleans.push(boolean);
188         return boolean;
189 }
190
191 Boolean *CSolver::getBooleanTrue() {
192         return boolTrue;
193 }
194
195 Boolean *CSolver::getBooleanFalse() {
196         return boolFalse;
197 }
198
199 Boolean *CSolver::applyPredicate(Predicate *predicate, Element **inputs, uint numInputs) {
200         return applyPredicateTable(predicate, inputs, numInputs, NULL);
201 }
202
203 Boolean *CSolver::applyPredicateTable(Predicate *predicate, Element **inputs, uint numInputs, Boolean *undefinedStatus) {
204         BooleanPredicate *boolean = new BooleanPredicate(predicate, inputs, numInputs, undefinedStatus);
205         Boolean * b = boolMap.get(boolean);
206         if (b == NULL) {
207                 boolMap.put(boolean, boolean);
208                 allBooleans.push(boolean);
209                 return boolean;
210         } else {
211                 delete boolean;
212                 return b;
213         }
214 }
215
216 Boolean *CSolver::applyLogicalOperation(LogicOp op, Boolean **array, uint asize) {
217         Boolean * newarray[asize];
218         switch(op) {
219         case SATC_NOT: {
220                 if (array[0]->type == LOGICOP && ((BooleanLogic *)array[0])->op==SATC_NOT) {
221                         return ((BooleanLogic *) array[0])->inputs.get(0);
222                 } else if (array[0]->type == BOOLCONST) {
223                         bool isTrue = ((BooleanConst *) array[0])->isTrue;
224                         return isTrue ? boolFalse : boolTrue;
225                 }
226                 break;
227         }
228         case SATC_XOR: {
229                 for(uint i=0;i<2;i++) {
230                         if (array[i]->type == BOOLCONST) {
231                                 bool isTrue = ((BooleanConst *) array[i])->isTrue;
232                                 if (isTrue) {
233                                         newarray[0]=array[1-i];
234                                         return applyLogicalOperation(SATC_NOT, newarray, 1);
235                                 } else
236                                         return array[1-i];
237                         }
238                 }
239                 break;
240         }
241         case SATC_OR: {
242                 uint newindex=0;
243                 for(uint i=0;i<asize;i++) {
244                         Boolean *b=array[i];
245                         if (b->type == BOOLCONST) {
246                                 bool isTrue = ((BooleanConst *) b)->isTrue;
247                                 if (isTrue)
248                                         return b;
249                                 else
250                                         continue;
251                         } else
252                                 newarray[newindex++]=b;
253                 }
254                 if (newindex==1)
255                         return newarray[0];
256                 else if (newindex == 2) {
257                         bool isNot0 = (newarray[0]->type==BOOLCONST) && ((BooleanLogic *)newarray[0])->op == SATC_NOT;
258                         bool isNot1 = (newarray[1]->type==BOOLCONST) && ((BooleanLogic *)newarray[1])->op == SATC_NOT;
259
260                         if (isNot0 != isNot1) {
261                                 if (isNot0) {
262                                         newarray[0] = ((BooleanLogic *) newarray[0])->inputs.get(0);
263                                 } else {
264                                         Boolean *tmp =  ((BooleanLogic *) array[1])->inputs.get(0);
265                                         array[1] = array[0];
266                                         array[0] = tmp;
267                                 }
268                                 return applyLogicalOperation(SATC_IMPLIES, newarray, 2);
269                         }
270                 } else {
271                         array = newarray;
272                         asize = newindex;
273                 }
274                 break;
275         }
276         case SATC_AND: {
277                 uint newindex=0;
278                 for(uint i=0;i<asize;i++) {
279                         Boolean *b=array[i];
280                         if (b->type == BOOLCONST) {
281                                 bool isTrue = ((BooleanConst *) b)->isTrue;
282                                 if (isTrue)
283                                         continue;
284                                 else
285                                         return b;
286                         } else
287                                 newarray[newindex++]=b;
288                 }
289                 if(newindex==1) {
290                         return newarray[0];
291                 } else {
292                         array = newarray;
293                         asize = newindex;
294                 }
295                 break;
296         }
297         case SATC_IMPLIES: {
298                 if (array[0]->type == BOOLCONST) {
299                         BooleanConst *b=(BooleanConst *) array[0];
300                         if (b->isTrue) {
301                                 return array[1];
302                         } else {
303                                 return boolTrue;
304                         }
305                 } else if (array[1]->type == BOOLCONST) {
306                         BooleanConst *b=(BooleanConst *) array[0];
307                         if (b->isTrue) {
308                                 return b;
309                         } else {
310                                 return applyLogicalOperation(SATC_NOT, array, 1);
311                         }
312                 }
313                 break;
314         }
315         }
316         
317         Boolean *boolean = new BooleanLogic(this, op, array, asize);
318         Boolean *b = boolMap.get(boolean);
319         if (b == NULL) {
320                 boolMap.put(boolean, boolean);
321                 allBooleans.push(boolean);
322                 return boolean;         
323         } else {
324                 delete boolean;
325                 return b;
326         }
327 }
328
329 Boolean *CSolver::orderConstraint(Order *order, uint64_t first, uint64_t second) {
330         Boolean *constraint = new BooleanOrder(order, first, second);
331         allBooleans.push(constraint);
332         return constraint;
333 }
334
335 void CSolver::addConstraint(Boolean *constraint) {
336         if (constraint == boolTrue)
337                 return;
338         else if (constraint == boolFalse)
339                 setUnSAT();
340         else
341                 constraints.add(constraint);
342 }
343
344 Order *CSolver::createOrder(OrderType type, Set *set) {
345         Order *order = new Order(type, set);
346         allOrders.push(order);
347         return order;
348 }
349
350 int CSolver::startEncoding() {
351         bool deleteTuner = false;
352         if (tuner == NULL) {
353                 tuner = new DefaultTuner();
354                 deleteTuner = true;
355         }
356                 
357         long long startTime = getTimeNano();
358         computePolarities(this);
359         orderAnalysis(this);
360         naiveEncodingDecision(this);
361         satEncoder->encodeAllSATEncoder(this);
362         int result = unsat ? IS_UNSAT : satEncoder->solve();
363         long long finishTime = getTimeNano();
364         elapsedTime = finishTime - startTime;
365         if (deleteTuner) {
366                 delete tuner;
367                 tuner = NULL;
368         }
369         return result;
370 }
371
372 uint64_t CSolver::getElementValue(Element *element) {
373         switch (element->type) {
374         case ELEMSET:
375         case ELEMCONST:
376         case ELEMFUNCRETURN:
377                 return getElementValueSATTranslator(this, element);
378         default:
379                 ASSERT(0);
380         }
381         exit(-1);
382 }
383
384 bool CSolver::getBooleanValue(Boolean *boolean) {
385         switch (boolean->type) {
386         case BOOLEANVAR:
387                 return getBooleanVariableValueSATTranslator(this, boolean);
388         default:
389                 ASSERT(0);
390         }
391         exit(-1);
392 }
393
394 HappenedBefore CSolver::getOrderConstraintValue(Order *order, uint64_t first, uint64_t second) {
395         return getOrderConstraintValueSATTranslator(this, order, first, second);
396 }
397
398 long long CSolver::getEncodeTime() { return satEncoder->getEncodeTime(); }
399
400 long long CSolver::getSolveTime() { return satEncoder->getSolveTime(); }
401
402 void CSolver::autoTune(uint budget) {
403         AutoTuner * autotuner=new AutoTuner(budget);
404         autotuner->addProblem(this);
405         autotuner->tune();
406         delete autotuner;
407 }