/*
* 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.binary;
import gnu.trove.map.hash.THashMap;
import solver.Solver;
import solver.constraints.Propagator;
import solver.constraints.PropagatorPriority;
import solver.exception.ContradictionException;
import solver.explanations.Deduction;
import solver.explanations.Explanation;
import solver.explanations.VariableState;
import solver.variables.IntVar;
import solver.variables.Variable;
import solver.variables.events.IntEventType;
import util.ESat;
/**
* X >= Y
* <p/>
* <br/>
*
* @author Charles Prud'homme
* @since 1 oct. 2010
*/
public final class PropGreaterOrEqualX_Y extends Propagator<IntVar> {
final IntVar x;
final IntVar y;
@SuppressWarnings({"unchecked"})
public PropGreaterOrEqualX_Y(IntVar[] vars) {
super(vars, PropagatorPriority.BINARY, true);
this.x = vars[0];
this.y = vars[1];
}
@Override
public int getPropagationConditions(int vIdx) {
if (vIdx == 0) {
return IntEventType.INSTANTIATE.getMask() + IntEventType.DECUPP.getMask();
} else {
return IntEventType.INSTANTIATE.getMask() + IntEventType.INCLOW.getMask();
}
}
@Override
public void propagate(int evtmask) throws ContradictionException {
x.updateLowerBound(y.getLB(), aCause);
y.updateUpperBound(x.getUB(), aCause);
if (x.getLB() >= y.getUB()) {
this.setPassive();
}
}
@Override
public void propagate(int varIdx, int mask) throws ContradictionException {
if (varIdx == 0) {
y.updateUpperBound(x.getUB(), aCause);
} else {
x.updateLowerBound(y.getLB(), aCause);
}
if (x.getLB() >= y.getUB()) {
this.setPassive();
}
}
@Override
public ESat isEntailed() {
if (x.getUB() < y.getLB())
return ESat.FALSE;
else if (x.getLB() >= y.getUB())
return ESat.TRUE;
else
return ESat.UNDEFINED;
}
@Override
public String toString() {
StringBuilder bf = new StringBuilder();
bf.append("prop(").append(vars[0].getName()).append(".GEQ.").append(vars[1].getName()).append(")");
return bf.toString();
}
@Override
public void explain(Deduction d, Explanation e) {
e.add(solver.getExplainer().getPropagatorActivation(this));
e.add(aCause);
// the current deduction is due to the current domain of the involved variables
Variable var = d.getVar();
if (var.equals(x)) {
// a deduction has been made on x ; this is related to y only
y.explain(VariableState.LB, e);
} else if (var.equals(y)) {
x.explain(VariableState.UB, e);
} else {
super.explain(d, e);
}
}
@Override
public void duplicate(Solver solver, THashMap<Object, Object> identitymap) {
if (!identitymap.containsKey(this)) {
this.vars[0].duplicate(solver, identitymap);
IntVar X = (IntVar) identitymap.get(this.vars[0]);
this.vars[1].duplicate(solver, identitymap);
IntVar Y = (IntVar) identitymap.get(this.vars[1]);
identitymap.put(this, new PropGreaterOrEqualX_Y(new IntVar[]{X, Y}));
}
}
}