521848a2a46e9833b96c74fb61ee78be778c4dbf
[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 "orderdecompose.h"
15 #include "autotuner.h"
16
17 CSolver::CSolver() :
18         unsat(false),
19         tuner(NULL),
20         elapsedTime(0)
21 {
22         satEncoder = new SATEncoder(this);
23 }
24
25 /** This function tears down the solver and the entire AST */
26
27 CSolver::~CSolver() {
28         uint size = allBooleans.getSize();
29         for (uint i = 0; i < size; i++) {
30                 delete allBooleans.get(i);
31         }
32
33         size = allSets.getSize();
34         for (uint i = 0; i < size; i++) {
35                 delete allSets.get(i);
36         }
37
38         size = allElements.getSize();
39         for (uint i = 0; i < size; i++) {
40                 delete allElements.get(i);
41         }
42
43         size = allTables.getSize();
44         for (uint i = 0; i < size; i++) {
45                 delete allTables.get(i);
46         }
47
48         size = allPredicates.getSize();
49         for (uint i = 0; i < size; i++) {
50                 delete allPredicates.get(i);
51         }
52
53         size = allOrders.getSize();
54         for (uint i = 0; i < size; i++) {
55                 delete allOrders.get(i);
56         }
57
58         size = allFunctions.getSize();
59         for (uint i = 0; i < size; i++) {
60                 delete allFunctions.get(i);
61         }
62
63         delete satEncoder;
64 }
65
66 CSolver *CSolver::clone() {
67         CSolver *copy = new CSolver();
68         CloneMap map;
69         HSIteratorBoolean *it = getConstraints();
70         while (it->hasNext()) {
71                 Boolean *b = it->next();
72                 copy->addConstraint(b->clone(copy, &map));
73         }
74         delete it;
75         return copy;
76 }
77
78 Set *CSolver::createSet(VarType type, uint64_t *elements, uint numelements) {
79         Set *set = new Set(type, elements, numelements);
80         allSets.push(set);
81         return set;
82 }
83
84 Set *CSolver::createRangeSet(VarType type, uint64_t lowrange, uint64_t highrange) {
85         Set *set = new Set(type, lowrange, highrange);
86         allSets.push(set);
87         return set;
88 }
89
90 MutableSet *CSolver::createMutableSet(VarType type) {
91         MutableSet *set = new MutableSet(type);
92         allSets.push(set);
93         return set;
94 }
95
96 void CSolver::addItem(MutableSet *set, uint64_t element) {
97         set->addElementMSet(element);
98 }
99
100 uint64_t CSolver::createUniqueItem(MutableSet *set) {
101         uint64_t element = set->low++;
102         set->addElementMSet(element);
103         return element;
104 }
105
106 Element *CSolver::getElementVar(Set *set) {
107         Element *element = new ElementSet(set);
108         allElements.push(element);
109         return element;
110 }
111
112 Element *CSolver::getElementConst(VarType type, uint64_t value) {
113         uint64_t array[] = {value};
114         Set *set = new Set(type, array, 1);
115         allSets.push(set);
116         Element *element = new ElementConst(value, type, set);
117         allElements.push(element);
118         return element;
119 }
120
121 Boolean *CSolver::getBooleanVar(VarType type) {
122         Boolean *boolean = new BooleanVar(type);
123         allBooleans.push(boolean);
124         return boolean;
125 }
126
127 Function *CSolver::createFunctionOperator(ArithOp op, Set **domain, uint numDomain, Set *range,OverFlowBehavior overflowbehavior) {
128         Function *function = new FunctionOperator(op, domain, numDomain, range, overflowbehavior);
129         allFunctions.push(function);
130         return function;
131 }
132
133 Predicate *CSolver::createPredicateOperator(CompOp op, Set **domain, uint numDomain) {
134         Predicate *predicate = new PredicateOperator(op, domain,numDomain);
135         allPredicates.push(predicate);
136         return predicate;
137 }
138
139 Predicate *CSolver::createPredicateTable(Table *table, UndefinedBehavior behavior) {
140         Predicate *predicate = new PredicateTable(table, behavior);
141         allPredicates.push(predicate);
142         return predicate;
143 }
144
145 Table *CSolver::createTable(Set **domains, uint numDomain, Set *range) {
146         Table *table = new Table(domains,numDomain,range);
147         allTables.push(table);
148         return table;
149 }
150
151 Table *CSolver::createTableForPredicate(Set **domains, uint numDomain) {
152         return createTable(domains, numDomain, NULL);
153 }
154
155 void CSolver::addTableEntry(Table *table, uint64_t *inputs, uint inputSize, uint64_t result) {
156         table->addNewTableEntry(inputs, inputSize, result);
157 }
158
159 Function *CSolver::completeTable(Table *table, UndefinedBehavior behavior) {
160         Function *function = new FunctionTable(table, behavior);
161         allFunctions.push(function);
162         return function;
163 }
164
165 Element *CSolver::applyFunction(Function *function, Element **array, uint numArrays, Boolean *overflowstatus) {
166         Element *element = new ElementFunction(function,array,numArrays,overflowstatus);
167         allElements.push(element);
168         return element;
169 }
170
171 Boolean *CSolver::applyPredicate(Predicate *predicate, Element **inputs, uint numInputs) {
172         return applyPredicateTable(predicate, inputs, numInputs, NULL);
173 }
174
175 Boolean *CSolver::applyPredicateTable(Predicate *predicate, Element **inputs, uint numInputs, Boolean *undefinedStatus) {
176         BooleanPredicate *boolean = new BooleanPredicate(predicate, inputs, numInputs, undefinedStatus);
177         allBooleans.push(boolean);
178         return boolean;
179 }
180
181 Boolean *CSolver::applyLogicalOperation(LogicOp op, Boolean **array, uint asize) {
182         Boolean *boolean = new BooleanLogic(this, op, array, asize);
183         allBooleans.push(boolean);
184         return boolean;
185 }
186
187 void CSolver::addConstraint(Boolean *constraint) {
188         constraints.add(constraint);
189 }
190
191 Order *CSolver::createOrder(OrderType type, Set *set) {
192         Order *order = new Order(type, set);
193         allOrders.push(order);
194         return order;
195 }
196
197 Boolean *CSolver::orderConstraint(Order *order, uint64_t first, uint64_t second) {
198         Boolean *constraint = new BooleanOrder(order, first, second);
199         allBooleans.push(constraint);
200         return constraint;
201 }
202
203 int CSolver::startEncoding() {
204         bool deleteTuner = false;
205         if (tuner == NULL) {
206                 tuner = new DefaultTuner();
207                 deleteTuner = true;
208         }
209                 
210         long long startTime = getTimeNano();
211         computePolarities(this);
212         orderAnalysis(this);
213         naiveEncodingDecision(this);
214         satEncoder->encodeAllSATEncoder(this);
215         int result = unsat ? IS_UNSAT : satEncoder->solve();
216         long long finishTime = getTimeNano();
217         elapsedTime = finishTime - startTime;
218         if (deleteTuner) {
219                 delete tuner;
220                 tuner = NULL;
221         }
222         return result;
223 }
224
225 uint64_t CSolver::getElementValue(Element *element) {
226         switch (GETELEMENTTYPE(element)) {
227         case ELEMSET:
228         case ELEMCONST:
229         case ELEMFUNCRETURN:
230                 return getElementValueSATTranslator(this, element);
231         default:
232                 ASSERT(0);
233         }
234         exit(-1);
235 }
236
237 bool CSolver::getBooleanValue(Boolean *boolean) {
238         switch (GETBOOLEANTYPE(boolean)) {
239         case BOOLEANVAR:
240                 return getBooleanVariableValueSATTranslator(this, boolean);
241         default:
242                 ASSERT(0);
243         }
244         exit(-1);
245 }
246
247 HappenedBefore CSolver::getOrderConstraintValue(Order *order, uint64_t first, uint64_t second) {
248         return getOrderConstraintValueSATTranslator(this, order, first, second);
249 }
250
251 long long CSolver::getEncodeTime() { return satEncoder->getEncodeTime(); }
252
253 long long CSolver::getSolveTime() { return satEncoder->getSolveTime(); }
254
255 void CSolver::autoTune(uint budget) {
256         AutoTuner * autotuner=new AutoTuner(budget);
257         autotuner->addProblem(this);
258         autotuner->tune();
259         delete autotuner;
260 }