    001package org.cpsolver.ifs.example.csp;
    002
    003import java.util.Iterator;
    004import java.util.Random;
    005
    006import org.cpsolver.ifs.model.Constraint;
    007import org.cpsolver.ifs.model.Model;
    008
    009
    010/**
    011 * Random Binary CSP with uniform distribution. <br>
    012 * <br>
    013 * A random CSP is defined by a four-tuple (n, d, p1, p2), where n denotes the
    014 * number of variables and d denotes the domain size of each variable, p1 and p2
    015 * are two probabilities. They are used to generate randomly the binary
    016 * constraints among the variables. p1 represents the probability that a
    017 * constraint exists between two different variables and p2 represents the
    018 * probability that a pair of values in the domains of two variables connected
    019 * by a constraint are incompatible. <br>
    020 * <br>
    021 * We use a so called model B of Random CSP (n, d, n1, n2) where n1 =
    022 * p1*n*(n-1)/2 pairs of variables are randomly and uniformly selected and
    023 * binary constraints are posted between them. For each constraint, n2 = p1*d^2
    024 * randomly and uniformly selected pairs of values are picked as incompatible.
    025 *
    026 * @version IFS 1.3 (Iterative Forward Search)<br>
    027 *          Copyright (C) 2006 - 2014 Tomáš Müller<br>
    028 *          <a href="mailto:muller@unitime.org">muller@unitime.org</a><br>
    029 *          <a href="http://muller.unitime.org">http://muller.unitime.org</a><br>
    030 * <br>
    031 *          This library is free software; you can redistribute it and/or modify
    032 *          it under the terms of the GNU Lesser General Public License as
    033 *          published by the Free Software Foundation; either version 3 of the
    034 *          License, or (at your option) any later version. <br>
    035 * <br>
    036 *          This library is distributed in the hope that it will be useful, but
    037 *          WITHOUT ANY WARRANTY; without even the implied warranty of
    038 *          MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
    039 *          Lesser General Public License for more details. <br>
    040 * <br>
    041 *          You should have received a copy of the GNU Lesser General Public
    042 *          License along with this library; if not see
    043 *          <a href='http://www.gnu.org/licenses/'>http://www.gnu.org/licenses/</a>.
    044 */
    045public class CSPModel extends Model<CSPVariable, CSPValue> {
    046
    047    /**
    048     * Constructor
    049     *
    050     * @param nrVariables
    051     *            number of variables in the problem
    052     * @param nrValues
    053     *            number of values of each variable
    054     * @param nrConstraints
    055     *            number of constraints in the problem
    056     * @param nrCompatiblePairs
    057     *            number of compatible pairs of values for every constraint
    058     * @param seed
    059     *            seed for random number generator (use
    060     *            {@link System#currentTimeMillis} if not bother)
    061     */
    062    public CSPModel(int nrVariables, int nrValues, int nrConstraints, int nrCompatiblePairs, long seed) {
    063        generate(nrVariables, nrValues, nrConstraints, nrCompatiblePairs, seed);
    064    }
    065
    066    public CSPModel() {
    067    }
    068
    069    private void swap(CSPVariable[][] allPairs, int first, int second) {
    070        CSPVariable[] a = allPairs[first];
    071        allPairs[first] = allPairs[second];
    072        allPairs[second] = a;
    073    }
    074
    075    private void buildBinaryConstraintGraph(Random rnd) {
    076        int numberOfAllPairs = variables().size() * (variables().size() - 1) / 2;
    077        CSPVariable[][] allPairs = new CSPVariable[numberOfAllPairs][];
    078        int idx = 0;
    079        for (CSPVariable v1 : variables()) {
    080            for (CSPVariable v2 : variables()) {
    081                if (v1.getId() >= v2.getId())
    082                    continue;
    083                allPairs[idx++] = new CSPVariable[] { v1, v2 };
    084            }
    085        }
    086        idx = 0;
    087        for (Iterator<Constraint<CSPVariable, CSPValue>> i = constraints().iterator(); i.hasNext();) {
    088            CSPBinaryConstraint c = (CSPBinaryConstraint) i.next();
    089            swap(allPairs, idx, idx + (int) (rnd.nextDouble() * (numberOfAllPairs - idx)));
    090            c.addVariable(allPairs[idx][0]);
    091            c.addVariable(allPairs[idx][1]);
    092            c.init(rnd);
    093            idx++;
    094        }
    095    }
    096
    097    private void generate(int nrVariables, int nrValues, int nrConstraints, int nrCompatiblePairs, long seed) {
    098        Random rnd = new Random(seed);
    099
    100        for (int i = 0; i < nrVariables; i++) {
    101            CSPVariable var = new CSPVariable(i + 1, nrValues);
    102            addVariable(var);
    103        }
    104
    105        for (int i = 0; i < nrConstraints; i++) {
    106            CSPBinaryConstraint c = new CSPBinaryConstraint(i + 1, nrCompatiblePairs);
    107            addConstraint(c);
    108        }
    109
    110        buildBinaryConstraintGraph(rnd);
    111    }
    112}