001    package net.sf.cpsolver.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     * @version
011     * IFS 1.1 (Iterative Forward Search)<br>
012     * Copyright (C) 2006 Tomáš Müller<br>
013     * <a href="mailto:muller@unitime.org">muller@unitime.org</a><br>
014     * Lazenska 391, 76314 Zlin, Czech Republic<br>
015     * <br>
016     * This library is free software; you can redistribute it and/or
017     * modify it under the terms of the GNU Lesser General Public
018     * License as published by the Free Software Foundation; either
019     * version 2.1 of the License, or (at your option) any later version.
020     * <br><br>
021     * This library is distributed in the hope that it will be useful,
022     * but WITHOUT ANY WARRANTY; without even the implied warranty of
023     * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
024     * Lesser General Public License for more details.
025     * <br><br>
026     * You should have received a copy of the GNU Lesser General Public
027     * License along with this library; if not, write to the Free Software
028     * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
029     */
030    public class CSPVariable extends net.sf.cpsolver.ifs.model.Variable {
031        private int iKernelId=-1;
032        
033        /**
034         * Constructor
035         * @param domainSize number of values of the variable
036         */
037        public CSPVariable(int id, int domainSize) {
038            this(id, domainSize, -1);
039        }
040    
041        /**
042         * Constructor
043         * @param domainSize number of values of the variable
044         * @param kernelId kernel id (for structured CSP)
045         */
046        public CSPVariable(int id, int domainSize, int kernelId) {
047            super(null);
048            iId = id;
049            iKernelId = kernelId;
050            setValues(computeValues(domainSize));
051        }
052        
053        /** Get kernel id */
054        public int getKernelId() { return iKernelId; }
055        
056        /** Generate an intial value (for MPP and for forcing of existance of a solution) */
057        public void generateInitialValue(Random rnd) {
058            CSPValue aValue = (CSPValue)values().elementAt((int)(rnd.nextFloat()*values().size()));
059            setInitialAssignment(aValue);
060        }
061        
062        private Vector computeValues(int domainSize) {
063            Vector values = new Vector();
064            for (int i=0; i< domainSize; i++) {
065                CSPValue value = new CSPValue(this, i);
066                values.addElement(value);
067            }
068            return values;
069        }
070        
071        public String getName() { return "V"+getId(); }
072    }