Merging + fixing memory bugs
[satune.git] / src / csolver.h
index fbf22da5bb7aa3f97284a69b5bbb72946e7c3cd6..eba7b3ccf47ffa7abb4dbae45e398591a8fd025c 100644 (file)
 #ifndef CSOLVER_H
 #define CSOLVER_H
-#include "classlist.h"
+#include "classes.h"
 #include "ops.h"
-#include "structs.h"
+#include "corestructs.h"
+#include "asthash.h"
+#include "solver_interface.h"
+#include "common.h"
 
-struct CSolver {
-       SATEncoder *satEncoder;
-       bool unsat;
-       Tuner *tuner;
-       
-       /** This is a vector of constraints that must be satisfied. */
-       HashSetBoolean *constraints;
+class CSolver {
+public:
+       CSolver();
+       ~CSolver();
+       void resetSolver();
+       /** This function creates a set containing the elements passed in the array. */
+       Set *createSet(VarType type, uint64_t *elements, uint num);
 
-       /** This is a vector of all boolean structs that we have allocated. */
-       VectorBoolean *allBooleans;
+       /** This function creates a set from lowrange to highrange (inclusive). */
 
-       /** This is a vector of all set structs that we have allocated. */
-       VectorSet *allSets;
+       Set *createRangeSet(VarType type, uint64_t lowrange, uint64_t highrange);
 
-       /** This is a vector of all element structs that we have allocated. */
-       VectorElement *allElements;
+       bool itemExistInSet(Set *set, uint64_t item);
 
-       /** This is a vector of all predicate structs that we have allocated. */
-       VectorPredicate *allPredicates;
+       VarType getSetVarType(Set *set);
 
-       /** This is a vector of all table structs that we have allocated. */
-       VectorTable *allTables;
+       Element *createRangeVar(VarType type, uint64_t lowrange, uint64_t highrange);
 
-       /** This is a vector of all order structs that we have allocated. */
-       VectorOrder *allOrders;
+       /** This function creates a mutable set.
+        * Note: You should use addItem for adding new item to Mutable sets, and
+        * at the end, you should call finalizeMutableSet!
+        */
 
-       /** This is a vector of all function structs that we have allocated. */
-       VectorFunction *allFunctions;
-};
+       MutableSet *createMutableSet(VarType type);
+
+       /** This function adds a new item to a set. */
+
+       //Deprecating this unless we need it...
+       void addItem(MutableSet *set, uint64_t element);
+
+       /** This function adds a new unique item to the set and returns it.
+           This function cannot be used in conjunction with manually adding
+           items to the set. */
+
+       uint64_t createUniqueItem(MutableSet *set);
+
+       /**
+        * Freeze and finalize the mutableSet ...
+        */
+       void finalizeMutableSet(MutableSet *set);
+
+       /** This function creates an element variable over a set. */
+
+       Element *getElementVar(Set *set);
+
+       /** This function creates an element constrant. */
+       Element *getElementConst(VarType type, uint64_t value);
+
+       Set *getElementRange (Element *element);
+
+       void mustHaveValue(Element *element);
 
-/** Create a new solver instance. */
+       BooleanEdge getBooleanTrue();
 
-CSolver *allocCSolver();
+       BooleanEdge getBooleanFalse();
 
-/** Delete solver instance. */
+       /** This function creates a boolean variable. */
 
-void deleteSolver(CSolver *This);
+       BooleanEdge getBooleanVar(VarType type);
 
-/** This function creates a set containing the elements passed in the array. */
+       /** This function creates a function operator. */
 
-Set *createSet(CSolver *, VarType type, uint64_t *elements, uint num);
+       Function *createFunctionOperator(ArithOp op, Set *range,
+                                                                                                                                        OverFlowBehavior overflowbehavior);
 
-/** This function creates a set from lowrange to highrange (inclusive). */
+       /** This function creates a predicate operator. */
 
-Set *createRangeSet(CSolver *, VarType type, uint64_t lowrange, uint64_t highrange);
+       Predicate *createPredicateOperator(CompOp op);
 
-/** This function creates a mutable set. */
+       Predicate *createPredicateTable(Table *table, UndefinedBehavior behavior);
 
-MutableSet *createMutableSet(CSolver *, VarType type);
+       /** This function creates an empty instance table.*/
 
-/** This function adds a new item to a set. */
+       Table *createTable(Set *range);
 
-void addItem(CSolver *, MutableSet *set, uint64_t element);
+       Table *createTableForPredicate();
+       /** This function adds an input output relation to a table. */
 
-/** This function adds a new unique item to the set and returns it.
-    This function cannot be used in conjunction with manually adding
-    items to the set. */
+       void addTableEntry(Table *table, uint64_t *inputs, uint inputSize, uint64_t result);
 
-uint64_t createUniqueItem(CSolver *, MutableSet *set);
+       /** This function converts a completed table into a function. */
 
-/** This function creates an element variable over a set. */
+       Function *completeTable(Table *, UndefinedBehavior behavior);
 
-Element *getElementVar(CSolver *, Set *set);
+       /** This function applies a function to the Elements in its input. */
 
-/** This function creates an element constrant. */
-Element *getElementConst(CSolver *, VarType type, uint64_t value);
+       Element *applyFunction(Function *function, Element **array, uint numArrays, BooleanEdge overflowstatus);
 
-/** This function creates a boolean variable. */
+       /** This function applies a predicate to the Elements in its input. */
 
-Boolean *getBooleanVar(CSolver *, VarType type);
+       BooleanEdge applyPredicateTable(Predicate *predicate, Element **inputs, uint numInputs, BooleanEdge undefinedStatus);
 
-/** This function creates a function operator. */
+       BooleanEdge applyPredicate(Predicate *predicate, Element **inputs, uint numInputs);
 
-Function *createFunctionOperator(CSolver *solver, ArithOp op, Set **domain, uint numDomain, Set *range,
-                                                                                                                                OverFlowBehavior overflowbehavior);
+       /** This exactly one element of array can be true! (i.e. a1 + a2 + a3 + ... + an = 1)*/
+       BooleanEdge applyExactlyOneConstraint (BooleanEdge *array, uint asize);
 
-/** This function creates a predicate operator. */
+       /** This function applies a logical operation to the Booleans in its input. */
 
-Predicate *createPredicateOperator(CSolver *solver, CompOp op, Set **domain, uint numDomain);
+       BooleanEdge applyLogicalOperation(LogicOp op, BooleanEdge *array, uint asize);
 
-Predicate *createPredicateTable(CSolver *solver, Table *table, UndefinedBehavior behavior);
+       /** This function applies a logical operation to the Booleans in its input. */
 
-/** This function creates an empty instance table.*/
+       BooleanEdge applyLogicalOperation(LogicOp op, BooleanEdge arg1, BooleanEdge arg2);
 
-Table *createTable(CSolver *solver, Set **domains, uint numDomain, Set *range);
+       /** This function applies a logical operation to the Booleans in its input. */
 
-Table *createTableForPredicate(CSolver *solver, Set **domains, uint numDomain);
-/** This function adds an input output relation to a table. */
+       BooleanEdge applyLogicalOperation(LogicOp op, BooleanEdge arg);
 
-void addTableEntry(CSolver *solver, Table *table, uint64_t *inputs, uint inputSize, uint64_t result);
+       /** This function adds a boolean constraint to the set of constraints
+           to be satisfied */
 
-/** This function converts a completed table into a function. */
+       void addConstraint(BooleanEdge constraint);
 
-Function *completeTable(CSolver *, Table *, UndefinedBehavior behavior);
+       /** This function instantiates an order of type type over the set set. */
+       Order *createOrder(OrderType type, Set *set);
 
-/** This function applies a function to the Elements in its input. */
+       /** This function instantiates a boolean on two items in an order. */
+       BooleanEdge orderConstraint(Order *order, uint64_t first, uint64_t second);
 
-Element *applyFunction(CSolver *, Function *function, Element **array, uint numArrays, Boolean *overflowstatus);
+       /** When everything is done, the client calls this function and then csolver starts to encode*/
+       int solve();
+       /**
+        * Incremental Solving for SATUNE.
+        * It only supports incremental solving for elements!
+        * No support for BooleanVar, BooleanOrder or using interpreters
+        * @return
+        */
+       int solveIncremental();
 
-/** This function applies a predicate to the Elements in its input. */
+       /** After getting the solution from the SAT solver, client can get the value of an element via this function*/
+       uint64_t getElementValue(Element *element);
 
-Boolean *applyPredicateTable(CSolver *, Predicate *predicate, Element **inputs, uint numInputs, Boolean *undefinedStatus);
+       void freezeElement(Element *e);
+       void turnoffOptimizations(){optimizationsOff = true;}
+       /** After getting the solution from the SAT solver, client can get the value of a boolean via this function*/
+       bool getBooleanValue(BooleanEdge boolean);
 
-Boolean *applyPredicate(CSolver *, Predicate *predicate, Element **inputs, uint numInputs);
+       bool getOrderConstraintValue(Order *order, uint64_t first, uint64_t second);
 
-/** This function applies a logical operation to the Booleans in its input. */
+       bool isTrue(BooleanEdge b);
+       bool isFalse(BooleanEdge b);
 
-Boolean *applyLogicalOperation(CSolver *, LogicOp op, Boolean **array, uint asize);
+       void setUnSAT() { model_print("Setting UNSAT %%%%%%\n"); unsat = true; }
+       void setSatSolverTimeout(long seconds) { satsolverTimeout = seconds;}
+       bool isUnSAT() { return unsat; }
+       bool isBooleanVarUsed() {return booleanVarUsed;}
+       void printConstraint(BooleanEdge boolean);
+       void printConstraints();
 
-/** This function adds a boolean constraint to the set of constraints
-    to be satisfied */
+       Vector<Order *> *getOrders() { return &allOrders;}
+       HashsetOrder *getActiveOrders() { return &activeOrders;}
 
-void addConstraint(CSolver *, Boolean *constraint);
+       Tuner *getTuner() { return tuner; }
 
-/** This function instantiates an order of type type over the set set. */
-Order *createOrder(CSolver *, OrderType type, Set *set);
+       SetIteratorBooleanEdge *getConstraints() { return constraints.iterator(); }
+       bool isConstraintEncoded(BooleanEdge be) { return encodedConstraints.contains(be);}
+       void addEncodedConstraint(BooleanEdge be) {encodedConstraints.add(be);}
 
-/** This function instantiates a boolean on two items in an order. */
-Boolean *orderConstraint(CSolver *, Order *order, uint64_t first, uint64_t second);
+       SATEncoder *getSATEncoder() {return satEncoder;}
 
-/** When everything is done, the client calls this function and then csolver starts to encode*/
-int startEncoding(CSolver *);
+       void replaceBooleanWithTrue(BooleanEdge bexpr);
+       void replaceBooleanWithTrueNoRemove(BooleanEdge bexpr);
+       void replaceBooleanWithFalseNoRemove(BooleanEdge bexpr);
+       void replaceBooleanWithFalse(BooleanEdge bexpr);
+       void replaceBooleanWithBoolean(BooleanEdge oldb, BooleanEdge newb);
+       CSolver *clone();
+       void serialize();
+       static CSolver *deserialize(const char *file, InterpreterType itype = SATUNE);
+       void autoTune(uint budget);
+       void inferFixedOrders();
+       void inferFixedOrder(Order *order);
+       void setInterpreter(InterpreterType type);
+       bool useInterpreter() {return interpreter != NULL;}
+       void setTuner(Tuner *_tuner) { tuner = _tuner; }
+       long long getElapsedTime() { return elapsedTime; }
+       long long getEncodeTime();
+       long long getSolveTime();
+       long getSatSolverTimeout() { return satsolverTimeout;}
+       bool isIncrementalMode() {return incrementalMode;}
+       void freezeElementsVariables();
+       CMEMALLOC;
 
-/** After getting the solution from the SAT solver, client can get the value of an element via this function*/
-uint64_t getElementValue(CSolver *, Element *element);
+private:
+       void handleIFFTrue(BooleanLogic *bexpr, BooleanEdge child);
+       void handleANDTrue(BooleanLogic *bexpr, BooleanEdge child);
+       void handleFunction(ElementFunction *ef, BooleanEdge child);
 
-/** After getting the solution from the SAT solver, client can get the value of a boolean via this function*/
-bool getBooleanValue( CSolver *, Boolean *boolean);
+       //These two functions are helpers if the client has a pointer to a
+       //Boolean object that we have since replaced
+       BooleanEdge rewriteLogicalOperation(LogicOp op, BooleanEdge *array, uint asize);
+       BooleanEdge doRewrite(BooleanEdge b);
+       /** This is a vector of constraints that must be satisfied. */
+       HashsetBooleanEdge constraints;
+       HashsetBooleanEdge encodedConstraints;
+
+       /** This is a vector of all boolean structs that we have allocated. */
+       Vector<Boolean *> allBooleans;
+
+       /** This is a vector of all set structs that we have allocated. */
+       Vector<Set *> allSets;
+
+       /** This is a vector of all element structs that we have allocated. */
+       Vector<Element *> allElements;
+
+       /** This is a vector of all predicate structs that we have allocated. */
+       Vector<Predicate *> allPredicates;
+
+       /** This is a vector of all table structs that we have allocated. */
+       Vector<Table *> allTables;
 
-HappenedBefore getOrderConstraintValue(CSolver *, Order *order, uint64_t first, uint64_t second);
+       /** This is a vector of all order structs that we have allocated. */
+       Vector<Order *> allOrders;
+
+       HashsetOrder activeOrders;
+
+       /** This is a vector of all function structs that we have allocated. */
+       Vector<Function *> allFunctions;
+
+       BooleanEdge boolTrue;
+       BooleanEdge boolFalse;
+
+       /** These two tables are used for deduplicating entries. */
+       BooleanMatchMap boolMap;
+       ElementMatchMap elemMap;
+
+       SATEncoder *satEncoder;
+       bool unsat;
+       bool booleanVarUsed;
+       bool incrementalMode;
+       bool optimizationsOff;
+       Tuner *tuner;
+       long long elapsedTime;
+       long satsolverTimeout;
+       Interpreter *interpreter;
+       bool noOptimization;
+       friend class ElementOpt;
+       friend class VarOrderingOpt;
+};
 
+inline CompOp flipOp(CompOp op) {
+       switch (op) {
+       case SATC_EQUALS:
+               return SATC_EQUALS;
+       case SATC_LT:
+               return SATC_GT;
+       case SATC_GT:
+               return SATC_LT;
+       case SATC_LTE:
+               return SATC_GTE;
+       case SATC_GTE:
+               return SATC_LTE;
+       }
+       ASSERT(0);
+}
+
+inline CompOp negateOp(CompOp op) {
+       switch (op) {
+       case SATC_EQUALS:
+               ASSERT(0);
+       case SATC_LT:
+               return SATC_GTE;
+       case SATC_GT:
+               return SATC_LTE;
+       case SATC_LTE:
+               return SATC_GT;
+       case SATC_GTE:
+               return SATC_LT;
+       }
+       ASSERT(0);
+}
 #endif