- java.lang.Object
-
- org.chocosolver.solver.Model
-
- All Implemented Interfaces:
IConstraintFactory
,IDecompositionFactory
,IIntConstraintFactory
,IRealConstraintFactory
,IReificationFactory
,ISatFactory
,ISetConstraintFactory
,IModel
,ISelf<Model>
,IVariableFactory
,IViewFactory
public class Model extends Object implements IModel
TheModel
is the header component of Constraint Programming. It embeds the list ofVariable
(and theirDomain
), theConstraint
's network, and aIPropagationEngine
to pilot the propagation.
Model
includes aAbstractSearchLoop
to 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 String
CLAUSES_HOOK_NAME
static String
CLAUSESBUILDER_HOOK_NAME
static String
IBEX_HOOK_NAME
static boolean
MAXIMIZE
static boolean
MINIMIZE
static String
MINISAT_HOOK_NAME
static String
NOGOODS_HOOK_NAME
static String
TASK_SET_HOOK_NAME
Name 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 void
addHook(String hookName, Object hookObject)
Adds thehookObject
to store in this model, associated with the namehookName
.void
associates(Variable variable)
Link a variable tothis
.void
clearObjective()
Removes any objective and set problem to a satisfaction problemConstraint
falseConstraint()
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.ClauseBuilder
getClauseBuilder()
ClauseConstraint
getClauseConstraint()
Return a constraint embedding a signed-clauses store.long
getCreationTime()
Get the creation time (in milliseconds) of the model (to estimate modeling duration)Constraint[]
getCstrs()
Returns the array ofConstraint
objects posted in thisModel
.IEnvironment
getEnvironment()
Return the backtracking environment ofthis
model.Object
getHook(String hookName)
Returns the object associated with the namedhookName
protected Map<String,Object>
getHooks()
Returns the map containing declared hooks.IbexHandler
getIbexHandler()
Return a constraint embedding an instance of Ibex (continuous solver).SatConstraint
getMinisat()
Returns the unique constraint embedding a minisat model.String
getName()
Return the name ofthis
model.int
getNbBoolVar()
Returns the number ofBoolVar
of the model involved inthis
, It also counts FIXED variables and VIEWS, if any.int
getNbCstrs()
Return the number of constraints posted inthis
.int
getNbIntVar(boolean includeBoolVar)
int
getNbRealVar()
Returns the number ofRealVar
of the model involved inthis
, It also counts FIXED variables and VIEWS, if any.int
getNbSetVar()
Returns the number ofSetVar
of the model involved inthis
, It also counts FIXED variables and VIEWS, if any.int
getNbVars()
Returns the number of variables involved inthis
.NogoodConstraint
getNogoodStore()
Return a constraint embedding a nogood store (based on a sat model).Variable
getObjective()
Return the (possibly null) objective variabledouble
getPrecision()
In case of real variable(s) to optimize, a precision is required.ResolutionPolicy
getResolutionPolicy()
Get the resolution policy of the modellong
getSeed()
Gets the seed used random number generator.Settings
getSettings()
Return the current settings for the solverSolver
getSolver()
Returns the unique and internal propagation and search object to solve this model.Variable
getVar(int i)
Returns the ith variable within the array of variables defined inthis
.Variable[]
getVars()
Returns the array ofVariable
objects declared in thisModel
.int
nextId()
Get a free single-use id to identify a new variable.int
nextNameId()
Get a free single-use name id to identify a variable created internally.void
post(Constraint... cs)
Posts constraintscs
permanently in the constraints network ofthis
: - add them to the data structure, - set the fixed idx, - checks for restrictionsvoid
postTemp(Constraint... cs)
Posts constraintscs
temporary, that is, they will be unposted upon backtrack.Model
ref()
void
removeAllHooks()
Empties the hooks attached to this model.void
removeHook(String hookName)
Removes the hook namedhookName
void
removeMinisat()
Unpost minisat constraint from model, if any.void
removeNogoodStore()
Unpost nogood store constraint from model, if any.BoolVar[]
retrieveBoolVars()
Iterate over the variable ofthis
and build an array that contains theBoolVar
only.IntVar[]
retrieveIntVars(boolean includeBoolVar)
Iterate over the variable ofthis
and build an array that contains all theIntVar
of the model.RealVar[]
retrieveRealVars()
Iterate over the variable ofthis
and build an array that contains theRealVar
only.SetVar[]
retrieveSetVars()
Iterate over the variable ofthis
and build an array that contains theSetVar
only.void
setName(String name)
Changes the name of this model to be equal to the argumentname
.void
setObjective(boolean maximize, Variable objective)
void
setPrecision(double p)
In case of real variable to optimize, a precision is required.void
setSeed(long seed)
Sets the seed used for random number generator using a singlelong
seed.String
toString()
Return a string describing the CSP defined inthis
model.Constraint
trueConstraint()
The basic "true" constraint, which is always satisfiedvoid
unassociates(Variable variable)
Unlink the variable fromthis
.void
unpost(Constraint... constraints)
Remove permanently the constraintc
from 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.IDecompositionFactory
binPackingDec, cumulativeTimeDec, element, element, 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, member, min, min, min, mod, mod, mod, multiCostRegular, multiCostRegular, not, notAllEqual, notMember, 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
element, eq, ibex, realIbexGenericConstraint, scalar
-
Methods inherited from interface org.chocosolver.solver.constraints.IReificationFactory
ifOnlyIf, ifThen, ifThen, ifThenElse, ifThenElse, reification, reifyXeqC, reifyXeqY, reifyXeqYC, reifyXgeY, reifyXgtC, reifyXgtY, reifyXgtYC, reifyXinS, reifyXleY, reifyXltC, reifyXltY, reifyXltYC, reifyXneC, reifyXneY, reifyXneYC, reifyXnotinS
-
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, taskVar, taskVarArray, taskVarMatrix, toBoolVar
-
Methods inherited from interface org.chocosolver.solver.variables.IViewFactory
boolNotView, intAbsView, intAffineView, intEqView, intGeView, intLeView, intMinusView, intNeView, 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
-
CLAUSES_HOOK_NAME
public static final String CLAUSES_HOOK_NAME
- See Also:
- Constant Field Values
-
CLAUSESBUILDER_HOOK_NAME
public static final String CLAUSESBUILDER_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 namedname
and 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 namedname
and 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 namedname
and 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 namedname
and 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
Resolver
object.
-
getVars
public Variable[] getVars()
Returns the array ofVariable
objects 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 ofBoolVar
of the model involved inthis
, It also counts FIXED variables and VIEWS, if any.- Returns:
- the number of
BoolVar
of the model involved inthis
-
retrieveBoolVars
public BoolVar[] retrieveBoolVars()
Iterate over the variable ofthis
and build an array that contains theBoolVar
only. It also contains FIXED variables and VIEWS, if any.- Returns:
- array of
BoolVar
inthis
model
-
getNbSetVar
public int getNbSetVar()
Returns the number ofSetVar
of the model involved inthis
, It also counts FIXED variables and VIEWS, if any.- Returns:
- the number of
SetVar
of the model involved inthis
-
retrieveSetVars
public SetVar[] retrieveSetVars()
Iterate over the variable ofthis
and build an array that contains theSetVar
only. It also contains FIXED variables and VIEWS, if any.- Returns:
- array of SetVars in
this
model
-
getNbRealVar
public int getNbRealVar()
Returns the number ofRealVar
of the model involved inthis
, It also counts FIXED variables and VIEWS, if any.- Returns:
- the number of
RealVar
of the model involved inthis
-
retrieveRealVars
public RealVar[] retrieveRealVars()
Iterate over the variable ofthis
and build an array that contains theRealVar
only. It also contains FIXED variables and VIEWS, if any.- Returns:
- array of
RealVar
inthis
model
-
getCstrs
public Constraint[] getCstrs()
Returns the array ofConstraint
objects 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 ofthis
model.- Returns:
- this model's name
-
getEnvironment
public IEnvironment getEnvironment()
Return the backtracking environment ofthis
model.- 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.
-
getClauseConstraint
public ClauseConstraint getClauseConstraint()
Return a constraint embedding a signed-clauses store. A call to this method will create and post the constraint if it does not exist already.- Returns:
- the signed-clauses store constraint
-
getClauseBuilder
public ClauseBuilder getClauseBuilder()
- Returns:
- an instance of
ClauseBuilder
that helps creating clause during resolution.
-
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)
-
setSeed
public void setSeed(long seed)
Sets the seed used for random number generator using a singlelong
seed.- Parameters:
seed
- the initial seed- See Also:
Random.setSeed(long)
-
getSeed
public long getSeed()
Gets the seed used random number generator.- Returns:
- the seed
-
addHook
public void addHook(String hookName, Object hookObject)
Adds thehookObject
to 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 constraintscs
permanently 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 constraintscs
temporary, that is, they will be unposted upon backtrack. The unpost instruction is defined by anIOperation
saved in theIEnvironment
- Parameters:
cs
- a set of constraints to add- Throws:
ContradictionException
- if the addition of constraintscs
detects 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 constraintc
from 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 inthis
model.
-
-