Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Reduce object creation during solving #985

Merged
merged 4 commits into from
Dec 7, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -110,13 +110,13 @@ public void propagate(int evtmask) throws ContradictionException {
}
}
}
pruneList.clear();
pruneList.resetQuick();
for(int k = 0; k<boxesToCompute.size(); k++) {
int i = boxesToCompute.getQuick(k);
energyCheck(i);
hasFiltered |= prune(i);
}
boxesToCompute.clear();
boxesToCompute.resetQuick();
for(int k = 0; k< pruneList.size(); k++) {
prop(pruneList.getQuick(k));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ public class PropGraphCumulative extends PropCumulative {
* @param e end variables
* @param h height variables
* @param capa capacity variable
* @param fast reduces the number of propagation (less filtering)
* @param fast reduces the number of propagation (less filtering)
* @param filters filtering algorithm to use
*/
public PropGraphCumulative(IntVar[] s, IntVar[] d, IntVar[] e, IntVar[] h, IntVar capa, boolean fast,
Expand All @@ -66,7 +66,7 @@ public PropGraphCumulative(IntVar[] s, IntVar[] d, IntVar[] e, IntVar[] h, IntVa
this.g = new UndirectedGraph(model, n, SetType.BITSET, true);
this.tasks = SetFactory.makeBipartiteSet(0);
this.toCompute = SetFactory.makeBipartiteSet(0);
this.fast = fast;
this.fast = fast;
}

//***********************************************************************************
Expand All @@ -79,26 +79,28 @@ public void propagate(int evtmask) throws ContradictionException {
super.propagate(evtmask);
graphComputation();
} else {
if(full){
if (full) {
filter(allTasks);
}else {
} else {
int count = 0;
ISetIterator tcIt = toCompute.iterator();
while (tcIt.hasNext()){
while (tcIt.hasNext()) {
int i = tcIt.nextInt();
for (int j : g.getNeighborsOf(i)) {
ISetIterator it = g.getNeighborsOf(i).iterator();
while (it.hasNext()) {
int j = it.nextInt();
if (disjoint(i, j)) {
g.removeEdge(i, j);
}
}
count += g.getNeighborsOf(i).size();
if(count >= 2*n)break;
if (count >= 2 * n) break;
}
if (count >= 2*n) {
if (count >= 2 * n) {
filter(allTasks);
} else {
ISetIterator iter = toCompute.iterator();
while (iter.hasNext()){
while (iter.hasNext()) {
filterAround(iter.nextInt());
}
}
Expand All @@ -117,20 +119,20 @@ public void propagate(int varIdx, int mask) throws ContradictionException {
}
if (varIdx < 4 * n) {
int v = varIdx % n;
if(h[v].getUB()==0 || d[v].getUB()==0){
if (h[v].getUB() == 0 || d[v].getUB() == 0) {
allTasks.remove(v);
ISetIterator gIt = g.getNeighborsOf(v).iterator();
while (gIt.hasNext()){
g.removeEdge(v,gIt.nextInt());
while (gIt.hasNext()) {
g.removeEdge(v, gIt.nextInt());
}
}else if(s[v].getUB()<e[v].getLB() || !fast){
} else if (s[v].getUB() < e[v].getLB() || !fast) {
toCompute.add(v);
}
} else {
updateMaxCapa();
full = true;
}
forcePropagate(PropagatorEventType.CUSTOM_PROPAGATION);
forcePropagate(PropagatorEventType.CUSTOM_PROPAGATION);
}

protected void filterAround(int taskIndex) throws ContradictionException {
Expand Down Expand Up @@ -172,10 +174,10 @@ private void graphComputation() {
Event event = events[timeIndex++];
switch (event.type) {
case (START):
boolean eok = h[event.index].getUB()>0 && d[event.index].getUB()>0;
if(eok) {
boolean eok = h[event.index].getUB() > 0 && d[event.index].getUB() > 0;
if (eok) {
for (int i = tprune.nextSetBit(0); i >= 0; i = tprune.nextSetBit(i + 1)) {
if(h[i].getUB()>0 && d[i].getUB()>0) {
if (h[i].getUB() > 0 && d[i].getUB() > 0) {
g.addEdge(i, event.index);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,9 @@ public void filter(IntVar[] s, IntVar[] d, IntVar[] e, IntVar[] h, IntVar capa,
h[i].updateUpperBound(minH,aCause);
}
}
for (int i : tasks) {
tIter = tasks.iterator();
while (tIter.hasNext()){
int i = tIter.nextInt();
if (h[i].getLB() > 0) {
// filters
if (s[i].getLB() + d[i].getLB() > min) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.function.BiConsumer;
import java.util.function.BiFunction;
import java.util.stream.Collectors;

Expand All @@ -53,21 +54,6 @@ public Element(int count, int w0, int w1) {
}
}

private final BiFunction<Propagator<?>, double[], double[]> remapWeights =
(p, w) -> {
if (w == null) {
// if absent
w = new double[p.getNbVars()];
} else if (w.length < p.getNbVars()) {
// may happen propagators (like PropSat) with dynamic variable addition
w = new double[p.getNbVars()];
double[] nw = new double[p.getNbVars()];
System.arraycopy(w, 0, nw, 0, w.length);
w = nw;
}
return w;
};

protected static final int FLUSH_TOPS = 20;
protected static final double FLUSH_RATIO = .9 * FLUSH_TOPS;
protected int flushThs;
Expand Down Expand Up @@ -118,6 +104,35 @@ public Element(int count, int w0, int w1) {
final HashMap<Propagator<?>, double[]> refinedWeights = new HashMap<>();
static final double[] rw = {0.};

private final BiFunction<Propagator<?>, double[], double[]> remapWeights =
(p, w) -> {
if (w == null) {
// if absent
w = new double[p.getNbVars()];
} else if (w.length < p.getNbVars()) {
// may happen propagators (like PropSat) with dynamic variable addition
w = new double[p.getNbVars()];
double[] nw = new double[p.getNbVars()];
System.arraycopy(w, 0, nw, 0, w.length);
w = nw;
}
return w;
};

private final BiConsumer<Variable, Propagator<?>> updater = (v, p) -> {
Element elt = failCount.get(p);
if (elt != null) {
if (p.getVar(elt.ws[0]) == v) {
updateFutvars(p, elt, 0);
} else if (p.getVar(elt.ws[1]) == v) {
updateFutvars(p, elt, 1);
}
}
};

private final BiFunction<Variable, Integer, Integer> incr = (v, c) -> c + 1;
private final BiFunction<Variable, Integer, Integer> decr = (v, c) -> c - 1;

public AbstractCriterionBasedVariableSelector(V[] vars, long seed, int flush) {
this.random = new java.util.Random(seed);
this.solver = vars[0].getModel().getSolver();
Expand Down Expand Up @@ -264,13 +279,13 @@ final void plug(Variable var) {
observed.put(var, 1);
var.addMonitor(this);
} else {
observed.computeIfPresent(var, (v, c) -> c + 1);
observed.computeIfPresent(var, incr);
}
}

private void unplug(Variable var) {
assert observed.containsKey(var);
Integer obs = observed.computeIfPresent(var, (v, c) -> c - 1);
Integer obs = observed.computeIfPresent(var, decr);
if (obs != null && obs == 0) {
var.removeMonitor(this);
observed.remove(var);
Expand All @@ -290,16 +305,7 @@ private int next(Propagator<?> pro, int w0, int w1) {
@Override
public final void onUpdate(Variable var, IEventType evt) {
if (var.isInstantiated()) {
var.streamPropagators().forEach(p -> {
Element elt = failCount.get(p);
if (elt != null) {
if (p.getVar(elt.ws[0]) == var) {
updateFutvars(p, elt, 0);
} else if (p.getVar(elt.ws[1]) == var) {
updateFutvars(p, elt, 1);
}
}
});
var.forEachPropagator(updater);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import org.chocosolver.solver.variables.view.IView;
import org.chocosolver.util.iterators.EvtScheduler;

import java.util.function.BiConsumer;
import java.util.stream.Stream;

/**
Expand Down Expand Up @@ -137,6 +138,13 @@ public interface Variable extends Identity, Comparable<Variable> {
*/
Stream<Propagator<?>> streamPropagators();

/**
* Performs an action for each propagator of this variable.
*
* @param action action to perform on the elements
*/
void forEachPropagator(BiConsumer<Variable, Propagator<?>> action);

/**
* Return the number of propagators
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@

import java.util.Arrays;
import java.util.Spliterator;
import java.util.function.BiConsumer;
import java.util.function.Consumer;
import java.util.stream.Stream;
import java.util.stream.StreamSupport;
Expand Down Expand Up @@ -237,6 +238,7 @@ public void swapOnPassivate(Propagator<?> propagator, int idxInProp) {

@Override
public Stream<Propagator<?>> streamPropagators() {
//noinspection Convert2Diamond
Spliterator<Propagator<?>> it = new Spliterator<Propagator<?>>() {

int c = 0;
Expand Down Expand Up @@ -278,6 +280,22 @@ public int characteristics() {
return StreamSupport.stream(it, false);
}

@Override
public void forEachPropagator(BiConsumer<Variable, Propagator<?>> action) {
int c = 0;
int i = propagators[c].first;
do {
if (i < propagators[c].last) {
action.accept(this, propagators[c].propagators[i++]);
} else {
c++;
if (c < propagators.length) {
i = propagators[c].first;
}
}
} while (c < propagators.length);
}

@Override
public final int getNbProps() {
return nbPropagators;
Expand Down Expand Up @@ -541,9 +559,9 @@ public int add(Propagator<?> propagator, int idxInVar) {
/**
* Remove the propagator <i>p</i> from {@link #propagators}.
*
* @param propagator
* @param idxInProp
* @param var
* @param propagator the propagator to remove
* @param idxInProp the index of the variable in the propagator
* @param var the variable (for assertions only)
*/
public void remove(Propagator<?> propagator, int idxInProp, final AbstractVariable var) {
int p = propagator.getVIndice(idxInProp);
Expand Down Expand Up @@ -640,6 +658,7 @@ Stream<Propagator<?>> stream() {
shiftTail();
}
}
//noinspection Convert2Diamond
Spliterator<Propagator<?>> it = new Spliterator<Propagator<?>>() {
int i = s;
@Override
Expand Down