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