X-Git-Url: http://plrg.eecs.uci.edu/git/?p=satune.git;a=blobdiff_plain;f=src%2Fcsolver.cc;h=0270758af28faa087efc0d2f2cc358e15a221d53;hp=e2bb4bc278f189c5dcd3f1cb9365ed3701873109;hb=ef5d7a44cfe435c24e5f104e320b1a81835626e7;hpb=7997cc8a8bbee380ba714aa52362374f2aa68ef1 diff --git a/src/csolver.cc b/src/csolver.cc index e2bb4bc..0270758 100644 --- a/src/csolver.cc +++ b/src/csolver.cc @@ -10,12 +10,21 @@ #include "satencoder.h" #include "sattranslator.h" #include "tunable.h" -#include "orderencoder.h" #include "polarityassignment.h" - -CSolver::CSolver() : unsat(false) { - tuner = allocTuner(); - satEncoder = allocSATEncoder(this); +#include "transformer.h" +#include "autotuner.h" +#include "astops.h" +#include "structs.h" + +CSolver::CSolver() : + boolTrue(new BooleanConst(true)), + boolFalse(new BooleanConst(false)), + unsat(false), + tuner(NULL), + elapsedTime(0) +{ + satEncoder = new SATEncoder(this); + transformer = new Transformer(this); } /** This function tears down the solver and the entire AST */ @@ -56,8 +65,22 @@ CSolver::~CSolver() { delete allFunctions.get(i); } - deleteSATEncoder(satEncoder); - deleteTuner(tuner); + delete boolTrue; + delete boolFalse; + delete satEncoder; + delete transformer; +} + +CSolver *CSolver::clone() { + CSolver *copy = new CSolver(); + CloneMap map; + SetIteratorBoolean *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) { @@ -72,6 +95,11 @@ Set *CSolver::createRangeSet(VarType type, uint64_t lowrange, uint64_t highrange return set; } +Element *CSolver::createRangeVar(VarType type, uint64_t lowrange, uint64_t highrange) { + Set *s = createRangeSet(type, lowrange, highrange); + return getElementVar(s); +} + MutableSet *CSolver::createMutableSet(VarType type) { MutableSet *set = new MutableSet(type); allSets.push(set); @@ -83,7 +111,7 @@ void CSolver::addItem(MutableSet *set, uint64_t element) { } uint64_t CSolver::createUniqueItem(MutableSet *set) { - uint64_t element = set->low++; + uint64_t element = set->getNewUniqueItem(); set->addElementMSet(element); return element; } @@ -95,15 +123,33 @@ Element *CSolver::getElementVar(Set *set) { } Element *CSolver::getElementConst(VarType type, uint64_t value) { - Element *element = new ElementConst(value, type); - allElements.push(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); - allBooleans.push(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) { @@ -144,10 +190,18 @@ Function *CSolver::completeTable(Table *table, UndefinedBehavior behavior) { return function; } -Element *CSolver::applyFunction(Function *function, Element **array, uint numArrays, Boolean *overflowstatus) { - Element *element = new ElementFunction(function,array,numArrays,overflowstatus); - allElements.push(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) { @@ -155,23 +209,155 @@ 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); - allBooleans.push(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); +bool CSolver::isTrue(Boolean *b) { + return b->isTrue(); } -void CSolver::addConstraint(Boolean *constraint) { - constraints.add(constraint); +bool CSolver::isFalse(Boolean *b) { + return b->isFalse(); } -Order *CSolver::createOrder(OrderType type, Set *set) { - Order *order = new Order(type, set); - allOrders.push(order); - return order; +Boolean *CSolver::applyLogicalOperation(LogicOp op, Boolean * arg1, Boolean * arg2) { + Boolean * array[] = {arg1, arg2}; + return applyLogicalOperation(op, array, 2); +} + +Boolean *CSolver::applyLogicalOperation(LogicOp op, Boolean *arg) { + Boolean * array[] = {arg}; + return applyLogicalOperation(op, array, 1); +} + + +Boolean *CSolver::applyLogicalOperation(LogicOp op, Boolean **array, uint 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) { + return array[0]->isTrue() ? boolFalse : boolTrue; + } + break; + } + case SATC_IFF: { + for(uint i=0;i<2;i++) { + if (array[i]->type == BOOLCONST) { + if (array[i]->isTrue()) { + return array[1-i]; + } else { + newarray[0]=array[1-i]; + return applyLogicalOperation(SATC_NOT, newarray, 1); + } + } + } + break; + } + case SATC_XOR: { + for(uint i=0;i<2;i++) { + if (array[i]->type == BOOLCONST) { + if (array[i]->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;itype == BOOLCONST) { + if (b->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;itype == BOOLCONST) { + if (b->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) { + if (array[0]->isTrue()) { + return array[1]; + } else { + return boolTrue; + } + } else if (array[1]->type == BOOLCONST) { + if (array[1]->isTrue()) { + return array[1]; + } 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) { @@ -180,22 +366,45 @@ Boolean *CSolver::orderConstraint(Order *order, uint64_t first, uint64_t second) return constraint; } -int CSolver::startEncoding() { - naiveEncodingDecision(this); +void CSolver::addConstraint(Boolean *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); + allOrders.push(order); + return order; +} + +int CSolver::solve() { + 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]); - } - model_print("\n"); + transformer->orderAnalysis(); + 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; + } return result; } uint64_t CSolver::getElementValue(Element *element) { - switch (GETELEMENTTYPE(element)) { + switch (element->type) { case ELEMSET: case ELEMCONST: case ELEMFUNCRETURN: @@ -207,7 +416,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: @@ -220,3 +429,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; +}