    001package org.cpsolver.studentsct.heuristics;
    002
    003import java.util.ArrayList;
    004import java.util.List;
    005
    006import org.cpsolver.ifs.heuristics.NeighbourSelection;
    007import org.cpsolver.ifs.model.Neighbour;
    008import org.cpsolver.ifs.solution.Solution;
    009import org.cpsolver.ifs.solver.Solver;
    010import org.cpsolver.ifs.util.DataProperties;
    011import org.cpsolver.studentsct.StudentSectioningModel;
    012import org.cpsolver.studentsct.model.Enrollment;
    013import org.cpsolver.studentsct.model.Request;
    014import org.cpsolver.studentsct.model.Student;
    015
    016
    017/**
    018 * Two-phase (Batch) student sectioning neighbour selection. It is based on
    019 * {@link StudentSctNeighbourSelection}, however in the first round, only real
    020 * students are sectioned. All dummy students are removed from the problem
    021 * during initialization of this neighbour selection, they are returned into the
    022 * problem after the first round of {@link StudentSctNeighbourSelection}.
    023 *
    024 * <br>
    025 * <br>
    026 *
    027 * @version StudentSct 1.3 (Student Sectioning)<br>
    028 *          Copyright (C) 2007 - 2014 Tomáš Müller<br>
    029 *          <a href="mailto:muller@unitime.org">muller@unitime.org</a><br>
    030 *          <a href="http://muller.unitime.org">http://muller.unitime.org</a><br>
    031 * <br>
    032 *          This library is free software; you can redistribute it and/or modify
    033 *          it under the terms of the GNU Lesser General Public License as
    034 *          published by the Free Software Foundation; either version 3 of the
    035 *          License, or (at your option) any later version. <br>
    036 * <br>
    037 *          This library is distributed in the hope that it will be useful, but
    038 *          WITHOUT ANY WARRANTY; without even the implied warranty of
    039 *          MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
    040 *          Lesser General Public License for more details. <br>
    041 * <br>
    042 *          You should have received a copy of the GNU Lesser General Public
    043 *          License along with this library; if not see
    044 *          <a href='http://www.gnu.org/licenses/'>http://www.gnu.org/licenses/</a>.
    045 */
    046
    047public class TwoPhaseStudentSctNeighbourSelection extends StudentSctNeighbourSelection {
    048    private int iNrRounds = 7;
    049
    050    public TwoPhaseStudentSctNeighbourSelection(DataProperties properties) throws Exception {
    051        super(properties);
    052        iNrRounds = properties.getPropertyInt("TwoPhaseSectioning.NrRoundsFirstPhase", iNrRounds);
    053    }
    054
    055    /** Initialization -- also remove all the dummy students from the problem */
    056    @Override
    057    public void init(Solver<Request, Enrollment> solver) {
    058        super.init(solver);
    059        if (removeDummyStudents(solver.currentSolution()))
    060            registerSelection(new RestoreDummyStudents());
    061    }
    062
    063    List<Student> iDummyStudents = null;
    064
    065    private boolean removeDummyStudents(Solution<Request, Enrollment> solution) {
    066        StudentSectioningModel model = (StudentSectioningModel) solution.getModel();
    067        if (model.getNrLastLikeStudents(false) == 0 || model.getNrRealStudents(false) == 0)
    068            return false;
    069        iDummyStudents = new ArrayList<Student>();
    070        for (Student student : new ArrayList<Student>(model.getStudents())) {
    071            if (student.isDummy()) {
    072                iDummyStudents.add(student);
    073                model.removeStudent(student);
    074            }
    075        }
    076        return true;
    077    }
    078
    079    private boolean addDummyStudents(Solution<Request, Enrollment> solution) {
    080        if (iDummyStudents == null || iDummyStudents.isEmpty())
    081            return false;
    082        iNrRounds--;
    083        if (iNrRounds > 0)
    084            return false;
    085        solution.restoreBest();
    086        StudentSectioningModel model = (StudentSectioningModel) solution.getModel();
    087        for (Student student : iDummyStudents) {
    088            model.addStudent(student);
    089        }
    090        iDummyStudents = null;
    091        solution.saveBest();
    092        return true;
    093    }
    094
    095    /**
    096     * Return all dummy students into the problem, executed as the last phase of
    097     * the first round
    098     */
    099    protected class RestoreDummyStudents implements NeighbourSelection<Request, Enrollment> {
    100        public RestoreDummyStudents() {
    101        }
    102
    103        @Override
    104        public void init(Solver<Request, Enrollment> solver) {
    105        }
    106
    107        /** Return all (removed) dummy students into the problem */
    108        @Override
    109        public Neighbour<Request, Enrollment> selectNeighbour(Solution<Request, Enrollment> solution) {
    110            addDummyStudents(solution);
    111            return null;
    112        }
    113    }
    114}