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