Module org.chocosolver
Class IntView<I extends IntVar>
- java.lang.Object
-
- org.chocosolver.solver.variables.impl.AbstractVariable
-
- org.chocosolver.solver.variables.view.IntView<I>
-
- All Implemented Interfaces:
Comparable<Variable>
,Iterable<Integer>
,ArExpression
,ICause
,Identity
,IntVar
,Variable
,IView
- Direct Known Subclasses:
BoolNotView
,IntBoolView
,MinusView
,OffsetView
,ScaleView
public abstract class IntView<I extends IntVar> extends AbstractVariable implements IView, IntVar
"A view implements the same operations as a variable. A view stores a reference to a variable. Invoking an operation on the view executes the appropriate operation on the view's variable." Based on "Views and Iterators for Generic Constraint Implementations"
C. Shulte and G. Tack.
Eleventh International Conference on Principles and Practice of Constraint Programming- Since:
- 18/03/11
- Author:
- Charles Prud'homme
-
-
Nested Class Summary
-
Nested classes/interfaces inherited from interface org.chocosolver.solver.expression.discrete.arithmetic.ArExpression
ArExpression.Operator
-
-
Field Summary
Fields Modifier and Type Field Description protected IntDelta
delta
To store removed valuesprotected I
var
Observed variable-
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
-
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description int
compareTo(Variable o)
void
createDelta()
Create a delta, if necessary, in order to observe removed values of a this.protected boolean
doInstantiateVar(int value)
Action to execute onvar
when this view requires to instantiate itprotected boolean
doRemoveIntervalFromVar(int from, int to)
Action to execute onvar
when this view requires to remove an interval from itprotected boolean
doRemoveValueFromVar(int value)
Action to execute onvar
when this view requires to remove a value from itprotected boolean
doUpdateLowerBoundOfVar(int value)
Action to execute onvar
when this view requires to update its lower boundprotected boolean
doUpdateUpperBoundOfVar(int value)
Action to execute onvar
when this view requires to update its upper boundvoid
forEachIntVar(Consumer<IntVar> action)
Apply an action on each variable declared on the scope of this cause, if any.IDelta
getDelta()
Return the delta domain of thisint
getDomainSize()
Returns the size of this domain, that is the number of elements in this domain.int
getRange()
Returns the range of this domain, that is, the difference between the upper bound and the lower bound.DisposableRangeIterator
getRangeIterator(boolean bottomUp)
Retrieves an iterator over ranges (or intervals) ofthis
.int
getTypeAndKind()
Return a MASK composed of 2 main information: TYPE and KIND.DisposableValueIterator
getValueIterator(boolean bottomUp)
Retrieves an iterator over values ofthis
.IntVar
getVariable()
Return the basis variableboolean
hasEnumeratedDomain()
Indicates wether (or not)this
has an enumerated domain (represented in extension) or not (only bounds)boolean
instantiateTo(int value, ICause cause)
Instantiates the domain ofthis
tovalue
.boolean
isInstantiated()
Indicates whetherthis
is instantiated (see implemtations to know what instantiation means).Iterator<Integer>
iterator()
void
notify(IEventType event)
To notify a view that the variable is observed has been modified.boolean
removeAllValuesBut(IntIterableSet values, ICause cause)
Removes all values from the domain ofthis
except those invalues
.boolean
removeInterval(int from, int to, ICause cause)
Removes values between [from, to
] from the domain ofthis
.boolean
removeValue(int value, ICause cause)
Removesvalue
from the domain ofthis
.boolean
removeValues(IntIterableSet values, ICause cause)
Removes the value invalues
from the domain ofthis
.IEventType
transformEvent(IEventType evt)
boolean
updateBounds(int lb, int ub, ICause cause)
Updates the lower bound and the upper bound of the domain ofthis
to, resp.boolean
updateLowerBound(int value, ICause cause)
Updates the lower bound of the domain ofthis
tovalue
.boolean
updateUpperBound(int value, ICause cause)
Updates the upper bound of the domain ofthis
tovalue
.-
Methods inherited from class org.chocosolver.solver.variables.impl.AbstractVariable
addMonitor, asBoolVar, asIntVar, asRealVar, asSetVar, clearEvents, contradiction, createScheduler, getCause, getDindex, getEvtScheduler, getId, getIndexInPropagator, getMask, getModel, getName, getNbProps, getNbViews, getPIndices, getPropagator, getPropagators, getView, isAConstant, isBool, link, notifyMonitors, notifyPropagators, notifyViews, removeMonitor, setPIndice, storeEvents, subscribeView, swapOnActivate, swapOnPassivate, toString, 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, extractVar, ge, ge, getExpressionChild, getModel, gt, gt, ieval, 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.IntVar
contains, getLB, getNoChild, getUB, getValue, intVar, isBool, isExpressionLeaf, isInstantiatedTo, monitorDelta, nextValue, nextValueOut, previousValue, previousValueOut
-
Methods inherited from interface java.lang.Iterable
forEach, spliterator
-
Methods inherited from interface org.chocosolver.solver.variables.view.IView
explain, justifyEvent
-
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, getNbViews, getPIndices, getPropagator, getPropagators, getView, isAConstant, link, notifyMonitors, notifyPropagators, notifyViews, removeMonitor, setPIndice, storeEvents, subscribeView, swapOnActivate, swapOnPassivate, unlink
-
-
-
-
Method Detail
-
doInstantiateVar
protected boolean doInstantiateVar(int value) throws ContradictionException
Action to execute onvar
when this view requires to instantiate it- Parameters:
value
- value before modification of the view- Returns:
- true if
var
has been modified - Throws:
ContradictionException
- if modification fails
-
doUpdateLowerBoundOfVar
protected boolean doUpdateLowerBoundOfVar(int value) throws ContradictionException
Action to execute onvar
when this view requires to update its lower bound- Parameters:
value
- value before modification of the view- Returns:
- true if
var
has been modified - Throws:
ContradictionException
- if modification fails
-
doUpdateUpperBoundOfVar
protected boolean doUpdateUpperBoundOfVar(int value) throws ContradictionException
Action to execute onvar
when this view requires to update its upper bound- Parameters:
value
- value before modification of the view- Returns:
- true if
var
has been modified - Throws:
ContradictionException
- if modification fails
-
doRemoveValueFromVar
protected boolean doRemoveValueFromVar(int value) throws ContradictionException
Action to execute onvar
when this view requires to remove a value from it- Parameters:
value
- value before modification of the view- Returns:
- true if
var
has been modified - Throws:
ContradictionException
- if modification fails
-
doRemoveIntervalFromVar
protected boolean doRemoveIntervalFromVar(int from, int to) throws ContradictionException
Action to execute onvar
when this view requires to remove an interval from it- Parameters:
from
- first value of the interval before modification of the viewto
- last value of the interval before modification of the view- Returns:
- true if
var
has been modified - Throws:
ContradictionException
- if modification fails
-
removeValue
public boolean removeValue(int value, ICause cause) throws ContradictionException
Description copied from interface:IntVar
Removesvalue
from the domain ofthis
. The instruction comes frompropagator
.- If
value
is out of the domain, nothing is done and the return value isfalse
, - if removing
value
leads to a dead-end (domain wipe-out), aContradictionException
is thrown, - otherwise, if removing
value
from 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:
removeValue
in 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:IntVar
Removes the value invalues
from 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
ContradictionException
is thrown, - otherwise, if removing the
values
from 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:
removeValues
in 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
-
removeInterval
public boolean removeInterval(int from, int to, ICause cause) throws ContradictionException
Description copied from interface:IntVar
Removes 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
value
leads to a dead-end (domain wipe-out), aContradictionException
is thrown, - otherwise, if removing at least a
value
from 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:
removeInterval
in 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
-
removeAllValuesBut
public boolean removeAllValuesBut(IntIterableSet values, ICause cause) throws ContradictionException
Description copied from interface:IntVar
Removes all values from the domain ofthis
except those invalues
. The instruction comes frompropagator
.- If all values are out of the domain,
a
ContradictionException
is thrown, - if the domain is a subset of values,
nothing is done and the return value is
false
, - otherwise, if removing all values but
values
from 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:
removeAllValuesBut
in 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
-
instantiateTo
public boolean instantiateTo(int value, ICause cause) throws ContradictionException
Description copied from interface:IntVar
Instantiates the domain ofthis
tovalue
. The instruction comes frompropagator
.- If the domain of
this
is already instantiated tovalue
, nothing is done and the return value isfalse
, - If the domain of
this
is already instantiated to another value, then aContradictionException
is thrown, - Otherwise, the domain of
this
is restricted tovalue
and the observers are notified and the return value istrue
.
- Specified by:
instantiateTo
in 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 ContradictionException
Description copied from interface:IntVar
Updates the lower bound of the domain ofthis
tovalue
. The instruction comes frompropagator
.- If
value
is smaller than the lower bound of the domain, nothing is done and the return value isfalse
, - if updating the lower bound to
value
leads to a dead-end (domain wipe-out), aContradictionException
is thrown, - otherwise, if updating the lower bound to
value
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:
updateLowerBound
in 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 ContradictionException
Description copied from interface:IntVar
Updates the upper bound of the domain ofthis
tovalue
. The instruction comes frompropagator
.- If
value
is greater than the upper bound of the domain, nothing is done and the return value isfalse
, - if updating the upper bound to
value
leads to a dead-end (domain wipe-out), aContradictionException
is thrown, - otherwise, if updating the upper bound to
value
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:
updateUpperBound
in 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 ContradictionException
Description copied from interface:IntVar
Updates the lower bound and the upper bound of the domain ofthis
to, resp.lb
andub
. The instruction comes frompropagator
.- If
lb
is smaller than the lower bound of the domain andub
is 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 toub
leads to a dead-end (domain wipe-out), or iflb
is strictly greater thanub
, aContradictionException
is thrown, - otherwise, if updating the lower bound to
lb
and/or the upper bound toub
can 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:
updateBounds
in 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
-
getTypeAndKind
public int getTypeAndKind()
Description copied from interface:Variable
Return 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:
getTypeAndKind
in interfaceVariable
- Returns:
- an int representing the type and kind of the variable
-
getVariable
public IntVar getVariable()
Description copied from interface:IView
Return the basis variable- Specified by:
getVariable
in interfaceIView
- Returns:
- variable observed
-
getDomainSize
public int getDomainSize()
Description copied from interface:IntVar
Returns the size of this domain, that is the number of elements in this domain.- Specified by:
getDomainSize
in interfaceIntVar
- Returns:
- size of the domain
-
getRange
public int getRange()
Description copied from interface:IntVar
Returns the range of this domain, that is, the difference between the upper bound and the lower bound.
-
hasEnumeratedDomain
public boolean hasEnumeratedDomain()
Description copied from interface:IntVar
Indicates wether (or not)this
has an enumerated domain (represented in extension) or not (only bounds)- Specified by:
hasEnumeratedDomain
in interfaceIntVar
- Returns:
true
if the domain is enumerated,false
otherwise.
-
isInstantiated
public boolean isInstantiated()
Description copied from interface:Variable
Indicates whetherthis
is instantiated (see implemtations to know what instantiation means).- Specified by:
isInstantiated
in interfaceVariable
- Returns:
true
ifthis
is instantiated
-
getDelta
public IDelta getDelta()
Description copied from interface:Variable
Return the delta domain of this
-
createDelta
public void createDelta()
Description copied from interface:Variable
Create a delta, if necessary, in order to observe removed values of a this. If the delta already exists, has no effect.- Specified by:
createDelta
in interfaceVariable
-
compareTo
public int compareTo(Variable o)
- Specified by:
compareTo
in interfaceComparable<I extends IntVar>
- Overrides:
compareTo
in classAbstractVariable
-
notify
public void notify(IEventType event) throws ContradictionException
Description copied from interface:IView
To notify a view that the variable is observed has been modified.- Specified by:
notify
in interfaceIView
- Parameters:
event
- the event received by the observed variable- Throws:
ContradictionException
- if a failure occurs
-
transformEvent
public IEventType transformEvent(IEventType evt)
- Specified by:
transformEvent
in interfaceIntVar
- Parameters:
evt
- original event- Returns:
- transforms the original event wrt this IntVar
-
getValueIterator
public DisposableValueIterator getValueIterator(boolean bottomUp)
Description copied from interface:IntVar
Retrieves 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:
getValueIterator
in interfaceIntVar
- Parameters:
bottomUp
- way to iterate over values.true
means from lower bound to upper bound,false
means from upper bound to lower bound.- Returns:
- a disposable iterator over values of
this
.
-
getRangeIterator
public DisposableRangeIterator getRangeIterator(boolean bottomUp)
Description copied from interface:IntVar
Retrieves 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:
getRangeIterator
in interfaceIntVar
- Parameters:
bottomUp
- way to iterate over ranges.true
means from lower bound to upper bound,false
means from upper bound to lower bound.- Returns:
- a disposable iterator over ranges of
this
.
-
forEachIntVar
public void forEachIntVar(Consumer<IntVar> action)
Description copied from interface:ICause
Apply an action on each variable declared on the scope of this cause, if any.- Specified by:
forEachIntVar
in interfaceICause
- Specified by:
forEachIntVar
in interfaceIntVar
- Parameters:
action
- action to perform on each variable declared in this cause.
-
-