Package org.chocosolver.solver
Class Model
- java.lang.Object
-
- org.chocosolver.solver.Model
-
- All Implemented Interfaces:
IDecompositionFactory,IConstraintFactory,IIntConstraintFactory,IRealConstraintFactory,IReificationFactory,ISatFactory,ISetConstraintFactory,IModel,ISelf<Model>,IVariableFactory,IViewFactory
public class Model extends Object implements IModel
TheModelis the header component of Constraint Programming. It embeds the list ofVariable(and theirDomain), theConstraint's network, and aIPropagationEngineto pilot the propagation.
Modelincludes aAbstractSearchLoopto guide the search loop: applying decisions and propagating, running backups and rollbacks and storing solutions.- Since:
- 0.01
- Version:
- 0.01, june 2010
- Author:
- Xavier Lorca, Charles Prud'homme, Jean-Guillaume Fages, Arnaud Malapert
- See Also:
Variable,Constraint
-
-
Field Summary
Fields Modifier and Type Field Description static StringIBEX_HOOK_NAMEstatic booleanMAXIMIZEstatic booleanMINIMIZEstatic StringMINISAT_HOOK_NAMEstatic StringNOGOODS_HOOK_NAMEstatic StringTASK_SET_HOOK_NAMEName of internal hook dedicated to store declaredTask.-
Fields inherited from interface org.chocosolver.solver.variables.IVariableFactory
CSTE_NAME
-
-
Constructor Summary
Constructors Constructor Description Model()Creates a Model object to formulate a decision problem by declaring variables and posting constraints.Model(String name)Creates a Model object to formulate a decision problem by declaring variables and posting constraints.Model(String name, Settings settings)Creates a Model object to formulate a decision problem by declaring variables and posting constraints.Model(IEnvironment environment, String name)Creates a Model object to formulate a decision problem by declaring variables and posting constraints.Model(IEnvironment environment, String name, Settings settings)Creates a Model object to formulate a decision problem by declaring variables and posting constraints.Model(Settings settings)Creates a Model object to formulate a decision problem by declaring variables and posting constraints.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description voidaddHook(String hookName, Object hookObject)Adds thehookObjectto store in this model, associated with the namehookName.voidassociates(Variable variable)Link a variable tothis.voidclearObjective()Removes any objective and set problem to a satisfaction problemConstraintfalseConstraint()The basic "false" constraint, which is always violatedgnu.trove.map.hash.TIntObjectHashMap<IntVar>getCachedConstants()Get the map of constant IntVar the have default names to avoid creating multiple identical constants.longgetCreationTime()Get the creation time (in milliseconds) of the model (to estimate modeling duration)Constraint[]getCstrs()Returns the array ofConstraintobjects posted in thisModel.IEnvironmentgetEnvironment()Return the backtracking environment ofthismodel.ObjectgetHook(String hookName)Returns the object associated with the namedhookNameprotected Map<String,Object>getHooks()Returns the map containing declared hooks.IbexHandlergetIbexHandler()Return a constraint embedding an instance of Ibex (continuous solver).SatConstraintgetMinisat()Returns the unique constraint embedding a minisat model.StringgetName()Return the name ofthismodel.intgetNbBoolVar()Returns the number ofBoolVarof the model involved inthis, It also counts FIXED variables and VIEWS, if any.intgetNbCstrs()Return the number of constraints posted inthis.intgetNbIntVar(boolean includeBoolVar)intgetNbRealVar()Returns the number ofRealVarof the model involved inthis, It also counts FIXED variables and VIEWS, if any.intgetNbSetVar()Returns the number ofSetVarof the model involved inthis, It also counts FIXED variables and VIEWS, if any.intgetNbVars()Returns the number of variables involved inthis.NogoodConstraintgetNogoodStore()Return a constraint embedding a nogood store (based on a sat model).VariablegetObjective()Return the (possibly null) objective variabledoublegetPrecision()In case of real variable(s) to optimize, a precision is required.ResolutionPolicygetResolutionPolicy()Get the resolution policy of the modelSettingsgetSettings()Return the current settings for the solverSolvergetSolver()Returns the unique and internal propagation and search object to solve this model.VariablegetVar(int i)Returns the ith variable within the array of variables defined inthis.Variable[]getVars()Returns the array ofVariableobjects declared in thisModel.intnextId()Get a free single-use id to identify a new variable.intnextNameId()Get a free single-use name id to identify a variable created internally.voidpost(Constraint... cs)Posts constraintscspermanently in the constraints network ofthis: - add them to the data structure, - set the fixed idx, - checks for restrictionsvoidpostTemp(Constraint... cs)Posts constraintscstemporary, that is, they will be unposted upon backtrack.Modelref()voidremoveAllHooks()Empties the hooks attached to this model.voidremoveHook(String hookName)Removes the hook namedhookNamevoidremoveMinisat()Unpost minisat constraint from model, if any.voidremoveNogoodStore()Unpost nogood store constraint from model, if any.BoolVar[]retrieveBoolVars()Iterate over the variable ofthisand build an array that contains theBoolVaronly.IntVar[]retrieveIntVars(boolean includeBoolVar)Iterate over the variable ofthisand build an array that contains all theIntVarof the model.RealVar[]retrieveRealVars()Iterate over the variable ofthisand build an array that contains theRealVaronly.SetVar[]retrieveSetVars()Iterate over the variable ofthisand build an array that contains theSetVaronly.voidsetName(String name)Changes the name of this model to be equal to the argumentname.voidsetObjective(boolean maximize, Variable objective)voidsetPrecision(double p)In case of real variable to optimize, a precision is required.StringtoString()Return a string describing the CSP defined inthismodel.ConstrainttrueConstraint()The basic "true" constraint, which is always satisfiedvoidunassociates(Variable variable)Unlink the variable fromthis.voidunpost(Constraint... constraints)Remove permanently the constraintcfrom the constraint network.-
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
-
Methods inherited from interface org.chocosolver.solver.constraints.binary.IDecompositionFactory
cumulativeTimeDecomp, regularDec
-
Methods inherited from interface org.chocosolver.solver.constraints.IIntConstraintFactory
absolute, allDifferent, allDifferent, allDifferentExcept0, allDifferentUnderCondition, allEqual, among, and, and, arithm, arithm, arithm, arithm, atLeastNValues, atMostNValues, binPacking, bitsIntChanneling, boolsIntChanneling, circuit, circuit, circuit, clausesIntChanneling, costRegular, count, count, cumulative, cumulative, cumulative, cumulative, cumulative, diffN, distance, distance, div, element, element, element, getDomainUnion, globalCardinality, intValuePrecedeChain, intValuePrecedeChain, inverseChanneling, inverseChanneling, keySort, knapsack, lexChainLess, lexChainLessEq, lexLess, lexLessEq, max, max, max, mddc, member, member, min, min, min, mod, multiCostRegular, multiCostRegular, not, notAllEqual, notMember, notMember, nValues, or, or, path, path, regular, scalar, scalar, scalar, scalar, sort, square, subCircuit, subPath, sum, sum, sum, sum, sum, sum, sum, table, table, table, table, times, times, times, tree, tree
-
Methods inherited from interface org.chocosolver.solver.constraints.IRealConstraintFactory
ibex, realIbexGenericConstraint, scalar
-
Methods inherited from interface org.chocosolver.solver.constraints.IReificationFactory
ifOnlyIf, ifThen, ifThen, ifThenElse, ifThenElse, reification, reifyXeqC, reifyXeqY, reifyXgtC, reifyXinS, reifyXleY, reifyXltC, reifyXltY, reifyXltYC, reifyXneC, reifyXneY
-
Methods inherited from interface org.chocosolver.solver.constraints.ISatFactory
addClauseFalse, addClauses, addClauses, addClausesAtMostNMinusOne, addClausesAtMostOne, addClausesBoolAndArrayEqualFalse, addClausesBoolAndArrayEqVar, addClausesBoolAndEqVar, addClausesBoolEq, addClausesBoolIsEqVar, addClausesBoolIsLeVar, addClausesBoolIsLtVar, addClausesBoolIsNeqVar, addClausesBoolLe, addClausesBoolLt, addClausesBoolNot, addClausesBoolOrArrayEqualTrue, addClausesBoolOrArrayEqVar, addClausesBoolOrEqVar, addClausesBoolXorEqVar, addClausesMaxBoolArrayLessEqVar, addClausesSumBoolArrayGreaterEqVar, addClausesSumBoolArrayLessEqVar, addClauseTrue, addConstructiveDisjunction
-
Methods inherited from interface org.chocosolver.solver.constraints.ISetConstraintFactory
allDifferent, allDisjoint, allEqual, disjoint, element, element, intersection, intersection, inverseSet, max, max, member, member, member, min, min, nbEmpty, nbEmpty, notEmpty, notMember, notMember, offSet, partition, setBoolsChanneling, setBoolsChanneling, setsIntsChanneling, setsIntsChanneling, subsetEq, sum, sumElements, sumElements, symmetric, symmetric, union, union
-
Methods inherited from interface org.chocosolver.solver.variables.IVariableFactory
boolVar, boolVar, boolVar, boolVar, boolVarArray, boolVarArray, boolVarMatrix, boolVarMatrix, checkIntDomainRange, checkRealDomainRange, generateName, generateName, intVar, intVar, intVar, intVar, intVar, intVar, intVar, intVar, intVarArray, intVarArray, intVarArray, intVarArray, intVarArray, intVarArray, intVarMatrix, intVarMatrix, intVarMatrix, intVarMatrix, intVarMatrix, intVarMatrix, realVar, realVar, realVar, realVar, realVar, realVarArray, realVarArray, realVarMatrix, realVarMatrix, setVar, setVar, setVar, setVar, setVarArray, setVarArray, setVarMatrix, setVarMatrix, taskVar, taskVar, taskVar, taskVarArray, taskVarMatrix, toBoolVar
-
Methods inherited from interface org.chocosolver.solver.variables.IViewFactory
boolNotView, intAbsView, intAffineView, intMinusView, intOffsetView, intScaleView, realIntView, realIntViewArray, realIntViewMatrix
-
-
-
-
Field Detail
-
MAXIMIZE
public static boolean MAXIMIZE
-
MINIMIZE
public static boolean MINIMIZE
-
TASK_SET_HOOK_NAME
public static final String TASK_SET_HOOK_NAME
Name of internal hook dedicated to store declaredTask.- See Also:
- Constant Field Values
-
MINISAT_HOOK_NAME
public static final String MINISAT_HOOK_NAME
- See Also:
- Constant Field Values
-
NOGOODS_HOOK_NAME
public static final String NOGOODS_HOOK_NAME
- See Also:
- Constant Field Values
-
IBEX_HOOK_NAME
public static final String IBEX_HOOK_NAME
- See Also:
- Constant Field Values
-
-
Constructor Detail
-
Model
public Model(IEnvironment environment, String name, Settings settings)
Creates a Model object to formulate a decision problem by declaring variables and posting constraints. The model is namednameand it uses a specific backtrackingenvironment.- Parameters:
environment- a backtracking environment to allow searchname- The name of the model (for logging purpose)settings- settings to use
-
Model
public Model(IEnvironment environment, String name)
Creates a Model object to formulate a decision problem by declaring variables and posting constraints. The model is namednameand it uses a specific backtrackingenvironment.- Parameters:
environment- a backtracking environment to allow searchname- The name of the model (for logging purpose)
-
Model
public Model(String name, Settings settings)
Creates a Model object to formulate a decision problem by declaring variables and posting constraints. The model is namednameand it uses a specific backtrackingenvironment.- Parameters:
name- The name of the model (for logging purpose)settings- settings to use
-
Model
public Model(String name)
Creates a Model object to formulate a decision problem by declaring variables and posting constraints. The model is namednameand uses the default (trailing) backtracking environment.- Parameters:
name- The name of the model (for logging purpose)- See Also:
Model(org.chocosolver.memory.IEnvironment, String, Settings)
-
Model
public Model(Settings settings)
Creates a Model object to formulate a decision problem by declaring variables and posting constraints. The model is uses the default (trailing) backtracking environment.- Parameters:
settings- settings to use- See Also:
Model(org.chocosolver.memory.IEnvironment, String, Settings)
-
Model
public Model()
Creates a Model object to formulate a decision problem by declaring variables and posting constraints. The model uses the default (trailing) backtracking environment.- See Also:
Model(String)
-
-
Method Detail
-
getCreationTime
public long getCreationTime()
Get the creation time (in milliseconds) of the model (to estimate modeling duration)- Returns:
- the time (in ms) of the creation of the model
-
getResolutionPolicy
public ResolutionPolicy getResolutionPolicy()
Get the resolution policy of the model- Returns:
- the resolution policy of the model
- See Also:
ResolutionPolicy
-
getCachedConstants
public gnu.trove.map.hash.TIntObjectHashMap<IntVar> getCachedConstants()
Get the map of constant IntVar the have default names to avoid creating multiple identical constants. Should not be called by the user.- Returns:
- the map of constant IntVar having default names.
-
trueConstraint
public Constraint trueConstraint()
The basic "true" constraint, which is always satisfied- Returns:
- a "true" constraint
-
falseConstraint
public Constraint falseConstraint()
The basic "false" constraint, which is always violated- Returns:
- a "false" constraint
-
getSolver
public Solver getSolver()
Returns the unique and internal propagation and search object to solve this model.- Returns:
- the unique and internal
Resolverobject.
-
getVars
public Variable[] getVars()
Returns the array ofVariableobjects declared in thisModel.- Returns:
- array of all variables in this model
-
getNbVars
public int getNbVars()
Returns the number of variables involved inthis.- Returns:
- number of variables in this model
-
getVar
public Variable getVar(int i)
Returns the ith variable within the array of variables defined inthis.- Parameters:
i- index of the variable to return.- Returns:
- the ith variable of this model
-
getNbIntVar
public int getNbIntVar(boolean includeBoolVar)
-
retrieveIntVars
public IntVar[] retrieveIntVars(boolean includeBoolVar)
-
getNbBoolVar
public int getNbBoolVar()
Returns the number ofBoolVarof the model involved inthis, It also counts FIXED variables and VIEWS, if any.- Returns:
- the number of
BoolVarof the model involved inthis
-
retrieveBoolVars
public BoolVar[] retrieveBoolVars()
Iterate over the variable ofthisand build an array that contains theBoolVaronly. It also contains FIXED variables and VIEWS, if any.- Returns:
- array of
BoolVarinthismodel
-
getNbSetVar
public int getNbSetVar()
Returns the number ofSetVarof the model involved inthis, It also counts FIXED variables and VIEWS, if any.- Returns:
- the number of
SetVarof the model involved inthis
-
retrieveSetVars
public SetVar[] retrieveSetVars()
Iterate over the variable ofthisand build an array that contains theSetVaronly. It also contains FIXED variables and VIEWS, if any.- Returns:
- array of SetVars in
thismodel
-
getNbRealVar
public int getNbRealVar()
Returns the number ofRealVarof the model involved inthis, It also counts FIXED variables and VIEWS, if any.- Returns:
- the number of
RealVarof the model involved inthis
-
retrieveRealVars
public RealVar[] retrieveRealVars()
Iterate over the variable ofthisand build an array that contains theRealVaronly. It also contains FIXED variables and VIEWS, if any.- Returns:
- array of
RealVarinthismodel
-
getCstrs
public Constraint[] getCstrs()
Returns the array ofConstraintobjects posted in thisModel.- Returns:
- array of posted constraints
-
getNbCstrs
public int getNbCstrs()
Return the number of constraints posted inthis.- Returns:
- number of posted constraints.
-
getName
public String getName()
Return the name ofthismodel.- Returns:
- this model's name
-
getEnvironment
public IEnvironment getEnvironment()
Return the backtracking environment ofthismodel.- Returns:
- the backtracking environment of this model
-
getObjective
public Variable getObjective()
Return the (possibly null) objective variable- Returns:
- a variable (null for satisfaction problems)
-
getPrecision
public double getPrecision()
In case of real variable(s) to optimize, a precision is required.- Returns:
- the precision used
-
getHook
public Object getHook(String hookName)
Returns the object associated with the namedhookName- Parameters:
hookName- the name of the hook to return- Returns:
- the object associated to the name
hookName
-
getHooks
protected Map<String,Object> getHooks()
Returns the map containing declared hooks. This map is mutable.- Returns:
- the map of hooks.
-
getMinisat
public SatConstraint getMinisat()
Returns the unique constraint embedding a minisat model. A call to this method will create and post the constraint if it does not exist already.- Returns:
- the minisat constraint
-
removeMinisat
public void removeMinisat()
Unpost minisat constraint from model, if any.
-
getNogoodStore
public NogoodConstraint getNogoodStore()
Return a constraint embedding a nogood store (based on a sat model). A call to this method will create and post the constraint if it does not exist already.- Returns:
- the no good constraint
-
removeNogoodStore
public void removeNogoodStore()
Unpost nogood store constraint from model, if any.
-
getIbexHandler
public IbexHandler getIbexHandler()
Return a constraint embedding an instance of Ibex (continuous solver). A call to this method will create and post the constraint if it does not exist already.- Returns:
- the Ibex constraint
-
getSettings
public Settings getSettings()
Return the current settings for the solver- Returns:
- a
Settings
-
setObjective
public void setObjective(boolean maximize, Variable objective)Defines the variable to optimize (maximize or minimize) By default, each solution forces either :- Parameters:
maximize- whether to maximize (true) or minimize (false) the objectiveobjective- variable to optimize- See Also:
IObjectiveManager.setStrictDynamicCut(),IObjectiveManager.setWalkingDynamicCut(),IObjectiveManager.setCutComputer(Function)
-
clearObjective
public void clearObjective()
Removes any objective and set problem to a satisfaction problem
-
setPrecision
public void setPrecision(double p)
In case of real variable to optimize, a precision is required.- Parameters:
p- the precision (default is 0.0001D)
-
addHook
public void addHook(String hookName, Object hookObject)
Adds thehookObjectto store in this model, associated with the namehookName. A hook is a simple map "hookName" <-> hookObject.- Parameters:
hookName- name of the hookhookObject- hook to store
-
removeHook
public void removeHook(String hookName)
Removes the hook namedhookName- Parameters:
hookName- name of the hookObject to remove
-
removeAllHooks
public void removeAllHooks()
Empties the hooks attached to this model.
-
setName
public void setName(String name)
Changes the name of this model to be equal to the argumentname.- Parameters:
name- the new name of this model.
-
associates
public void associates(Variable variable)
Link a variable tothis. This is executed AUTOMATICALLY in variable constructor, so no checked are done on multiple occurrences of the very same variable. Should not be called by the user.- Parameters:
variable- a newly created variable, not already added
-
unassociates
public void unassociates(Variable variable)
Unlink the variable fromthis. Should not be called by the user.- Parameters:
variable- variable to un-associate
-
nextId
public int nextId()
Get a free single-use id to identify a new variable. Should not be called by the user.- Returns:
- a free id to use
-
nextNameId
public int nextNameId()
Get a free single-use name id to identify a variable created internally. Should not be called by the user.- Returns:
- a free id to use
-
post
public void post(Constraint... cs) throws SolverException
Posts constraintscspermanently in the constraints network ofthis: - add them to the data structure, - set the fixed idx, - checks for restrictions- Parameters:
cs- Constraints- Throws:
SolverException- if the constraint is posted twice, posted although reified or reified twice.
-
postTemp
public void postTemp(Constraint... cs) throws ContradictionException
Posts constraintscstemporary, that is, they will be unposted upon backtrack. The unpost instruction is defined by anIOperationsaved in theIEnvironment- Parameters:
cs- a set of constraints to add- Throws:
ContradictionException- if the addition of constraintscsdetects inconsistency.SolverException- if a constraint is posted twice, posted although reified or reified twice.
-
unpost
public void unpost(Constraint... constraints) throws SolverException
Remove permanently the constraintcfrom the constraint network.- Parameters:
constraints- the constraints to remove- Throws:
SolverException- if a constraint is unknown from the model
-
toString
public String toString()
Return a string describing the CSP defined inthismodel.
-
-