    001package org.cpsolver.studentsct.heuristics.selection;
    002
    003import java.util.ArrayList;
    004import java.util.Collections;
    005import java.util.LinkedList;
    006import java.util.List;
    007import java.util.Queue;
    008
    009import org.cpsolver.ifs.assignment.Assignment;
    010import org.cpsolver.ifs.heuristics.ValueSelection;
    011import org.cpsolver.ifs.heuristics.VariableSelection;
    012import org.cpsolver.ifs.solution.Solution;
    013import org.cpsolver.ifs.solver.Solver;
    014import org.cpsolver.ifs.util.DataProperties;
    015import org.cpsolver.studentsct.filter.StudentFilter;
    016import org.cpsolver.studentsct.model.Enrollment;
    017import org.cpsolver.studentsct.model.Request;
    018import org.cpsolver.studentsct.model.Request.RequestPriority;
    019
    020/**
    021 * Use the standard IFS search for the unassigned critical course requests.
    022 * This selection is based on {@link StandardSelection} using
    023 * {@link UnassignedCriticalCourseRequestSelection} as variable selection.
    024 * Unlike {@link StandardSelection}, it allows for a critical course request to be
    025 * unassigned.
    026 *
    027 * @author  Tomáš Müller
    028 * @version StudentSct 1.3 (Student Sectioning)<br>
    029 *          Copyright (C) 2007 - 2014 Tomáš Müller<br>
    030 *          <a href="mailto:muller@unitime.org">muller@unitime.org</a><br>
    031 *          <a href="http://muller.unitime.org">http://muller.unitime.org</a><br>
    032 * <br>
    033 *          This library is free software; you can redistribute it and/or modify
    034 *          it under the terms of the GNU Lesser General Public License as
    035 *          published by the Free Software Foundation; either version 3 of the
    036 *          License, or (at your option) any later version. <br>
    037 * <br>
    038 *          This library is distributed in the hope that it will be useful, but
    039 *          WITHOUT ANY WARRANTY; without even the implied warranty of
    040 *          MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
    041 *          Lesser General Public License for more details. <br>
    042 * <br>
    043 *          You should have received a copy of the GNU Lesser General Public
    044 *          License along with this library; if not see
    045 *          <a href='http://www.gnu.org/licenses/'>http://www.gnu.org/licenses/</a>.
    046 */
    047public class CriticalStandardSelection extends StandardSelection {
    048    private RequestPriority iPriority;
    049    private boolean iAllowCriticalUnassignment = false;
    050
    051    public CriticalStandardSelection(DataProperties properties, VariableSelection<Request, Enrollment> variableSelection, ValueSelection<Request, Enrollment> valueSelection, RequestPriority priority) {
    052        super(properties, variableSelection, valueSelection);
    053        iPriority = priority;
    054        iAllowCriticalUnassignment = properties.getPropertyBoolean("Neighbour.AllowCriticalUnassignment", iAllowCriticalUnassignment);
    055        iCanWorsen = properties.getPropertyBoolean("Neighbour.CriticalStandardCanWorsen", false);
    056    }
    057
    058    public CriticalStandardSelection(DataProperties properties, ValueSelection<Request, Enrollment> valueSelection, RequestPriority priority) {
    059        this(properties, new UnassignedCriticalCourseRequestSelection(priority), valueSelection, priority);
    060    }
    061
    062    public CriticalStandardSelection(DataProperties properties, ValueSelection<Request, Enrollment> valueSelection) {
    063        this(properties, valueSelection, RequestPriority.Critical);
    064    }
    065
    066    @Override
    067    public void init(Solver<Request, Enrollment> solver) {
    068        StudentFilter filter = null;
    069        if (iVariableSelection instanceof UnassignedRequestSelection)
    070            filter = ((UnassignedRequestSelection)iVariableSelection).getFilter();
    071        init(solver, iPriority.name() + " Ifs" + (filter == null ? "" : " (" + filter.getName().toLowerCase() + " students)") + "...");
    072    }
    073
    074    @Override
    075    public boolean canUnassign(Enrollment enrollment, Enrollment conflict, Assignment<Request, Enrollment> assignment) {
    076        if (!iAllowCriticalUnassignment) return super.canUnassign(enrollment, conflict, assignment);
    077        if (!iCanConflict) return false;
    078        if (!iCanHigherPriorityConflict && conflict.getRequest().getPriority() < enrollment.getRequest().getPriority()) return false;
    079        if (conflict.getRequest().isMPP() && conflict.equals(conflict.getRequest().getInitialAssignment())) return false;
    080        if (iPreferPriorityStudents || conflict.getRequest().getRequestPriority().isSame(enrollment.getRequest())) {
    081            if (conflict.getStudent().getPriority().isHigher(enrollment.getStudent())) return false;
    082        }
    083        // Override to allow unassignment of critical course requests
    084        return true;
    085    }
    086
    087    /**
    088     * Returns the unassigned critical course requests in a random order.
    089     */
    090    static class UnassignedCriticalCourseRequestSelection implements VariableSelection<Request, Enrollment>{
    091        protected int iNrRounds = 0;
    092        protected Queue<Request> iRequests = null;
    093        private RequestPriority iPriority;
    094
    095        public UnassignedCriticalCourseRequestSelection(RequestPriority priority) {
    096            iPriority = priority;
    097        }
    098
    099        @Override
    100        public void init(Solver<Request, Enrollment> solver) {
    101            iRequests = new LinkedList<Request>();
    102            iNrRounds = solver.getProperties().getPropertyInt("Neighbour.CriticalRounds", 10);
    103        }
    104
    105        @Override
    106        public Request selectVariable(Solution<Request, Enrollment> solution) {
    107            return nextRequest(solution);
    108        }
    109
    110        protected synchronized Request nextRequest(Solution<Request, Enrollment> solution) {
    111            if (iRequests.isEmpty() && iNrRounds > 0) {
    112                iNrRounds --;
    113                List<Request> variables = new ArrayList<Request>();
    114                for (Request r: solution.getModel().unassignedVariables(solution.getAssignment()))
    115                    if (iPriority.isCritical(r)) variables.add(r);
    116                Collections.shuffle(variables);
    117                iRequests.addAll(variables);
    118            }
    119            return iRequests.poll();
    120        }
    121    }
    122}