3 #include "mutableset.h"
10 #include "satencoder.h"
11 #include "sattranslator.h"
13 #include "polarityassignment.h"
14 #include "orderdecompose.h"
15 #include "autotuner.h"
19 tuner(new DefaultTuner()),
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);
67 CSolver *CSolver::clone() {
68 CSolver *copy = new CSolver();
70 HSIteratorBoolean *it = getConstraints();
71 while (it->hasNext()) {
72 Boolean *b = it->next();
79 Set *CSolver::createSet(VarType type, uint64_t *elements, uint numelements) {
80 Set *set = new Set(type, elements, numelements);
85 Set *CSolver::createRangeSet(VarType type, uint64_t lowrange, uint64_t highrange) {
86 Set *set = new Set(type, lowrange, highrange);
91 MutableSet *CSolver::createMutableSet(VarType type) {
92 MutableSet *set = new MutableSet(type);
97 void CSolver::addItem(MutableSet *set, uint64_t element) {
98 set->addElementMSet(element);
101 uint64_t CSolver::createUniqueItem(MutableSet *set) {
102 uint64_t element = set->low++;
103 set->addElementMSet(element);
107 Element *CSolver::getElementVar(Set *set) {
108 Element *element = new ElementSet(set);
109 allElements.push(element);
113 Element *CSolver::getElementConst(VarType type, uint64_t value) {
114 uint64_t array[] = {value};
115 Set *set = new Set(type, array, 1);
117 Element *element = new ElementConst(value, type, set);
118 allElements.push(element);
122 Boolean *CSolver::getBooleanVar(VarType type) {
123 Boolean *boolean = new BooleanVar(type);
124 allBooleans.push(boolean);
128 Function *CSolver::createFunctionOperator(ArithOp op, Set **domain, uint numDomain, Set *range,OverFlowBehavior overflowbehavior) {
129 Function *function = new FunctionOperator(op, domain, numDomain, range, overflowbehavior);
130 allFunctions.push(function);
134 Predicate *CSolver::createPredicateOperator(CompOp op, Set **domain, uint numDomain) {
135 Predicate *predicate = new PredicateOperator(op, domain,numDomain);
136 allPredicates.push(predicate);
140 Predicate *CSolver::createPredicateTable(Table *table, UndefinedBehavior behavior) {
141 Predicate *predicate = new PredicateTable(table, behavior);
142 allPredicates.push(predicate);
146 Table *CSolver::createTable(Set **domains, uint numDomain, Set *range) {
147 Table *table = new Table(domains,numDomain,range);
148 allTables.push(table);
152 Table *CSolver::createTableForPredicate(Set **domains, uint numDomain) {
153 return createTable(domains, numDomain, NULL);
156 void CSolver::addTableEntry(Table *table, uint64_t *inputs, uint inputSize, uint64_t result) {
157 table->addNewTableEntry(inputs, inputSize, result);
160 Function *CSolver::completeTable(Table *table, UndefinedBehavior behavior) {
161 Function *function = new FunctionTable(table, behavior);
162 allFunctions.push(function);
166 Element *CSolver::applyFunction(Function *function, Element **array, uint numArrays, Boolean *overflowstatus) {
167 Element *element = new ElementFunction(function,array,numArrays,overflowstatus);
168 allElements.push(element);
172 Boolean *CSolver::applyPredicate(Predicate *predicate, Element **inputs, uint numInputs) {
173 return applyPredicateTable(predicate, inputs, numInputs, NULL);
176 Boolean *CSolver::applyPredicateTable(Predicate *predicate, Element **inputs, uint numInputs, Boolean *undefinedStatus) {
177 BooleanPredicate *boolean = new BooleanPredicate(predicate, inputs, numInputs, undefinedStatus);
178 allBooleans.push(boolean);
182 Boolean *CSolver::applyLogicalOperation(LogicOp op, Boolean **array, uint asize) {
183 Boolean *boolean = new BooleanLogic(this, op, array, asize);
184 allBooleans.push(boolean);
188 void CSolver::addConstraint(Boolean *constraint) {
189 constraints.add(constraint);
192 Order *CSolver::createOrder(OrderType type, Set *set) {
193 Order *order = new Order(type, set);
194 allOrders.push(order);
198 Boolean *CSolver::orderConstraint(Order *order, uint64_t first, uint64_t second) {
199 Boolean *constraint = new BooleanOrder(order, first, second);
200 allBooleans.push(constraint);
204 int CSolver::startEncoding() {
205 long long startTime = getTimeNano();
206 computePolarities(this);
208 naiveEncodingDecision(this);
209 satEncoder->encodeAllSATEncoder(this);
210 int result = satEncoder->solve();
211 long long finishTime = getTimeNano();
212 elapsedTime = finishTime - startTime;
216 uint64_t CSolver::getElementValue(Element *element) {
217 switch (GETELEMENTTYPE(element)) {
221 return getElementValueSATTranslator(this, element);
228 bool CSolver::getBooleanValue(Boolean *boolean) {
229 switch (GETBOOLEANTYPE(boolean)) {
231 return getBooleanVariableValueSATTranslator(this, boolean);
238 HappenedBefore CSolver::getOrderConstraintValue(Order *order, uint64_t first, uint64_t second) {
239 return getOrderConstraintValueSATTranslator(this, order, first, second);
242 long long CSolver::getEncodeTime() { return satEncoder->getEncodeTime(); }
244 long long CSolver::getSolveTime() { return satEncoder->getSolveTime(); }
246 void CSolver::autoTune() {
247 AutoTuner * autotuner=new AutoTuner();
248 autotuner->tune(this);