/*
* 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.nValue;
import gnu.trove.list.array.TIntArrayList;
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 util.ESat;
import util.tools.ArrayUtils;
/**
* Propagator for the atMostNValues constraint
* The number of distinct values in the set of variables vars is at most equal to nValues
* No level of consistency but better than BC in general (for enumerated domains with holes)
*
* @author Jean-Guillaume Fages
*/
public class PropAtLeastNValues extends Propagator<IntVar> {
//***********************************************************************************
// VARIABLES
//***********************************************************************************
private TIntArrayList concernedValues;
private int n;
private int[] mate;
private boolean allEnum; // all variables are enumerated
//***********************************************************************************
// CONSTRUCTORS
//***********************************************************************************
/**
* Propagator for the NValues constraint
* The number of distinct values among concerned values in the set of variables vars is exactly equal to nValues
* No level of consistency for the filtering
*
* @param variables
* @param concernedValues will be sorted!
* @param nValues
*/
public PropAtLeastNValues(IntVar[] variables, TIntArrayList concernedValues, IntVar nValues) {
super(ArrayUtils.append(variables, new IntVar[]{nValues}), PropagatorPriority.QUADRATIC, false);
n = variables.length;
concernedValues.sort();
this.concernedValues = concernedValues;
mate = new int[concernedValues.size()];
allEnum = true;
for (int i = 0; i < n && allEnum; i++) {
allEnum &= vars[i].hasEnumeratedDomain();
}
}
//***********************************************************************************
// PROPAGATION
//***********************************************************************************
@Override
public void propagate(int evtmask) throws ContradictionException {
vars[n].updateUpperBound(n, aCause);
int count = 0;
int countMax = 0;
for (int i = concernedValues.size() - 1; i >= 0; i--) {
boolean possible = false;
boolean mandatory = false;
mate[i] = -1;
int value = concernedValues.get(i);
for (int v = 0; v < n; v++) {
if (vars[v].contains(value)) {
possible = true;
if (mate[i] == -1) {
mate[i] = v;
} else {
mate[i] = -2;
if (mandatory) {
break;
}
}
if (vars[v].isInstantiated()) {
mandatory = true;
if (mate[i] == -2) {
break;
}
}
}
}
if (possible) {
countMax++;
}
if (mandatory) {
count++;
}
}
// filtering cardinality variable
vars[n].updateUpperBound(countMax, aCause);
// filtering decision variables
if (count != countMax && countMax == vars[n].getLB()) {
for (int i = concernedValues.size() - 1; i >= 0; i--) {
if (mate[i] >= 0) {
vars[mate[i]].instantiateTo(concernedValues.get(i), aCause);
}
}
}
if(count>=vars[n].getUB()){
setPassive();
}
}
//***********************************************************************************
// INFO
//***********************************************************************************
@Override
public ESat isEntailed() {
int countMin = 0;
int countMax = 0;
for (int i = 0; i < concernedValues.size(); i++) {
boolean possible = false;
boolean mandatory = false;
for (int v = 0; v < n; v++) {
if (vars[v].contains(concernedValues.get(i))) {
possible = true;
if (vars[v].isInstantiated()) {
mandatory = true;
break;
}
}
}
if (possible) {
countMax++;
}
if (mandatory) {
countMin++;
}
}
if (countMin >= vars[n].getUB()) {
return ESat.TRUE;
}
if (countMax < vars[n].getLB()) {
return ESat.FALSE;
}
return ESat.UNDEFINED;
}
@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 PropAtLeastNValues(aVars, this.concernedValues, aVar));
}
}
}