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.Enrollment;
013    import net.sf.cpsolver.studentsct.model.Request;
014    import net.sf.cpsolver.studentsct.model.Student;
015    
016    /**
017     * Resection studends with empty schedule. An extension of
018     * {@link BranchBoundSelection}, where only students that have no enrollments (
019     * {@link Student#nrAssignedRequests()} is zero) are resectioned.
020     * 
021     * <br>
022     * <br>
023     * 
024     * @version StudentSct 1.2 (Student Sectioning)<br>
025     *          Copyright (C) 2007 - 2010 Tomas Muller<br>
026     *          <a href="mailto:muller@unitime.org">muller@unitime.org</a><br>
027     *          <a href="http://muller.unitime.org">http://muller.unitime.org</a><br>
028     * <br>
029     *          This library is free software; you can redistribute it and/or modify
030     *          it under the terms of the GNU Lesser General Public License as
031     *          published by the Free Software Foundation; either version 3 of the
032     *          License, or (at your option) any later version. <br>
033     * <br>
034     *          This library is distributed in the hope that it will be useful, but
035     *          WITHOUT ANY WARRANTY; without even the implied warranty of
036     *          MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
037     *          Lesser General Public License for more details. <br>
038     * <br>
039     *          You should have received a copy of the GNU Lesser General Public
040     *          License along with this library; if not see
041     *          <a href='http://www.gnu.org/licenses/'>http://www.gnu.org/licenses/</a>.
042     */
043    
044    public class ResectionUnassignedStudentsSelection extends BranchBoundSelection {
045        private static Logger sLog = Logger.getLogger(ResectionUnassignedStudentsSelection.class);
046    
047        public ResectionUnassignedStudentsSelection(DataProperties properties) {
048            super(properties);
049            iOrder = new StudentRandomOrder(properties);
050            if (properties.getProperty("Neighbour.ResectionUnassignedStudentsOrder") != null) {
051                try {
052                    iOrder = (StudentOrder) Class.forName(
053                            properties.getProperty("Neighbour.ResectionUnassignedStudentsOrder")).getConstructor(
054                            new Class[] { DataProperties.class }).newInstance(new Object[] { properties });
055                } catch (Exception e) {
056                    sLog.error("Unable to set student order, reason:" + e.getMessage(), e);
057                }
058            }
059        }
060    
061        @Override
062        public void init(Solver<Request, Enrollment> solver) {
063            init(solver, "Resection unassigned students...");
064        }
065    
066        /**
067         * Select neighbour. All students with an empty schedule are taken, one by
068         * one in a random order. For each student a branch & bound search is
069         * employed.
070         */
071        @Override
072        public Neighbour<Request, Enrollment> selectNeighbour(Solution<Request, Enrollment> solution) {
073            while (iStudentsEnumeration.hasNext()) {
074                Student student = iStudentsEnumeration.next();
075                Progress.getInstance(solution.getModel()).incProgress();
076                if (student.nrAssignedRequests() == 0 && !student.getRequests().isEmpty()) {
077                    Neighbour<Request, Enrollment> neighbour = getSelection(student).select();
078                    if (neighbour != null)
079                        return neighbour;
080                }
081            }
082            return null;
083        }
084    
085    }