Module logicng

Class SATSolver

  • Direct Known Subclasses:
    MiniSat

    public abstract class SATSolver
    extends Object
    A generic interface for LogicNG's SAT solvers.
    Since:
    1.0
    Version:
    2.3.0
    • Constructor Detail

      • SATSolver

        protected SATSolver​(FormulaFactory f)
        Constructor.
        Parameters:
        f - the formula factory
    • Method Detail

      • add

        public void add​(Formula formula)
        Adds a formula to the solver. The formula is first converted to CNF.
        Parameters:
        formula - the formula
      • add

        public abstract void add​(Formula formula,
                                 Proposition proposition)
        Adds a formula to the solver. The formula is first converted to CNF.
        Parameters:
        formula - the formula
        proposition - the proposition of this formula
      • addWithoutUnknown

        @Deprecated
        public abstract void addWithoutUnknown​(Formula formula)
        Deprecated.
        Due to simplifications of formulas in LogicNG the solver might not know all original variables of the formulas added. If such a variable is contained in the given formula, this method sets the variable to false. For a caller this behavior is misleading and can yield to erroneous results. Instead of using this method, a caller should track the unknown/forbidden variables by itself and add each variable negated to the solver.
        Adds a formula to the solver, but sets all variables to false which are not known to the solver.
        Parameters:
        formula - the formula
      • addPropositions

        public void addPropositions​(Collection<? extends Proposition> propositions)
        Adds a given set of propositions to the solver.
        Parameters:
        propositions - the set of propositions
      • addPropositions

        public void addPropositions​(Proposition... propositions)
        Adds a given set of propositions to the solver.
        Parameters:
        propositions - the set of propositions
      • add

        public void add​(Proposition proposition)
        Adds a proposition to the solver. The formulas of the proposition are first converted to CNF.
        Parameters:
        proposition - the proposition
      • add

        public void add​(Collection<? extends Formula> formulas)
        Adds a collection of formulas to the solver.
        Parameters:
        formulas - the collection of formulas
      • addWithRelaxation

        public void addWithRelaxation​(Variable relaxationVar,
                                      Formula formula)
        Adds a formula to the solver by using the given relaxation variable, i.e. by adding the disjunction of the relaxation variable and the formula.
        Parameters:
        relaxationVar - the relaxation variable
        formula - the formula
      • addWithRelaxation

        @Deprecated
        public void addWithRelaxation​(Variable relaxationVar,
                                      Proposition proposition)
        Deprecated.
        since this method only adds the formula of the proposition but not the proposition itself, this method has a confusing behaviour and will be removed in upcoming releases.
        Adds a proposition to the solver by using the given relaxation variable, i.e. by adding the disjunction of the relaxation variable and the formula of the proposition. Only the formula is added to the solver, the proposition is thrown away.
        Parameters:
        relaxationVar - the relaxation variable
        proposition - the proposition
      • addWithRelaxation

        public void addWithRelaxation​(Variable relaxationVar,
                                      Collection<? extends Formula> formulas)
        Adds a collection of formulas to the solver by using the given relaxation variable, i.e. for each formula adding the disjunction of the relaxation variable and the formula.
        Parameters:
        relaxationVar - the relaxation variable
        formulas - the collection of formulas
      • addIncrementalCC

        public abstract CCIncrementalData addIncrementalCC​(CardinalityConstraint cc)
        Adds a cardinality constraint and returns its incremental data in order to refine the constraint on the solver.

        Usage constraints: - "<": Cannot be used with right-hand side 2, returns null for right-hand side 1, but constraint is added to solver. - "<=": Cannot be used with right-hand side 1, returns null for right-hand side 0, but constraint is added to solver. - ">": Returns null for right-hand side 0 or number of variables -1, but constraint is added to solver. Adds false to solver for right-hand side >= number of variables. - ">=": Returns null for right-hand side 1 or number of variables, but constraint is added to solver. Adds false to solver for right-hand side > number of variables.

        Parameters:
        cc - the cardinality constraint
        Returns:
        the incremental data of this constraint, or null if the right-hand side of cc is 1
      • addClause

        protected abstract void addClause​(Formula formula,
                                          Proposition proposition)
        Adds a formula which must be a clause to the solver.
        Parameters:
        formula - the clause
        proposition - a proposition (if required for proof tracing)
      • sat

        public Tristate sat()
        Returns Tristate.TRUE if the current formula in the solver is satisfiable, @{code Tristate.FALSE} if it is unsatisfiable, or UNDEF if the solving process was aborted.
        Returns:
        the satisfiability of the formula in the solver
      • sat

        public abstract Tristate sat​(SATHandler handler)
        Returns Tristate.TRUE if the current formula in the solver is satisfiable, @{code Tristate.FALSE} if it is unsatisfiable, or UNDEF if the solving process was aborted.
        Parameters:
        handler - the SAT handler
        Returns:
        the satisfiability of the formula in the solver
      • sat

        public Tristate sat​(Literal literal)
        Returns Tristate.TRUE if the current formula in the solver and a given literal are satisfiable, Tristate.FALSE if it is unsatisfiable, or UNDEF if the solving process was aborted.
        Parameters:
        literal - the assumed literal
        Returns:
        the satisfiability of the formula in the solver
      • sat

        public Tristate sat​(Collection<? extends Literal> assumptions)
        Returns Tristate.TRUE if the current formula in the solver and a given collection of assumed literals are satisfiable, Tristate.FALSE if it is unsatisfiable, or UNDEF if the solving process was aborted. The assumptions can be seen as an additional conjunction of literals. Note: Use ordered collections to ensure determinism in the solving process and thus in the resulting model or conflict.
        Parameters:
        assumptions - a collection of literals
        Returns:
        the satisfiability of the formula in the solver
      • sat

        public abstract Tristate sat​(SATHandler handler,
                                     Literal literal)
        Returns Tristate.TRUE if the current formula in the solver and a given literal are satisfiable, Tristate.FALSE if it is unsatisfiable, or UNDEF if the solving process was aborted.
        Parameters:
        handler - the SAT handler
        literal - the assumed literal
        Returns:
        the satisfiability of the formula in the solver
      • sat

        public abstract Tristate sat​(SATHandler handler,
                                     Collection<? extends Literal> assumptions)
        Returns Tristate.TRUE if the current formula in the solver and a given collection of assumed literals are satisfiable, Tristate.FALSE if it is unsatisfiable, or UNDEF if the solving process was aborted. The assumptions can be seen as an additional conjunction of literals. Note: Use ordered collections to ensure determinism in the solving process and thus in the resulting model or conflict.
        Parameters:
        handler - the SAT handler
        assumptions - a collection of literals
        Returns:
        the satisfiability of the formula in the solver
      • satWithSelectionOrder

        public Tristate satWithSelectionOrder​(List<? extends Literal> selectionOrder)
        Solves the formula on the solver with a given selection order.

        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.

        Example: Order a, ~b, c. The solver picks variable `a`, if not assigned yet, and checks if setting `a` to true leads to a satisfying assignment. Next, the solver picks variable b and checks if setting b to false leads to a satisfying assignment.

        Parameters:
        selectionOrder - the order of the literals for the selection order
        Returns:
        the satisfiability of the formula in the solver
      • satWithSelectionOrder

        public Tristate satWithSelectionOrder​(List<? extends Literal> selectionOrder,
                                              SATHandler handler,
                                              Collection<? extends Literal> assumptions)
        Solves the formula on the solver with a given selection order, a given SAT handler and a list of additional assumptions.

        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.

        Example: Order a, ~b, c. The solver picks variable `a`, if not assigned yet, and checks if setting `a` to true leads to a satisfying assignment. Next, the solver picks variable b and checks if setting b to false leads to a satisfying assignment.

        Parameters:
        selectionOrder - the order of the literals for the selection order
        handler - the SAT handler
        assumptions - a collection of literals
        Returns:
        the satisfiability of the formula in the solver
      • reset

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

        public Assignment model()
        Returns a model of the current formula on the solver. If the formula is UNSAT, null will be returned.
        Returns:
        a model of the current formula
      • model

        public Assignment model​(Variable[] variables)
        Returns a model of the current formula on the solver wrt. a given set of variables. If the set is null, all variables are considered relevant. If the formula is UNSAT, null will be returned. The formula in the solver has to be solved first, before a model can be obtained.
        Parameters:
        variables - the set of variables
        Returns:
        a model of the current formula
        Throws:
        IllegalStateException - if the formula is not yet solved
      • model

        public abstract Assignment model​(Collection<Variable> variables)
        Returns a model of the current formula on the solver wrt. a given set of variables. If the set is null, all variables are considered relevant. If the formula is UNSAT, null will be returned.
        Parameters:
        variables - the set of variables
        Returns:
        a model of the current formula
      • execute

        public abstract <RESULT> RESULT execute​(SolverFunction<RESULT> function)
        Executes a solver function on this solver.
        Type Parameters:
        RESULT - the result type of the function
        Parameters:
        function - the solver function
        Returns:
        the result of executing the solver function on the current solver
      • enumerateAllModels

        public List<Assignment> enumerateAllModels()
        Enumerates all models of the current formula.
        Returns:
        the list of models
      • enumerateAllModels

        public List<Assignment> enumerateAllModels​(Collection<Variable> variables)
        Enumerates all models of the current formula wrt. a given set of variables. If the set is null, all variables are considered relevant.
        Parameters:
        variables - the set of variables
        Returns:
        the list of models
      • enumerateAllModels

        public List<Assignment> enumerateAllModels​(Variable... variables)
        Enumerates all models of the current formula wrt. a given set of variables. If the set is null, all variables are considered relevant.
        Parameters:
        variables - the set of variables
        Returns:
        the list of models
      • setSolverToUndef

        public void setSolverToUndef()
        Sets the solver state to UNDEF (required if you fiddle e.g. with the underlying solver).
      • knownVariables

        public abstract SortedSet<Variable> knownVariables()
        Returns the set of variables currently known by the solver. NOTE: Due to the incremental/decremental interface of some solvers, this set is generated each time, the method is called. So if you can maintain a list of relevant/known variables in your own application, this is recommended.
        Returns:
        the set of variables currently known by the solver
      • unsatCore

        public UNSATCore<Proposition> unsatCore()
        Returns an unsat core of the current problem. Only works if the SAT solver is configured to record the information required to generate a proof trace and an unsat core. In particular, this method returns the unsat core only if the parameter "proofGeneration" in the MiniSatConfig is set to "true".
        Returns:
        the unsat core
      • backbone

        public Backbone backbone​(Collection<Variable> relevantVariables)
        Computes a backbone with both positive and negative variables of the current formula on the solver.
        Parameters:
        relevantVariables - the variables which should be considered for the backbone
        Returns:
        the backbone
      • backbone

        public Backbone backbone​(Collection<Variable> relevantVariables,
                                 BackboneType type)
        Computes a backbone of the current formula on the solver.
        Parameters:
        relevantVariables - the variables which should be considered for the backbone
        type - the type of backbone which should be computed
        Returns:
        the backbone
      • factory

        public FormulaFactory factory()
        Returns the formula factory for this solver.
        Returns:
        the formula factory
      • setSelectionOrder

        protected abstract void setSelectionOrder​(List<? extends Literal> selectionOrder)
        Sets the selection order of the variables and their polarity.

        Parameters:
        selectionOrder - the variable order and their polarity that should be checked first
      • resetSelectionOrder

        protected abstract void resetSelectionOrder()
        Resets the selection order on the solver. The internal activity heuristics for the variable ordering will be used again.
      • canSaveLoadState

        public abstract boolean canSaveLoadState()
        Returns whether this solver instance can save and load solver states.
        Returns:
        true when the solver can save and load states, false otherwise
      • canGenerateProof

        public abstract boolean canGenerateProof()
        Returns whether this solver instance can generate proofs.
        Returns:
        true when the solver can generate proofs, false otherwise