- java.lang.Object
-
- org.logicng.solvers.sat.MiniSatStyleSolver
-
- Direct Known Subclasses:
GlucoseSyrup,MiniCard,MiniSat2Solver
public abstract class MiniSatStyleSolver extends Object
The super class for all MiniSAT-style solvers.- Since:
- 1.0
- Version:
- 2.1.0
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static classMiniSatStyleSolver.ProofInformationClass containing the information required for generating a proof.
-
Field Summary
-
Constructor Summary
Constructors Modifier Constructor Description protectedMiniSatStyleSolver(MiniSatConfig config)Constructs a new MiniSAT-style solver with a given configuration.
-
Method Summary
All Methods Static Methods Instance Methods Abstract Methods Concrete Methods Modifier and Type Method Description protected intabstractLevel(int x)Helper function used to maintain an abstraction of levels involved during conflict analysis.protected voidaddBackboneLiteral(int lit)Adds the given literal to the backbone result and optionally adds the literal to the solver.booleanaddClause(int lit, Proposition proposition)Adds a unit clause to the solver.abstract booleanaddClause(LNGIntVector ps, Proposition proposition)Adds a clause to the solver.voidaddName(String name, int id)Adds a new variable name with a given variable index to this solver.protected abstract voidanalyzeFinal(int p, LNGIntVector outConflict)Analysis the final conflict if there were assumptions.protected abstract voidattachClause(MSClause c)Attaches a given clause to the solver (i.e.protected BackbonebuildBackbone(Collection<Variable> variables, BackboneType type)Builds the backbone object from the computed backbone literals.protected voidcancelUntil(int level)protected voidclaBumpActivity(MSClause c)Bumps the activity of the given clause.protected voidclaDecayActivity()Decays the clause activity increment by the clause decay factor.LNGVector<MSClause>clauses()Returns the clauses loaded on the solver.BackbonecomputeBackbone(Collection<Variable> variables, BackboneType type)Computes the backbone of the given variables with respect to the formulas added to the solver.BackbonecomputeBackbone(Collection<Variable> variables, BackboneType type, SATHandler handler)Computes the backbone of the given variables with respect to the formulas added to the solver.protected voidcomputeBackbone(List<Integer> variables, BackboneType type, SATHandler handler)Computes the backbone for the given variables.LNGIntVectorconflict()Returns the current conflict of the solver or an empty vector if there is none.protected Stack<Integer>createInitialCandidates(List<Integer> variables, BackboneType type)Creates the initial candidate literals for the backbone computation.protected voiddecayActivities()protected intdecisionLevel()Returns the current decision level of the solver.protected abstract voiddetachClause(MSClause c)Detaches a given clause (e.g.Map<String,Integer>getName2idx()Returns the name-to-index mapping for variables.protected List<Integer>getRelevantVarIndices(Collection<Variable> variables)Returns a list of relevant variable indices.intidxForName(String name)Returns the variable index for a given variable name.protected voidinitBackboneDS(List<Integer> variables)Initializes the internal solver state for backbones.protected voidinitialize()Initializes the internal solver state.protected voidinitializeConfig()Initializes the solver configuration.protected voidinsertVarOrder(int x)Inserts a variable (given by its index) into the heap of decision variables.protected booleanisBothOrNegativeType(BackboneType type)protected booleanisBothOrPositiveType(BackboneType type)protected booleanisBothType(BackboneType type)protected booleanisRotatable(int lit)Tests the given literal whether it is rotatable in the current model.protected booleanisUnit(int lit, MSClause clause)Tests the given literal whether it is unit in the given clause.protected booleanisUPZeroLit(int var)Tests the given variable whether it is a unit propagated literal on level 0.protected abstract booleanlitRedundant(int p, int abstractLevels)Returnstrueif a given literal is redundant in the current conflict analysis,falseotherwise.abstract voidloadState(int[] state)Loads a given state in the solver.protected booleanlocked(MSClause c)Returnstrueif the given clause is locked and therefore cannot be removed,falseotherwise.booleanlt(int x, int y)Compares two variables by their activity.protected static doubleluby(double y, int x)Computes the next number in the Luby sequence.static intmkLit(int var, boolean sign)Creates a literal for a given variable number and literal.LNGBooleanVectormodel()Returns the current model of the solver or an empty vector if there is none.Map<String,Integer>name2idx()Returns the mapping from variable names to internal solver indices.StringnameForIdx(int var)Returns the name for a given variable index.protected intnAssigns()Returns the number of assigned variables.abstract intnewVar(boolean sign, boolean dvar)Adds a new variable to the solver.static intnot(int lit)Negates a given literal.intnVars()Returns the number of variables of the solver.booleanok()Returnsfalseif this solver is known to be in a conflicting state, otherwisetrue.LNGVector<MiniSatStyleSolver.ProofInformation>pgOriginalClauses()Returns the original clauses for proof generation.LNGVector<LNGIntVector>pgProof()Returns the proof clauses for proof generation.protected intpickBranchLit()Picks the next branching literal.protected abstract MSClausepropagate()Performs unit propagation.protected voidrebuildOrderHeap()Rebuilds the heap of decision variables.protected abstract voidreduceDB()Reduces the database of learnt clauses.protected voidrefineUpperBound()Refines the upper bound by optional checks (UP zero literal, complement model literal, rotatable literal).protected abstract voidremoveClause(MSClause c)Removes a given clause.protected abstract voidremoveSatisfied(LNGVector<MSClause> cs)Removes all clauses which are satisfied under the current assignment of a set of clauses.abstract voidreset()Resets the solver state.voidresetSelectionOrder()Resets a previously set selection order.protected abstract booleansatisfied(MSClause c)Returnstrueif a given clause is satisfied under the current assignment,falseotherwise.abstract int[]saveState()Saves and returns the solver state expressed as an integer array which stores the length of the internal data structures.voidsetSelectionOrder(List<? extends Literal> selectionOrder)Sets the variable's selection order that is used to solve the formula on the solver.static booleansign(int lit)Returnstrueif a given literal is negated,falseotherwise.protected abstract booleansimplify()Simplifies the database of clauses.abstract Tristatesolve(SATHandler handler)Solves the formula currently stored in the solver.Tristatesolve(SATHandler handler, LNGIntVector assumptions)Solves the formula currently stored in the solver together with the given assumption literals.protected booleansolveWithLit(int lit, SATHandler handler)Tests the given literal with the formula on the solver for satisfiability.StringtoString()protected abstract voiduncheckedEnqueue(int lit, MSClause reason)Assigns a literal (= a variable to the respective value).LNGIntVectorupZeroLiterals()Returns the unit propagated literals on level zero.protected MSVariablev(int lit)Returns the variable for a given literal.protected Tristatevalue(int lit)Returns the assigned value of a given literal.static intvar(int lit)Returns the variable index for a given literal.protected voidvarBumpActivity(int v)Bumps the activity of the variable at a given index.protected voidvarBumpActivity(int v, double inc)Bumps the activity of the variable at a given index by a given value.protected voidvarDecayActivity()Decays the variable activity increment by the variable decay factor.LNGVector<MSVariable>variables()Returns the variables known by the solver.
-
-
-
Field Detail
-
LIT_UNDEF
public static final int LIT_UNDEF
The undefined literal- See Also:
- Constant Field Values
-
config
protected final MiniSatConfig config
-
ok
protected boolean ok
-
qhead
protected int qhead
-
vars
protected LNGVector<MSVariable> vars
-
orderHeap
protected LNGHeap orderHeap
-
trail
protected LNGIntVector trail
-
trailLim
protected LNGIntVector trailLim
-
model
protected LNGBooleanVector model
-
conflict
protected LNGIntVector conflict
-
assumptions
protected LNGIntVector assumptions
-
seen
protected LNGBooleanVector seen
-
analyzeStack
protected LNGIntVector analyzeStack
-
analyzeToClear
protected LNGIntVector analyzeToClear
-
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
-
ccminMode
protected MiniSatConfig.ClauseMinimization ccminMode
-
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
-
handler
protected SATHandler handler
-
canceledByHandler
protected boolean canceledByHandler
-
pgOriginalClauses
protected LNGVector<MiniSatStyleSolver.ProofInformation> pgOriginalClauses
-
pgProof
protected LNGVector<LNGIntVector> pgProof
-
backboneAssumptions
protected LNGIntVector backboneAssumptions
-
computingBackbone
protected boolean computingBackbone
-
selectionOrder
protected LNGIntVector selectionOrder
-
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 numbersign-trueif the literal is negative,falseotherwise- 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)
Returnstrueif a given literal is negated,falseotherwise.- Parameters:
lit- the literal- Returns:
trueif 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 incrementx- 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 variabley- the second variable- Returns:
trueif 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 nameid- 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,trueif negative,falseif positivedvar-trueif this variable can be used as a decision variable,falseif 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 literalproposition- a proposition (if required for proof tracing)- Returns:
trueif the clause was added successfully,falseotherwise
-
addClause
public abstract boolean addClause(LNGIntVector ps, Proposition proposition)
Adds a clause to the solver.- Parameters:
ps- the literals of the clauseproposition- a proposition (if required for proof tracing)- Returns:
trueif the clause was added successfully,falseotherwise
-
solve
public abstract Tristate solve(SATHandler handler)
Solves the formula currently stored in the solver. ReturnsTristate.TRUEif the formula is satisfiable (SAT),Tristate.FALSEif the formula is unsatisfiable (UNSAT), orTristate.UNDEFif the computation was canceled by aSATHandler. Ifnullis passed as handler, the solver will run until the satisfiability is decided.- Parameters:
handler- a sat handler- Returns:
Tristate.TRUEif the formula is satisfiable,Tristate.FALSEif the formula is not satisfiable, orTristate.UNDEFif 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. ReturnsTristate.TRUEif the formula and the assumptions are satisfiable (SAT),Tristate.FALSEif the formula and the assumptions are not satisfiable together (UNSAT), orTristate.UNDEFif the computation was canceled by aSATHandler. Ifnullis passed as handler, the solver will run until the satisfiability is decided.- Parameters:
handler- a sat handlerassumptions- the assumptions as a given vector of literals- Returns:
Tristate.TRUEif the formula and the assumptions are satisfiable,Tristate.FALSEif they are not satisfiable, orTristate.UNDEFif 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()
Returnsfalseif this solver is known to be in a conflicting state, otherwisetrue.- Returns:
falseif this solver is known to be in a conflicting state, otherwisetrue
-
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/loadingIllegalStateException- 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/loadingIllegalStateException- 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 indexinc- the increment value
-
rebuildOrderHeap
protected void rebuildOrderHeap()
Rebuilds the heap of decision variables.
-
locked
protected boolean locked(MSClause c)
Returnstrueif the given clause is locked and therefore cannot be removed,falseotherwise.- Parameters:
c- the clause- Returns:
trueif 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 literalreason- the reason clause of the assignment (conflict resolution) ornullif 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
nullif there was none
-
litRedundant
protected abstract boolean litRedundant(int p, int abstractLevels)Returnstrueif a given literal is redundant in the current conflict analysis,falseotherwise.- Parameters:
p- the literalabstractLevels- an abstraction of levels- Returns:
trueif 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 literaloutConflict- 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)
Returnstrueif a given clause is satisfied under the current assignment,falseotherwise.- Parameters:
c- the clause- Returns:
trueif 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:
trueif simplification was successful and no conflict was found,falseif a conflict was found during the simplification
-
decayActivities
protected void decayActivities()
-
pgOriginalClauses
public LNGVector<MiniSatStyleSolver.ProofInformation> pgOriginalClauses()
Returns the original clauses for proof generation.- Returns:
- the original clauses for proof generation
-
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 testtype- 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 testtype- backbone typehandler- the handler- Returns:
- the backbone projected to the relevant variables or
nullif 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 testtype- the type of the backbonehandler- 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 testtype- 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 testhandler- the handler- Returns:
trueif satisfiable, otherwisefalse
-
buildBackbone
protected Backbone buildBackbone(Collection<Variable> variables, BackboneType type)
Builds the backbone object from the computed backbone literals.- Parameters:
variables- relevant variablestype- 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:
trueif the variable is a unit propagated literal on level 0, otherwisefalse
-
isUnit
protected boolean isUnit(int lit, MSClause clause)Tests the given literal whether it is unit in the given clause.- Parameters:
lit- literal to testclause- clause containing the literal- Returns:
trueif the literal is unit,falseotherwise
-
isRotatable
protected boolean isRotatable(int lit)
Tests the given literal whether it is rotatable in the current model.- Parameters:
lit- literal to test- Returns:
trueif the literal is rotatable, otherwisefalse
-
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.
-
-