001    package net.sf.cpsolver.studentsct.heuristics.selection;
002    
003    import org.apache.log4j.Logger;
004    
005    import net.sf.cpsolver.ifs.model.Neighbour;
006    import net.sf.cpsolver.ifs.solution.Solution;
007    import net.sf.cpsolver.ifs.solver.Solver;
008    import net.sf.cpsolver.ifs.util.DataProperties;
009    import net.sf.cpsolver.ifs.util.Progress;
010    import net.sf.cpsolver.studentsct.heuristics.studentord.StudentOrder;
011    import net.sf.cpsolver.studentsct.heuristics.studentord.StudentRandomOrder;
012    import net.sf.cpsolver.studentsct.model.Student;
013    
014    /**
015     * Resection studends with empty schedule.
016     * An extension of {@link BranchBoundSelection}, where only students that have
017     * no enrollments ({@link Student#nrAssignedRequests()} is zero) are 
018     * resectioned.
019     *  
020     * <br><br>
021     * 
022     * @version
023     * StudentSct 1.1 (Student Sectioning)<br>
024     * Copyright (C) 2007 Tomáš Müller<br>
025     * <a href="mailto:muller@unitime.org">muller@unitime.org</a><br>
026     * Lazenska 391, 76314 Zlin, Czech Republic<br>
027     * <br>
028     * This library is free software; you can redistribute it and/or
029     * modify it under the terms of the GNU Lesser General Public
030     * License as published by the Free Software Foundation; either
031     * version 2.1 of the License, or (at your option) any later version.
032     * <br><br>
033     * This library is distributed in the hope that it will be useful,
034     * but WITHOUT ANY WARRANTY; without even the implied warranty of
035     * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
036     * Lesser General Public License for more details.
037     * <br><br>
038     * You should have received a copy of the GNU Lesser General Public
039     * License along with this library; if not, write to the Free Software
040     * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
041     */
042    
043    public class ResectionUnassignedStudentsSelection extends BranchBoundSelection {
044        private static Logger sLog = Logger.getLogger(ResectionUnassignedStudentsSelection.class);
045    
046        public ResectionUnassignedStudentsSelection(DataProperties properties) {
047            super(properties);
048            iOrder = new StudentRandomOrder(properties);
049            if (properties.getProperty("Neighbour.ResectionUnassignedStudentsOrder")!=null) {
050                try {
051                    iOrder = (StudentOrder)Class.forName(properties.getProperty("Neighbour.ResectionUnassignedStudentsOrder")).
052                        getConstructor(new Class[] {DataProperties.class}).
053                        newInstance(new Object[] {properties});
054                } catch (Exception e) {
055                    sLog.error("Unable to set student order, reason:"+e.getMessage(),e);
056                }
057            }
058        }
059        
060        public void init(Solver solver) {
061            init(solver, "Resection unassigned students...");
062        }
063        
064        /**
065         * Select neighbour. All students with an empty schedule are taken, 
066         * one by one in a random order. For each student a branch & bound search is employed. 
067         */
068        public Neighbour selectNeighbour(Solution solution) {
069            while (iStudentsEnumeration.hasMoreElements()) {
070                Student student = (Student)iStudentsEnumeration.nextElement();
071                Progress.getInstance(solution.getModel()).incProgress();
072                if (student.nrAssignedRequests()==0 && !student.getRequests().isEmpty()) {
073                    Neighbour neighbour = getSelection(student).select();
074                    if (neighbour!=null) return neighbour;
075                }
076            }
077            return null;
078        }
079        
080    }