All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.chocosolver.solver.constraints.nary.nvalue.PropNValue Maven / Gradle / Ivy

There is a newer version: 4.10.17
Show newest version
/*
 * This file is part of choco-solver, http://choco-solver.org/
 *
 * Copyright (c) 2020, IMT Atlantique. All rights reserved.
 *
 * Licensed under the BSD 4-clause license.
 *
 * See LICENSE file in the project root for full license information.
 */
package org.chocosolver.solver.constraints.nary.nvalue;

import gnu.trove.list.array.TIntArrayList;
import gnu.trove.set.hash.TIntHashSet;
import org.chocosolver.solver.constraints.Propagator;
import org.chocosolver.solver.constraints.PropagatorPriority;
import org.chocosolver.solver.exception.ContradictionException;
import org.chocosolver.solver.variables.IntVar;
import org.chocosolver.solver.variables.events.IntEventType;
import org.chocosolver.solver.variables.events.PropagatorEventType;
import org.chocosolver.util.ESat;
import org.chocosolver.util.objects.setDataStructures.ISet;
import org.chocosolver.util.objects.setDataStructures.SetFactory;
import org.chocosolver.util.objects.setDataStructures.SetType;
import org.chocosolver.util.tools.ArrayUtils;

import java.util.Arrays;
import java.util.Random;

/**
 * @author Arthur Godet 
 * @since 13/03/2020
*/
public class PropNValue extends Propagator {
    private IntVar nValue;
    private final int n;
    private int[] concernedValues;
    private int[] witness;
    private ISet mandatoryValues;
    private ISet possibleValues;
    private TIntArrayList listForRandomPick;
    private Random rnd = new Random(vars[0].getModel().getSeed());

    public PropNValue(IntVar[] vars, IntVar nvalue) {
        super(ArrayUtils.concat(vars, nvalue), PropagatorPriority.LINEAR, false);
        this.nValue = nvalue;
        n = vars.length;
        TIntHashSet set = new TIntHashSet();
        int min = Integer.MAX_VALUE;
        for(int i = 0; i nValue.getValue()) {
            fails();
        } else if(nValue.isInstantiated() && nValue.getValue() == concernedValues.length) {
            boolean hasFiltered;
            do {
                hasFiltered = false;
                for(int i = 0; i < n; i++) {
                    if(vars[i].isInstantiated()) {
                        for(int j = 0; j < n; j++) {
                            if(i != j) {
                                hasFiltered |= vars[j].removeValue(vars[i].getValue(), this);
                            }
                        }
                    }
                }
            } while(hasFiltered);
        }
    }

    @Override
    public ESat isEntailed() {
        int countMin = 0;
        int countMax = 0;
        for (int i = 0; i < concernedValues.length; i++) {
            boolean possible = false;
            boolean mandatory = false;
            for (int v = 0; v < n; v++) {
                if (vars[v].contains(concernedValues[i])) {
                    possible = true;
                    if (vars[v].isInstantiated()) {
                        mandatory = true;
                        break;
                    }
                }
            }
            if (possible) {
                countMax++;
            }
            if (mandatory) {
                countMin++;
            }
        }
        if (countMin > vars[n].getUB() || countMax < vars[n].getLB()) {
            return ESat.FALSE;
        } else if (isCompletelyInstantiated() && countMin == nValue.getValue()) {
            return ESat.TRUE;
        }
        return ESat.UNDEFINED;
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy