001    package net.sf.cpsolver.studentsct.heuristics;
002    
003    import java.util.Enumeration;
004    
005    import net.sf.cpsolver.ifs.heuristics.RouletteWheelSelection;
006    import net.sf.cpsolver.ifs.heuristics.VariableSelection;
007    import net.sf.cpsolver.ifs.model.Variable;
008    import net.sf.cpsolver.ifs.solution.Solution;
009    import net.sf.cpsolver.ifs.solver.Solver;
010    import net.sf.cpsolver.ifs.util.DataProperties;
011    import net.sf.cpsolver.studentsct.model.Enrollment;
012    import net.sf.cpsolver.studentsct.model.Request;
013    
014    /**
015     * Variable ({@link Request}) selection using {@link RouletteWheelSelection}.
016     * Unassigned request has 10 points, an assigned request has 1 point for 
017     * each section that exceeds its bound. 
018     * 
019     * <br><br>
020     * 
021     * @version
022     * StudentSct 1.1 (Student Sectioning)<br>
023     * Copyright (C) 2007 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 RouletteWheelRequestSelection implements VariableSelection {
042        RouletteWheelSelection iRoulette = null;
043        
044        /**
045         * Constructor
046         * @param properties configuration
047         */
048        public RouletteWheelRequestSelection(DataProperties properties) {
049            super();
050        }
051        
052        /** Initialization */
053        public void init(Solver solver) {
054            
055        }
056        
057        /** Populate roulette wheel selection, if null or empty. */
058        protected RouletteWheelSelection getRoulette(Solution solution) {
059            if (iRoulette!=null && iRoulette.hasMoreElements()) {
060                if (iRoulette.getUsedPoints()<0.1*iRoulette.getTotalPoints()) return iRoulette;
061            }
062            iRoulette = new RouletteWheelSelection();
063            for (Enumeration e=solution.getModel().variables().elements();e.hasMoreElements();) {
064                Request request = (Request)e.nextElement();
065                double points = 0;
066                if (request.getAssignment()==null)
067                    points +=10;
068                else {
069                    Enrollment enrollment = (Enrollment)request.getAssignment();
070                    if (enrollment.toDouble()>request.getBound())
071                        points +=1;
072                }
073                if (points>0)
074                    iRoulette.add(request, points);
075            }
076            return iRoulette;
077        }
078        
079        /** 
080         * Variable selection. {@link RouletteWheelSelection} is used.
081         * Unassigned request has 10 points, an assigned request has 1 point for
082         * each section that exceeds its bound. 
083         */
084        public Variable selectVariable(Solution solution) {
085            return (Variable)getRoulette(solution).nextElement();
086        }
087    }