Move constraints to set
[satune.git] / src / csolver.c
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
13 CSolver *allocCSolver() {
14         CSolver *This = (CSolver *) ourmalloc(sizeof(CSolver));
15         This->constraints = allocDefHashSetBoolean();
16         This->allBooleans = allocDefVectorBoolean();
17         This->allSets = allocDefVectorSet();
18         This->allElements = allocDefVectorElement();
19         This->allPredicates = allocDefVectorPredicate();
20         This->allTables = allocDefVectorTable();
21         This->allOrders = allocDefVectorOrder();
22         This->allFunctions = allocDefVectorFunction();
23         This->satEncoder = allocSATEncoder();
24         return This;
25 }
26
27 /** This function tears down the solver and the entire AST */
28
29 void deleteSolver(CSolver *This) {
30         deleteHashSetBoolean(This->constraints);
31
32         uint size = getSizeVectorBoolean(This->allBooleans);
33         for (uint i = 0; i < size; i++) {
34                 deleteBoolean(getVectorBoolean(This->allBooleans, i));
35         }
36         deleteVectorBoolean(This->allBooleans);
37
38         size = getSizeVectorSet(This->allSets);
39         for (uint i = 0; i < size; i++) {
40                 deleteSet(getVectorSet(This->allSets, i));
41         }
42         deleteVectorSet(This->allSets);
43
44         size = getSizeVectorElement(This->allElements);
45         for (uint i = 0; i < size; i++) {
46                 deleteElement(getVectorElement(This->allElements, i));
47         }
48         deleteVectorElement(This->allElements);
49
50         size = getSizeVectorTable(This->allTables);
51         for (uint i = 0; i < size; i++) {
52                 deleteTable(getVectorTable(This->allTables, i));
53         }
54         deleteVectorTable(This->allTables);
55
56         size = getSizeVectorPredicate(This->allPredicates);
57         for (uint i = 0; i < size; i++) {
58                 deletePredicate(getVectorPredicate(This->allPredicates, i));
59         }
60         deleteVectorPredicate(This->allPredicates);
61
62         size = getSizeVectorOrder(This->allOrders);
63         for (uint i = 0; i < size; i++) {
64                 deleteOrder(getVectorOrder(This->allOrders, i));
65         }
66         deleteVectorOrder(This->allOrders);
67
68         size = getSizeVectorFunction(This->allFunctions);
69         for (uint i = 0; i < size; i++) {
70                 deleteFunction(getVectorFunction(This->allFunctions, i));
71         }
72         deleteVectorFunction(This->allFunctions);
73         deleteSATEncoder(This->satEncoder);
74         ourfree(This);
75 }
76
77 Set *createSet(CSolver *This, VarType type, uint64_t *elements, uint numelements) {
78         Set *set = allocSet(type, elements, numelements);
79         pushVectorSet(This->allSets, set);
80         return set;
81 }
82
83 Set *createRangeSet(CSolver *This, VarType type, uint64_t lowrange, uint64_t highrange) {
84         Set *set = allocSetRange(type, lowrange, highrange);
85         pushVectorSet(This->allSets, set);
86         return set;
87 }
88
89 MutableSet *createMutableSet(CSolver *This, VarType type) {
90         MutableSet *set = allocMutableSet(type);
91         pushVectorSet(This->allSets, set);
92         return set;
93 }
94
95 void addItem(CSolver *This, MutableSet *set, uint64_t element) {
96         addElementMSet(set, element);
97 }
98
99 uint64_t createUniqueItem(CSolver *This, MutableSet *set) {
100         uint64_t element = set->low++;
101         addElementMSet(set, element);
102         return element;
103 }
104
105 Element *getElementVar(CSolver *This, Set *set) {
106         Element *element = allocElementSet(set);
107         pushVectorElement(This->allElements, element);
108         return element;
109 }
110
111 Element *getElementConst(CSolver *This, VarType type, uint64_t value) {
112         Element *element = allocElementConst(value, type);
113         pushVectorElement(This->allElements, element);
114         return element;
115 }
116
117 Boolean *getBooleanVar(CSolver *This, VarType type) {
118         Boolean *boolean = allocBooleanVar(type);
119         pushVectorBoolean(This->allBooleans, boolean);
120         return boolean;
121 }
122
123 Function *createFunctionOperator(CSolver *This, ArithOp op, Set **domain, uint numDomain, Set *range,OverFlowBehavior overflowbehavior) {
124         Function *function = allocFunctionOperator(op, domain, numDomain, range, overflowbehavior);
125         pushVectorFunction(This->allFunctions, function);
126         return function;
127 }
128
129 Predicate *createPredicateOperator(CSolver *This, CompOp op, Set **domain, uint numDomain) {
130         Predicate *predicate = allocPredicateOperator(op, domain,numDomain);
131         pushVectorPredicate(This->allPredicates, predicate);
132         return predicate;
133 }
134
135 Predicate *createPredicateTable(CSolver *This, Table *table, UndefinedBehavior behavior) {
136         Predicate *predicate = allocPredicateTable(table, behavior);
137         pushVectorPredicate(This->allPredicates, predicate);
138         return predicate;
139 }
140
141 Table *createTable(CSolver *This, Set **domains, uint numDomain, Set *range) {
142         Table *table = allocTable(domains,numDomain,range);
143         pushVectorTable(This->allTables, table);
144         return table;
145 }
146
147 Table *createTableForPredicate(CSolver *solver, Set **domains, uint numDomain) {
148         return createTable(solver, domains, numDomain, NULL);
149 }
150
151 void addTableEntry(CSolver *This, Table *table, uint64_t *inputs, uint inputSize, uint64_t result) {
152         addNewTableEntry(table,inputs, inputSize,result);
153 }
154
155 Function *completeTable(CSolver *This, Table *table, UndefinedBehavior behavior) {
156         Function *function = allocFunctionTable(table, behavior);
157         pushVectorFunction(This->allFunctions,function);
158         return function;
159 }
160
161 Element *applyFunction(CSolver *This, Function *function, Element **array, uint numArrays, Boolean *overflowstatus) {
162         Element *element = allocElementFunction(function,array,numArrays,overflowstatus);
163         pushVectorElement(This->allElements, element);
164         return element;
165 }
166
167 Boolean *applyPredicate(CSolver *This, Predicate *predicate, Element **inputs, uint numInputs) {
168         return applyPredicateTable(This, predicate, inputs, numInputs, NULL);
169 }
170 Boolean *applyPredicateTable(CSolver *This, Predicate *predicate, Element **inputs, uint numInputs, Boolean *undefinedStatus) {
171         Boolean *boolean = allocBooleanPredicate(predicate, inputs, numInputs, undefinedStatus);
172         pushVectorBoolean(This->allBooleans, boolean);
173         return boolean;
174 }
175
176 Boolean *applyLogicalOperation(CSolver *This, LogicOp op, Boolean **array, uint asize) {
177         return allocBooleanLogicArray(This, op, array, asize);
178 }
179
180 void addConstraint(CSolver *This, Boolean *constraint) {
181         addHashSetBoolean(This->constraints, constraint);
182 }
183
184 Order *createOrder(CSolver *This, OrderType type, Set *set) {
185         Order *order = allocOrder(type, set);
186         pushVectorOrder(This->allOrders, order);
187         return order;
188 }
189
190 Boolean *orderConstraint(CSolver *This, Order *order, uint64_t first, uint64_t second) {
191         Boolean *constraint = allocBooleanOrder(order, first, second);
192         pushVectorBoolean(This->allBooleans,constraint);
193         return constraint;
194 }
195
196 int startEncoding(CSolver *This) {
197         naiveEncodingDecision(This);
198         SATEncoder *satEncoder = This->satEncoder;
199         encodeAllSATEncoder(This, satEncoder);
200         int result = solveCNF(satEncoder->cnf);
201         model_print("sat_solver's result:%d\tsolutionSize=%d\n", result, satEncoder->cnf->solver->solutionsize);
202         for (uint i = 1; i <= satEncoder->cnf->solver->solutionsize; i++) {
203                 model_print("%d, ", satEncoder->cnf->solver->solution[i]);
204         }
205         model_print("\n");
206         return result;
207 }
208
209 uint64_t getElementValue(CSolver *This, Element *element) {
210         switch (GETELEMENTTYPE(element)) {
211         case ELEMSET:
212         case ELEMCONST:
213         case ELEMFUNCRETURN:
214                 return getElementValueSATTranslator(This, element);
215         default:
216                 ASSERT(0);
217         }
218         exit(-1);
219 }
220
221 bool getBooleanValue( CSolver *This, Boolean *boolean) {
222         switch (GETBOOLEANTYPE(boolean)) {
223         case BOOLEANVAR:
224                 return getBooleanVariableValueSATTranslator(This, boolean);
225         default:
226                 ASSERT(0);
227         }
228         exit(-1);
229 }
230
231 HappenedBefore getOrderConstraintValue(CSolver *This, Order *order, uint64_t first, uint64_t second) {
232         return getOrderConstraintValueSATTranslator(This, order, first, second);
233 }
234