#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() :
+ 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);
+ }
+
+ 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));
}
- deleteVectorFunction(allFunctions);
- deleteSATEncoder(satEncoder);
- deleteTuner(tuner);
+ 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);
+ 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;
}
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::applyPredicate(Predicate *predicate, Element **inputs, uint numInputs) {
}
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 *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);
+ 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:
}
bool CSolver::getBooleanValue(Boolean *boolean) {
- switch (GETBOOLEANTYPE(boolean)) {
+ switch (boolean->type) {
case BOOLEANVAR:
return getBooleanVariableValueSATTranslator(this, boolean);
default:
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;
+}