org.chocosolver.solver.constraints.extension.binary.PropBinFC Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of choco-solver Show documentation
Show all versions of choco-solver Show documentation
Open-source constraint solver.
/**
* Copyright (c) 2016, Ecole des Mines de Nantes
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. All advertising materials mentioning features or use of this software
* must display the following acknowledgement:
* This product includes software developed by the .
* 4. Neither the name of the nor the
* names of its contributors may be used to endorse or promote products
* derived from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY ''AS IS'' AND ANY
* EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL BE LIABLE FOR ANY
* DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
package org.chocosolver.solver.constraints.extension.binary;
import org.chocosolver.solver.constraints.extension.Tuples;
import org.chocosolver.solver.exception.ContradictionException;
import org.chocosolver.solver.variables.IntVar;
import org.chocosolver.solver.variables.events.IntEventType;
import org.chocosolver.util.iterators.DisposableValueIterator;
import org.chocosolver.util.objects.setDataStructures.iterable.IntIterableBitSet;
/**
* Forward checking algorithm for table constraint
*
*
* @author Charles Prud'homme, Hadrien Cambazard
* @since 24/04/2014
*/
public class PropBinFC extends PropBinCSP {
private final IntIterableBitSet vrms;
public PropBinFC(IntVar x, IntVar y, Tuples tuples) {
this(x, y, new CouplesTable(tuples, x, y));
}
private PropBinFC(IntVar x, IntVar y, CouplesTable table) {
super(x, y, table);
vrms = new IntIterableBitSet();
}
@Override
public int getPropagationConditions(int vIdx) {
return IntEventType.instantiation();
}
@Override
public void propagate(int evtmask) throws ContradictionException {
if (v0.isInstantiated())
onInstantiation0();
if (v1.isInstantiated())
onInstantiation1();
}
@Override
public void propagate(int idxVarInProp, int mask) throws ContradictionException {
if (idxVarInProp == 0) onInstantiation0();
else onInstantiation1();
}
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
private void onInstantiation0() throws ContradictionException {
int value = v0.getValue();
DisposableValueIterator values = v1.getValueIterator(true);
vrms.clear();
vrms.setOffset(v1.getLB());
try {
while (values.hasNext()) {
int val = values.next();
if (!relation.isConsistent(value, val)) {
vrms.add(val);
}
}
v1.removeValues(vrms, this);
} finally {
values.dispose();
}
}
private void onInstantiation1() throws ContradictionException {
int value = v1.getValue();
DisposableValueIterator values = v0.getValueIterator(true);
vrms.clear();
vrms.setOffset(v0.getLB());
try {
while (values.hasNext()) {
int val = values.next();
if (!relation.isConsistent(val, value)) {
vrms.add(val);
}
}
v0.removeValues(vrms, this);
} finally {
values.dispose();
}
}
}