Package solver.constraints.extension.nary

Source Code of solver.constraints.extension.nary.PropLargeGAC3rm

/**
*  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.extension.nary;

import gnu.trove.map.hash.THashMap;
import solver.Solver;
import solver.constraints.extension.Tuples;
import solver.exception.ContradictionException;
import solver.exception.SolverException;
import solver.variables.IntVar;
import solver.variables.events.PropagatorEventType;
import util.iterators.DisposableValueIterator;

import java.util.Arrays;

/**
* <br/>
*
* @author Charles Prud'homme, Hadrien Cambazard
* @since 24/04/2014
*/
public class PropLargeGAC3rm extends PropLargeCSP<LargeRelation> {

    // Last valid supports Last(x_i, val) = supports( (blocks(i) + val) * size )
    protected int[] supports;

    protected int[] blocks;

    // Cardinality
    protected int size;

    // offsets(i) = Min(x_i)
    protected int[] offsets;

    protected DisposableValueIterator[] seekIter;


    private PropLargeGAC3rm(IntVar[] vs, LargeRelation relation) {
        super(vs, relation);
        this.size = vs.length;
        this.blocks = new int[size];
        this.offsets = new int[size];

        int nbElt = 0;

        for (int i = 0; i < size; i++) {
            offsets[i] = vs[i].getLB();
            blocks[i] = nbElt;
            if (!vars[i].hasEnumeratedDomain()) {
                nbElt += 2;
            } else nbElt += vars[i].getUB() - vars[i].getLB() + 1;
        }

        this.supports = new int[nbElt * size];
        this.seekIter = new DisposableValueIterator[size];
        for (int i = 0; i < size; i++) {
            seekIter[i] = vars[i].getValueIterator(true);
        }
        Arrays.fill(supports, Integer.MIN_VALUE);
    }

    public PropLargeGAC3rm(IntVar[] vs, Tuples tuples) {
        this(vs, makeRelation(tuples, vs));
    }

    private static LargeRelation makeRelation(Tuples tuples, IntVar[] vars) {
        long totalSize = 1;
        for (int i = 0; i < vars.length && totalSize > 0; i++) { // to prevent from long overflow
            totalSize *= vars[i].getDomainSize();
        }
        if (totalSize < 0) {
            throw new SolverException("Tuples required too much memory ...");
        }
        if (totalSize / 8 > 50 * 1024 * 1024) {
            return new TuplesLargeTable(tuples, vars);
        }
        return new TuplesTable(tuples, vars);
    }

    @Override
    public void propagate(int evtmask) throws ContradictionException {
        if ((evtmask & PropagatorEventType.FULL_PROPAGATION.getMask()) != 0) {
            for (int i = 0; i < vars.length; i++) {
                initializeSupports(i);
            }
        }

        for (int i = 0; i < size; i++)
            reviseVar(i);
    }


    @Override
    public void propagate(int idxVarInProp, int mask) throws ContradictionException {
        for (int i = 0; i < size; i++)
            if (idxVarInProp != i) reviseVar(i);
        if (!vars[idxVarInProp].hasEnumeratedDomain()) {
            reviseVar(idxVarInProp);
        }
    }

    ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
    ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
    ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

    /**
     * initialize the supports of each value of indexVar
     *
     * @throws ContradictionException
     */
    public void initializeSupports(int indexVar) throws ContradictionException {
        int[] currentSupport;
        int val;
        if (vars[indexVar].hasEnumeratedDomain()) {
            DisposableValueIterator it = vars[indexVar].getValueIterator(true);
            int left = Integer.MIN_VALUE;
            int right = left;
            try {
                while (it.hasNext()) {
                    val = it.next();
                    if (lastSupport(indexVar, val)[0] == Integer.MIN_VALUE) { // no supports initialized yet for this value
                        currentSupport = seekNextSupport(indexVar, val);
                        if (currentSupport != null) {
                            setSupport(currentSupport);
                        } else {
                            if (val == right + 1) {
                                right = val;
                            } else {
                                vars[indexVar].removeInterval(left, right, this);
                                left = right = val;
                            }
                            //                        vars[indexVar].removeVal(val, this, false);
                        }
                    }
                }
                vars[indexVar].removeInterval(left, right, this);
            } finally {
                it.dispose();
            }
        } else {
            for (val = vars[indexVar].getLB(); val <= vars[indexVar].getUB(); val++) {
                currentSupport = seekNextSupport(indexVar, val);
                if (currentSupport != null) {
                    setBoundSupport(indexVar, 0, currentSupport);
                    break; //stop at the first consistent lower bound !
                }
            }
            vars[indexVar].updateLowerBound(val, this);
            for (val = vars[indexVar].getUB(); val >= vars[indexVar].getLB(); val--) {
                currentSupport = seekNextSupport(indexVar, val);
                if (currentSupport != null) {
                    setBoundSupport(indexVar, 1, currentSupport);
                    break; //stop at the first consistent upper bound !
                }
            }
            vars[indexVar].updateUpperBound(val, this);
        }
    }

    // updates the support for all values in the domain of variable
    // and remove unsupported values for variable
    public void reviseVar(int indexVar) throws ContradictionException {
        int[] currentSupport;
        int val;
        if (vars[indexVar].hasEnumeratedDomain()) {
            DisposableValueIterator it = vars[indexVar].getValueIterator(true);
            int left = Integer.MIN_VALUE;
            int right = left;
            try {
                while (it.hasNext()) {
                    val = it.next();
                    if (!isValid(lastSupport(indexVar, val))) {
                        currentSupport = seekNextSupport(indexVar, val);
                        if (currentSupport != null) {
                            setSupport(currentSupport);
                        } else {
                            if (val == right + 1) {
                                right = val;
                            } else {
                                vars[indexVar].removeInterval(left, right, this);
                                left = right = val;
                            }
                            //                            vars[indexVar].removeVal(val, this, false);
                        }
                    }
                }
                vars[indexVar].removeInterval(left, right, this);
            } finally {
                it.dispose();
            }
        } else {
            int[] inf_supports = lastBoundSupport(indexVar, 0);
            if (vars[indexVar].getLB() != inf_supports[indexVar] || !isValid(inf_supports)) {
                for (val = vars[indexVar].getLB(); val <= vars[indexVar].getUB(); val++) {
                    currentSupport = seekNextSupport(indexVar, val);
                    if (currentSupport != null) {
                        setBoundSupport(indexVar, 0, currentSupport);
                        break; //stop at the first consistent lower bound !
                    }
                }
                vars[indexVar].updateLowerBound(val, this);
            }
            int[] sup_supports = lastBoundSupport(indexVar, 1);
            if (vars[indexVar].getUB() != sup_supports[indexVar] || !isValid(sup_supports)) {
                for (val = vars[indexVar].getUB(); val >= vars[indexVar].getLB(); val--) {
                    currentSupport = seekNextSupport(indexVar, val);
                    if (currentSupport != null) {
                        setBoundSupport(indexVar, 1, currentSupport);
                        break; //stop at the first consistent upper bound !
                    }
                }
                vars[indexVar].updateUpperBound(val, this);
            }
        }
    }

    // Store Last(x_i, val) = support
    public void setSupport(int[] support) {
        for (int i = 0; i < vars.length; i++) {
            if (vars[i].hasEnumeratedDomain())
                setOneSupport(i, support[i], support);
        }
    }

    public void setOneSupport(int indexVar, int value, int[] support) {
        System.arraycopy(support, 0, supports, (blocks[indexVar] + value - offsets[indexVar]) * size, vars.length);
    }


    // Store Last(x_i, val) = support
    public void setBoundSupport(int indexVar, int idxBound, int[] support) {
        System.arraycopy(support, 0, supports, (blocks[indexVar] + idxBound) * size, vars.length);
    }


    // Get Last(x_i, val)
    public int[] getUBport(int indexVar, int value) {
        int[] resultat = new int[size];
        System.arraycopy(supports, (blocks[indexVar] + value - offsets[indexVar]) * size, resultat, 0, size);
        return resultat;
    }


    // return the support standing for the lower bound
    // of indexVar if idxBound = 0 or upperbound if idxBound = 1
    public int[] getBoundSupport(int indexVar, int idxBound) {
        int[] resultat = new int[size];
        System.arraycopy(supports, (blocks[indexVar] + idxBound) * size, resultat, 0, size);
        return resultat;
    }

    // Get Last(x_i, val)
    public int[] lastSupport(int indexVar, int value) {
        return getUBport(indexVar, value);
    }

    // return the support standing for the lower bound
    // of indexVar if idxBound = 0 or upperbound if idxBound = 1
    public int[] lastBoundSupport(int indexVar, int idxBound) {
        return getBoundSupport(indexVar, idxBound);
    }


    // Is tuple valide ?
    public boolean isValid(int[] tuple) {
        for (int i = 0; i < size; i++)
            if (!vars[i].contains(tuple[i])) return false;
        return true;
    }


    // seek a new support for (variable, value), the smallest tuple greater than currentSupport
    public int[] seekNextSupport(int indexVar, int val) {
        int[] currentSupport = new int[size];
        int k = 0;
        for (int i = 0; i < size; i++) {
            seekIter[i].dispose();
            seekIter[i] = vars[i].getValueIterator(true);
            if (i != indexVar)
                currentSupport[i] = seekIter[i].next();
            else currentSupport[i] = val;
        }
        if (relation.isConsistent(currentSupport)) {
            return currentSupport;
        }

        while (k < vars.length) {
            if (k == indexVar) k++;
            if (k < vars.length) {
                if (!seekIter[k].hasNext()) {
                    seekIter[k].dispose();
                    seekIter[k] = vars[k].getValueIterator(true);
                    currentSupport[k] = seekIter[k].next();
                    k++;
                } else {
                    currentSupport[k] = seekIter[k].next();
                    if ((relation.isConsistent(currentSupport))) {
                        return currentSupport;
                    }
                    k = 0;
                }
            }
        }

        return null;
    }

    @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 PropLargeGAC3rm(aVars, relation.duplicate()));
        }
    }
}
TOP

Related Classes of solver.constraints.extension.nary.PropLargeGAC3rm

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.