Package org.jacop.satwrapper
Class SatWrapper
java.lang.Object
org.jacop.constraints.DecomposedConstraint<Constraint>
org.jacop.constraints.Constraint
org.jacop.satwrapper.SatWrapper
- All Implemented Interfaces:
SatisfiedPresent,Stateful,SolverComponent,ConflictListener,ExplanationListener,SolutionListener,StartStopListener
public final class SatWrapper
extends Constraint
implements ConflictListener, ExplanationListener, StartStopListener, SolutionListener, Stateful, SatisfiedPresent
wrapper to communicate between SAT solver and CP solver.
It listens for SAT conflicts, so that it can force the CP solver to
backtrack until the conflict is resolved in SAT.
It listens to propagations, to know which literals are asserted in SAT, to
report those assertions on CP variables domains.
- Version:
- 4.10
-
Field Summary
FieldsModifier and TypeFieldDescriptionprivate MapClauseInteger[]private int(package private) booleanprivate booleanintprivate final ArrayDeque<int[]> private booleanInteger[]private IntQueueprivate TrailintFields inherited from class org.jacop.constraints.Constraint
atomicExecution, consistencyPruningEvents, constraintScope, earlyTerminationOK, increaseWeight, numberId, scope, traceFields inherited from class org.jacop.constraints.DecomposedConstraint
queueIndex -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionfinal voidaddModelClause(int[] clause) final voidaddModelClause(Collection<Integer> clause) add model (globally valid) clause to solver, in a delayed fashionprivate voidadds one level for SAT side, and remembers the association between CP and SAT levelsfinal voidaddSolverComponent(SolverComponent module) to add some module to the solverfinal voidaddWrapperComponent(WrapperComponent module) add a componentIt returns the variables in a scope of the constraint.final intboolVarToCpValue(int literal) transform a literal 'x=v' into a value 'v' for some CP variablefinal IntVarboolVarToCpVar(int literal) get the IntVar back from a literalfinal SatCPBridgeboolVarToDomain(int literal) returns the CpVarDomain associated with this literalvoidconsistency(Store store) The point where all operations are effectively done in the SAT solver, until no operation remains or a conflict occursfinal intcpVarToBoolVar(IntVar variable, int value, boolean isEquality) given a CP variable and a value, retrieve the associated boolean literal for either 'variable = value' or either 'variable <= value'final voidforget()asks the solver to forget useless clauses, to free memoryintIt retrieves the pruning event which causes reevaluation of the constraint.intintasks the solver for which literal is the most active.id()It gives the id string of a constraint.final voidimpose(Constraint constraint) add the constraint to the wrapper (ie, constraint.imposeToSat(this))final voidIt imposes the constraint in a given store.voidIt increases the weight of the variables in the constraint scope.voidinitialize(Core core) initializes the component with the given solver.final booleanisEqualityBoolVar(int literal) checks if the boolean variable represents an assertion 'x=v' or 'x<=v'final booleanisVarLiteral(int literal) checks if this literal corresponds to some CP variablebooleanlog method, similar to printf.voidonConflict(MapClause clause, int level) wrapper listens for conflicts.voidwrapper listens for explanations, to know how deep to backtrackvoidonSolution(boolean satisfiable) a handler called when a solution is found.voidonStart()called when the solver starts search.voidonStop()called when the solver stop search, for any reasonprivate voidassert the next literal from toAssertLiteralsvoidqueueVariable(int level, Var var) This is a function called to indicate which variable in a scope of constraint has changed.voidvoidregisters the variable so that we can use it in SAT solvervoidIt removes the constraint by removing this constraint from all variables.voidremoveLevel(int cpLevel) when the CP solver decides to remove a level, the wrapper must force the SAT solver to backtrack accordingly, to keep mappings between the two search trees consistent.booleanIt checks if the constraint is satisfied.private voidsetBoolVariable(int variable, boolean value) called when a boolean variable is set to some boolean valueshowClauseMeaning(Iterable<Integer> literals) showLiteralMeaning(int literal) (for debug) show what a literal meansvoidtoCNF(BufferedWriter output) toString()It produces a string representation of a constraint state.Methods inherited from class org.jacop.constraints.Constraint
afc, cleanAfterFailure, decompose, getGuideConstraint, getGuideValue, getGuideVariable, grounded, grounded, impose, imposeDecomposition, intArrayToString, long2int, numberArgs, requiresMonotonicity, setConsistencyPruningEvent, setConstraintScope, setScope, setScope, setScope, setScope, setScope, setWatchedVariableGrounded, supplyGuideFeedback, updateAFC, watchedVariableGroundedMethods inherited from class org.jacop.constraints.DecomposedConstraint
auxiliaryVariables, checkInput, checkInput, checkInputForDuplication, checkInputForDuplicationSkipSingletons, checkInputForNullness, checkInputForNullness, checkInputForNullness, derivative, getDubletonsSkipSingletons, imposeDecompositionMethods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, waitMethods inherited from interface org.jacop.api.Stateful
isStateful
-
Field Details
-
empty
boolean empty -
core
-
activity
-
assertionModule
-
boolVarToDomains
-
satChangesListener
-
store
-
pool
-
domainDatabase
-
domainTranslator
-
registeredVars
-
levelToBackjumpTo
public int levelToBackjumpTo -
satToCpLevels
-
cpToSatLevels
-
verbosity
public int verbosity -
trail
-
currentSatLevel
private int currentSatLevel -
modelClausesToAdd
-
toAssertLiterals
-
clauseToLearn
-
mustBacktrack
private boolean mustBacktrack -
hasSolution
private boolean hasSolution
-
-
Constructor Details
-
SatWrapper
public SatWrapper()creates everything in the right order
-
-
Method Details
-
register
-
register
registers the variable so that we can use it in SAT solver- Parameters:
variable- the CP IntVar variabletranslate- indicate whether to use == or <=
-
consistency
The point where all operations are effectively done in the SAT solver, until no operation remains or a conflict occurs- Specified by:
consistencyin classConstraint- Parameters:
store- constraint store within which the constraint consistency is being checked.
-
processOneLiteral
private void processOneLiteral()assert the next literal from toAssertLiterals -
addSatLevel
private void addSatLevel()adds one level for SAT side, and remembers the association between CP and SAT levels -
onConflict
wrapper listens for conflicts.- Specified by:
onConflictin interfaceConflictListener- Parameters:
clause- the conflict (unsatisfiable) clauselevel- the level at which the conflict occurred
-
onExplain
wrapper listens for explanations, to know how deep to backtrack- Specified by:
onExplainin interfaceExplanationListener- Parameters:
explanation- the explanation clause
-
onSolution
public void onSolution(boolean satisfiable) Description copied from interface:SolutionListenera handler called when a solution is found.- Specified by:
onSolutionin interfaceSolutionListener- Parameters:
satisfiable- true when the solution is Satisfiable, false if it is Unsatisfiable.
-
removeLevel
public void removeLevel(int cpLevel) when the CP solver decides to remove a level, the wrapper must force the SAT solver to backtrack accordingly, to keep mappings between the two search trees consistent. This is also the place where the wrapper can decide that a conflict in the SAT solver has been solved.- Specified by:
removeLevelin interfaceStateful- Parameters:
cpLevel- the level which is being removed.
-
queueVariable
Description copied from class:ConstraintThis is a function called to indicate which variable in a scope of constraint has changed. It also indicates a store level at which the change has occurred.- Overrides:
queueVariablein classConstraint- Parameters:
level- the level of the store at which the change has occurred.var- variable which has changed.
-
setBoolVariable
private void setBoolVariable(int variable, boolean value) called when a boolean variable is set to some boolean value- Parameters:
variable- the boolean variablevalue- the value (true or false) of this variable
-
getConsistencyPruningEvent
Description copied from class:ConstraintIt retrieves the pruning event which causes reevaluation of the constraint.- Overrides:
getConsistencyPruningEventin classConstraint- Parameters:
var- variable for which pruning event is retrieved- Returns:
- it returns the int code of the pruning event (GROUND, BOUND, ANY, NONE)
-
getDefaultConsistencyPruningEvent
public int getDefaultConsistencyPruningEvent()- Specified by:
getDefaultConsistencyPruningEventin classConstraint
-
getMostActiveLiteral
public int getMostActiveLiteral()asks the solver for which literal is the most active. It will return a literal, which can be transformed into a variable and a value from the variable domain. Useful when the CP solver does not know which variable to set to continue research- Returns:
- a literal corresponding to some possible (variable,value)
-
showLiteralMeaning
(for debug) show what a literal means- Parameters:
literal- literal for showing its meaning- Returns:
- literal meaning
-
showClauseMeaning
-
id
Description copied from class:ConstraintIt gives the id string of a constraint.- Overrides:
idin classConstraint- Returns:
- string id of the constraint.
-
removeConstraint
public void removeConstraint()Description copied from class:ConstraintIt removes the constraint by removing this constraint from all variables.- Overrides:
removeConstraintin classConstraint
-
satisfied
public boolean satisfied()Description copied from interface:SatisfiedPresentIt checks if the constraint is satisfied. It can return false even if constraint is satisfied but not all variables in its scope are grounded. It needs to return true if all variables in its scope are grounded and constraint is satisfied.Implementations of this interface for constraints that are not PrimitiveConstraint may require constraint imposition and consistency check as a requirement to work correctly.
- Specified by:
satisfiedin interfaceSatisfiedPresent- Returns:
- true if constraint is possible to verify that it is satisfied.
-
toString
Description copied from class:ConstraintIt produces a string representation of a constraint state.- Overrides:
toStringin classConstraint
-
increaseWeight
public void increaseWeight()Description copied from class:ConstraintIt increases the weight of the variables in the constraint scope.- Overrides:
increaseWeightin classConstraint
-
arguments
Description copied from class:ConstraintIt returns the variables in a scope of the constraint.- Overrides:
argumentsin classConstraint- Returns:
- variables in a scope of the constraint.
-
addSolverComponent
to add some module to the solver- Parameters:
module- the module to add
-
addWrapperComponent
add a component- Parameters:
module- the component
-
forget
public final void forget()asks the solver to forget useless clauses, to free memory -
addModelClause
add model (globally valid) clause to solver, in a delayed fashion- Parameters:
clause- the clause to add
-
addModelClause
public final void addModelClause(int[] clause) -
impose
add the constraint to the wrapper (ie, constraint.imposeToSat(this))- Parameters:
constraint- the constraint to add
-
impose
Description copied from class:ConstraintIt imposes the constraint in a given store.- Overrides:
imposein classConstraint- Parameters:
store- the constraint store to which the constraint is imposed to.
-
cpVarToBoolVar
given a CP variable and a value, retrieve the associated boolean literal for either 'variable = value' or either 'variable <= value'- Parameters:
variable- the CP variablevalue- a value in the range of this variableisEquality- a boolean, true if we want the literal that stands for 'x=d', false for 'x<=d'- Returns:
- the corresponding literal, or 0 if it is out of bounds
-
boolVarToDomain
returns the CpVarDomain associated with this literal- Parameters:
literal- the boolean literal- Returns:
- a range
-
boolVarToCpVar
get the IntVar back from a literal- Parameters:
literal- the literal- Returns:
- IntVar represented by the literal
-
boolVarToCpValue
public final int boolVarToCpValue(int literal) transform a literal 'x=v' into a value 'v' for some CP variable- Parameters:
literal- literal to be transformed to value it represents- Returns:
- the value represented by this literal
-
isEqualityBoolVar
public final boolean isEqualityBoolVar(int literal) checks if the boolean variable represents an assertion 'x=v' or 'x<=v'- Parameters:
literal- the boolean literal- Returns:
- true if the literal represents a proposition 'x=v', false if it represents 'x<=v'
-
isVarLiteral
public final boolean isVarLiteral(int literal) checks if this literal corresponds to some CP variable- Parameters:
literal- the literal- Returns:
- true if this literal stands for some 'x=v' or 'x<=v' proposition
-
log
log method, similar to printf. Example: wrapper.log(this, "%s is %d", "foo", 42);- Parameters:
o- the object that logs something (usethis)format- the format string (the message, if no formatting)args- the arguments to fill in the format- Returns:
- always true
-
onStart
public void onStart()Description copied from interface:StartStopListenercalled when the solver starts search. It will be called only once.- Specified by:
onStartin interfaceStartStopListener
-
onStop
public void onStop()Description copied from interface:StartStopListenercalled when the solver stop search, for any reason- Specified by:
onStopin interfaceStartStopListener
-
initialize
Description copied from interface:SolverComponentinitializes the component with the given solver. May be called only once. This method must register the component to the solver for the run.- Specified by:
initializein interfaceSolverComponent- Parameters:
core- core component to initialize
-
toCNF
- Throws:
IOException
-