/*
* Copyright (c) 1999-2014, 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:
*
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* * 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.
* * Neither the name of the Ecole des Mines de Nantes 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 THE REGENTS AND CONTRIBUTORS ``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 THE REGENTS AND CONTRIBUTORS 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 solver.constraints.nary.count;
import gnu.trove.map.hash.THashMap;
import solver.Solver;
import solver.constraints.Propagator;
import solver.constraints.PropagatorPriority;
import solver.exception.ContradictionException;
import solver.variables.IntVar;
import solver.variables.events.IntEventType;
import solver.variables.events.PropagatorEventType;
import util.ESat;
import util.objects.setDataStructures.ISet;
import util.objects.setDataStructures.SetFactory;
import util.objects.setDataStructures.SetType;
import util.tools.ArrayUtils;
/**
* Define a COUNT constraint setting size{forall v in lvars | v = occval} = occVar
* assumes the occVar variable to be the last of the variables of the constraint:
* vars = [lvars | occVar]
* Arc Consistent algorithm
* with lvars = list of variables for which the occurrence of occval in their domain is constrained
* <br/>
*
* @author Jean-Guillaume Fages
*/
public class PropCount_AC extends Propagator<IntVar> {
//***********************************************************************************
// VARIABLES
//***********************************************************************************
private int n;
private int value;
private ISet possibles, mandatories;
//***********************************************************************************
// CONSTRUCTORS
//***********************************************************************************
/**
* Propagator for Count Constraint for integer variables
* Performs Arc Consistency
*
* @param decvars
* @param restrictedValue
* @param valueCardinality
*/
public PropCount_AC(IntVar[] decvars, int restrictedValue, IntVar valueCardinality) {
super(ArrayUtils.append(decvars, new IntVar[]{valueCardinality}), PropagatorPriority.LINEAR, true);
this.value = restrictedValue;
this.n = decvars.length;
this.possibles = SetFactory.makeStoredSet(SetType.BITSET, n, solver);
this.mandatories = SetFactory.makeStoredSet(SetType.BITSET, n, solver);
}
@Override
public String toString() {
StringBuilder st = new StringBuilder();
st.append("PropFastCount_(");
int i = 0;
for (; i < Math.min(4, vars.length); i++) {
st.append(vars[i].getName()).append(", ");
}
if (i < vars.length - 2) {
st.append("...,");
}
st.append(vars[vars.length - 1].getName()).append(")");
return st.toString();
}
//***********************************************************************************
// PROPAGATION
//***********************************************************************************
@Override
public void propagate(int evtmask) throws ContradictionException {
if (PropagatorEventType.isFullPropagation(evtmask)) {// initialization
mandatories.clear();
possibles.clear();
for (int i = 0; i < n; i++) {
IntVar v = vars[i];
int ub = v.getUB();
if (v.isInstantiated()) {
if (ub == value) {
mandatories.add(i);
}
} else {
if (v.contains(value)) {
possibles.add(i);
}
}
}
}
filter();
}
@Override
public void propagate(int varIdx, int mask) throws ContradictionException {
if (varIdx < n) {
if (possibles.contain(varIdx)) {
if (!vars[varIdx].contains(value)) {
possibles.remove(varIdx);
filter();
} else if (vars[varIdx].isInstantiated()) {
possibles.remove(varIdx);
mandatories.add(varIdx);
filter();
}
}
} else {
filter();
}
}
private void filter() throws ContradictionException {
vars[n].updateLowerBound(mandatories.getSize(), aCause);
vars[n].updateUpperBound(mandatories.getSize() + possibles.getSize(), aCause);
if (vars[n].isInstantiated()) {
int nb = vars[n].getValue();
if (possibles.getSize() + mandatories.getSize() == nb) {
for (int j = possibles.getFirstElement(); j >= 0; j = possibles.getNextElement()) {
vars[j].instantiateTo(value, aCause);
}
setPassive();
} else if (mandatories.getSize() == nb) {
for (int j = possibles.getFirstElement(); j >= 0; j = possibles.getNextElement()) {
if (vars[j].removeValue(value, aCause)) {
possibles.remove(j);
}
}
if (possibles.isEmpty()) {
setPassive();
}
}
}
}
//***********************************************************************************
// INFO
//***********************************************************************************
@Override
public int getPropagationConditions(int vIdx) {
if (vIdx >= n) {// cardinality variables
return IntEventType.boundAndInst();
}
return IntEventType.all();
}
@Override
public ESat isEntailed() {
int min = 0;
int max = 0;
IntVar v;
for (int i = 0; i < n; i++) {
v = vars[i];
if (v.isInstantiatedTo(value)) {
min++;
max++;
} else {
if (v.contains(value)) {
max++;
}
}
}
if (vars[n].getLB() > max || vars[n].getUB() < min) {
return ESat.FALSE;
}
if (!(vars[n].isInstantiated() && max == min)) {
return ESat.UNDEFINED;
}
return ESat.TRUE;
}
@Override
public void duplicate(Solver solver, THashMap<Object, Object> identitymap) {
if (!identitymap.containsKey(this)) {
int size = this.vars.length - 1;
IntVar[] aVars = new IntVar[size];
for (int i = 0; i < size; i++) {
this.vars[i].duplicate(solver, identitymap);
aVars[i] = (IntVar) identitymap.get(this.vars[i]);
}
this.vars[size].duplicate(solver, identitymap);
IntVar aVar = (IntVar) identitymap.get(this.vars[size]);
identitymap.put(this, new PropCount_AC(aVars, this.value, aVar));
}
}
}