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.model.Enrollment;
016import org.cpsolver.studentsct.model.Request;
017
018/**
019 * Use the standard IFS search for the unassigned critical course requests.
020 * This selection is based on {@link StandardSelection} using
021 * {@link UnassignedCriticalCourseRequestSelection} as variable selection.
022 * Unlike {@link StandardSelection}, it allows for a critical course request to be
023 * unassigned.
024 * 
025 * @version StudentSct 1.3 (Student Sectioning)<br>
026 *          Copyright (C) 2007 - 2014 Tomas Muller<br>
027 *          <a href="mailto:muller@unitime.org">muller@unitime.org</a><br>
028 *          <a href="http://muller.unitime.org">http://muller.unitime.org</a><br>
029 * <br>
030 *          This library is free software; you can redistribute it and/or modify
031 *          it under the terms of the GNU Lesser General Public License as
032 *          published by the Free Software Foundation; either version 3 of the
033 *          License, or (at your option) any later version. <br>
034 * <br>
035 *          This library is distributed in the hope that it will be useful, but
036 *          WITHOUT ANY WARRANTY; without even the implied warranty of
037 *          MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
038 *          Lesser General Public License for more details. <br>
039 * <br>
040 *          You should have received a copy of the GNU Lesser General Public
041 *          License along with this library; if not see
042 *          <a href='http://www.gnu.org/licenses/'>http://www.gnu.org/licenses/</a>.
043 */
044public class CriticalStandardSelection extends StandardSelection {
045    
046    public CriticalStandardSelection(DataProperties properties, ValueSelection<Request, Enrollment> valueSelection) {
047        super(properties, new UnassignedCriticalCourseRequestSelection(), valueSelection);
048    }
049    
050    @Override
051    public void init(Solver<Request, Enrollment> solver) {
052        init(solver, "Ifs (critical)...");
053    }
054    
055    @Override
056    public boolean canUnassign(Enrollment enrollment, Assignment<Request, Enrollment> assignment) {
057        if (enrollment.getRequest().isMPP() && enrollment.equals(enrollment.getRequest().getInitialAssignment())) return false;
058        // Override to allow unassignment of critical course requests
059        return true;
060    }
061    
062    /**
063     * Returns the unassigned critical course requests in a random order.
064     */
065    static class UnassignedCriticalCourseRequestSelection implements VariableSelection<Request, Enrollment>{
066        protected int iNrRounds = 0;
067        protected Queue<Request> iRequests = null;
068        
069        @Override
070        public void init(Solver<Request, Enrollment> solver) {
071            iRequests = new LinkedList<Request>();
072            iNrRounds = solver.getProperties().getPropertyInt("Neighbour.CriticalRounds", 10);
073        }
074
075        @Override
076        public Request selectVariable(Solution<Request, Enrollment> solution) {
077            return nextRequest(solution);
078        }
079        
080        protected synchronized Request nextRequest(Solution<Request, Enrollment> solution) {
081            if (iRequests.isEmpty() && iNrRounds > 0) {
082                iNrRounds --;
083                List<Request> variables = new ArrayList<Request>();
084                for (Request r: solution.getModel().unassignedVariables(solution.getAssignment()))
085                    if (r.isCritical()) variables.add(r);
086                Collections.shuffle(variables);
087                iRequests.addAll(variables);
088            }
089            return iRequests.poll();
090        }
091    }
092}