edit
[satune.git] / src / Backend / satorderencoder.cc
1 #include "satencoder.h"
2 #include "structs.h"
3 #include "common.h"
4 #include "order.h"
5 #include "csolver.h"
6 #include "orderpair.h"
7 #include "set.h"
8 #include "tunable.h"
9 #include "orderanalysis.h"
10 #include "ordergraph.h"
11 #include "orderedge.h"
12 #include "element.h"
13 #include "predicate.h"
14 #include "orderelement.h"
15 #include "orderpairresolver.h"
16
17 Edge SATEncoder::encodeOrderSATEncoder(BooleanOrder *constraint) {
18         switch ( constraint->order->type) {
19         case SATC_PARTIAL:
20                 return encodePartialOrderSATEncoder(constraint);
21         case SATC_TOTAL:
22                 return encodeTotalOrderSATEncoder(constraint);
23         default:
24                 ASSERT(0);
25         }
26         return E_BOGUS;
27 }
28
29 Edge SATEncoder::inferOrderConstraintFromGraph(Order *order, uint64_t _first, uint64_t _second) {
30         if (order->graph != NULL) {
31                 OrderGraph *graph = order->graph;
32                 OrderNode *first = graph->lookupOrderNodeFromOrderGraph(_first);
33                 OrderNode *second = graph->lookupOrderNodeFromOrderGraph(_second);
34                 if ((first != NULL) && (second != NULL)) {
35                         OrderEdge *edge = graph->lookupOrderEdgeFromOrderGraph(first, second);
36                         if (edge != NULL) {
37                                 if (edge->mustPos)
38                                         return E_True;
39                                 else if (edge->mustNeg)
40                                         return E_False;
41                         }
42                         OrderEdge *invedge = graph->lookupOrderEdgeFromOrderGraph(second, first);
43                         if (invedge != NULL) {
44                                 if (invedge->mustPos)
45                                         return E_False;
46                                 else if (invedge->mustNeg)
47                                         return E_True;
48                         }
49                 }
50         }
51         return E_NULL;
52 }
53
54 Edge SATEncoder::getPairConstraint(Order *order, OrderPair *pair) {
55         Edge gvalue = inferOrderConstraintFromGraph(order, pair->first, pair->second);
56         if (!edgeIsNull(gvalue))
57                 return gvalue;
58
59         HashtableOrderPair *table = order->getOrderPairTable();
60         bool negate = false;
61         OrderPair flipped;
62         if (pair->first < pair->second) {
63                 negate = true;
64                 flipped.first = pair->second;
65                 flipped.second = pair->first;
66                 pair = &flipped;
67         }
68         OrderPair *tmp;
69         if (!(table->contains(pair))) {
70                 tmp = new OrderPair(pair->first, pair->second, getNewVarSATEncoder());
71                 table->put(tmp, tmp);
72         } else {
73                 tmp = table->get(pair);
74         }
75         return negate ? tmp->getNegatedConstraint() : tmp->getConstraint();
76 }
77
78 Edge SATEncoder::getPartialPairConstraint(Order *order, OrderPair *pair) {
79         Edge gvalue = inferOrderConstraintFromGraph(order, pair->first, pair->second);
80         if (!edgeIsNull(gvalue))
81                 return gvalue;
82
83         HashtableOrderPair *table = order->getOrderPairTable();
84
85         OrderPair *tmp;
86         if (!(table->contains(pair))) {
87                 Edge constraint = getNewVarSATEncoder();
88                 tmp = new OrderPair(pair->first, pair->second, constraint);
89                 table->put(tmp, tmp);
90                 Edge constraint2 = getNewVarSATEncoder();
91                 OrderPair *swap = new OrderPair(pair->second, pair->first, constraint2);
92                 table->put(swap, swap);
93                 addConstraintCNF(cnf, constraintNegate(constraintAND2(cnf, constraint, constraint2)));
94         } else {
95                 tmp = table->get(pair);
96         }
97         return tmp->getConstraint();
98 }
99
100 Edge SATEncoder::encodeTotalOrderSATEncoder(BooleanOrder *boolOrder) {
101         ASSERT(boolOrder->order->type == SATC_TOTAL);
102         if (boolOrder->order->encoding.resolver == NULL) {
103                 //This is pairwised encoding ...
104                 boolOrder->order->setOrderResolver(new OrderPairResolver(solver, boolOrder->order));
105                 bool doOptOrderStructure = GETVARTUNABLE(solver->getTuner(), boolOrder->order->type, OPTIMIZEORDERSTRUCTURE, &onoff);
106                 if (doOptOrderStructure) {
107                         ASSERT(boolOrder->order->graph == NULL);
108                         boolOrder->order->graph = buildMustOrderGraph(boolOrder->order);
109                         reachMustAnalysis(solver, boolOrder->order->graph, true);
110                 }
111                 createAllTotalOrderConstraintsSATEncoder(boolOrder->order);
112         }
113         OrderPair pair(boolOrder->first, boolOrder->second, E_NULL);
114         Edge constraint = getPairConstraint(boolOrder->order, &pair);
115         return constraint;
116 }
117
118
119 void SATEncoder::createAllTotalOrderConstraintsSATEncoder(Order *order) {
120 #ifdef CONFIG_DEBUG
121         model_print("in total order ...\n");
122 #endif
123         ASSERT(order->type == SATC_TOTAL);
124         Set *set = order->set;
125         uint size = order->set->getSize();
126         for (uint i = 0; i < size; i++) {
127                 uint64_t valueI = set->getElement(i);
128                 for (uint j = i + 1; j < size; j++) {
129                         uint64_t valueJ = set->getElement(j);
130                         OrderPair pairIJ(valueI, valueJ, E_NULL);
131                         Edge constIJ = getPairConstraint(order, &pairIJ);
132                         for (uint k = j + 1; k < size; k++) {
133                                 uint64_t valueK = set->getElement(k);
134                                 OrderPair pairJK(valueJ, valueK, E_NULL);
135                                 OrderPair pairIK(valueI, valueK, E_NULL);
136                                 Edge constIK = getPairConstraint(order, &pairIK);
137                                 Edge constJK = getPairConstraint(order, &pairJK);
138                                 addConstraintCNF(cnf, generateTransOrderConstraintSATEncoder(constIJ, constJK, constIK));
139                         }
140                 }
141         }
142 }
143
144 Edge SATEncoder::generateTransOrderConstraintSATEncoder(Edge constIJ,Edge constJK,Edge constIK) {
145         Edge carray[] = {constIJ, constJK, constraintNegate(constIK)};
146         Edge loop1 = constraintOR(cnf, 3, carray);
147         Edge carray2[] = {constraintNegate(constIJ), constraintNegate(constJK), constIK};
148         Edge loop2 = constraintOR(cnf, 3, carray2 );
149         return constraintAND2(cnf, loop1, loop2);
150 }
151
152 Edge SATEncoder::generatePartialOrderConstraintsSATEncoder(Edge ij,Edge ji, Edge jk, Edge kj,Edge ik, Edge ki) {
153         Edge uoIJ = constraintAND2(cnf, constraintNegate(ij), constraintNegate(ji));
154         Edge uoJK = constraintAND2(cnf, constraintNegate(jk), constraintNegate(kj));
155         Edge uoIK = constraintAND2(cnf, constraintNegate(ik), constraintNegate(ki));
156
157         Edge t1[] = {ij, jk, ik};
158         Edge t2[] = {ji, jk, ik};
159         Edge t3[] = {ij, kj, ki};
160         Edge t4[] = {ij, kj, ik};
161         Edge t5[] = {ji, jk, ki};
162         Edge t6[] = {ji, kj, ki};
163         Edge ct1 = constraintAND(cnf, 3, t1);
164         Edge ct2 = constraintAND(cnf, 3, t2);
165         Edge ct3 = constraintAND(cnf, 3, t3);
166         Edge ct4 = constraintAND(cnf, 3, t4);
167         Edge ct5 = constraintAND(cnf, 3, t5);
168         Edge ct6 = constraintAND(cnf, 3, t6);
169
170         Edge p1[] = {uoIJ, jk, ik};
171         Edge p2[] = {ij, kj, uoIK};
172         Edge p3[] = {ji, uoJK, ki};
173         Edge p4[] = {uoIJ, kj, ki};
174         Edge p5[] = {ji, jk, uoIK};
175         Edge p6[] = {ij, uoJK, ik};
176         Edge cp1 = constraintAND(cnf, 3, p1);
177         Edge cp2 = constraintAND(cnf, 3, p2);
178         Edge cp3 = constraintAND(cnf, 3, p3);
179         Edge cp4 = constraintAND(cnf, 3, p4);
180         Edge cp5 = constraintAND(cnf, 3, p5);
181         Edge cp6 = constraintAND(cnf, 3, p6);
182
183         Edge o1[] = {uoIJ, uoJK, ik};
184         Edge o2[] = {ij, uoJK, uoIK};
185         Edge o3[] = {uoIK, jk, uoIK};
186         Edge o4[] = {ji, uoJK, uoIK};
187         Edge o5[] = {uoIJ, uoJK, ki};
188         Edge o6[] = {uoIJ, kj, uoIK};
189         Edge co1 = constraintAND(cnf, 3, o1);
190         Edge co2 = constraintAND(cnf, 3, o2);
191         Edge co3 = constraintAND(cnf, 3, o3);
192         Edge co4 = constraintAND(cnf, 3, o4);
193         Edge co5 = constraintAND(cnf, 3, o5);
194         Edge co6 = constraintAND(cnf, 3, o6);
195
196         Edge unorder [] = {uoIJ, uoJK, uoIK};
197         Edge cunorder = constraintAND(cnf, 3, unorder);
198
199
200         Edge res[] = {ct1,ct2,ct3,ct4,ct5,ct6,
201                                                                 cp1,cp2,cp3,cp4,cp5,cp6,
202                                                                 co1,co2,co3,co4,co5,co6,
203                                                                 cunorder};
204         return constraintOR(cnf, 19, res);
205 }
206
207 Edge SATEncoder::encodePartialOrderSATEncoder(BooleanOrder *boolOrder) {
208         ASSERT(boolOrder->order->type == SATC_PARTIAL);
209         if (boolOrder->order->encoding.resolver == NULL) {
210                 //This is pairwised encoding ...
211                 boolOrder->order->setOrderResolver(new OrderPairResolver(solver, boolOrder->order));
212                 bool doOptOrderStructure = GETVARTUNABLE(solver->getTuner(), boolOrder->order->type, OPTIMIZEORDERSTRUCTURE, &onoff);
213                 if (doOptOrderStructure) {
214                         boolOrder->order->graph = buildMustOrderGraph(boolOrder->order);
215                         reachMustAnalysis(solver, boolOrder->order->graph, true);
216                 }
217                 createAllPartialOrderConstraintsSATEncoder(boolOrder->order);
218         }
219         OrderPair pair(boolOrder->first, boolOrder->second, E_NULL);
220         Edge constraint = getPartialPairConstraint(boolOrder->order, &pair);
221         return constraint;
222 }
223
224
225 void SATEncoder::createAllPartialOrderConstraintsSATEncoder(Order *order) {
226 #ifdef CONFIG_DEBUG
227         model_print("in partial order ...\n");
228 #endif
229         ASSERT(order->type == SATC_TOTAL);
230         Set *set = order->set;
231         uint size = order->set->getSize();
232         for (uint i = 0; i < size; i++) {
233                 uint64_t valueI = set->getElement(i);
234                 for (uint j = i + 1; j < size; j++) {
235                         uint64_t valueJ = set->getElement(j);
236                         OrderPair pairIJ(valueI, valueJ, E_NULL);
237                         OrderPair pairJI(valueJ, valueI, E_NULL);
238                         Edge constIJ = getPartialPairConstraint(order, &pairIJ);
239                         Edge constJI = getPartialPairConstraint(order, &pairJI);
240                         for (uint k = j + 1; k < size; k++) {
241                                 uint64_t valueK = set->getElement(k);
242                                 OrderPair pairJK(valueJ, valueK, E_NULL);
243                                 OrderPair pairIK(valueI, valueK, E_NULL);
244                                 Edge constIK = getPartialPairConstraint(order, &pairIK);
245                                 Edge constJK = getPartialPairConstraint(order, &pairJK);
246                                 OrderPair pairKJ(valueK, valueJ, E_NULL);
247                                 OrderPair pairKI(valueK, valueI, E_NULL);
248                                 Edge constKI = getPartialPairConstraint(order, &pairKI);
249                                 Edge constKJ = getPartialPairConstraint(order, &pairKJ);
250                                 addConstraintCNF(cnf, generatePartialOrderConstraintsSATEncoder(constIJ, constJI,
251                                                                                                                                                                                                                                                                                                 constJK, constKJ, constIK, constKI));
252                         }
253                 }
254         }
255 }
256
257