Module org.chocosolver.solver
Class BitsetArrayIntVarImpl
- java.lang.Object
-
- org.chocosolver.solver.variables.impl.AbstractVariable
-
- org.chocosolver.solver.variables.impl.BitsetArrayIntVarImpl
-
- All Implemented Interfaces:
java.lang.Comparable<Variable>
,java.lang.Iterable<java.lang.Integer>
,ArExpression
,ICause
,Identity
,IntVar
,Variable
public final class BitsetArrayIntVarImpl extends AbstractVariable implements IntVar
IntVar implementation for quite small domains bit with very distant values e.g. {-51900,42,235923}- Since:
- 14/05/2013
- Author:
- Charles Prud'homme, Jean-Guillaume Fages
-
-
Nested Class Summary
-
Nested classes/interfaces inherited from interface org.chocosolver.solver.expression.discrete.arithmetic.ArExpression
ArExpression.IntPrimitive, ArExpression.Operator
-
-
Field Summary
-
Fields inherited from class org.chocosolver.solver.variables.impl.AbstractVariable
mIdx, model, monitors, MSG_EMPTY, MSG_INST, name, scheduled
-
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 BitsetArrayIntVarImpl(java.lang.String name, int[] sortedValues, Model model)
Creates anIntVar
based on an array of non-consecutive but ordered values.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description boolean
contains(int aValue)
Checks if a valuev
belongs to the domain ofthis
void
createDelta()
Create a delta, if necessary, in order to observe removed values of a this.void
createLit(IntIterableRangeSet rootDomain)
Create the signed literal.protected EvtScheduler<IntEventType>
createScheduler()
IEnumDelta
getDelta()
Return the delta domain of thisint
getDomainSize()
Returns the size of this domain, that is the number of elements in this domain.int
getLB()
Retrieves the lower bound of the variableSignedLiteral
getLit()
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.int
getUB()
Retrieves the upper bound of the variableint
getValue()
Retrieves the current value of the variable if instantiatedDisposableValueIterator
getValueIterator(boolean bottomUp)
Retrieves an iterator over values ofthis
.boolean
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 implementations to know what instantiation means).boolean
isInstantiatedTo(int value)
Checks wetherthis
is instantiated toval
java.util.Iterator<java.lang.Integer>
iterator()
IIntDeltaMonitor
monitorDelta(ICause propagator)
Allow to monitor removed values ofthis
.int
nextValue(int aValue)
Returns the first value just after v inthis
which is in the domain.int
nextValueOut(int aValue)
Returns the first value just after v inthis
which is out of the domain.int
previousValue(int aValue)
Returns the previous value just before v inthis
.int
previousValueOut(int aValue)
Returns the first value just before v inthis
which is out of the domain.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
.java.lang.String
toString()
boolean
updateBounds(int aLB, int aUB, 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, compareTo, contradiction, equals, forEachPropagator, getCause, getEvtScheduler, getId, getMask, getModel, getName, getNbProps, getNbViews, getView, hashCode, instantiationWorldIndex, isAConstant, isBool, isScheduled, link, notifyMonitors, notifyPropagators, notifyViews, recordWorldIndex, removeMonitor, schedule, schedulePropagators, storeEvents, streamPropagators, subscribeView, swapOnPassivate, unlink, unschedule
-
Methods inherited from class java.lang.Object
clone, finalize, getClass, 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, in, in, le, le, lt, lt, max, max, max, min, min, min, mod, mod, mul, mul, mul, ne, ne, neg, notin, notin, pow, pow, primitive, sqr, sub, sub
-
Methods inherited from interface org.chocosolver.solver.variables.IntVar
flushLit, forEachIntVar, getNoChild, instantiateTo, intersectLit, intersectLit, intersectLit, intVar, isBool, isExpressionLeaf, removeValue, stream, transformEvent, unionLit, unionLit, unionLit, updateLowerBound, updateUpperBound
-
Methods inherited from interface org.chocosolver.solver.variables.Variable
addMonitor, asBoolVar, asIntVar, asRealVar, asSetVar, clearEvents, contradiction, forEachPropagator, getCause, getEnvironment, getEvtScheduler, getMask, getModel, getName, getNbProps, getNbViews, getView, instantiationWorldIndex, isAConstant, isScheduled, link, notifyMonitors, notifyPropagators, notifyViews, recordWorldIndex, removeMonitor, schedule, schedulePropagators, storeEvents, streamPropagators, subscribeView, swapOnPassivate, unlink, unschedule
-
-
-
-
Constructor Detail
-
BitsetArrayIntVarImpl
public BitsetArrayIntVarImpl(java.lang.String name, int[] sortedValues, Model model)
Creates anIntVar
based on an array of non-consecutive but ordered values. Non-consecutive condition is the main reason which motivates this class.- Parameters:
name
- name of the variablesortedValues
- domain valuesmodel
- the model to declare this variable in
-
-
Method Detail
-
removeValue
public boolean removeValue(int value, ICause cause) throws ContradictionException
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
-
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
-
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
-
instantiateTo
public boolean instantiateTo(int value, ICause cause) throws ContradictionException
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
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
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 aLB, int aUB, 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:
aLB
- new lower bound (included)aUB
- 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
-
isInstantiated
public boolean isInstantiated()
Description copied from interface:Variable
Indicates whetherthis
is instantiated (see implementations to know what instantiation means).- Specified by:
isInstantiated
in interfaceVariable
- Returns:
true
ifthis
is instantiated
-
isInstantiatedTo
public boolean isInstantiatedTo(int value)
Description copied from interface:IntVar
Checks wetherthis
is instantiated toval
- Specified by:
isInstantiatedTo
in interfaceIntVar
- Parameters:
value
- int- Returns:
- true if
this
is instantiated toval
, false otherwise
-
contains
public boolean contains(int aValue)
Description copied from interface:IntVar
Checks if a valuev
belongs to the domain ofthis
-
getValue
public int getValue() throws java.lang.IllegalStateException
Description copied from interface:IntVar
Retrieves the current value of the variable if instantiated
-
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:Variable
Returns the size of this domain, that is the number of elements in this domain.- Specified by:
getDomainSize
in interfaceVariable
- 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.
-
nextValue
public int nextValue(int aValue)
Description copied from interface:IntVar
Returns the first value just after v inthis
which 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 aValue)
Description copied from interface:IntVar
Returns the first value just after v inthis
which 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:
nextValueOut
in interfaceIntVar
- Parameters:
aValue
- the value to start checking (exclusive)- Returns:
- the next value out of the domain
-
previousValue
public int previousValue(int aValue)
Description copied from interface:IntVar
Returns 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:
previousValue
in interfaceIntVar
- Parameters:
aValue
- the value to start checking (exclusive)- Returns:
- the previous value in the domain
-
previousValueOut
public int previousValueOut(int aValue)
Description copied from interface:IntVar
Returns the first value just before v inthis
which 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:
previousValueOut
in interfaceIntVar
- Parameters:
aValue
- the value to start checking (exclusive)- Returns:
- the previous value out of the domain
-
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.
-
getDelta
public IEnumDelta getDelta()
Description copied from interface:Variable
Return the delta domain of this
-
toString
public java.lang.String toString()
- Overrides:
toString
in classAbstractVariable
-
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
-
monitorDelta
public IIntDeltaMonitor monitorDelta(ICause propagator)
Description copied from interface:IntVar
Allow to monitor removed values ofthis
.- Specified by:
monitorDelta
in interfaceIntVar
- Parameters:
propagator
- the cause that requires to monitor delta- Returns:
- a delta monitor
-
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
-
createScheduler
protected EvtScheduler<IntEventType> createScheduler()
- Specified by:
createScheduler
in classAbstractVariable
-
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 a
IntVar
, 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 a
IntVar
, 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
.
-
iterator
public java.util.Iterator<java.lang.Integer> iterator()
- Specified by:
iterator
in interfacejava.lang.Iterable<java.lang.Integer>
-
createLit
public void createLit(IntIterableRangeSet rootDomain)
Description copied from interface:IntVar
Create the signed literal.
-
getLit
public SignedLiteral getLit()
-
-