Package solver.constraints.set

Source Code of solver.constraints.set.PropElement

/**
*  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.
*/

/**
* Created by IntelliJ IDEA.
* User: Jean-Guillaume Fages
* Date: 14/01/13
* Time: 16:36
*/

package solver.constraints.set;

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 solver.variables.SetVar;
import solver.variables.Variable;
import util.ESat;
import util.tools.ArrayUtils;

/**
* Propagator for element constraint over sets
* states that
* array[index-offSet] = set
*
* @author Jean-Guillaume Fages
*/
public class PropElement extends Propagator<Variable> {

    //***********************************************************************************
    // VARIABLES
    //***********************************************************************************

    private TIntArrayList constructiveDisjunction;
    private IntVar index;
    private SetVar set;
    private SetVar[] array;
    private int offSet;

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

    /**
     * Propagator for element constraint over sets
     * states that array[index-offSet] = set
     *
     * @param index
     * @param array
     * @param offSet
     * @param set
     */
    public PropElement(IntVar index, SetVar[] array, int offSet, SetVar set) {
        super(ArrayUtils.append(array, new Variable[]{set, index}), PropagatorPriority.LINEAR, false);
        this.index = (IntVar) vars[vars.length - 1];
        this.set = (SetVar) vars[vars.length - 2];
        this.array = new SetVar[array.length];
        for (int i = 0; i < array.length; i++) {
            this.array[i] = (SetVar) vars[i];
        }
        this.offSet = offSet;
        constructiveDisjunction = new TIntArrayList();
    }

    //***********************************************************************************
    // METHODS
    //***********************************************************************************

    @Override
    public void propagate(int evtmask) throws ContradictionException {
        index.updateLowerBound(offSet, aCause);
        index.updateUpperBound(array.length - 1 + offSet, aCause);
        if (index.isInstantiated()) {
            // filter set and array
            setEq(set, array[index.getValue() - offSet]);
            setEq(array[index.getValue() - offSet], set);
        } else {
            // filter index
            int ub = index.getUB();
            boolean noEmptyKer = true;
            for (int i = index.getLB(); i <= ub; i = index.nextValue(i)) {
                if (disjoint(set, array[i - offSet]) || disjoint(array[i - offSet], set)) {// array[i] != set
                    index.removeValue(i, aCause);
                } else {
                    if (array[i - offSet].getKernelSize() == 0) {
                        noEmptyKer = false;
                    }
                }
            }
            ub = index.getUB();
            // filter set (constructive disjunction)
            if (noEmptyKer) {// from ker
                constructiveDisjunction.clear();
                SetVar v = array[index.getLB() - offSet];
                for (int j = v.getKernelFirst(); j != SetVar.END; j = v.getKernelNext()) {
                    if (!set.kernelContains(j)) {
                        constructiveDisjunction.add(j);
                    }
                }
                for (int cd = constructiveDisjunction.size() - 1; cd >= 0; cd--) {
                    int j = constructiveDisjunction.get(cd);
                    for (int i = index.nextValue(index.getLB()); i <= ub; i = index.nextValue(i)) {
                        if (!array[i - offSet].kernelContains(j)) {
                            constructiveDisjunction.remove(j);
                            break;
                        }
                    }
                }
                for (int cd = constructiveDisjunction.size() - 1; cd >= 0; cd--) {
                    int j = constructiveDisjunction.get(cd);
                    set.addToKernel(j, aCause);
                }
            }
            if (!set.isInstantiated()) {// from env
                for (int j = set.getEnvelopeFirst(); j != SetVar.END; j = set.getEnvelopeNext()) {
                    boolean valueExists = false;
                    for (int i = index.getLB(); i <= ub; i = index.nextValue(i)) {
                        if (array[i - offSet].envelopeContains(j)) {
                            valueExists = true;
                            break;
                        }
                    }
                    if (!valueExists) {
                        set.removeFromEnvelope(j, aCause);
                    }
                }
            }
        }
    }

    private void setEq(SetVar s1, SetVar s2) throws ContradictionException {
        for (int j = s2.getKernelFirst(); j != SetVar.END; j = s2.getKernelNext()) {
            s1.addToKernel(j, aCause);
        }
        for (int j = s1.getEnvelopeFirst(); j != SetVar.END; j = s1.getEnvelopeNext()) {
            if (!s2.envelopeContains(j)) {
                s1.removeFromEnvelope(j, aCause);
            }
        }
    }

    private boolean disjoint(SetVar s1, SetVar s2) {
        for (int j = s2.getKernelFirst(); j != SetVar.END; j = s2.getKernelNext()) {
            if (!s1.envelopeContains(j)) {
                return true;
            }
        }
        return false;
    }

    @Override
    public ESat isEntailed() {
        if (index.isInstantiated()) {
            if (disjoint(set, array[index.getValue() - offSet]) || disjoint(array[index.getValue() - offSet], set)) {
                return ESat.FALSE;
            } else {
                if (set.isInstantiated() && array[index.getValue() - offSet].isInstantiated()) {
                    return ESat.TRUE;
                } else {
                    return ESat.UNDEFINED;
                }
            }
        }
        return ESat.UNDEFINED;
    }

    @Override
    public void duplicate(Solver solver, THashMap<Object, Object> identitymap) {
        if (!identitymap.containsKey(this)) {
            int size = this.vars.length - 2;
            SetVar[] aVars = new SetVar[size];
            for (int i = 0; i < size; i++) {
                this.vars[i].duplicate(solver, identitymap);
                aVars[i] = (SetVar) identitymap.get(this.vars[i]);
            }
            set.duplicate(solver, identitymap);
            SetVar S = (SetVar) identitymap.get(set);

            index.duplicate(solver, identitymap);
            IntVar I = (IntVar) identitymap.get(index);

            identitymap.put(this, new PropElement(I, aVars, offSet, S));
        }
    }
}
TOP

Related Classes of solver.constraints.set.PropElement

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.