001package org.cpsolver.studentsct.heuristics;
002
003import org.cpsolver.ifs.heuristics.NeighbourSelection;
004import org.cpsolver.ifs.model.Neighbour;
005import org.cpsolver.ifs.solution.Solution;
006import org.cpsolver.ifs.solver.Solver;
007import org.cpsolver.ifs.util.DataProperties;
008import org.cpsolver.ifs.util.Progress;
009import org.cpsolver.studentsct.model.Enrollment;
010import org.cpsolver.studentsct.model.Request;
011
012/**
013 * A simple step that checks whether the best solution has improved since the last check.
014 * If there is no improvement, restore the best solution (reset the search to start from the best
015 * solution again). The checking is done in the seletion's initialization phase,
016 * no neighbors are actually computed.
017 * 
018 * @author  Tomáš Müller
019 * @version StudentSct 1.3 (Student Sectioning)<br>
020 *          Copyright (C) 2007 - 2014 Tomáš Müller<br>
021 *          <a href="mailto:muller@unitime.org">muller@unitime.org</a><br>
022 *          <a href="http://muller.unitime.org">http://muller.unitime.org</a><br>
023 * <br>
024 *          This library is free software; you can redistribute it and/or modify
025 *          it under the terms of the GNU Lesser General Public License as
026 *          published by the Free Software Foundation; either version 3 of the
027 *          License, or (at your option) any later version. <br>
028 * <br>
029 *          This library is distributed in the hope that it will be useful, but
030 *          WITHOUT ANY WARRANTY; without even the implied warranty of
031 *          MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
032 *          Lesser General Public License for more details. <br>
033 * <br>
034 *          You should have received a copy of the GNU Lesser General Public
035 *          License along with this library; if not see
036 *          <a href='http://www.gnu.org/licenses/'>http://www.gnu.org/licenses/</a>.
037 */
038public class RestoreBestSolution implements NeighbourSelection<Request, Enrollment> {
039    private Double iBestValue = null;
040    
041    public RestoreBestSolution(DataProperties config) {
042    }
043
044    @Override
045    public void init(Solver<Request, Enrollment> solver) {
046        Progress.getInstance(solver.currentSolution().getModel()).setPhase("Restore best...", 1);
047        if (solver.currentSolution().getBestInfo() == null) return; // no best saved yet
048        if (iBestValue == null || iBestValue > solver.currentSolution().getBestValue()) {
049            Progress.getInstance(solver.currentSolution().getModel()).debug("best value marked");
050            iBestValue = solver.currentSolution().getBestValue();
051        } else {
052            Progress.getInstance(solver.currentSolution().getModel()).debug("best solution restored");
053            solver.currentSolution().restoreBest();
054        }
055    }
056
057
058    @Override
059    public Neighbour<Request, Enrollment> selectNeighbour(Solution<Request, Enrollment> solution) {
060        return null;
061    }
062
063}