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