    001    package ifs.example.csp;
    002
    003    import java.util.*;
    004
    005    /**
    006     * CSP variable.
    007     * <br><br>
    008     * This class only implements generation of variable's values (domain)
    009     *
    010     * @author <a href="mailto:muller@ktiml.mff.cuni.cz">Tomáš Müller</a>
    011     * @version 1.0
    012     */
    013    public class CSPVariable extends ifs.model.Variable {
    014        private int iKernelId=-1;
    015        private int iId = 0;
    016
    017        /**
    018         * Constructor
    019         * @param domainSize number of values of the variable
    020         */
    021        public CSPVariable(int id, int domainSize) {
    022            this(id, domainSize, -1);
    023        }
    024
    025        /**
    026         * Constructor
    027         * @param domainSize number of values of the variable
    028         * @param kernelId kernel id (for structured CSP)
    029         */
    030        public CSPVariable(int id, int domainSize, int kernelId) {
    031            super(null);
    032            iId = id;
    033            iKernelId = kernelId;
    034            setValues(computeValues(domainSize));
    035        }
    036
    037        /** Get kernel id */
    038        public int getKernelId() { return iKernelId; }
    039
    040        /** Generate an intial value (for MPP and for forcing of existance of a solution) */
    041        public void generateInitialValue(Random rnd) {
    042            CSPValue aValue = (CSPValue)values().elementAt((int)(rnd.nextFloat()*values().size()));
    043            setInitialAssignment(aValue);
    044        }
    045
    046        private Vector computeValues(int domainSize) {
    047            Vector values = new Vector();
    048            for (int i=0; i< domainSize; i++) {
    049                CSPValue value = new CSPValue(this, i);
    050                values.addElement(value);
    051            }
    052            return values;
    053        }
    054
    055        public String getName() { return "V"+getId(); }
    056    }