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