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