Module logicng

Class MiniSatStyleSolver

    • Field Detail

      • ok

        protected boolean ok
      • qhead

        protected int qhead
      • orderHeap

        protected LNGHeap orderHeap
      • analyzeBtLevel

        protected int analyzeBtLevel
      • claInc

        protected double claInc
      • simpDBAssigns

        protected int simpDBAssigns
      • simpDBProps

        protected int simpDBProps
      • clausesLiterals

        protected int clausesLiterals
      • learntsLiterals

        protected int learntsLiterals
      • varDecay

        protected double varDecay
      • varInc

        protected double varInc
      • restartFirst

        protected int restartFirst
      • restartInc

        protected double restartInc
      • clauseDecay

        protected double clauseDecay
      • shouldRemoveSatsisfied

        protected boolean shouldRemoveSatsisfied
      • learntsizeFactor

        protected double learntsizeFactor
      • learntsizeInc

        protected double learntsizeInc
      • incremental

        protected boolean incremental
      • canceledByHandler

        protected boolean canceledByHandler
      • backboneCandidates

        protected Stack<Integer> backboneCandidates
      • backboneAssumptions

        protected LNGIntVector backboneAssumptions
      • computingBackbone

        protected boolean computingBackbone
      • selectionOrderIdx

        protected int selectionOrderIdx
      • learntsizeAdjustConfl

        protected double learntsizeAdjustConfl
      • learntsizeAdjustCnt

        protected int learntsizeAdjustCnt
      • learntsizeAdjustStartConfl

        protected int learntsizeAdjustStartConfl
      • learntsizeAdjustInc

        protected double learntsizeAdjustInc
      • maxLearnts

        protected double maxLearnts
    • Constructor Detail

      • MiniSatStyleSolver

        protected MiniSatStyleSolver​(MiniSatConfig config)
        Constructs a new MiniSAT-style solver with a given configuration.
        Parameters:
        config - the configuration
    • Method Detail

      • getName2idx

        public Map<String,​Integer> getName2idx()
        Returns the name-to-index mapping for variables.
        Returns:
        the name-to-index mapping
      • mkLit

        public static int mkLit​(int var,
                                boolean sign)
        Creates a literal for a given variable number and literal.
        Parameters:
        var - the variable number
        sign - true if the literal is negative, false otherwise
        Returns:
        the literal (as integer value)
      • not

        public static int not​(int lit)
        Negates a given literal.
        Parameters:
        lit - the literal
        Returns:
        the negated literal
      • sign

        public static boolean sign​(int lit)
        Returns true if a given literal is negated, false otherwise.
        Parameters:
        lit - the literal
        Returns:
        true if the literal is negated
      • var

        public static int var​(int lit)
        Returns the variable index for a given literal.
        Parameters:
        lit - the literal
        Returns:
        the variable index of the literal
      • luby

        protected static double luby​(double y,
                                     int x)
        Computes the next number in the Luby sequence.
        Parameters:
        y - the restart increment
        x - the current number of restarts
        Returns:
        the next number in the Luby sequence
      • initialize

        protected void initialize()
        Initializes the internal solver state.
      • initializeConfig

        protected void initializeConfig()
        Initializes the solver configuration.
      • v

        protected MSVariable v​(int lit)
        Returns the variable for a given literal.
        Parameters:
        lit - the literal
        Returns:
        the variable of the literal
      • value

        protected Tristate value​(int lit)
        Returns the assigned value of a given literal.
        Parameters:
        lit - the literal
        Returns:
        the assigned value of the literal
      • lt

        public boolean lt​(int x,
                          int y)
        Compares two variables by their activity.
        Parameters:
        x - the first variable
        y - the second variable
        Returns:
        true if the first variable's activity is larger than the second one's
      • idxForName

        public int idxForName​(String name)
        Returns the variable index for a given variable name.
        Parameters:
        name - the variable name
        Returns:
        the variable index for the name
      • nameForIdx

        public String nameForIdx​(int var)
        Returns the name for a given variable index.
        Parameters:
        var - the variable index
        Returns:
        the name for the index
      • addName

        public void addName​(String name,
                            int id)
        Adds a new variable name with a given variable index to this solver.
        Parameters:
        name - the variable name
        id - the variable index
      • newVar

        public abstract int newVar​(boolean sign,
                                   boolean dvar)
        Adds a new variable to the solver.
        Parameters:
        sign - the initial polarity of the new variable, true if negative, false if positive
        dvar - true if this variable can be used as a decision variable, false if it should not be used as a decision variable
        Returns:
        the index of the new variable
      • addClause

        public boolean addClause​(int lit,
                                 Proposition proposition)
        Adds a unit clause to the solver.
        Parameters:
        lit - the unit clause's literal
        proposition - a proposition (if required for proof tracing)
        Returns:
        true if the clause was added successfully, false otherwise
      • addClause

        public abstract boolean addClause​(LNGIntVector ps,
                                          Proposition proposition)
        Adds a clause to the solver.
        Parameters:
        ps - the literals of the clause
        proposition - a proposition (if required for proof tracing)
        Returns:
        true if the clause was added successfully, false otherwise
      • solve

        public abstract Tristate solve​(SATHandler handler)
        Solves the formula currently stored in the solver. Returns Tristate.TRUE if the formula is satisfiable (SAT), Tristate.FALSE if the formula is unsatisfiable (UNSAT), or Tristate.UNDEF if the computation was canceled by a SATHandler. If null is passed as handler, the solver will run until the satisfiability is decided.
        Parameters:
        handler - a sat handler
        Returns:
        Tristate.TRUE if the formula is satisfiable, Tristate.FALSE if the formula is not satisfiable, or Tristate.UNDEF if the computation was canceled.
      • solve

        public Tristate solve​(SATHandler handler,
                              LNGIntVector assumptions)
        Solves the formula currently stored in the solver together with the given assumption literals. Returns Tristate.TRUE if the formula and the assumptions are satisfiable (SAT), Tristate.FALSE if the formula and the assumptions are not satisfiable together (UNSAT), or Tristate.UNDEF if the computation was canceled by a SATHandler. If null is passed as handler, the solver will run until the satisfiability is decided.
        Parameters:
        handler - a sat handler
        assumptions - the assumptions as a given vector of literals
        Returns:
        Tristate.TRUE if the formula and the assumptions are satisfiable, Tristate.FALSE if they are not satisfiable, or Tristate.UNDEF if the computation was canceled.
      • reset

        public abstract void reset()
        Resets the solver state.
      • model

        public LNGBooleanVector model()
        Returns the current model of the solver or an empty vector if there is none.
        Returns:
        the current model of the solver
      • ok

        public boolean ok()
        Returns false if this solver is known to be in a conflicting state, otherwise true.
        Returns:
        false if this solver is known to be in a conflicting state, otherwise true
      • conflict

        public LNGIntVector conflict()
        Returns the current conflict of the solver or an empty vector if there is none.
        Returns:
        the current conflict of the solver
      • saveState

        public abstract int[] saveState()
        Saves and returns the solver state expressed as an integer array which stores the length of the internal data structures.
        Returns:
        the current solver state
        Throws:
        UnsupportedOperationException - if the solver does not support state saving/loading
        IllegalStateException - if the solver is not in incremental mode
      • loadState

        public abstract void loadState​(int[] state)
        Loads a given state in the solver.

        ATTENTION: You can only load a state which was created by this instance of the solver before the current state. Only the sizes of the internal data structures are stored, meaning you can track back in time and restore a solver state with fewer variables and/or fewer clauses. It is not possible to import a solver state from another solver or another solving execution.

        Parameters:
        state - the solver state to load
        Throws:
        UnsupportedOperationException - if the solver does not support state saving/loading
        IllegalStateException - if the solver is not in incremental mode
      • nVars

        public int nVars()
        Returns the number of variables of the solver.
        Returns:
        the number of variables of the solver
      • name2idx

        public Map<String,​Integer> name2idx()
        Returns the mapping from variable names to internal solver indices.
        Returns:
        the mapping from variable names to internal solver indices
      • nAssigns

        protected int nAssigns()
        Returns the number of assigned variables.
        Returns:
        the number of assigned variables
      • decisionLevel

        protected int decisionLevel()
        Returns the current decision level of the solver.
        Returns:
        the current decision level of the solver
      • abstractLevel

        protected int abstractLevel​(int x)
        Helper function used to maintain an abstraction of levels involved during conflict analysis.
        Parameters:
        x - a variable index
        Returns:
        the abstraction of levels
      • insertVarOrder

        protected void insertVarOrder​(int x)
        Inserts a variable (given by its index) into the heap of decision variables.
        Parameters:
        x - the variable index
      • pickBranchLit

        protected int pickBranchLit()
        Picks the next branching literal.
        Returns:
        the literal or -1 if there are no unassigned literals left
      • varDecayActivity

        protected void varDecayActivity()
        Decays the variable activity increment by the variable decay factor.
      • varBumpActivity

        protected void varBumpActivity​(int v)
        Bumps the activity of the variable at a given index.
        Parameters:
        v - the variable index
      • varBumpActivity

        protected void varBumpActivity​(int v,
                                       double inc)
        Bumps the activity of the variable at a given index by a given value.
        Parameters:
        v - the variable index
        inc - the increment value
      • rebuildOrderHeap

        protected void rebuildOrderHeap()
        Rebuilds the heap of decision variables.
      • locked

        protected boolean locked​(MSClause c)
        Returns true if the given clause is locked and therefore cannot be removed, false otherwise.
        Parameters:
        c - the clause
        Returns:
        true if the given clause is locked
      • claDecayActivity

        protected void claDecayActivity()
        Decays the clause activity increment by the clause decay factor.
      • claBumpActivity

        protected void claBumpActivity​(MSClause c)
        Bumps the activity of the given clause.
        Parameters:
        c - the clause
      • uncheckedEnqueue

        protected abstract void uncheckedEnqueue​(int lit,
                                                 MSClause reason)
        Assigns a literal (= a variable to the respective value).
        Parameters:
        lit - the literal
        reason - the reason clause of the assignment (conflict resolution) or null if it was a decision
      • attachClause

        protected abstract void attachClause​(MSClause c)
        Attaches a given clause to the solver (i.e. the watchers for this clause are initialized).
        Parameters:
        c - the clause
      • detachClause

        protected abstract void detachClause​(MSClause c)
        Detaches a given clause (e.g. removes all watchers pointing to this clause).
        Parameters:
        c - the clause
      • removeClause

        protected abstract void removeClause​(MSClause c)
        Removes a given clause.
        Parameters:
        c - the clause to remove
      • propagate

        protected abstract MSClause propagate()
        Performs unit propagation.
        Returns:
        the conflicting clause if a conflict arose during unit propagation or null if there was none
      • litRedundant

        protected abstract boolean litRedundant​(int p,
                                                int abstractLevels)
        Returns true if a given literal is redundant in the current conflict analysis, false otherwise.
        Parameters:
        p - the literal
        abstractLevels - an abstraction of levels
        Returns:
        true if a given literal is redundant in the current conflict analysis
      • analyzeFinal

        protected abstract void analyzeFinal​(int p,
                                             LNGIntVector outConflict)
        Analysis the final conflict if there were assumptions.
        Parameters:
        p - the conflicting literal
        outConflict - the vector to store the final conflict
      • cancelUntil

        protected void cancelUntil​(int level)
      • reduceDB

        protected abstract void reduceDB()
        Reduces the database of learnt clauses. Only clauses of the first half of the clauses with the most activity are possibly removed. A clause is only removed if it is not locked, i.e. is the reason of an assignment for a variable.
      • removeSatisfied

        protected abstract void removeSatisfied​(LNGVector<MSClause> cs)
        Removes all clauses which are satisfied under the current assignment of a set of clauses.
        Parameters:
        cs - the set of clauses
      • satisfied

        protected abstract boolean satisfied​(MSClause c)
        Returns true if a given clause is satisfied under the current assignment, false otherwise.
        Parameters:
        c - the clause
        Returns:
        true if a given clause is satisfied under the current assignment
      • simplify

        protected abstract boolean simplify()
        Simplifies the database of clauses. This method is only executed on level 0. All learnt clauses which are satisfied on level 0 are removed. Depending on the configuration of the solver, also original clauses which are satisfied at level 0 are removed.
        Returns:
        true if simplification was successful and no conflict was found, false if a conflict was found during the simplification
      • decayActivities

        protected void decayActivities()
      • pgProof

        public LNGVector<LNGIntVector> pgProof()
        Returns the proof clauses for proof generation.
        Returns:
        the proof clauses for proof generation
      • upZeroLiterals

        public LNGIntVector upZeroLiterals()
        Returns the unit propagated literals on level zero.
        Returns:
        unit propagated literal on level zero
      • computeBackbone

        public Backbone computeBackbone​(Collection<Variable> variables,
                                        BackboneType type)
        Computes the backbone of the given variables with respect to the formulas added to the solver.
        Parameters:
        variables - variables to test
        type - backbone type
        Returns:
        the backbone projected to the relevant variables
      • computeBackbone

        public Backbone computeBackbone​(Collection<Variable> variables,
                                        BackboneType type,
                                        SATHandler handler)
        Computes the backbone of the given variables with respect to the formulas added to the solver.
        Parameters:
        variables - variables to test
        type - backbone type
        handler - the handler
        Returns:
        the backbone projected to the relevant variables or null if the computation was aborted by the handler
      • getRelevantVarIndices

        protected List<Integer> getRelevantVarIndices​(Collection<Variable> variables)
        Returns a list of relevant variable indices. A relevant variable is known by the solver.
        Parameters:
        variables - variables to convert and filter
        Returns:
        list of relevant variable indices
      • initBackboneDS

        protected void initBackboneDS​(List<Integer> variables)
        Initializes the internal solver state for backbones.
        Parameters:
        variables - to test
      • computeBackbone

        protected void computeBackbone​(List<Integer> variables,
                                       BackboneType type,
                                       SATHandler handler)
        Computes the backbone for the given variables.
        Parameters:
        variables - variables to test
        type - the type of the backbone
        handler - the handler
      • createInitialCandidates

        protected Stack<Integer> createInitialCandidates​(List<Integer> variables,
                                                         BackboneType type)
        Creates the initial candidate literals for the backbone computation.
        Parameters:
        variables - variables to test
        type - the type of the backbone
        Returns:
        initial candidates
      • refineUpperBound

        protected void refineUpperBound()
        Refines the upper bound by optional checks (UP zero literal, complement model literal, rotatable literal).
      • solveWithLit

        protected boolean solveWithLit​(int lit,
                                       SATHandler handler)
        Tests the given literal with the formula on the solver for satisfiability.
        Parameters:
        lit - literal to test
        handler - the handler
        Returns:
        true if satisfiable, otherwise false
      • buildBackbone

        protected Backbone buildBackbone​(Collection<Variable> variables,
                                         BackboneType type)
        Builds the backbone object from the computed backbone literals.
        Parameters:
        variables - relevant variables
        type - the type of the backbone
        Returns:
        backbone
      • isUPZeroLit

        protected boolean isUPZeroLit​(int var)
        Tests the given variable whether it is a unit propagated literal on level 0.

        Assumption: The formula on the solver has successfully been tested to be satisfiable before.

        Parameters:
        var - variable index to test
        Returns:
        true if the variable is a unit propagated literal on level 0, otherwise false
      • isUnit

        protected boolean isUnit​(int lit,
                                 MSClause clause)
        Tests the given literal whether it is unit in the given clause.
        Parameters:
        lit - literal to test
        clause - clause containing the literal
        Returns:
        true if the literal is unit, false otherwise
      • isRotatable

        protected boolean isRotatable​(int lit)
        Tests the given literal whether it is rotatable in the current model.
        Parameters:
        lit - literal to test
        Returns:
        true if the literal is rotatable, otherwise false
      • addBackboneLiteral

        protected void addBackboneLiteral​(int lit)
        Adds the given literal to the backbone result and optionally adds the literal to the solver.
        Parameters:
        lit - literal to add
      • isBothOrPositiveType

        protected boolean isBothOrPositiveType​(BackboneType type)
      • isBothOrNegativeType

        protected boolean isBothOrNegativeType​(BackboneType type)
      • isBothType

        protected boolean isBothType​(BackboneType type)
      • clauses

        public LNGVector<MSClause> clauses()
        Returns the clauses loaded on the solver.
        Returns:
        the clauses loaded on the solver
      • variables

        public LNGVector<MSVariable> variables()
        Returns the variables known by the solver.
        Returns:
        the variables
      • setSelectionOrder

        public void setSelectionOrder​(List<? extends Literal> selectionOrder)
        Sets the variable's selection order that is used to solve the formula on the solver.

        If a custom selection order is set, the solver will pick a variable from the custom order in order to branch on it during the search. The given polarity in the selection order is used as assignment for the variable. If all variables in the custom order are already assigned, the solver falls back to the activity based variable selection.

        Parameters:
        selectionOrder - the custom selection order
      • resetSelectionOrder

        public void resetSelectionOrder()
        Resets a previously set selection order.