3 #include "mutableset.h"
10 #include "satencoder.h"
11 #include "sattranslator.h"
13 #include "polarityassignment.h"
14 #include "orderdecompose.h"
15 #include "autotuner.h"
22 satEncoder = new SATEncoder(this);
25 /** This function tears down the solver and the entire AST */
28 uint size = allBooleans.getSize();
29 for (uint i = 0; i < size; i++) {
30 delete allBooleans.get(i);
33 size = allSets.getSize();
34 for (uint i = 0; i < size; i++) {
35 delete allSets.get(i);
38 size = allElements.getSize();
39 for (uint i = 0; i < size; i++) {
40 delete allElements.get(i);
43 size = allTables.getSize();
44 for (uint i = 0; i < size; i++) {
45 delete allTables.get(i);
48 size = allPredicates.getSize();
49 for (uint i = 0; i < size; i++) {
50 delete allPredicates.get(i);
53 size = allOrders.getSize();
54 for (uint i = 0; i < size; i++) {
55 delete allOrders.get(i);
58 size = allFunctions.getSize();
59 for (uint i = 0; i < size; i++) {
60 delete allFunctions.get(i);
66 CSolver *CSolver::clone() {
67 CSolver *copy = new CSolver();
69 HSIteratorBoolean *it = getConstraints();
70 while (it->hasNext()) {
71 Boolean *b = it->next();
72 copy->addConstraint(b->clone(copy, &map));
78 Set *CSolver::createSet(VarType type, uint64_t *elements, uint numelements) {
79 Set *set = new Set(type, elements, numelements);
84 Set *CSolver::createRangeSet(VarType type, uint64_t lowrange, uint64_t highrange) {
85 Set *set = new Set(type, lowrange, highrange);
90 MutableSet *CSolver::createMutableSet(VarType type) {
91 MutableSet *set = new MutableSet(type);
96 void CSolver::addItem(MutableSet *set, uint64_t element) {
97 set->addElementMSet(element);
100 uint64_t CSolver::createUniqueItem(MutableSet *set) {
101 uint64_t element = set->low++;
102 set->addElementMSet(element);
106 Element *CSolver::getElementVar(Set *set) {
107 Element *element = new ElementSet(set);
108 allElements.push(element);
112 Element *CSolver::getElementConst(VarType type, uint64_t value) {
113 uint64_t array[] = {value};
114 Set *set = new Set(type, array, 1);
116 Element *element = new ElementConst(value, type, set);
117 allElements.push(element);
121 Element *CSolver::applyFunction(Function *function, Element **array, uint numArrays, Boolean *overflowstatus) {
122 Element *element = new ElementFunction(function,array,numArrays,overflowstatus);
123 allElements.push(element);
127 Function *CSolver::createFunctionOperator(ArithOp op, Set **domain, uint numDomain, Set *range,OverFlowBehavior overflowbehavior) {
128 Function *function = new FunctionOperator(op, domain, numDomain, range, overflowbehavior);
129 allFunctions.push(function);
133 Predicate *CSolver::createPredicateOperator(CompOp op, Set **domain, uint numDomain) {
134 Predicate *predicate = new PredicateOperator(op, domain,numDomain);
135 allPredicates.push(predicate);
139 Predicate *CSolver::createPredicateTable(Table *table, UndefinedBehavior behavior) {
140 Predicate *predicate = new PredicateTable(table, behavior);
141 allPredicates.push(predicate);
145 Table *CSolver::createTable(Set **domains, uint numDomain, Set *range) {
146 Table *table = new Table(domains,numDomain,range);
147 allTables.push(table);
151 Table *CSolver::createTableForPredicate(Set **domains, uint numDomain) {
152 return createTable(domains, numDomain, NULL);
155 void CSolver::addTableEntry(Table *table, uint64_t *inputs, uint inputSize, uint64_t result) {
156 table->addNewTableEntry(inputs, inputSize, result);
159 Function *CSolver::completeTable(Table *table, UndefinedBehavior behavior) {
160 Function *function = new FunctionTable(table, behavior);
161 allFunctions.push(function);
165 Boolean *CSolver::getBooleanVar(VarType type) {
166 Boolean *boolean = new BooleanVar(type);
167 allBooleans.push(boolean);
171 Boolean *CSolver::applyPredicate(Predicate *predicate, Element **inputs, uint numInputs) {
172 return applyPredicateTable(predicate, inputs, numInputs, NULL);
175 Boolean *CSolver::applyPredicateTable(Predicate *predicate, Element **inputs, uint numInputs, Boolean *undefinedStatus) {
176 BooleanPredicate *boolean = new BooleanPredicate(predicate, inputs, numInputs, undefinedStatus);
177 allBooleans.push(boolean);
181 Boolean *CSolver::applyLogicalOperation(LogicOp op, Boolean **array, uint asize) {
182 Boolean *boolean = new BooleanLogic(this, op, array, asize);
183 allBooleans.push(boolean);
187 Boolean *CSolver::orderConstraint(Order *order, uint64_t first, uint64_t second) {
188 Boolean *constraint = new BooleanOrder(order, first, second);
189 allBooleans.push(constraint);
193 void CSolver::addConstraint(Boolean *constraint) {
194 constraints.add(constraint);
197 Order *CSolver::createOrder(OrderType type, Set *set) {
198 Order *order = new Order(type, set);
199 allOrders.push(order);
203 int CSolver::startEncoding() {
204 bool deleteTuner = false;
206 tuner = new DefaultTuner();
210 long long startTime = getTimeNano();
211 computePolarities(this);
213 naiveEncodingDecision(this);
214 satEncoder->encodeAllSATEncoder(this);
215 int result = unsat ? IS_UNSAT : satEncoder->solve();
216 long long finishTime = getTimeNano();
217 elapsedTime = finishTime - startTime;
225 uint64_t CSolver::getElementValue(Element *element) {
226 switch (element->type) {
230 return getElementValueSATTranslator(this, element);
237 bool CSolver::getBooleanValue(Boolean *boolean) {
238 switch (boolean->type) {
240 return getBooleanVariableValueSATTranslator(this, boolean);
247 HappenedBefore CSolver::getOrderConstraintValue(Order *order, uint64_t first, uint64_t second) {
248 return getOrderConstraintValueSATTranslator(this, order, first, second);
251 long long CSolver::getEncodeTime() { return satEncoder->getEncodeTime(); }
253 long long CSolver::getSolveTime() { return satEncoder->getSolveTime(); }
255 void CSolver::autoTune(uint budget) {
256 AutoTuner * autotuner=new AutoTuner(budget);
257 autotuner->addProblem(this);