    001package org.cpsolver.studentsct.heuristics.selection;
    002
    003import java.util.Set;
    004
    005import org.cpsolver.ifs.heuristics.NeighbourSelection;
    006import org.cpsolver.ifs.heuristics.RoundRobinNeighbourSelection;
    007import org.cpsolver.ifs.model.Neighbour;
    008import org.cpsolver.ifs.solution.Solution;
    009import org.cpsolver.ifs.solver.Solver;
    010import org.cpsolver.ifs.util.DataProperties;
    011import org.cpsolver.ifs.util.Progress;
    012import org.cpsolver.ifs.util.ToolBox;
    013import org.cpsolver.studentsct.model.Enrollment;
    014import org.cpsolver.studentsct.model.Request;
    015import org.cpsolver.studentsct.model.Request.RequestPriority;
    016import org.cpsolver.studentsct.model.Student;
    017
    018
    019/**
    020 * Random unassignment of some problematic students. Problematic students are to
    021 * be provided by a neighbour selection that was used before this one by
    022 * {@link RoundRobinNeighbourSelection}.
    023 *
    024 * <br>
    025 * <br>
    026 * In each step a problematic student is randomly selected with the given
    027 * probabilty. Null is returned otherwise (the controll is passed to the next
    028 * {@link NeighbourSelection}).
    029 *
    030 * <br>
    031 * <br>
    032 * Parameters: <br>
    033 * <table border='1'><caption>Related Solver Parameters</caption>
    034 * <tr>
    035 * <th>Parameter</th>
    036 * <th>Type</th>
    037 * <th>Comment</th>
    038 * </tr>
    039 * <tr>
    040 * <td>Neighbour.RandomUnassignmentOfProblemStudentProb</td>
    041 * <td>{@link Double}</td>
    042 * <td>Probability of a random selection of a student from the given set of
    043 * problematic students.</td>
    044 * </tr>
    045 * </table>
    046 * <br>
    047 * <br>
    048 *
    049 * @author  Tomáš Müller
    050 * @version StudentSct 1.3 (Student Sectioning)<br>
    051 *          Copyright (C) 2007 - 2014 Tomáš Müller<br>
    052 *          <a href="mailto:muller@unitime.org">muller@unitime.org</a><br>
    053 *          <a href="http://muller.unitime.org">http://muller.unitime.org</a><br>
    054 * <br>
    055 *          This library is free software; you can redistribute it and/or modify
    056 *          it under the terms of the GNU Lesser General Public License as
    057 *          published by the Free Software Foundation; either version 3 of the
    058 *          License, or (at your option) any later version. <br>
    059 * <br>
    060 *          This library is distributed in the hope that it will be useful, but
    061 *          WITHOUT ANY WARRANTY; without even the implied warranty of
    062 *          MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
    063 *          Lesser General Public License for more details. <br>
    064 * <br>
    065 *          You should have received a copy of the GNU Lesser General Public
    066 *          License along with this library; if not see
    067 *          <a href='http://www.gnu.org/licenses/'>http://www.gnu.org/licenses/</a>.
    068 */
    069public class RndUnProblStudSelection extends RandomUnassignmentSelection {
    070    private ProblemStudentsProvider iProblemStudentsProvider = null;
    071    private Set<Student> iProblemStudents = null;
    072
    073    /**
    074     * Constructor
    075     *
    076     * @param properties
    077     *            configuration
    078     * @param psp
    079     *            a class that provides the set of problematic students
    080     */
    081    public RndUnProblStudSelection(DataProperties properties, ProblemStudentsProvider psp) {
    082        super(properties);
    083        iProblemStudentsProvider = psp;
    084        iRandom = properties.getPropertyDouble("Neighbour.RandomUnassignmentOfProblemStudentProb", 0.9);
    085    }
    086
    087    /**
    088     * Initialization -- {@link ProblemStudentsProvider#getProblemStudents()} is
    089     * called
    090     */
    091    @Override
    092    public void init(Solver<Request, Enrollment> solver) {
    093        iProblemStudents = iProblemStudentsProvider.getProblemStudents();
    094        Progress.getInstance(solver.currentSolution().getModel()).setPhase(
    095                "Random unassignment of problematic students...", 1);
    096    }
    097
    098    /**
    099     * With the given probabilty, a problematic student is randomly selected to
    100     * be unassigned. Null is returned otherwise.
    101     */
    102    @Override
    103    public synchronized Neighbour<Request, Enrollment> selectNeighbour(Solution<Request, Enrollment> solution) {
    104        if (Math.random() < iRandom) {
    105            while (!iProblemStudents.isEmpty()) {
    106                Student student = ToolBox.random(iProblemStudents);
    107                iProblemStudents.remove(student);
    108                if (student.hasMinCredit() && student.getAssignedCredit(solution.getAssignment()) < student.getMinCredit()) continue;
    109                return new UnassignStudentNeighbour(student, solution.getAssignment(), RequestPriority.Important);
    110            }
    111        }
    112        Progress.getInstance(solution.getModel()).incProgress();
    113        return null;
    114    }
    115}