001package org.cpsolver.studentsct.heuristics.selection;
002
003import org.apache.log4j.Logger;
004import org.cpsolver.ifs.assignment.Assignment;
005import org.cpsolver.ifs.model.Neighbour;
006import org.cpsolver.ifs.solution.Solution;
007import org.cpsolver.ifs.solver.Solver;
008import org.cpsolver.ifs.util.DataProperties;
009import org.cpsolver.ifs.util.Progress;
010import org.cpsolver.studentsct.heuristics.studentord.StudentOrder;
011import org.cpsolver.studentsct.heuristics.studentord.StudentRandomOrder;
012import org.cpsolver.studentsct.model.Enrollment;
013import org.cpsolver.studentsct.model.Request;
014import org.cpsolver.studentsct.model.Student;
015
016
017/**
018 * Resection studends with empty schedule. An extension of
019 * {@link BranchBoundSelection}, where only students that have no enrollments (
020 * {@link Student#nrAssignedRequests(Assignment)} is zero) are resectioned.
021 * 
022 * <br>
023 * <br>
024 * 
025 * @version StudentSct 1.3 (Student Sectioning)<br>
026 *          Copyright (C) 2007 - 2014 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 ResectionUnassignedStudentsSelection extends BranchBoundSelection {
046    private static Logger sLog = Logger.getLogger(ResectionUnassignedStudentsSelection.class);
047
048    public ResectionUnassignedStudentsSelection(DataProperties properties) {
049        super(properties);
050        iOrder = new StudentRandomOrder(properties);
051        if (properties.getProperty("Neighbour.ResectionUnassignedStudentsOrder") != null) {
052            try {
053                iOrder = (StudentOrder) Class.forName(
054                        properties.getProperty("Neighbour.ResectionUnassignedStudentsOrder")).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 unassigned students...");
065    }
066
067    /**
068     * Select neighbour. All students with an empty schedule are taken, one by
069     * one in a random order. For each student a branch &amp; bound search is
070     * employed.
071     */
072    @Override
073    public Neighbour<Request, Enrollment> selectNeighbour(Solution<Request, Enrollment> solution) {
074        Student student = null;
075        while ((student = nextStudent()) != null) {
076            Progress.getInstance(solution.getModel()).incProgress();
077            if (student.nrAssignedRequests(solution.getAssignment()) == 0 && !student.getRequests().isEmpty()) {
078                Neighbour<Request, Enrollment> neighbour = getSelection(solution.getAssignment(), student).select();
079                if (neighbour != null)
080                    return neighbour;
081            }
082        }
083        return null;
084    }
085
086}