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