Moving OrderPairTable to the resolver + hide translation complexity in OrderPair...
[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::encodeTotalOrderSATEncoder(BooleanOrder *boolOrder) {
79         ASSERT(boolOrder->order->type == SATC_TOTAL);
80         if (boolOrder->order->encoding.resolver == NULL) {
81                 //This is pairwised encoding ...
82                 boolOrder->order->setOrderResolver(new OrderPairResolver(solver, boolOrder->order));
83                 bool doOptOrderStructure = GETVARTUNABLE(solver->getTuner(), boolOrder->order->type, OPTIMIZEORDERSTRUCTURE, &onoff);
84                 if (doOptOrderStructure) {
85                         boolOrder->order->graph = buildMustOrderGraph(boolOrder->order);
86                         reachMustAnalysis(solver, boolOrder->order->graph, true);
87                 }
88                 createAllTotalOrderConstraintsSATEncoder(boolOrder->order);
89         }
90         OrderPair pair(boolOrder->first, boolOrder->second, E_NULL);
91         Edge constraint = getPairConstraint(boolOrder->order, &pair);
92         return constraint;
93 }
94
95
96 void SATEncoder::createAllTotalOrderConstraintsSATEncoder(Order *order) {
97 #ifdef CONFIG_DEBUG
98         model_print("in total order ...\n");
99 #endif
100         ASSERT(order->type == SATC_TOTAL);
101         Set *set = order->set;
102         uint size = order->set->getSize();
103         for (uint i = 0; i < size; i++) {
104                 uint64_t valueI = set->getMemberAt(i);
105                 for (uint j = i + 1; j < size; j++) {
106                         uint64_t valueJ = set->getMemberAt(j);
107                         OrderPair pairIJ(valueI, valueJ, E_NULL);
108                         Edge constIJ = getPairConstraint(order, &pairIJ);
109                         for (uint k = j + 1; k < size; k++) {
110                                 uint64_t valueK = set->getMemberAt(k);
111                                 OrderPair pairJK(valueJ, valueK, E_NULL);
112                                 OrderPair pairIK(valueI, valueK, E_NULL);
113                                 Edge constIK = getPairConstraint(order, &pairIK);
114                                 Edge constJK = getPairConstraint(order, &pairJK);
115                                 addConstraintCNF(cnf, generateTransOrderConstraintSATEncoder(constIJ, constJK, constIK));
116                         }
117                 }
118         }
119 }
120
121 Edge SATEncoder::generateTransOrderConstraintSATEncoder(Edge constIJ,Edge constJK,Edge constIK) {
122         Edge carray[] = {constIJ, constJK, constraintNegate(constIK)};
123         Edge loop1 = constraintOR(cnf, 3, carray);
124         Edge carray2[] = {constraintNegate(constIJ), constraintNegate(constJK), constIK};
125         Edge loop2 = constraintOR(cnf, 3, carray2 );
126         return constraintAND2(cnf, loop1, loop2);
127 }
128
129 Edge SATEncoder::encodePartialOrderSATEncoder(BooleanOrder *constraint) {
130         ASSERT(constraint->order->type == SATC_PARTIAL);
131         return E_BOGUS;
132 }