Package solver.constraints.nary.alldifferent.conditions

Source Code of solver.constraints.nary.alldifferent.conditions.PropCondAllDiff_AC

/*
* 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.alldifferent.conditions;

import gnu.trove.map.hash.THashMap;
import solver.Solver;
import solver.constraints.Propagator;
import solver.constraints.PropagatorPriority;
import solver.constraints.nary.alldifferent.algo.AlgoAllDiffAC;
import solver.exception.ContradictionException;
import solver.variables.IntVar;
import solver.variables.events.PropagatorEventType;
import util.ESat;

/**
* Propagator for AllDifferent AC constraint for integer variables
* subject to conditions (e.g. allDifferent_except_0)
* AllDiff only applies on the subset of variables satisfying the given condition
*
* @author Jean-Guillaume Fages
*/
public class PropCondAllDiff_AC extends Propagator<IntVar> {

    private Condition condition;
    private AlgoAllDiffAC filter;

    //***********************************************************************************
    // CONSTRUCTORS
    //***********************************************************************************

    /**
     * AllDifferent constraint for integer variables
     * Holds only on the subset of variables satisfying the given condition
     *
     * @param variables
     * @param condition defines the subset of variables which is considered by the
     *                  alldifferent constraint
     */
    public PropCondAllDiff_AC(IntVar[] variables, Condition condition) {
        super(variables, PropagatorPriority.QUADRATIC, false);
        this.condition = condition;
    }

    //***********************************************************************************
    // PROPAGATION
    //***********************************************************************************

    @Override
    public void propagate(int evtmask) throws ContradictionException {
        int nb = 0;
        for (IntVar v : vars) {
            if (condition.holdOnVar(v)) {
                nb++;
            }
        }
        IntVar[] vs = new IntVar[nb];
        for (IntVar v : vars) {
            if (condition.holdOnVar(v)) {
                nb--;
                vs[nb] = v;
            }
        }
        filter = new AlgoAllDiffAC(vs, aCause);
        filter.propagate();
    }

    @Override
    public void propagate(int varIdx, int mask) throws ContradictionException {
        forcePropagate(PropagatorEventType.CUSTOM_PROPAGATION);
    }

    //***********************************************************************************
    // INFO
    //***********************************************************************************
    @Override
    public ESat isEntailed() {
        int nbInst = 0;
        for (int i = 0; i < vars.length; i++) {
            if (vars[i].isInstantiated()) {
                nbInst++;
                if (condition.holdOnVar(vars[i])) {
                    for (int j = i + 1; j < vars.length; j++) {
                        if (condition.holdOnVar(vars[j]))
                            if (vars[j].isInstantiated() && vars[i].getValue() == vars[j].getValue()) {
                                return ESat.FALSE;
                            }
                    }
                }
            }
        }
        if (nbInst == vars.length) {
            return ESat.TRUE;
        }
        return ESat.UNDEFINED;
    }

    @Override
    public void duplicate(Solver solver, THashMap<Object, Object> identitymap) {
        if (!identitymap.containsKey(this)) {
            int size = this.vars.length;
            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]);
            }
            identitymap.put(this, new PropCondAllDiff_AC(aVars, this.condition));
        }
    }
}
TOP

Related Classes of solver.constraints.nary.alldifferent.conditions.PropCondAllDiff_AC

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.