001 package net.sf.cpsolver.exam.heuristics; 002 003 import java.util.Enumeration; 004 import java.util.Vector; 005 006 import org.apache.log4j.Logger; 007 008 import net.sf.cpsolver.ifs.heuristics.VariableSelection; 009 import net.sf.cpsolver.ifs.model.Model; 010 import net.sf.cpsolver.ifs.model.Variable; 011 import net.sf.cpsolver.ifs.solution.Solution; 012 import net.sf.cpsolver.ifs.solver.Solver; 013 import net.sf.cpsolver.ifs.util.DataProperties; 014 import net.sf.cpsolver.ifs.util.ToolBox; 015 016 /** 017 * Unassigned variable selection. The "biggest" variable (using {@link Variable#compareTo(Object)}) 018 * unassigned variable is selected. One is selected randomly if there are more than one of 019 * such variables. 020 * 021 * @version 022 * ExamTT 1.1 (Examination Timetabling)<br> 023 * Copyright (C) 2008 Tomáš Müller<br> 024 * <a href="mailto:muller@unitime.org">muller@unitime.org</a><br> 025 * Lazenska 391, 76314 Zlin, Czech Republic<br> 026 * <br> 027 * This library is free software; you can redistribute it and/or 028 * modify it under the terms of the GNU Lesser General Public 029 * License as published by the Free Software Foundation; either 030 * version 2.1 of the License, or (at your option) any later version. 031 * <br><br> 032 * This library is distributed in the hope that it will be useful, 033 * but WITHOUT ANY WARRANTY; without even the implied warranty of 034 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 035 * Lesser General Public License for more details. 036 * <br><br> 037 * You should have received a copy of the GNU Lesser General Public 038 * License along with this library; if not, write to the Free Software 039 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA 040 */ 041 public class ExamUnassignedVariableSelection implements VariableSelection { 042 private static Logger sLog = Logger.getLogger(ExamUnassignedVariableSelection.class); 043 private Vector iVariables = null; 044 private long iIteration = -1; 045 private boolean iRandomSelection = true; 046 047 /** Constructor */ 048 public ExamUnassignedVariableSelection(DataProperties properties) { 049 iRandomSelection = properties.getPropertyBoolean("ExamUnassignedVariableSelection.random", iRandomSelection); 050 } 051 052 /** Initialization */ 053 public void init(Solver solver) {} 054 055 /** Variable selection */ 056 public Variable selectVariable(Solution solution) { 057 Model model = solution.getModel(); 058 if (model.nrUnassignedVariables()==0) return null; 059 if (iRandomSelection) return (Variable)ToolBox.random(model.unassignedVariables()); 060 Variable variable = null; 061 for (Enumeration e=model.unassignedVariables().elements();e.hasMoreElements();) { 062 Variable v = (Variable)e.nextElement(); 063 if (variable==null || v.compareTo(variable)<0) variable = v; 064 } 065 return variable; 066 } 067 }