Small edits
[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=allocDefVectorBoolean();
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         deleteVectorBoolean(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 Boolean * getBooleanVar(CSolver *This, VarType type) {
112         Boolean* boolean= allocBooleanVar(type);
113         pushVectorBoolean(This->allBooleans, boolean);
114         return boolean;
115 }
116
117 Function * createFunctionOperator(CSolver *This, ArithOp op, Set ** domain, uint numDomain, Set * range,OverFlowBehavior overflowbehavior) {
118         Function* function = allocFunctionOperator(op, domain, numDomain, range, overflowbehavior);
119         pushVectorFunction(This->allFunctions, function);
120         return function;
121 }
122
123 Predicate * createPredicateOperator(CSolver *This, CompOp op, Set ** domain, uint numDomain) {
124         Predicate* predicate= allocPredicateOperator(op, domain,numDomain);
125         pushVectorPredicate(This->allPredicates, predicate);
126         return predicate;
127 }
128
129 Table * createTable(CSolver *This, Set **domains, uint numDomain, Set * range) {
130         Table* table= allocTable(domains,numDomain,range);
131         pushVectorTable(This->allTables, table);
132         return table;
133 }
134
135 void addTableEntry(CSolver *This, Table* table, uint64_t* inputs, uint inputSize, uint64_t result) {
136         addNewTableEntry(table,inputs, inputSize,result);
137 }
138
139 Function * completeTable(CSolver *This, Table * table) {
140         Function* function = allocFunctionTable(table);
141         pushVectorFunction(This->allFunctions,function);
142         return function;
143 }
144
145 Element * applyFunction(CSolver *This, Function * function, Element ** array, uint numArrays, Boolean * overflowstatus) {
146         Element* element= allocElementFunction(function,array,numArrays,overflowstatus);
147         pushVectorElement(This->allElements, element);
148         return element;
149 }
150
151 Boolean * applyPredicate(CSolver *This, Predicate * predicate, Element ** inputs, uint numInputs) {
152         Boolean* boolean= allocBooleanPredicate(predicate, inputs, numInputs);
153         pushVectorBoolean(This->allBooleans, boolean);
154         return boolean;
155 }
156
157 Boolean * applyLogicalOperation(CSolver *This, LogicOp op, Boolean ** array, uint asize) {
158         return allocBooleanLogicArray(This, op, array, asize);
159 }
160
161 void addConstraint(CSolver *This, Boolean * constraint) {
162         pushVectorBoolean(This->constraints, constraint);
163 }
164
165 Order * createOrder(CSolver *This, OrderType type, Set * set) {
166         Order* order = allocOrder(type, set);
167         pushVectorOrder(This->allOrders, order);
168         return order;
169 }
170
171 Boolean * orderConstraint(CSolver *This, Order * order, uint64_t first, uint64_t second) {
172         Boolean* constraint = allocBooleanOrder(order, first, second);
173         pushVectorBoolean(This->allBooleans,constraint);
174         return constraint;
175 }
176
177 void startEncoding(CSolver* This){
178         naiveEncodingDecision(This);
179         SATEncoder* satEncoder = This->satEncoder;
180         encodeAllSATEncoder(This, satEncoder);
181         int result= solveCNF(satEncoder->cnf);
182         model_print("sat_solver's result:%d\tsolutionSize=%d\n", result, satEncoder->cnf->solver->solutionsize);
183         for(uint i=1; i<=satEncoder->cnf->solver->solutionsize; i++){
184                 model_print("%d, ", satEncoder->cnf->solver->solution[i]);
185         }
186         model_print("\n");
187 }
188
189 uint64_t getElementValue(CSolver* This, Element* element){
190         switch(GETELEMENTTYPE(element)){
191                 case ELEMSET:
192                         return getElementValueSATTranslator(This, element);
193                 default:
194                         ASSERT(0);
195         }
196         exit(-1);
197 }
198
199 bool getBooleanValue( CSolver* This , Boolean* boolean){
200         switch(GETBOOLEANTYPE(boolean)){
201                 case BOOLEANVAR:
202                         return getBooleanVariableValueSATTranslator(This, boolean);
203                 default:
204                         ASSERT(0);
205         }
206         exit(-1);
207 }