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