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