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