renaming
[satune.git] / src / csolver.cc
index b6adc3f3039edc99e9bfb0a33e0b3e037ed471c5..86315183a179da649ff08a179ec1fc8a8c4284d3 100644 (file)
 #include "satencoder.h"
 #include "sattranslator.h"
 #include "tunable.h"
-#include "orderencoder.h"
 #include "polarityassignment.h"
-
-CSolver::CSolver() : unsat(false) {
-       constraints = allocDefHashSetBoolean();
-       allBooleans = allocDefVectorBoolean();
-       allSets = allocDefVectorSet();
-       allElements = allocDefVectorElement();
-       allPredicates = allocDefVectorPredicate();
-       allTables = allocDefVectorTable();
-       allOrders = allocDefVectorOrder();
-       allFunctions = allocDefVectorFunction();
-       tuner = allocTuner();
-       satEncoder = allocSATEncoder(this);
+#include "analyzer.h"
+#include "autotuner.h"
+
+CSolver::CSolver() :
+       boolTrue(new BooleanConst(true)),
+       boolFalse(new BooleanConst(false)),
+       unsat(false),
+       tuner(NULL),
+       elapsedTime(0)
+{
+       satEncoder = new SATEncoder(this);
 }
 
 /** This function tears down the solver and the entire AST */
 
 CSolver::~CSolver() {
-       deleteHashSetBoolean(constraints);
-
-       uint size = getSizeVectorBoolean(allBooleans);
+       uint size = allBooleans.getSize();
        for (uint i = 0; i < size; i++) {
-               delete getVectorBoolean(allBooleans, i);
+               delete allBooleans.get(i);
        }
-       deleteVectorBoolean(allBooleans);
 
-       size = getSizeVectorSet(allSets);
+       size = allSets.getSize();
        for (uint i = 0; i < size; i++) {
-               delete getVectorSet(allSets, i);
+               delete allSets.get(i);
        }
-       deleteVectorSet(allSets);
 
-       size = getSizeVectorElement(allElements);
+       size = allElements.getSize();
        for (uint i = 0; i < size; i++) {
-               delete getVectorElement(allElements, i);
+               delete allElements.get(i);
        }
-       deleteVectorElement(allElements);
 
-       size = getSizeVectorTable(allTables);
+       size = allTables.getSize();
        for (uint i = 0; i < size; i++) {
-               delete getVectorTable(allTables, i);
+               delete allTables.get(i);
        }
-       deleteVectorTable(allTables);
 
-       size = getSizeVectorPredicate(allPredicates);
+       size = allPredicates.getSize();
        for (uint i = 0; i < size; i++) {
-               delete getVectorPredicate(allPredicates, i);
+               delete allPredicates.get(i);
        }
-       deleteVectorPredicate(allPredicates);
 
-       size = getSizeVectorOrder(allOrders);
+       size = allOrders.getSize();
        for (uint i = 0; i < size; i++) {
-               delete getVectorOrder(allOrders, i);
+               delete allOrders.get(i);
        }
-       deleteVectorOrder(allOrders);
 
-       size = getSizeVectorFunction(allFunctions);
+       size = allFunctions.getSize();
        for (uint i = 0; i < size; i++) {
-               delete getVectorFunction(allFunctions, i);
+               delete allFunctions.get(i);
        }
-       deleteVectorFunction(allFunctions);
-       deleteSATEncoder(satEncoder);
-       deleteTuner(tuner);
+
+       delete boolTrue;
+       delete boolFalse;
+       delete satEncoder;
+}
+
+CSolver *CSolver::clone() {
+       CSolver *copy = new CSolver();
+       CloneMap map;
+       HSIteratorBoolean *it = getConstraints();
+       while (it->hasNext()) {
+               Boolean *b = it->next();
+               copy->addConstraint(b->clone(copy, &map));
+       }
+       delete it;
+       return copy;
 }
 
 Set *CSolver::createSet(VarType type, uint64_t *elements, uint numelements) {
        Set *set = new Set(type, elements, numelements);
-       pushVectorSet(allSets, set);
+       allSets.push(set);
        return set;
 }
 
 Set *CSolver::createRangeSet(VarType type, uint64_t lowrange, uint64_t highrange) {
        Set *set = new Set(type, lowrange, highrange);
-       pushVectorSet(allSets, set);
+       allSets.push(set);
        return set;
 }
 
 MutableSet *CSolver::createMutableSet(VarType type) {
-       MutableSet *set = allocMutableSet(type);
-       pushVectorSet(allSets, set);
+       MutableSet *set = new MutableSet(type);
+       allSets.push(set);
        return set;
 }
 
 void CSolver::addItem(MutableSet *set, uint64_t element) {
-       addElementMSet(set, element);
+       set->addElementMSet(element);
 }
 
 uint64_t CSolver::createUniqueItem(MutableSet *set) {
-       uint64_t element = set->low++;
-       addElementMSet(set, element);
+       uint64_t element = set->getNewUniqueItem();
+       set->addElementMSet(element);
        return element;
 }
 
 Element *CSolver::getElementVar(Set *set) {
        Element *element = new ElementSet(set);
-       pushVectorElement(allElements, element);
+       allElements.push(element);
        return element;
 }
 
 Element *CSolver::getElementConst(VarType type, uint64_t value) {
-       Element *element = new ElementConst(value, type);
-       pushVectorElement(allElements, element);
-       return element;
+       uint64_t array[] = {value};
+       Set *set = new Set(type, array, 1);
+       Element *element = new ElementConst(value, type, set);
+       Element *e = elemMap.get(element);
+       if (e == NULL) {
+               allSets.push(set);
+               allElements.push(element);
+               elemMap.put(element, element);
+               return element;
+       } else {
+               delete set;
+               delete element;
+               return e;
+       }
 }
 
-Boolean *CSolver::getBooleanVar(VarType type) {
-       Boolean *boolean = new BooleanVar(type);
-       pushVectorBoolean(allBooleans, boolean);
-       return boolean;
+Element *CSolver::applyFunction(Function *function, Element **array, uint numArrays, Boolean *overflowstatus) {
+       Element *element = new ElementFunction(function,array,numArrays,overflowstatus);
+       Element *e = elemMap.get(element);
+       if (e == NULL) {
+               allElements.push(element);
+               elemMap.put(element, element);
+               return element;
+       } else {
+               delete element;
+               return e;
+       }
 }
 
 Function *CSolver::createFunctionOperator(ArithOp op, Set **domain, uint numDomain, Set *range,OverFlowBehavior overflowbehavior) {
        Function *function = new FunctionOperator(op, domain, numDomain, range, overflowbehavior);
-       pushVectorFunction(allFunctions, function);
+       allFunctions.push(function);
        return function;
 }
 
 Predicate *CSolver::createPredicateOperator(CompOp op, Set **domain, uint numDomain) {
        Predicate *predicate = new PredicateOperator(op, domain,numDomain);
-       pushVectorPredicate(allPredicates, predicate);
+       allPredicates.push(predicate);
        return predicate;
 }
 
 Predicate *CSolver::createPredicateTable(Table *table, UndefinedBehavior behavior) {
        Predicate *predicate = new PredicateTable(table, behavior);
-       pushVectorPredicate(allPredicates, predicate);
+       allPredicates.push(predicate);
        return predicate;
 }
 
 Table *CSolver::createTable(Set **domains, uint numDomain, Set *range) {
        Table *table = new Table(domains,numDomain,range);
-       pushVectorTable(allTables, table);
+       allTables.push(table);
        return table;
 }
 
@@ -156,14 +177,22 @@ void CSolver::addTableEntry(Table *table, uint64_t *inputs, uint inputSize, uint
 
 Function *CSolver::completeTable(Table *table, UndefinedBehavior behavior) {
        Function *function = new FunctionTable(table, behavior);
-       pushVectorFunction(allFunctions,function);
+       allFunctions.push(function);
        return function;
 }
 
-Element *CSolver::applyFunction(Function *function, Element **array, uint numArrays, Boolean *overflowstatus) {
-       Element *element = new ElementFunction(function,array,numArrays,overflowstatus);
-       pushVectorElement(allElements, element);
-       return element;
+Boolean *CSolver::getBooleanVar(VarType type) {
+       Boolean *boolean = new BooleanVar(type);
+       allBooleans.push(boolean);
+       return boolean;
+}
+
+Boolean *CSolver::getBooleanTrue() {
+       return boolTrue;
+}
+
+Boolean *CSolver::getBooleanFalse() {
+       return boolFalse;
 }
 
 Boolean *CSolver::applyPredicate(Predicate *predicate, Element **inputs, uint numInputs) {
@@ -171,47 +200,176 @@ Boolean *CSolver::applyPredicate(Predicate *predicate, Element **inputs, uint nu
 }
 
 Boolean *CSolver::applyPredicateTable(Predicate *predicate, Element **inputs, uint numInputs, Boolean *undefinedStatus) {
-       Boolean *boolean = new BooleanPredicate(predicate, inputs, numInputs, undefinedStatus);
-       pushVectorBoolean(allBooleans, boolean);
-       return boolean;
+       BooleanPredicate *boolean = new BooleanPredicate(predicate, inputs, numInputs, undefinedStatus);
+       Boolean * b = boolMap.get(boolean);
+       if (b == NULL) {
+               boolMap.put(boolean, boolean);
+               allBooleans.push(boolean);
+               return boolean;
+       } else {
+               delete boolean;
+               return b;
+       }
 }
 
 Boolean *CSolver::applyLogicalOperation(LogicOp op, Boolean **array, uint asize) {
-       return new BooleanLogic(this, op, array, asize);
+       Boolean * newarray[asize];
+       switch(op) {
+       case SATC_NOT: {
+               if (array[0]->type == LOGICOP && ((BooleanLogic *)array[0])->op==SATC_NOT) {
+                       return ((BooleanLogic *) array[0])->inputs.get(0);
+               } else if (array[0]->type == BOOLCONST) {
+                       bool isTrue = ((BooleanConst *) array[0])->isTrue;
+                       return isTrue ? boolFalse : boolTrue;
+               }
+               break;
+       }
+       case SATC_XOR: {
+               for(uint i=0;i<2;i++) {
+                       if (array[i]->type == BOOLCONST) {
+                               bool isTrue = ((BooleanConst *) array[i])->isTrue;
+                               if (isTrue) {
+                                       newarray[0]=array[1-i];
+                                       return applyLogicalOperation(SATC_NOT, newarray, 1);
+                               } else
+                                       return array[1-i];
+                       }
+               }
+               break;
+       }
+       case SATC_OR: {
+               uint newindex=0;
+               for(uint i=0;i<asize;i++) {
+                       Boolean *b=array[i];
+                       if (b->type == BOOLCONST) {
+                               bool isTrue = ((BooleanConst *) b)->isTrue;
+                               if (isTrue)
+                                       return b;
+                               else
+                                       continue;
+                       } else
+                               newarray[newindex++]=b;
+               }
+               if (newindex==1)
+                       return newarray[0];
+               else if (newindex == 2) {
+                       bool isNot0 = (newarray[0]->type==BOOLCONST) && ((BooleanLogic *)newarray[0])->op == SATC_NOT;
+                       bool isNot1 = (newarray[1]->type==BOOLCONST) && ((BooleanLogic *)newarray[1])->op == SATC_NOT;
+
+                       if (isNot0 != isNot1) {
+                               if (isNot0) {
+                                       newarray[0] = ((BooleanLogic *) newarray[0])->inputs.get(0);
+                               } else {
+                                       Boolean *tmp =  ((BooleanLogic *) array[1])->inputs.get(0);
+                                       array[1] = array[0];
+                                       array[0] = tmp;
+                               }
+                               return applyLogicalOperation(SATC_IMPLIES, newarray, 2);
+                       }
+               } else {
+                       array = newarray;
+                       asize = newindex;
+               }
+               break;
+       }
+       case SATC_AND: {
+               uint newindex=0;
+               for(uint i=0;i<asize;i++) {
+                       Boolean *b=array[i];
+                       if (b->type == BOOLCONST) {
+                               bool isTrue = ((BooleanConst *) b)->isTrue;
+                               if (isTrue)
+                                       continue;
+                               else
+                                       return b;
+                       } else
+                               newarray[newindex++]=b;
+               }
+               if(newindex==1) {
+                       return newarray[0];
+               } else {
+                       array = newarray;
+                       asize = newindex;
+               }
+               break;
+       }
+       case SATC_IMPLIES: {
+               if (array[0]->type == BOOLCONST) {
+                       BooleanConst *b=(BooleanConst *) array[0];
+                       if (b->isTrue) {
+                               return array[1];
+                       } else {
+                               return boolTrue;
+                       }
+               } else if (array[1]->type == BOOLCONST) {
+                       BooleanConst *b=(BooleanConst *) array[0];
+                       if (b->isTrue) {
+                               return b;
+                       } else {
+                               return applyLogicalOperation(SATC_NOT, array, 1);
+                       }
+               }
+               break;
+       }
+       }
+       
+       Boolean *boolean = new BooleanLogic(this, op, array, asize);
+       Boolean *b = boolMap.get(boolean);
+       if (b == NULL) {
+               boolMap.put(boolean, boolean);
+               allBooleans.push(boolean);
+               return boolean;         
+       } else {
+               delete boolean;
+               return b;
+       }
+}
+
+Boolean *CSolver::orderConstraint(Order *order, uint64_t first, uint64_t second) {
+       Boolean *constraint = new BooleanOrder(order, first, second);
+       allBooleans.push(constraint);
+       return constraint;
 }
 
 void CSolver::addConstraint(Boolean *constraint) {
-       addHashSetBoolean(constraints, constraint);
+       if (constraint == boolTrue)
+               return;
+       else if (constraint == boolFalse)
+               setUnSAT();
+       else
+               constraints.add(constraint);
 }
 
 Order *CSolver::createOrder(OrderType type, Set *set) {
        Order *order = new Order(type, set);
-       pushVectorOrder(allOrders, order);
+       allOrders.push(order);
        return order;
 }
 
-Boolean *CSolver::orderConstraint(Order *order, uint64_t first, uint64_t second) {
-       Boolean *constraint = new BooleanOrder(order, first, second);
-       pushVectorBoolean(allBooleans,constraint);
-       return constraint;
-}
-
 int CSolver::startEncoding() {
-       naiveEncodingDecision(this);
+       bool deleteTuner = false;
+       if (tuner == NULL) {
+               tuner = new DefaultTuner();
+               deleteTuner = true;
+       }
+               
+       long long startTime = getTimeNano();
        computePolarities(this);
        orderAnalysis(this);
-       encodeAllSATEncoder(this, satEncoder);
-       int result = solveCNF(satEncoder->cnf);
-       model_print("sat_solver's result:%d\tsolutionSize=%d\n", result, satEncoder->cnf->solver->solutionsize);
-       for (int i = 1; i <= satEncoder->cnf->solver->solutionsize; i++) {
-               model_print("%d, ", satEncoder->cnf->solver->solution[i]);
+       naiveEncodingDecision(this);
+       satEncoder->encodeAllSATEncoder(this);
+       int result = unsat ? IS_UNSAT : satEncoder->solve();
+       long long finishTime = getTimeNano();
+       elapsedTime = finishTime - startTime;
+       if (deleteTuner) {
+               delete tuner;
+               tuner = NULL;
        }
-       model_print("\n");
        return result;
 }
 
 uint64_t CSolver::getElementValue(Element *element) {
-       switch (GETELEMENTTYPE(element)) {
+       switch (element->type) {
        case ELEMSET:
        case ELEMCONST:
        case ELEMFUNCRETURN:
@@ -223,7 +381,7 @@ uint64_t CSolver::getElementValue(Element *element) {
 }
 
 bool CSolver::getBooleanValue(Boolean *boolean) {
-       switch (GETBOOLEANTYPE(boolean)) {
+       switch (boolean->type) {
        case BOOLEANVAR:
                return getBooleanVariableValueSATTranslator(this, boolean);
        default:
@@ -236,3 +394,13 @@ HappenedBefore CSolver::getOrderConstraintValue(Order *order, uint64_t first, ui
        return getOrderConstraintValueSATTranslator(this, order, first, second);
 }
 
+long long CSolver::getEncodeTime() { return satEncoder->getEncodeTime(); }
+
+long long CSolver::getSolveTime() { return satEncoder->getSolveTime(); }
+
+void CSolver::autoTune(uint budget) {
+       AutoTuner * autotuner=new AutoTuner(budget);
+       autotuner->addProblem(this);
+       autotuner->tune();
+       delete autotuner;
+}