Class BoolVarImpl
- java.lang.Object
-
- org.chocosolver.solver.variables.impl.AbstractVariable
-
- org.chocosolver.solver.variables.impl.BoolVarImpl
-
- All Implemented Interfaces:
Cloneable,Comparable<Variable>,Iterable<Integer>,ILogical,ArExpression,ReExpression,ICause,Identity,BoolVar,IntVar,Variable
public class BoolVarImpl extends AbstractVariable implements BoolVar
- Since:
- 18 nov. 2010
- Author:
- Charles Prud'homme
-
-
Nested Class Summary
-
Nested classes/interfaces inherited from interface org.chocosolver.solver.expression.discrete.arithmetic.ArExpression
ArExpression.Operator
-
Nested classes/interfaces inherited from interface org.chocosolver.solver.expression.discrete.relational.ReExpression
ReExpression.Operator
-
-
Field Summary
-
Fields inherited from class org.chocosolver.solver.variables.impl.AbstractVariable
mIdx, model, monitors, MSG_EMPTY, MSG_INST, name, propagators
-
Fields inherited from interface org.chocosolver.solver.expression.discrete.arithmetic.ArExpression
NO_CHILD
-
Fields inherited from interface org.chocosolver.solver.variables.IntVar
MAX_INT_BOUND, MIN_INT_BOUND
-
-
Constructor Summary
Constructors Constructor Description BoolVarImpl(String name, Model model)Create a BoolVar {0,1} or {true, false}
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description void_setNot(BoolVar neg)booleancontains(int aValue)Checks if a valuevbelongs to the domain ofthisvoidcreateDelta()Create a delta, if necessary, in order to observe removed values of a this.protected EvtSchedulercreateScheduler()ESatgetBooleanValue()IEnumDeltagetDelta()Return the delta domain of thisintgetDomainSize()Returns the size of this domain, that is the number of elements in this domain.intgetLB()Retrieves the lower bound of the variableintgetRange()Returns the range of this domain, that is, the difference between the upper bound and the lower bound.DisposableRangeIteratorgetRangeIterator(boolean bottomUp)Retrieves an iterator over ranges (or intervals) ofthis.intgetTypeAndKind()Return a MASK composed of 2 main information: TYPE and KIND.intgetUB()Retrieves the upper bound of the variableintgetValue()Retrieves the current value of the variable if instantiated, otherwier the lower bound.DisposableValueIteratorgetValueIterator(boolean bottomUp)Retrieves an iterator over values ofthis.booleanhasEnumeratedDomain()Indicates wether (or not)thishas an enumerated domain (represented in extension) or not (only bounds)booleanhasNot()booleaninstantiateTo(int value, ICause cause)Instantiates the domain ofthistovalue.booleanisInstantiated()Indicates whetherthisis instantiated (see implemtations to know what instantiation means).booleanisInstantiatedTo(int aValue)Checks wetherthisis instantiated tovalbooleanisLit()Current tree is a literalbooleanisNot()Current tree is rooted with NOT logical operator This is a one way relationship: in case a = not(b) a.isNot() returns true whereas b.isNot() returns false (unless b = not(c)...)Iterator<Integer>iterator()IIntDeltaMonitormonitorDelta(ICause propagator)Allow to monitor removed values ofthis.intnextValue(int v)Returns the first value just after v inthiswhich is in the domain.intnextValueOut(int v)Returns the first value just after v inthiswhich is out of the domain.BoolVarnot()voidnotifyMonitors(IEventType event)Notify monitors of observed variable modificationsintpreviousValue(int v)Returns the previous value just before v inthis.intpreviousValueOut(int v)Returns the first value just before v inthiswhich is out of the domain.booleanremoveAllValuesBut(IntIterableSet values, ICause cause)Removes all values from the domain ofthisexcept those invalues.booleanremoveInterval(int from, int to, ICause cause)Removes values between [from, to] from the domain ofthis.booleanremoveValue(int value, ICause cause)Removesvaluefrom the domain ofthis.booleanremoveValues(IntIterableSet values, ICause cause)Removes the value invaluesfrom the domain ofthis.voidsetNot(boolean isNot)States whether or not this variable is the negation of another.booleansetToFalse(ICause cause)booleansetToTrue(ICause cause)StringtoString()booleanupdateBounds(int lb, int ub, ICause cause)Updates the lower bound and the upper bound of the domain ofthisto, resp.booleanupdateLowerBound(int value, ICause cause)Updates the lower bound of the domain ofthistovalue.booleanupdateUpperBound(int value, ICause cause)Updates the upper bound of the domain ofthistovalue.-
Methods inherited from class org.chocosolver.solver.variables.impl.AbstractVariable
addMonitor, asBoolVar, asIntVar, asRealVar, asSetVar, clearEvents, compareTo, contradiction, getCause, getDindex, getEvtScheduler, getId, getIndexInPropagator, getMask, getModel, getName, getNbProps, getPIndices, getPropagator, getPropagators, getViews, isAConstant, isBool, link, notifyPropagators, notifyViews, removeMonitor, setPIndice, storeEvents, subscribeView, swapOnActivate, swapOnPassivate, unlink
-
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
-
Methods inherited from interface org.chocosolver.solver.expression.discrete.arithmetic.ArExpression
abs, add, add, add, dist, dist, div, div, eq, eq, eq, ge, ge, getExpressionChild, gt, gt, le, le, lt, lt, max, max, max, min, min, min, mod, mod, mul, mul, mul, ne, ne, neg, pow, pow, sqr, sub, sub
-
Methods inherited from interface org.chocosolver.solver.variables.BoolVar
boolVar, extractVar, intVar
-
Methods inherited from interface java.lang.Comparable
compareTo
-
Methods inherited from interface org.chocosolver.solver.variables.IntVar
getNoChild, isBool, isExpressionLeaf, reverseValue, transformEvent, transformValue, why
-
Methods inherited from interface java.lang.Iterable
forEach, spliterator
-
Methods inherited from interface org.chocosolver.solver.expression.discrete.relational.ReExpression
and, beval, decompose, extension, getModel, ieval, iff, ift, ift, ift, ift, imp, or, post, xor
-
Methods inherited from interface org.chocosolver.solver.variables.Variable
addMonitor, asBoolVar, asIntVar, asRealVar, asSetVar, clearEvents, contradiction, getCause, getDindex, getEnvironment, getEvtScheduler, getIndexInPropagator, getMask, getModel, getName, getNbProps, getPIndices, getPropagator, getPropagators, getViews, isAConstant, link, notifyPropagators, notifyViews, removeMonitor, setPIndice, storeEvents, subscribeView, swapOnActivate, swapOnPassivate, unlink
-
-
-
-
Method Detail
-
removeValue
public boolean removeValue(int value, ICause cause) throws ContradictionExceptionRemovesvaluefrom the domain ofthis. The instruction comes frompropagator.- If
valueis out of the domain, nothing is done and the return value isfalse, - if removing
valueleads to a dead-end (domain wipe-out), aContradictionExceptionis thrown, - otherwise, if removing
valuefrom the domain can be done safely, the event type is created (the original event can be promoted) and observers are notified and the return value istrue
- Specified by:
removeValuein interfaceIntVar- Parameters:
value- value to remove from the domain (int)cause- removal releaser- Returns:
- true if the value has been removed, false otherwise
- Throws:
ContradictionException- if the domain become empty due to this action
- If
-
removeValues
public boolean removeValues(IntIterableSet values, ICause cause) throws ContradictionException
Description copied from interface:IntVarRemoves the value invaluesfrom the domain ofthis. The instruction comes frompropagator.- If all values are out of the domain, nothing is done and the return value is
false, - if removing a value leads to a dead-end (domain wipe-out),
a
ContradictionExceptionis thrown, - otherwise, if removing the
valuesfrom the domain can be done safely, the event type is created (the original event can be promoted) and observers are notified and the return value istrue
- Specified by:
removeValuesin interfaceIntVar- Parameters:
values- set of ordered values to removecause- removal release- Returns:
- true if at least a value has been removed, false otherwise
- Throws:
ContradictionException- if the domain become empty due to this action
- If all values are out of the domain, nothing is done and the return value is
-
removeAllValuesBut
public boolean removeAllValuesBut(IntIterableSet values, ICause cause) throws ContradictionException
Description copied from interface:IntVarRemoves all values from the domain ofthisexcept those invalues. The instruction comes frompropagator.- If all values are out of the domain,
a
ContradictionExceptionis thrown, - if the domain is a subset of values,
nothing is done and the return value is
false, - otherwise, if removing all values but
valuesfrom the domain can be done safely, the event type is created (the original event can be promoted) and observers are notified and the return value istrue
- Specified by:
removeAllValuesButin interfaceIntVar- Parameters:
values- set of ordered values to keep in the domaincause- removal release- Returns:
- true if a at least a value has been removed, false otherwise
- Throws:
ContradictionException- if the domain become empty due to this action
- If all values are out of the domain,
a
-
removeInterval
public boolean removeInterval(int from, int to, ICause cause) throws ContradictionExceptionDescription copied from interface:IntVarRemoves values between [from, to] from the domain ofthis. The instruction comes frompropagator.- If union between values and the current domain is empty, nothing is done and the return value is
false, - if removing a
valueleads to a dead-end (domain wipe-out), aContradictionExceptionis thrown, - otherwise, if removing at least a
valuefrom the domain can be done safely, the event type is created (the original event can be promoted) and observers are notified and the return value istrue
- Specified by:
removeIntervalin interfaceIntVar- Parameters:
from- lower bound of the interval to remove (int)to- upper bound of the interval to remove(int)cause- removal releaser- Returns:
- true if the value has been removed, false otherwise
- Throws:
ContradictionException- if the domain become empty due to this action
- If union between values and the current domain is empty, nothing is done and the return value is
-
instantiateTo
public boolean instantiateTo(int value, ICause cause) throws ContradictionExceptionInstantiates the domain ofthistovalue. The instruction comes frompropagator.- If the domain of
thisis already instantiated tovalue, nothing is done and the return value isfalse, - If the domain of
thisis already instantiated to another value, then aContradictionExceptionis thrown, - Otherwise, the domain of
thisis restricted tovalueand the observers are notified and the return value istrue.
- Specified by:
instantiateToin interfaceIntVar- Parameters:
value- instantiation value (int)cause- instantiation releaser- Returns:
- true if the instantiation is done, false otherwise
- Throws:
ContradictionException- if the domain become empty due to this action
- If the domain of
-
updateLowerBound
public boolean updateLowerBound(int value, ICause cause) throws ContradictionExceptionUpdates the lower bound of the domain ofthistovalue. The instruction comes frompropagator.- If
valueis smaller than the lower bound of the domain, nothing is done and the return value isfalse, - if updating the lower bound to
valueleads to a dead-end (domain wipe-out), aContradictionExceptionis thrown, - otherwise, if updating the lower bound to
valuecan be done safely, the event type is created (the original event can be promoted) and observers are notified and the return value istrue
- Specified by:
updateLowerBoundin interfaceIntVar- Parameters:
value- new lower bound (included)cause- updating releaser- Returns:
- true if the lower bound has been updated, false otherwise
- Throws:
ContradictionException- if the domain become empty due to this action
- If
-
updateUpperBound
public boolean updateUpperBound(int value, ICause cause) throws ContradictionExceptionUpdates the upper bound of the domain ofthistovalue. The instruction comes frompropagator.- If
valueis greater than the upper bound of the domain, nothing is done and the return value isfalse, - if updating the upper bound to
valueleads to a dead-end (domain wipe-out), aContradictionExceptionis thrown, - otherwise, if updating the upper bound to
valuecan be done safely, the event type is created (the original event can be promoted) and observers are notified and the return value istrue
- Specified by:
updateUpperBoundin interfaceIntVar- Parameters:
value- new upper bound (included)cause- update releaser- Returns:
- true if the upper bound has been updated, false otherwise
- Throws:
ContradictionException- if the domain become empty due to this action
- If
-
updateBounds
public boolean updateBounds(int lb, int ub, ICause cause) throws ContradictionExceptionDescription copied from interface:IntVarUpdates the lower bound and the upper bound of the domain ofthisto, resp.lbandub. The instruction comes frompropagator.- If
lbis smaller than the lower bound of the domain andubis greater than the upper bound of the domain,nothing is done and the return value is
false, - if updating the lower bound to
lb, or updating the upper bound toubleads to a dead-end (domain wipe-out), or iflbis strictly greater thanub, aContradictionExceptionis thrown, - otherwise, if updating the lower bound to
lband/or the upper bound toubcan be done safely can be done safely, the event type is created (the original event can be promoted) and observers are notified and the return value istrue
- Specified by:
updateBoundsin interfaceIntVar- Parameters:
lb- new lower bound (included)ub- new upper bound (included)cause- update releaser- Returns:
- true if the upper bound has been updated, false otherwise
- Throws:
ContradictionException- if the domain become empty due to this action
- If
-
setToTrue
public boolean setToTrue(ICause cause) throws ContradictionException
- Specified by:
setToTruein interfaceBoolVar- Throws:
ContradictionException
-
setToFalse
public boolean setToFalse(ICause cause) throws ContradictionException
- Specified by:
setToFalsein interfaceBoolVar- Throws:
ContradictionException
-
isInstantiated
public boolean isInstantiated()
Description copied from interface:VariableIndicates whetherthisis instantiated (see implemtations to know what instantiation means).- Specified by:
isInstantiatedin interfaceVariable- Returns:
trueifthisis instantiated
-
isInstantiatedTo
public boolean isInstantiatedTo(int aValue)
Description copied from interface:IntVarChecks wetherthisis instantiated toval- Specified by:
isInstantiatedToin interfaceIntVar- Parameters:
aValue- int- Returns:
- true if
thisis instantiated toval, false otherwise
-
contains
public boolean contains(int aValue)
Description copied from interface:IntVarChecks if a valuevbelongs to the domain ofthis
-
getValue
public int getValue()
Retrieves the current value of the variable if instantiated, otherwier the lower bound.
-
getBooleanValue
public ESat getBooleanValue()
- Specified by:
getBooleanValuein interfaceBoolVar
-
getLB
public int getLB()
Retrieves the lower bound of the variable
-
getUB
public int getUB()
Retrieves the upper bound of the variable
-
getDomainSize
public int getDomainSize()
Description copied from interface:IntVarReturns the size of this domain, that is the number of elements in this domain.- Specified by:
getDomainSizein interfaceIntVar- Returns:
- size of the domain
-
getRange
public int getRange()
Description copied from interface:IntVarReturns the range of this domain, that is, the difference between the upper bound and the lower bound.
-
nextValue
public int nextValue(int v)
Description copied from interface:IntVarReturns the first value just after v inthiswhich is in the domain. If no such value exists, returns Integer.MAX_VALUE;To iterate over the values in a
IntVar, use the following loop:int ub = iv.getUB(); for (int i = iv.getLB(); i <= ub; i = iv.nextValue(i)) { // operate on value i here }
-
nextValueOut
public int nextValueOut(int v)
Description copied from interface:IntVarReturns the first value just after v inthiswhich is out of the domain. If v is less than or equal toIntVar.getLB()-2, returns v + 1, if v is greater than or equal toIntVar.getUB(), returns v + 1.- Specified by:
nextValueOutin interfaceIntVar- Parameters:
v- the value to start checking (exclusive)- Returns:
- the next value out of the domain
-
previousValue
public int previousValue(int v)
Description copied from interface:IntVarReturns the previous value just before v inthis. If no such value exists, returns Integer.MIN_VALUE;To iterate over the values in a
IntVar, use the following loop:int lb = iv.getLB(); for (int i = iv.getUB(); i >= lb; i = iv.previousValue(i)) { // operate on value i here }- Specified by:
previousValuein interfaceIntVar- Parameters:
v- the value to start checking (exclusive)- Returns:
- the previous value in the domain
-
previousValueOut
public int previousValueOut(int v)
Description copied from interface:IntVarReturns the first value just before v inthiswhich is out of the domain. If v is greater than or equal toIntVar.getUB()+2, returns v - 1, if v is less than or equal toIntVar.getLB(), returns v - 1.- Specified by:
previousValueOutin interfaceIntVar- Parameters:
v- the value to start checking (exclusive)- Returns:
- the previous value out of the domain
-
hasEnumeratedDomain
public boolean hasEnumeratedDomain()
Description copied from interface:IntVarIndicates wether (or not)thishas an enumerated domain (represented in extension) or not (only bounds)- Specified by:
hasEnumeratedDomainin interfaceIntVar- Returns:
trueif the domain is enumerated,falseotherwise.
-
getDelta
public IEnumDelta getDelta()
Description copied from interface:VariableReturn the delta domain of this
-
toString
public String toString()
- Overrides:
toStringin classAbstractVariable
-
createDelta
public void createDelta()
Description copied from interface:VariableCreate a delta, if necessary, in order to observe removed values of a this. If the delta already exists, has no effect.- Specified by:
createDeltain interfaceVariable
-
monitorDelta
public IIntDeltaMonitor monitorDelta(ICause propagator)
Description copied from interface:IntVarAllow to monitor removed values ofthis.- Specified by:
monitorDeltain interfaceIntVar- Parameters:
propagator- the cause that requires to monitor delta- Returns:
- a delta monitor
-
notifyMonitors
public void notifyMonitors(IEventType event) throws ContradictionException
Description copied from interface:VariableNotify monitors of observed variable modifications- Specified by:
notifyMonitorsin interfaceVariable- Parameters:
event- the event which occurred on the variable- Throws:
ContradictionException- if the monitor detects contradiction.
-
getTypeAndKind
public int getTypeAndKind()
Description copied from interface:VariableReturn a MASK composed of 2 main information: TYPE and KIND.
TYPE is defined in the 3 first bits : VAR ( 1 << 0), CSTE (1 << 1) or VIEW (1 << 2)
KIND is defined on the other bits : INT (1 << 3), BOOL (INT + 1 << 4), GRAPH (1 << 5) or META (1 << 6) To get the TYPE of a variable:int type = var.getTypeAndKind() & Variable.TYPE;
To get the KIND of a variable:int kind = var.getTypeAndKind() & Variable.KIND;
To check a specific type or kind of a variable:boolean isVar = (var.getTypeAndKind() & Variable.VAR) !=0; boolean isInt = (var.getTypeAndKind() & Variable.INT) !=0;- Specified by:
getTypeAndKindin interfaceVariable- Returns:
- an int representing the type and kind of the variable
-
createScheduler
protected EvtScheduler createScheduler()
- Specified by:
createSchedulerin classAbstractVariable
-
getValueIterator
public DisposableValueIterator getValueIterator(boolean bottomUp)
Description copied from interface:IntVarRetrieves an iterator over values ofthis.The values can be iterated in a bottom-up way or top-down way.
To bottom-up iterate over the values in a
IntVar, use the following loop:DisposableValueIterator vit = var.getValueIterator(true); while(vit.hasNext()){ int v = vit.next(); // operate on value v here } vit.dispose();To top-down iterate over the values in aIntVar, use the following loop:DisposableValueIterator vit = var.getValueIterator(false); while(vit.hasPrevious()){ int v = vit.previous(); // operate on value v here } vit.dispose();Using both previous and next can lead to unexpected behaviour.- Specified by:
getValueIteratorin interfaceIntVar- Parameters:
bottomUp- way to iterate over values.truemeans from lower bound to upper bound,falsemeans from upper bound to lower bound.- Returns:
- a disposable iterator over values of
this.
-
getRangeIterator
public DisposableRangeIterator getRangeIterator(boolean bottomUp)
Description copied from interface:IntVarRetrieves an iterator over ranges (or intervals) ofthis.The ranges can be iterated in a bottom-up way or top-down way.
To bottom-up iterate over the values in a
IntVar, use the following loop:DisposableRangeIterator rit = var.getRangeIterator(true); while (rit.hasNext()) { int from = rit.min(); int to = rit.max(); // operate on range [from,to] here rit.next(); } rit.dispose();To top-down iterate over the values in aIntVar, use the following loop:DisposableRangeIterator rit = var.getRangeIterator(false); while (rit.hasPrevious()) { int from = rit.min(); int to = rit.max(); // operate on range [from,to] here rit.previous(); } rit.dispose();Using both previous and next can lead to unexpected behaviour.- Specified by:
getRangeIteratorin interfaceIntVar- Parameters:
bottomUp- way to iterate over ranges.truemeans from lower bound to upper bound,falsemeans from upper bound to lower bound.- Returns:
- a disposable iterator over ranges of
this.
-
not
public BoolVar not()
- Specified by:
notin interfaceBoolVar- Specified by:
notin interfaceReExpression- Returns:
- return the expression "¬x" where this is "x"
-
isLit
public boolean isLit()
Description copied from interface:ILogicalCurrent tree is a literal
-
isNot
public boolean isNot()
Description copied from interface:ILogicalCurrent tree is rooted with NOT logical operator This is a one way relationship: in case a = not(b) a.isNot() returns true whereas b.isNot() returns false (unless b = not(c)...)
-
setNot
public void setNot(boolean isNot)
Description copied from interface:ILogicalStates whether or not this variable is the negation of another. This is a one way relationship: in case a = not(b) a.isNot() returns true whereas b.isNot() returns false (unless b = not(c)...)
-
-