001package org.cpsolver.ifs.example.csp;
002
003import java.util.ArrayList;
004import java.util.List;
005import java.util.Random;
006
007import org.cpsolver.ifs.model.Variable;
008
009
010/**
011 * CSP variable. <br>
012 * <br>
013 * This class only implements generation of variable's values (domain)
014 * 
015 * @author  Tomáš Müller
016 * @version IFS 1.3 (Iterative Forward Search)<br>
017 *          Copyright (C) 2006 - 2014 Tomáš Müller<br>
018 *          <a href="mailto:muller@unitime.org">muller@unitime.org</a><br>
019 *          <a href="http://muller.unitime.org">http://muller.unitime.org</a><br>
020 * <br>
021 *          This library is free software; you can redistribute it and/or modify
022 *          it under the terms of the GNU Lesser General Public License as
023 *          published by the Free Software Foundation; either version 3 of the
024 *          License, or (at your option) any later version. <br>
025 * <br>
026 *          This library is distributed in the hope that it will be useful, but
027 *          WITHOUT ANY WARRANTY; without even the implied warranty of
028 *          MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
029 *          Lesser General Public License for more details. <br>
030 * <br>
031 *          You should have received a copy of the GNU Lesser General Public
032 *          License along with this library; if not see
033 *          <a href='http://www.gnu.org/licenses/'>http://www.gnu.org/licenses/</a>.
034 */
035public class CSPVariable extends Variable<CSPVariable, CSPValue> {
036    private int iKernelId = -1;
037
038    /**
039     * Constructor
040     * @param id variable id
041     * @param domainSize
042     *            number of values of the variable
043     */
044    public CSPVariable(int id, int domainSize) {
045        this(id, domainSize, -1);
046    }
047
048    /**
049     * Constructor
050     * 
051     * @param id variable id
052     * @param domainSize
053     *            number of values of the variable
054     * @param kernelId
055     *            kernel id (for structured CSP)
056     */
057    public CSPVariable(int id, int domainSize, int kernelId) {
058        super(null);
059        iId = id;
060        iKernelId = kernelId;
061        setValues(computeValues(domainSize));
062    }
063
064    /** Get kernel id
065     * @return kernel id
066     **/
067    public int getKernelId() {
068        return iKernelId;
069    }
070
071    /**
072     * Generate an intial value (for MPP and for forcing of existance of a
073     * solution)
074     * @param rnd random number generator
075     */
076    public void generateInitialValue(Random rnd) {
077        CSPValue aValue = values(null).get((int) (rnd.nextFloat() * values(null).size()));
078        setInitialAssignment(aValue);
079    }
080
081    private List<CSPValue> computeValues(int domainSize) {
082        List<CSPValue> values = new ArrayList<CSPValue>();
083        for (int i = 0; i < domainSize; i++) {
084            CSPValue value = new CSPValue(this, i);
085            values.add(value);
086        }
087        return values;
088    }
089
090    @Override
091    public String getName() {
092        return "V" + getId();
093    }
094}