X-Git-Url: http://plrg.eecs.uci.edu/git/?a=blobdiff_plain;f=src%2Fcsolver.c;h=d440aadc593c1d8ce9ec0b4e0b846cc6f8d976aa;hb=c12192d653bb50ff7890aa395af19f34b85cde2b;hp=b2ccfe7e5cc3a938ad1d170d30262b40d62f3a84;hpb=c393a0d9d17a8bae36b2635544db57fc919db906;p=satune.git diff --git a/src/csolver.c b/src/csolver.c index b2ccfe7..d440aad 100644 --- a/src/csolver.c +++ b/src/csolver.c @@ -5,133 +5,230 @@ #include "boolean.h" #include "predicate.h" #include "order.h" +#include "table.h" +#include "function.h" +#include "satencoder.h" +#include "sattranslator.h" CSolver * allocCSolver() { - CSolver * tmp=(CSolver *) ourmalloc(sizeof(CSolver)); - tmp->constraints=allocDefVectorBoolean(); - tmp->allBooleans=allocDefVectorBoolean(); - tmp->allSets=allocDefVectorSet(); - tmp->allElements=allocDefVectorElement(); - tmp->allPredicates = allocDefVectorPredicate(); - tmp->allTables = allocDefVectorTable(); - return tmp; + CSolver * This=(CSolver *) ourmalloc(sizeof(CSolver)); + This->constraints=allocDefVectorBoolean(); + This->allBooleans=allocDefVectorBoolean(); + This->allSets=allocDefVectorSet(); + This->allElements=allocDefVectorElement(); + This->allPredicates = allocDefVectorPredicate(); + This->allTables = allocDefVectorTable(); + This->allOrders = allocDefVectorOrder(); + This->allFunctions = allocDefVectorFunction(); + This->satEncoder = allocSATEncoder(); + return This; } /** This function tears down the solver and the entire AST */ -void deleteSolver(CSolver *this) { - deleteVectorBoolean(this->constraints); +void deleteSolver(CSolver *This) { + deleteVectorBoolean(This->constraints); - uint size=getSizeVectorBoolean(this->allBooleans); + uint size=getSizeVectorBoolean(This->allBooleans); for(uint i=0;iallBooleans, i)); + deleteBoolean(getVectorBoolean(This->allBooleans, i)); } + deleteVectorBoolean(This->allBooleans); - deleteVectorBoolean(this->allBooleans); + size=getSizeVectorSet(This->allSets); + for(uint i=0;iallSets, i)); + } + deleteVectorSet(This->allSets); + + size=getSizeVectorElement(This->allElements); + for(uint i=0;iallElements, i)); + } + deleteVectorElement(This->allElements); + + size=getSizeVectorTable(This->allTables); + for(uint i=0;iallTables, i)); + } + deleteVectorTable(This->allTables); - size=getSizeVectorSet(this->allSets); + size=getSizeVectorPredicate(This->allPredicates); for(uint i=0;iallSets, i)); + deletePredicate(getVectorPredicate(This->allPredicates, i)); } + deleteVectorPredicate(This->allPredicates); - deleteVectorSet(this->allSets); + size=getSizeVectorOrder(This->allOrders); + for(uint i=0;iallOrders, i)); + } + deleteVectorOrder(This->allOrders); - size=getSizeVectorElement(this->allElements); + size=getSizeVectorFunction(This->allFunctions); for(uint i=0;iallElements, i)); + deleteFunction(getVectorFunction(This->allFunctions, i)); } - //FIXME: Freeing alltables and allpredicates - deleteVectorElement(this->allElements); - ourfree(this); + deleteVectorFunction(This->allFunctions); + deleteSATEncoder(This->satEncoder); + ourfree(This); } -Set * createSet(CSolver * this, VarType type, uint64_t * elements, uint numelements) { +Set * createSet(CSolver * This, VarType type, uint64_t * elements, uint numelements) { Set * set=allocSet(type, elements, numelements); - pushVectorSet(this->allSets, set); + pushVectorSet(This->allSets, set); return set; } -Set * createRangeSet(CSolver * this, VarType type, uint64_t lowrange, uint64_t highrange) { +Set * createRangeSet(CSolver * This, VarType type, uint64_t lowrange, uint64_t highrange) { Set * set=allocSetRange(type, lowrange, highrange); - pushVectorSet(this->allSets, set); + pushVectorSet(This->allSets, set); return set; } -MutableSet * createMutableSet(CSolver * this, VarType type) { +MutableSet * createMutableSet(CSolver * This, VarType type) { MutableSet * set=allocMutableSet(type); - pushVectorSet(this->allSets, set); + pushVectorSet(This->allSets, set); return set; } -void addItem(CSolver *solver, MutableSet * set, uint64_t element) { +void addItem(CSolver *This, MutableSet * set, uint64_t element) { addElementMSet(set, element); } -uint64_t createUniqueItem(CSolver *solver, MutableSet * set) { +uint64_t createUniqueItem(CSolver *This, MutableSet * set) { uint64_t element=set->low++; addElementMSet(set, element); return element; } -Element * getElementVar(CSolver *this, Set * set) { - Element * element=allocElement(set); - pushVectorElement(this->allElements, element); +Element * getElementVar(CSolver *This, Set * set) { + Element * element=allocElementSet(set); + pushVectorElement(This->allElements, element); + return element; +} + +Element * getElementConst(CSolver *This, VarType type, uint64_t value) { + Element * element=allocElementConst(value, type); + pushVectorElement(This->allElements, element); return element; } -Boolean * getBooleanVar(CSolver *solver, VarType type) { - Boolean* boolean= allocBoolean(type); - pushVectorBoolean(solver->allBooleans, boolean); +Boolean * getBooleanVar(CSolver *This, VarType type) { + Boolean* boolean= allocBooleanVar(type); + pushVectorBoolean(This->allBooleans, boolean); return boolean; } -Function * createFunctionOperator(CSolver *solver, ArithOp op, Set ** domain, uint numDomain, Set * range, - OverFlowBehavior overflowbehavior) { - return NULL; +Function * createFunctionOperator(CSolver *This, ArithOp op, Set ** domain, uint numDomain, Set * range,OverFlowBehavior overflowbehavior) { + Function* function = allocFunctionOperator(op, domain, numDomain, range, overflowbehavior); + pushVectorFunction(This->allFunctions, function); + return function; +} + +Predicate * createPredicateOperator(CSolver *This, CompOp op, Set ** domain, uint numDomain) { + Predicate* predicate= allocPredicateOperator(op, domain,numDomain); + pushVectorPredicate(This->allPredicates, predicate); + return predicate; } -Predicate * createPredicateOperator(CSolver *solver, enum CompOp op, Set ** domain, uint numDomain) { - Predicate* predicate= allocPredicate(op, domain,numDomain); - pushVectorPredicate(solver->allPredicates, predicate); +Predicate * createPredicateTable(CSolver *This, Table* table, UndefinedBehavior behavior){ + Predicate* predicate = allocPredicateTable(table, behavior); + pushVectorPredicate(This->allPredicates, predicate); return predicate; } -Table * createTable(CSolver *solver, Set **domains, uint numDomain, Set * range) { +Table * createTable(CSolver *This, Set **domains, uint numDomain, Set * range) { Table* table= allocTable(domains,numDomain,range); - pushVectorTable(solver->allTables, table); + pushVectorTable(This->allTables, table); return table; } -void addTableEntry(CSolver *solver, Table* table, uint64_t* inputs, uint inputSize, uint64_t result) { - addNewTableEntry(table,inputs, inputSize,result); +Table * createTableForPredicate(CSolver *solver, Set **domains, uint numDomain){ + return createTable(solver, domains, numDomain, NULL); } -Function * completeTable(CSolver *solver, Table * table) { - return NULL; +void addTableEntry(CSolver *This, Table* table, uint64_t* inputs, uint inputSize, uint64_t result) { + addNewTableEntry(table,inputs, inputSize,result); } -Element * applyFunction(CSolver *solver, Function * function, Element ** array, Boolean * overflowstatus) { - return NULL; +Function * completeTable(CSolver *This, Table * table, UndefinedBehavior behavior) { + Function* function = allocFunctionTable(table, behavior); + pushVectorFunction(This->allFunctions,function); + return function; } -Boolean * applyPredicate(CSolver *solver, Predicate * predicate, Element ** inputs) { - return NULL; +Element * applyFunction(CSolver *This, Function * function, Element ** array, uint numArrays, Boolean * overflowstatus) { + Element* element= allocElementFunction(function,array,numArrays,overflowstatus); + pushVectorElement(This->allElements, element); + return element; +} + +Boolean * applyPredicate(CSolver *This, Predicate * predicate, Element ** inputs, uint numInputs) { + return applyPredicateTable(This, predicate, inputs, numInputs, NULL); +} +Boolean * applyPredicateTable(CSolver *This, Predicate * predicate, Element ** inputs, uint numInputs, Boolean* undefinedStatus) { + Boolean* boolean= allocBooleanPredicate(predicate, inputs, numInputs, undefinedStatus); + pushVectorBoolean(This->allBooleans, boolean); + return boolean; } -Boolean * applyLogicalOperation(CSolver *solver, LogicOp op, Boolean ** array) { - return NULL; +Boolean * applyLogicalOperation(CSolver *This, LogicOp op, Boolean ** array, uint asize) { + return allocBooleanLogicArray(This, op, array, asize); } -void addBoolean(CSolver *this, Boolean * constraint) { - pushVectorBoolean(this->constraints, constraint); +void addConstraint(CSolver *This, Boolean * constraint) { + pushVectorBoolean(This->constraints, constraint); } -Order * createOrder(CSolver *solver, OrderType type, Set * set) { - return allocOrder(type, set); +Order * createOrder(CSolver *This, OrderType type, Set * set) { + Order* order = allocOrder(type, set); + pushVectorOrder(This->allOrders, order); + return order; } -Boolean * orderConstraint(CSolver *solver, Order * order, uint64_t first, uint64_t second) { +Boolean * orderConstraint(CSolver *This, Order * order, uint64_t first, uint64_t second) { Boolean* constraint = allocBooleanOrder(order, first, second); - pushVectorBoolean(solver->allBooleans,constraint); + pushVectorBoolean(This->allBooleans,constraint); return constraint; } + +int startEncoding(CSolver* This){ + naiveEncodingDecision(This); + SATEncoder* satEncoder = This->satEncoder; + encodeAllSATEncoder(This, satEncoder); + int result= solveCNF(satEncoder->cnf); + model_print("sat_solver's result:%d\tsolutionSize=%d\n", result, satEncoder->cnf->solver->solutionsize); + for(uint i=1; i<=satEncoder->cnf->solver->solutionsize; i++){ + model_print("%d, ", satEncoder->cnf->solver->solution[i]); + } + model_print("\n"); + return result; +} + +uint64_t getElementValue(CSolver* This, Element* element){ + switch(GETELEMENTTYPE(element)){ + case ELEMSET: + case ELEMCONST: + case ELEMFUNCRETURN: + return getElementValueSATTranslator(This, element); + default: + ASSERT(0); + } + exit(-1); +} + +bool getBooleanValue( CSolver* This , Boolean* boolean){ + switch(GETBOOLEANTYPE(boolean)){ + case BOOLEANVAR: + return getBooleanVariableValueSATTranslator(This, boolean); + default: + ASSERT(0); + } + exit(-1); +} + +HappenedBefore getOrderConstraintValue(CSolver* This, Order * order, uint64_t first, uint64_t second){ + return getOrderConstraintValueSATTranslator(This, order, first, second); +} +