1 #include "satencoder.h"
9 #include "orderanalysis.h"
10 #include "ordergraph.h"
11 #include "orderedge.h"
13 #include "predicate.h"
14 #include "orderelement.h"
15 #include "orderpairresolver.h"
17 Edge SATEncoder::encodeOrderSATEncoder(BooleanOrder *constraint) {
18 switch ( constraint->order->type) {
20 return encodePartialOrderSATEncoder(constraint);
22 return encodeTotalOrderSATEncoder(constraint);
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);
39 else if (edge->mustNeg)
42 OrderEdge *invedge = graph->lookupOrderEdgeFromOrderGraph(second, first);
43 if (invedge != NULL) {
46 else if (invedge->mustNeg)
54 Edge SATEncoder::getPairConstraint(Order *order, OrderPair *pair) {
55 Edge gvalue = inferOrderConstraintFromGraph(order, pair->first, pair->second);
56 if (!edgeIsNull(gvalue))
59 HashtableOrderPair *table = order->getOrderPairTable();
62 if (pair->first < pair->second) {
64 flipped.first = pair->second;
65 flipped.second = pair->first;
69 if (!(table->contains(pair))) {
70 tmp = new OrderPair(pair->first, pair->second, getNewVarSATEncoder());
73 tmp = table->get(pair);
75 return negate ? tmp->getNegatedConstraint() : tmp->getConstraint();
78 Edge SATEncoder::getPartialPairConstraint(Order *order, OrderPair *pair) {
79 Edge gvalue = inferOrderConstraintFromGraph(order, pair->first, pair->second);
80 if (!edgeIsNull(gvalue))
83 HashtableOrderPair *table = order->getOrderPairTable();
86 if (!(table->contains(pair))) {
87 Edge constraint = getNewVarSATEncoder();
88 tmp = new OrderPair(pair->first, pair->second, constraint);
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)));
95 tmp = table->get(pair);
97 return tmp->getConstraint();
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);
111 createAllTotalOrderConstraintsSATEncoder(boolOrder->order);
113 OrderPair pair(boolOrder->first, boolOrder->second, E_NULL);
114 Edge constraint = getPairConstraint(boolOrder->order, &pair);
119 void SATEncoder::createAllTotalOrderConstraintsSATEncoder(Order *order) {
121 model_print("in total order ...\n");
123 ASSERT(order->type == SATC_TOTAL);
124 SetIterator64Int *iti = order->getUsedIterator();
125 while (iti->hasNext()) {
126 uint64_t valueI = iti->next();
127 SetIterator64Int *itj = new SetIterator64Int(iti);
128 while (itj->hasNext()) {
129 uint64_t valueJ = itj->next();
130 OrderPair pairIJ(valueI, valueJ, E_NULL);
131 Edge constIJ = getPairConstraint(order, &pairIJ);
132 SetIterator64Int *itk = new SetIterator64Int(itj);
133 while (itk->hasNext()) {
134 uint64_t valueK = itk->next();
135 OrderPair pairJK(valueJ, valueK, E_NULL);
136 OrderPair pairIK(valueI, valueK, E_NULL);
137 Edge constIK = getPairConstraint(order, &pairIK);
138 Edge constJK = getPairConstraint(order, &pairJK);
139 addConstraintCNF(cnf, generateTransOrderConstraintSATEncoder(constIJ, constJK, constIK));
145 Edge SATEncoder::generateTransOrderConstraintSATEncoder(Edge constIJ,Edge constJK,Edge constIK) {
146 Edge carray[] = {constIJ, constJK, constraintNegate(constIK)};
147 Edge loop1 = constraintOR(cnf, 3, carray);
148 Edge carray2[] = {constraintNegate(constIJ), constraintNegate(constJK), constIK};
149 Edge loop2 = constraintOR(cnf, 3, carray2 );
150 return constraintAND2(cnf, loop1, loop2);
153 Edge SATEncoder::generatePartialOrderConstraintsSATEncoder(Edge ij,Edge ji, Edge jk, Edge kj,Edge ik, Edge ki) {
154 Edge uoIJ = constraintAND2(cnf, constraintNegate(ij), constraintNegate(ji));
155 Edge uoJK = constraintAND2(cnf, constraintNegate(jk), constraintNegate(kj));
156 Edge uoIK = constraintAND2(cnf, constraintNegate(ik), constraintNegate(ki));
158 Edge t1[] = {ij, jk, ik};
159 Edge t2[] = {ji, jk, ik};
160 Edge t3[] = {ij, kj, ki};
161 Edge t4[] = {ij, kj, ik};
162 Edge t5[] = {ji, jk, ki};
163 Edge t6[] = {ji, kj, ki};
164 Edge ct1 = constraintAND(cnf, 3, t1);
165 Edge ct2 = constraintAND(cnf, 3, t2);
166 Edge ct3 = constraintAND(cnf, 3, t3);
167 Edge ct4 = constraintAND(cnf, 3, t4);
168 Edge ct5 = constraintAND(cnf, 3, t5);
169 Edge ct6 = constraintAND(cnf, 3, t6);
171 Edge p1[] = {uoIJ, jk, ik};
172 Edge p2[] = {ij, kj, uoIK};
173 Edge p3[] = {ji, uoJK, ki};
174 Edge p4[] = {uoIJ, kj, ki};
175 Edge p5[] = {ji, jk, uoIK};
176 Edge p6[] = {ij, uoJK, ik};
177 Edge cp1 = constraintAND(cnf, 3, p1);
178 Edge cp2 = constraintAND(cnf, 3, p2);
179 Edge cp3 = constraintAND(cnf, 3, p3);
180 Edge cp4 = constraintAND(cnf, 3, p4);
181 Edge cp5 = constraintAND(cnf, 3, p5);
182 Edge cp6 = constraintAND(cnf, 3, p6);
184 Edge o1[] = {uoIJ, uoJK, ik};
185 Edge o2[] = {ij, uoJK, uoIK};
186 Edge o3[] = {uoIK, jk, uoIK};
187 Edge o4[] = {ji, uoJK, uoIK};
188 Edge o5[] = {uoIJ, uoJK, ki};
189 Edge o6[] = {uoIJ, kj, uoIK};
190 Edge co1 = constraintAND(cnf, 3, o1);
191 Edge co2 = constraintAND(cnf, 3, o2);
192 Edge co3 = constraintAND(cnf, 3, o3);
193 Edge co4 = constraintAND(cnf, 3, o4);
194 Edge co5 = constraintAND(cnf, 3, o5);
195 Edge co6 = constraintAND(cnf, 3, o6);
197 Edge unorder [] = {uoIJ, uoJK, uoIK};
198 Edge cunorder = constraintAND(cnf, 3, unorder);
201 Edge res[] = {ct1,ct2,ct3,ct4,ct5,ct6,
202 cp1,cp2,cp3,cp4,cp5,cp6,
203 co1,co2,co3,co4,co5,co6,
205 return constraintOR(cnf, 19, res);
208 Edge SATEncoder::encodePartialOrderSATEncoder(BooleanOrder *boolOrder) {
209 ASSERT(boolOrder->order->type == SATC_PARTIAL);
210 if (boolOrder->order->encoding.resolver == NULL) {
211 //This is pairwised encoding ...
212 boolOrder->order->setOrderResolver(new OrderPairResolver(solver, boolOrder->order));
213 bool doOptOrderStructure = GETVARTUNABLE(solver->getTuner(), boolOrder->order->type, OPTIMIZEORDERSTRUCTURE, &onoff);
214 if (doOptOrderStructure) {
215 boolOrder->order->graph = buildMustOrderGraph(boolOrder->order);
216 reachMustAnalysis(solver, boolOrder->order->graph, true);
218 createAllPartialOrderConstraintsSATEncoder(boolOrder->order);
220 OrderPair pair(boolOrder->first, boolOrder->second, E_NULL);
221 Edge constraint = getPartialPairConstraint(boolOrder->order, &pair);
226 void SATEncoder::createAllPartialOrderConstraintsSATEncoder(Order *order) {
228 model_print("in partial order ...\n");
230 ASSERT(order->type == SATC_TOTAL);
231 SetIterator64Int *iti = order->getUsedIterator();
232 while (iti->hasNext()) {
233 uint64_t valueI = iti->next();
234 SetIterator64Int *itj = new SetIterator64Int(iti);
235 while (itj->hasNext()) {
236 uint64_t valueJ = itj->next();
237 OrderPair pairIJ(valueI, valueJ, E_NULL);
238 OrderPair pairJI(valueJ, valueI, E_NULL);
239 Edge constIJ = getPartialPairConstraint(order, &pairIJ);
240 Edge constJI = getPartialPairConstraint(order, &pairJI);
241 SetIterator64Int *itk = new SetIterator64Int(itj);
242 while (itk->hasNext()) {
243 uint64_t valueK = itk->next();
244 OrderPair pairJK(valueJ, valueK, E_NULL);
245 OrderPair pairIK(valueI, valueK, E_NULL);
246 Edge constIK = getPartialPairConstraint(order, &pairIK);
247 Edge constJK = getPartialPairConstraint(order, &pairJK);
248 OrderPair pairKJ(valueK, valueJ, E_NULL);
249 OrderPair pairKI(valueK, valueI, E_NULL);
250 Edge constKI = getPartialPairConstraint(order, &pairKI);
251 Edge constKJ = getPartialPairConstraint(order, &pairKJ);
252 addConstraintCNF(cnf, generatePartialOrderConstraintsSATEncoder(constIJ, constJI,
253 constJK, constKJ, constIK, constKI));