use of org.chocosolver.util.iterators.DisposableValueIterator in project scheduler by btrplace.
the class RandomVMPlacement method randomWithRankedValues.
/**
* Random value but that consider the rank of nodes.
* So values are picked up from the first rank possible.
*/
private int randomWithRankedValues(IntVar x) {
TIntArrayList[] values = new TIntArrayList[ranks.length];
DisposableValueIterator ite = x.getValueIterator(true);
try {
while (ite.hasNext()) {
int v = ite.next();
int i;
for (i = 0; i < ranks.length; i++) {
if (ranks[i].contains(v)) {
if (values[i] == null) {
values[i] = new TIntArrayList();
}
values[i].add(v);
}
}
}
} finally {
ite.dispose();
}
// We pick a random value in the first rank that is not empty (aka null here)
for (TIntArrayList rank : values) {
if (rank != null) {
int v = rnd.nextInt(rank.size());
return rank.get(v);
}
}
return -1;
}
use of org.chocosolver.util.iterators.DisposableValueIterator in project scheduler by btrplace.
the class RandomVMPlacement method randomValue.
/**
* Pick a random value inside the variable domain.
*/
private int randomValue(IntVar x) {
int i = rnd.nextInt(x.getDomainSize());
DisposableValueIterator ite = x.getValueIterator(true);
int pos = -1;
try {
while (i >= 0) {
pos = ite.next();
i--;
}
} finally {
ite.dispose();
}
return pos;
}
use of org.chocosolver.util.iterators.DisposableValueIterator in project scheduler by btrplace.
the class KnapsackDecorator method postInitialize.
/**
* initialize the lists of candidates.
*/
public void postInitialize() throws ContradictionException {
final int[] biggest = new int[prop.nbDims];
for (int i = 0; i < prop.bins.length; i++) {
for (int d = 0; d < prop.nbDims; d++) {
biggest[d] = Math.max(biggest[d], prop.iSizes[d][i]);
}
if (!prop.bins[i].isInstantiated()) {
final DisposableValueIterator it = prop.bins[i].getValueIterator(true);
try {
while (it.hasNext()) {
candidate.get(it.next()).set(i);
}
} finally {
it.dispose();
}
}
}
for (int b = 0; b < prop.nbBins; b++) {
for (int d = 0; d < prop.nbDims; d++) {
dynBiggest[d][b] = prop.getVars()[0].getEnvironment().makeInt(biggest[d]);
}
}
fullKnapsack();
}
use of org.chocosolver.util.iterators.DisposableValueIterator in project scheduler by btrplace.
the class VectorPackingPropagator method initialize.
// ***********************************************************************************************************************//
// HELPER
// ***********************************************************************************************************************//
/**
* initialize the internal data: sumItemSize, assignedLoad, potentialLoad, sumLoadInf, sumLoadSup, maxSlackBinHeap
* shrink the item-to-bins assignment variables: 0 <= bins[i] < nbBins
* shrink the bin load variables: assignedLoad <= binLoad <= potentialLoad
*/
@SuppressWarnings("squid:S3346")
private void initialize() throws ContradictionException {
sumISizes = new long[nbDims];
computeSumItemSizes();
int[][] rLoads = new int[nbDims][nbBins];
int[][] cLoads = new int[nbDims][nbBins];
for (int i = 0; i < bins.length; i++) {
bins[i].updateLowerBound(0, this);
bins[i].updateUpperBound(nbBins - 1, this);
if (bins[i].isInstantiated()) {
for (int d = 0; d < nbDims; d++) {
rLoads[d][bins[i].getValue()] += iSizes[d][i];
}
} else {
DisposableValueIterator it = bins[i].getValueIterator(true);
try {
while (it.hasNext()) {
int b = it.next();
for (int d = 0; d < nbDims; d++) {
cLoads[d][b] += iSizes[d][i];
}
}
} finally {
it.dispose();
}
}
}
int[] slb = new int[nbDims];
int[] slu = new int[nbDims];
for (int b = 0; b < nbBins; b++) {
for (int d = 0; d < nbDims; d++) {
assignedLoad[d][b].set(rLoads[d][b]);
potentialLoad[d][b].set(rLoads[d][b] + cLoads[d][b]);
loads[d][b].updateLowerBound(rLoads[d][b], this);
loads[d][b].updateUpperBound(rLoads[d][b] + cLoads[d][b], this);
slb[d] += loads[d][b].getLB();
slu[d] += loads[d][b].getUB();
}
}
for (int d = 0; d < nbDims; d++) {
sumLoadInf[d].set(slb[d]);
sumLoadSup[d].set(slu[d]);
}
loadsHaveChanged = getModel().getEnvironment().makeBool(false);
if (decoKPSimple != null) {
decoKPSimple.postInitialize();
}
assert checkLoadConsistency();
for (IIntDeltaMonitor delta : deltaMonitor) {
delta.unfreeze();
}
}
use of org.chocosolver.util.iterators.DisposableValueIterator in project scheduler by btrplace.
the class VectorPackingPropagator method checkLoadConsistency.
// ****************************************************************//
// ********* Checkers *********************************************//
// ****************************************************************//
/**
* Check the consistency of the assigned and candidate loads with regards to the assignment variables:
* for each bin: sumAssignedItemSizes == binAssignedLoad, sumAllPossibleItemSizes == binPotentialLoad
* rule 2, for each bin: binAssignedLoad <= binLoad <= binPotentialLoad
*
* @return {@code false} if not consistent.
*/
private boolean checkLoadConsistency() {
boolean check = true;
int[][] rs = new int[nbDims][nbBins];
int[][] cs = new int[nbDims][nbBins];
for (int i = 0; i < bins.length; i++) {
if (bins[i].isInstantiated()) {
for (int d = 0; d < nbDims; d++) {
rs[d][bins[i].getValue()] += iSizes[d][i];
}
} else {
DisposableValueIterator it = bins[i].getValueIterator(true);
try {
while (it.hasNext()) {
int v = it.next();
for (int d = 0; d < nbDims; d++) {
cs[d][v] += iSizes[d][i];
}
}
} finally {
it.dispose();
}
}
}
for (int d = 0; d < nbDims; d++) {
check = check && checkDimension(d, rs, cs);
}
if (!check) {
for (IntVar v : bins) {
System.err.println(v.toString());
}
}
return check;
}
Aggregations