514bad902442f4dd0e496f1d0bd2256560c321e2
[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         //This is pairwised encoding ...
19         constraint->order->setOrderResolver(new OrderPairResolver(solver, constraint->order));
20         switch ( constraint->order->type) {
21         case SATC_PARTIAL:
22                 return encodePartialOrderSATEncoder(constraint);
23         case SATC_TOTAL:
24                 return encodeTotalOrderSATEncoder(constraint);
25         default:
26                 ASSERT(0);
27         }
28         return E_BOGUS;
29 }
30
31 Edge SATEncoder::inferOrderConstraintFromGraph(Order *order, uint64_t _first, uint64_t _second) {
32         if (order->graph != NULL) {
33                 OrderGraph *graph = order->graph;
34                 OrderNode *first = graph->lookupOrderNodeFromOrderGraph(_first);
35                 OrderNode *second = graph->lookupOrderNodeFromOrderGraph(_second);
36                 if ((first != NULL) && (second != NULL)) {
37                         OrderEdge *edge = graph->lookupOrderEdgeFromOrderGraph(first, second);
38                         if (edge != NULL) {
39                                 if (edge->mustPos)
40                                         return E_True;
41                                 else if (edge->mustNeg)
42                                         return E_False;
43                         }
44                         OrderEdge *invedge = graph->lookupOrderEdgeFromOrderGraph(second, first);
45                         if (invedge != NULL) {
46                                 if (invedge->mustPos)
47                                         return E_False;
48                                 else if (invedge->mustNeg)
49                                         return E_True;
50                         }
51                 }
52         }
53         return E_NULL;
54 }
55
56 Edge SATEncoder::getPairConstraint(Order *order, OrderPair *pair) {
57         Edge gvalue = inferOrderConstraintFromGraph(order, pair->first, pair->second);
58         if (!edgeIsNull(gvalue))
59                 return gvalue;
60
61         HashtableOrderPair *table = order->orderPairTable;
62         bool negate = false;
63         OrderPair flipped;
64         if (pair->first < pair->second) {
65                 negate = true;
66                 flipped.first = pair->second;
67                 flipped.second = pair->first;
68                 pair = &flipped;
69         }
70         Edge constraint;
71         if (!(table->contains(pair))) {
72                 constraint = getNewVarSATEncoder();
73                 OrderPair *paircopy = new OrderPair(pair->first, pair->second, constraint);
74                 table->put(paircopy, paircopy);
75         } else
76                 constraint = table->get(pair)->constraint;
77
78         return negate ? constraintNegate(constraint) : constraint;
79 }
80
81 Edge SATEncoder::encodeTotalOrderSATEncoder(BooleanOrder *boolOrder) {
82         ASSERT(boolOrder->order->type == SATC_TOTAL);
83         if (boolOrder->order->orderPairTable == NULL) {
84                 boolOrder->order->initializeOrderHashtable();
85                 bool doOptOrderStructure = GETVARTUNABLE(solver->getTuner(), boolOrder->order->type, OPTIMIZEORDERSTRUCTURE, &onoff);
86                 if (doOptOrderStructure) {
87                         boolOrder->order->graph = buildMustOrderGraph(boolOrder->order);
88                         reachMustAnalysis(solver, boolOrder->order->graph, true);
89                 }
90                 createAllTotalOrderConstraintsSATEncoder(boolOrder->order);
91         }
92         OrderPair pair(boolOrder->first, boolOrder->second, E_NULL);
93         Edge constraint = getPairConstraint(boolOrder->order, &pair);
94         return constraint;
95 }
96
97
98 void SATEncoder::createAllTotalOrderConstraintsSATEncoder(Order *order) {
99 #ifdef CONFIG_DEBUG
100         model_print("in total order ...\n");
101 #endif
102         ASSERT(order->type == SATC_TOTAL);
103         Set *set = order->set;
104         uint size = order->set->getSize();
105         for (uint i = 0; i < size; i++) {
106                 uint64_t valueI = set->getMemberAt(i);
107                 for (uint j = i + 1; j < size; j++) {
108                         uint64_t valueJ = set->getMemberAt(j);
109                         OrderPair pairIJ(valueI, valueJ, E_NULL);
110                         Edge constIJ = getPairConstraint(order, &pairIJ);
111                         for (uint k = j + 1; k < size; k++) {
112                                 uint64_t valueK = set->getMemberAt(k);
113                                 OrderPair pairJK(valueJ, valueK, E_NULL);
114                                 OrderPair pairIK(valueI, valueK, E_NULL);
115                                 Edge constIK = getPairConstraint(order, &pairIK);
116                                 Edge constJK = getPairConstraint(order, &pairJK);
117                                 addConstraintCNF(cnf, generateTransOrderConstraintSATEncoder(constIJ, constJK, constIK));
118                         }
119                 }
120         }
121 }
122
123 Edge getOrderConstraint(HashtableOrderPair *table, OrderPair *pair) {
124         ASSERT(pair->first != pair->second);
125         bool negate = false;
126         OrderPair flipped;
127         if (pair->first < pair->second) {
128                 negate = true;
129                 flipped.first = pair->second;
130                 flipped.second = pair->first;
131                 pair = &flipped;
132         }
133         if (!table->contains(pair)) {
134                 return E_NULL;
135         }
136         Edge constraint = table->get(pair)->constraint;
137         ASSERT(!edgeIsNull(constraint));
138         return negate ? constraintNegate(constraint) : constraint;
139 }
140
141 Edge SATEncoder::generateTransOrderConstraintSATEncoder(Edge constIJ,Edge constJK,Edge constIK) {
142         Edge carray[] = {constIJ, constJK, constraintNegate(constIK)};
143         Edge loop1 = constraintOR(cnf, 3, carray);
144         Edge carray2[] = {constraintNegate(constIJ), constraintNegate(constJK), constIK};
145         Edge loop2 = constraintOR(cnf, 3, carray2 );
146         return constraintAND2(cnf, loop1, loop2);
147 }
148
149 Edge SATEncoder::encodePartialOrderSATEncoder(BooleanOrder *constraint) {
150         ASSERT(constraint->order->type == SATC_PARTIAL);
151         return E_BOGUS;
152 }