- java.lang.Object
-
- org.logicng.solvers.SATSolver
-
-
Field Summary
Fields Modifier and Type Field Description protected FormulaFactoryfprotected Tristateresult
-
Constructor Summary
Constructors Modifier Constructor Description protectedSATSolver(FormulaFactory f)Constructor.
-
Method Summary
All Methods Instance Methods Abstract Methods Concrete Methods Deprecated Methods Modifier and Type Method Description voidadd(Collection<? extends Formula> formulas)Adds a collection of formulas to the solver.voidadd(Formula formula)Adds a formula to the solver.abstract voidadd(Formula formula, Proposition proposition)Adds a formula to the solver.voidadd(Proposition proposition)Adds a proposition to the solver.protected abstract voidaddClause(Formula formula, Proposition proposition)Adds a formula which must be a clause to the solver.abstract CCIncrementalDataaddIncrementalCC(CardinalityConstraint cc)Adds a cardinality constraint and returns its incremental data in order to refine the constraint on the solver.voidaddPropositions(Collection<? extends Proposition> propositions)Adds a given set of propositions to the solver.voidaddPropositions(Proposition... propositions)Adds a given set of propositions to the solver.abstract voidaddWithoutUnknown(Formula formula)Deprecated.Due to simplifications of formulas in LogicNG the solver might not know all original variables of the formulas added.voidaddWithRelaxation(Variable relaxationVar, Collection<? extends Formula> formulas)Adds a collection of formulas to the solver by using the given relaxation variable, i.e.voidaddWithRelaxation(Variable relaxationVar, Formula formula)Adds a formula to the solver by using the given relaxation variable, i.e.voidaddWithRelaxation(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.Backbonebackbone(Collection<Variable> relevantVariables)Computes a backbone with both positive and negative variables of the current formula on the solver.Backbonebackbone(Collection<Variable> relevantVariables, BackboneType type)Computes a backbone of the current formula on the solver.abstract booleancanGenerateProof()Returns whether this solver instance can generate proofs.abstract booleancanSaveLoadState()Returns whether this solver instance can save and load solver states.List<Assignment>enumerateAllModels()Enumerates all models of the current formula.List<Assignment>enumerateAllModels(Collection<Variable> variables)Enumerates all models of the current formula wrt.List<Assignment>enumerateAllModels(Variable... variables)Enumerates all models of the current formula wrt.abstract <RESULT> RESULTexecute(SolverFunction<RESULT> function)Executes a solver function on this solver.FormulaFactoryfactory()Returns the formula factory for this solver.abstract SortedSet<Variable>knownVariables()Returns the set of variables currently known by the solver.abstract voidloadState(SolverState state)Loads a given solver state.Assignmentmodel()Returns a model of the current formula on the solver.abstract Assignmentmodel(Collection<Variable> variables)Returns a model of the current formula on the solver wrt.Assignmentmodel(Variable[] variables)Returns a model of the current formula on the solver wrt.abstract voidreset()Resets the SAT solver.protected abstract voidresetSelectionOrder()Resets the selection order on the solver.Tristatesat()ReturnsTristate.TRUEif the current formula in the solver is satisfiable, @{code Tristate.FALSE} if it is unsatisfiable, orUNDEFif the solving process was aborted.Tristatesat(Collection<? extends Literal> assumptions)ReturnsTristate.TRUEif the current formula in the solver and a given collection of assumed literals are satisfiable,Tristate.FALSEif it is unsatisfiable, orUNDEFif the solving process was aborted.Tristatesat(Literal literal)ReturnsTristate.TRUEif the current formula in the solver and a given literal are satisfiable,Tristate.FALSEif it is unsatisfiable, orUNDEFif the solving process was aborted.abstract Tristatesat(SATHandler handler)ReturnsTristate.TRUEif the current formula in the solver is satisfiable, @{code Tristate.FALSE} if it is unsatisfiable, orUNDEFif the solving process was aborted.abstract Tristatesat(SATHandler handler, Collection<? extends Literal> assumptions)ReturnsTristate.TRUEif the current formula in the solver and a given collection of assumed literals are satisfiable,Tristate.FALSEif it is unsatisfiable, orUNDEFif the solving process was aborted.abstract Tristatesat(SATHandler handler, Literal literal)ReturnsTristate.TRUEif the current formula in the solver and a given literal are satisfiable,Tristate.FALSEif it is unsatisfiable, orUNDEFif the solving process was aborted.TristatesatWithSelectionOrder(List<? extends Literal> selectionOrder)Solves the formula on the solver with a given selection order.TristatesatWithSelectionOrder(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.abstract SolverStatesaveState()Saves the current solver state.protected abstract voidsetSelectionOrder(List<? extends Literal> selectionOrder)Sets the selection order of the variables and their polarity.voidsetSolverToUndef()Sets the solver state to UNDEF (required if you fiddle e.g.UNSATCore<Proposition>unsatCore()Returns an unsat core of the current problem.
-
-
-
Field Detail
-
f
protected final FormulaFactory f
-
result
protected Tristate result
-
-
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 formulaproposition- 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 variableformula- 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 variableproposition- 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 variableformulas- 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 clauseproposition- a proposition (if required for proof tracing)
-
sat
public Tristate sat()
ReturnsTristate.TRUEif the current formula in the solver is satisfiable, @{code Tristate.FALSE} if it is unsatisfiable, orUNDEFif the solving process was aborted.- Returns:
- the satisfiability of the formula in the solver
-
sat
public abstract Tristate sat(SATHandler handler)
ReturnsTristate.TRUEif the current formula in the solver is satisfiable, @{code Tristate.FALSE} if it is unsatisfiable, orUNDEFif 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)
ReturnsTristate.TRUEif the current formula in the solver and a given literal are satisfiable,Tristate.FALSEif it is unsatisfiable, orUNDEFif 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)
ReturnsTristate.TRUEif the current formula in the solver and a given collection of assumed literals are satisfiable,Tristate.FALSEif it is unsatisfiable, orUNDEFif 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)
ReturnsTristate.TRUEif the current formula in the solver and a given literal are satisfiable,Tristate.FALSEif it is unsatisfiable, orUNDEFif the solving process was aborted.- Parameters:
handler- the SAT handlerliteral- the assumed literal- Returns:
- the satisfiability of the formula in the solver
-
sat
public abstract Tristate sat(SATHandler handler, Collection<? extends Literal> assumptions)
ReturnsTristate.TRUEif the current formula in the solver and a given collection of assumed literals are satisfiable,Tristate.FALSEif it is unsatisfiable, orUNDEFif 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 handlerassumptions- 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 orderhandler- the SAT handlerassumptions- 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,nullwill 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 isnull, all variables are considered relevant. If the formula is UNSAT,nullwill 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 isnull, all variables are considered relevant. If the formula is UNSAT,nullwill 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 isnull, 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 isnull, all variables are considered relevant.- Parameters:
variables- the set of variables- Returns:
- the list of models
-
saveState
public abstract SolverState saveState()
Saves the current solver state.- Returns:
- the current solver state
- Throws:
UnsupportedOperationException- if the solver does not support state saving/loadingIllegalStateException- if the solver is not in incremental mode
-
loadState
public abstract void loadState(SolverState state)
Loads a given solver state.- Parameters:
state- the solver state- Throws:
UnsupportedOperationException- if the solver does not support state saving/loadingIllegalStateException- if the solver is not in incremental modeIllegalArgumentException- if the given state has become invalid
-
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 backbonetype- 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
-
-