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