001package org.cpsolver.studentsct.heuristics;
002
003import java.util.ArrayList;
004import java.util.Iterator;
005
006import org.cpsolver.ifs.assignment.Assignment;
007import org.cpsolver.ifs.heuristics.BacktrackNeighbourSelection;
008import org.cpsolver.ifs.util.DataProperties;
009import org.cpsolver.studentsct.model.CourseRequest;
010import org.cpsolver.studentsct.model.Enrollment;
011import org.cpsolver.studentsct.model.Request;
012
013
014/**
015 * Randomized backtracking-based neighbour selection. This class extends
016 * {@link RandomizedBacktrackNeighbourSelection}, however, only a randomly
017 * selected subset of enrollments of each request is considered (
018 * {@link CourseRequest#computeRandomEnrollments(Assignment, int)} with the given limit is
019 * used).
020 * 
021 * <br>
022 * <br>
023 * Parameters: <br>
024 * <table border='1' summary='Related Solver Parameters'>
025 * <tr>
026 * <th>Parameter</th>
027 * <th>Type</th>
028 * <th>Comment</th>
029 * </tr>
030 * <tr>
031 * <td>Neighbour.MaxValues</td>
032 * <td>{@link Integer}</td>
033 * <td>Limit on the number of enrollments to be visited of each
034 * {@link CourseRequest}.</td>
035 * </tr>
036 * </table>
037 * <br>
038 * <br>
039 * 
040 * @version StudentSct 1.3 (Student Sectioning)<br>
041 *          Copyright (C) 2007 - 2014 Tomas Muller<br>
042 *          <a href="mailto:muller@unitime.org">muller@unitime.org</a><br>
043 *          <a href="http://muller.unitime.org">http://muller.unitime.org</a><br>
044 * <br>
045 *          This library is free software; you can redistribute it and/or modify
046 *          it under the terms of the GNU Lesser General Public License as
047 *          published by the Free Software Foundation; either version 3 of the
048 *          License, or (at your option) any later version. <br>
049 * <br>
050 *          This library is distributed in the hope that it will be useful, but
051 *          WITHOUT ANY WARRANTY; without even the implied warranty of
052 *          MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
053 *          Lesser General Public License for more details. <br>
054 * <br>
055 *          You should have received a copy of the GNU Lesser General Public
056 *          License along with this library; if not see
057 *          <a href='http://www.gnu.org/licenses/'>http://www.gnu.org/licenses/</a>.
058 */
059public class RandomizedBacktrackNeighbourSelection extends BacktrackNeighbourSelection<Request, Enrollment> {
060    private int iMaxValues = 100;
061
062    /**
063     * Constructor
064     * 
065     * @param properties
066     *            configuration
067     * @throws Exception thrown when the initialization fails
068     */
069    public RandomizedBacktrackNeighbourSelection(DataProperties properties) throws Exception {
070        super(properties);
071        iMaxValues = properties.getPropertyInt("Neighbour.MaxValues", iMaxValues);
072    }
073
074    /**
075     * List of values of a variable.
076     * {@link CourseRequest#computeRandomEnrollments(Assignment, int)} with the provided
077     * limit is used for a {@link CourseRequest}.
078     */
079    @Override
080    protected Iterator<Enrollment> values(BacktrackNeighbourSelection<Request, Enrollment>.BacktrackNeighbourSelectionContext context, Request variable) {
081        if (iMaxValues > 0 && variable instanceof CourseRequest) {
082            return new ArrayList<Enrollment>(((CourseRequest) variable).computeRandomEnrollments(context.getAssignment(), iMaxValues)).iterator();
083        }
084        return variable.computeEnrollments(context.getAssignment()).iterator();
085    }
086}