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' summary='Related Solver Parameters'>
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 * @version StudentSct 1.3 (Student Sectioning)<br>
050 *          Copyright (C) 2007 - 2014 Tomáš Müller<br>
051 *          <a href="mailto:muller@unitime.org">muller@unitime.org</a><br>
052 *          <a href="http://muller.unitime.org">http://muller.unitime.org</a><br>
053 * <br>
054 *          This library is free software; you can redistribute it and/or modify
055 *          it under the terms of the GNU Lesser General Public License as
056 *          published by the Free Software Foundation; either version 3 of the
057 *          License, or (at your option) any later version. <br>
058 * <br>
059 *          This library is distributed in the hope that it will be useful, but
060 *          WITHOUT ANY WARRANTY; without even the implied warranty of
061 *          MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
062 *          Lesser General Public License for more details. <br>
063 * <br>
064 *          You should have received a copy of the GNU Lesser General Public
065 *          License along with this library; if not see
066 *          <a href='http://www.gnu.org/licenses/'>http://www.gnu.org/licenses/</a>.
067 */
068public class RndUnProblStudSelection extends RandomUnassignmentSelection {
069    private ProblemStudentsProvider iProblemStudentsProvider = null;
070    private Set<Student> iProblemStudents = null;
071
072    /**
073     * Constructor
074     * 
075     * @param properties
076     *            configuration
077     * @param psp
078     *            a class that provides the set of problematic students
079     */
080    public RndUnProblStudSelection(DataProperties properties, ProblemStudentsProvider psp) {
081        super(properties);
082        iProblemStudentsProvider = psp;
083        iRandom = properties.getPropertyDouble("Neighbour.RandomUnassignmentOfProblemStudentProb", 0.9);
084    }
085
086    /**
087     * Initialization -- {@link ProblemStudentsProvider#getProblemStudents()} is
088     * called
089     */
090    @Override
091    public void init(Solver<Request, Enrollment> solver) {
092        iProblemStudents = iProblemStudentsProvider.getProblemStudents();
093        Progress.getInstance(solver.currentSolution().getModel()).setPhase(
094                "Random unassignment of problematic students...", 1);
095    }
096
097    /**
098     * With the given probabilty, a problematic student is randomly selected to
099     * be unassigned. Null is returned otherwise.
100     */
101    @Override
102    public synchronized Neighbour<Request, Enrollment> selectNeighbour(Solution<Request, Enrollment> solution) {
103        if (Math.random() < iRandom) {
104            while (!iProblemStudents.isEmpty()) {
105                Student student = ToolBox.random(iProblemStudents);
106                iProblemStudents.remove(student);
107                if (student.hasMinCredit() && student.getAssignedCredit(solution.getAssignment()) < student.getMinCredit()) continue;
108                return new UnassignStudentNeighbour(student, solution.getAssignment(), RequestPriority.Important);
109            }
110        }
111        Progress.getInstance(solution.getModel()).incProgress();
112        return null;
113    }
114}