001package org.cpsolver.studentsct.heuristics.selection;
002
003import java.util.ArrayList;
004import java.util.Collections;
005import java.util.LinkedList;
006import java.util.List;
007
008import org.cpsolver.ifs.solver.Solver;
009import org.cpsolver.ifs.util.DataProperties;
010import org.cpsolver.ifs.util.Progress;
011import org.cpsolver.studentsct.heuristics.RandomizedBacktrackNeighbourSelection;
012import org.cpsolver.studentsct.model.Enrollment;
013import org.cpsolver.studentsct.model.Request;
014import org.cpsolver.studentsct.model.Request.RequestPriority;
015
016/**
017 * Use backtrack neighbour selection. For all unassigned variables (in a random
018 * order) that are marked as critical, {@link RandomizedBacktrackNeighbourSelection} is being used.
019 * 
020 * <br>
021 * <br>
022 * 
023 * @author  Tomáš Müller
024 * @version StudentSct 1.3 (Student Sectioning)<br>
025 *          Copyright (C) 2007 - 2014 Tomáš Müller<br>
026 *          <a href="mailto:muller@unitime.org">muller@unitime.org</a><br>
027 *          <a href="http://muller.unitime.org">http://muller.unitime.org</a><br>
028 * <br>
029 *          This library is free software; you can redistribute it and/or modify
030 *          it under the terms of the GNU Lesser General Public License as
031 *          published by the Free Software Foundation; either version 3 of the
032 *          License, or (at your option) any later version. <br>
033 * <br>
034 *          This library is distributed in the hope that it will be useful, but
035 *          WITHOUT ANY WARRANTY; without even the implied warranty of
036 *          MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
037 *          Lesser General Public License for more details. <br>
038 * <br>
039 *          You should have received a copy of the GNU Lesser General Public
040 *          License along with this library; if not see
041 *          <a href='http://www.gnu.org/licenses/'>http://www.gnu.org/licenses/</a>.
042 */
043public class CriticalBacktrackSelection extends BacktrackSelection {
044    private RequestPriority iPriority;
045    
046    public CriticalBacktrackSelection(DataProperties properties, RequestPriority priority) {
047        super(properties);
048        iPriority = priority;
049        iIncludeAssignedRequests = properties.getPropertyBoolean("Neighbour.IncludeCriticalAssignedRequests", iIncludeAssignedRequests);
050    }
051    
052    public CriticalBacktrackSelection(DataProperties properties) {
053        this(properties, RequestPriority.Critical);
054    }
055    
056    @Override
057    public void init(Solver<Request, Enrollment> solver, String name) {
058        List<Request> variables = new ArrayList<Request>();
059        for (Request r: (iIncludeAssignedRequests ? solver.currentSolution().getModel().variables() : solver.currentSolution().getModel().unassignedVariables(solver.currentSolution().getAssignment())))
060            if (iPriority.isCritical(r)) variables.add(r);
061        Collections.shuffle(variables);
062        Collections.sort(variables, iRequestComparator);
063        iRequests = new LinkedList<Request>(variables);
064        if (iRBtNSel == null) {
065            try {
066                iRBtNSel = new RandomizedBacktrackNeighbourSelection(solver.getProperties());
067                iRBtNSel.init(solver);
068            } catch (Exception e) {
069                throw new RuntimeException(e.getMessage(), e);
070            }
071        }
072        Progress.getInstance(solver.currentSolution().getModel()).setPhase(iPriority.name() + " Backtracking" + (iFilter == null ? "" : " (" + iFilter.getName().toLowerCase() + " students)") + "...", variables.size());
073    }
074
075}