Merge
[satune.git] / src / csolver.cc
index be4a3a88063882cc05f5cfafca9218724b870f72..71205f68aab557baf933689510435ee4f312d328 100644 (file)
@@ -48,7 +48,8 @@ CSolver::~CSolver() {
 
        size = allElements.getSize();
        for (uint i = 0; i < size; i++) {
-               delete allElements.get(i);
+                Element* el = allElements.get(i);
+               delete el;
        }
 
        size = allTables.getSize();
@@ -65,7 +66,6 @@ CSolver::~CSolver() {
        for (uint i = 0; i < size; i++) {
                delete allOrders.get(i);
        }
-
        size = allFunctions.getSize();
        for (uint i = 0; i < size; i++) {
                delete allFunctions.get(i);
@@ -87,23 +87,22 @@ CSolver *CSolver::clone() {
        return copy;
 }
 
+CSolver* CSolver::deserialize(const char * file){
+       model_print("deserializing ...\n");
+       Deserializer deserializer(file);
+       return deserializer.deserialize();
+}
+
 void CSolver::serialize() {
        model_print("serializing ...\n");
-       {
-               Serializer serializer("dump");
-               SetIteratorBooleanEdge *it = getConstraints();
-               while (it->hasNext()) {
-                       BooleanEdge b = it->next();
-                       serializeBooleanEdge(&serializer, b);
-               }
-               delete it;
-       }
-       model_print("deserializing ...\n");
-       {
-               Deserializer deserializer("dump");
-               deserializer.deserialize();
+        printConstraints();
+       Serializer serializer("dump");
+       SetIteratorBooleanEdge *it = getConstraints();
+       while (it->hasNext()) {
+               BooleanEdge b = it->next();
+               serializeBooleanEdge(&serializer, b, true);
        }
-
+       delete it;
 }
 
 Set *CSolver::createSet(VarType type, uint64_t *elements, uint numelements) {
@@ -175,6 +174,7 @@ Element *CSolver::getElementConst(VarType type, uint64_t value) {
        }
 }
 
+
 Element *CSolver::applyFunction(Function *function, Element **array, uint numArrays, BooleanEdge overflowstatus) {
        Element *element = new ElementFunction(function,array,numArrays,overflowstatus);
        Element *e = elemMap.get(element);
@@ -311,13 +311,11 @@ BooleanEdge CSolver::applyLogicalOperation(LogicOp op, BooleanEdge *array, uint
        }
        case SATC_IFF: {
                for (uint i = 0; i < 2; i++) {
-                       if (array[i]->type == BOOLCONST) {
-                               if (isTrue(array[i])) { // It can be undefined
-                                       return array[1 - i];
-                               } else if (isFalse(array[i])) {
-                                       newarray[0] = array[1 - i];
-                                       return applyLogicalOperation(SATC_NOT, newarray, 1);
-                               }
+                       if (isTrue(array[i])) { // It can be undefined
+                               return array[1 - i];
+                       } else if (isFalse(array[i])) {
+                               newarray[0] = array[1 - i];
+                               return applyLogicalOperation(SATC_NOT, newarray, 1);
                        } else if (array[i]->type == LOGICOP) {
                                BooleanLogic *b = (BooleanLogic *)array[i].getBoolean();
                                if (b->replaced) {
@@ -341,12 +339,10 @@ BooleanEdge CSolver::applyLogicalOperation(LogicOp op, BooleanEdge *array, uint
                                if (((BooleanLogic *)b.getBoolean())->replaced)
                                        return rewriteLogicalOperation(op, array, asize);
                        }
-                       if (b->type == BOOLCONST) {
-                               if (isTrue(b))
-                                       continue;
-                               else {
-                                       return boolFalse;
-                               }
+                       if (isTrue(b))
+                               continue;
+                       else if (isFalse(b)) {
+                               return boolFalse;
                        } else
                                newarray[newindex++] = b;
                }
@@ -389,7 +385,7 @@ BooleanEdge CSolver::orderConstraint(Order *order, uint64_t first, uint64_t seco
        //      ASSERT(first != second);
        if (first == second)
                return getBooleanFalse();
-       
+
        bool negate = false;
        if (order->type == SATC_TOTAL) {
                if (first > second) {
@@ -416,6 +412,12 @@ BooleanEdge CSolver::orderConstraint(Order *order, uint64_t first, uint64_t seco
 }
 
 void CSolver::addConstraint(BooleanEdge constraint) {
+#ifdef TRACE_DEBUG
+        model_println("****New Constraint******");
+#endif
+        if(constraint.isNegated())
+                model_print("!");
+        constraint.getBoolean()->print();
        if (isTrue(constraint))
                return;
        else if (isFalse(constraint)) {
@@ -476,10 +478,10 @@ int CSolver::solve() {
        //IntegerEncodingTransform iet(this);
        //iet.doTransform();
 
-       //EncodingGraph eg(this);
-       //eg.buildGraph();
-       //eg.encode();
-       //printConstraints();
+       EncodingGraph eg(this);
+       eg.buildGraph();
+       eg.encode();
+//     printConstraints();
        naiveEncodingDecision(this);
        satEncoder->encodeAllSATEncoder(this);
        model_print("Is problem UNSAT after encoding: %d\n", unsat);
@@ -504,7 +506,6 @@ void CSolver::printConstraints() {
                model_print("\n");
        }
        delete it;
-
 }
 
 void CSolver::printConstraint(BooleanEdge b) {