    001package net.sf.cpsolver.coursett.model;
    002
    003import net.sf.cpsolver.ifs.model.Constraint;
    004import net.sf.cpsolver.ifs.model.ModelListener;
    005import net.sf.cpsolver.ifs.solver.Solver;
    006
    007/**
    008 * On fly student sectioning. <br>
    009 * <br>
    010 * In this mode, students are resectioned after each iteration, but only between
    011 * classes that are affected by the iteration. This slows down the solver, but
    012 * it can dramatically improve results in the case when there is more stress put
    013 * on student conflicts (e.g., Woebegon College example).
    014 *
    015 * <br>
    016 * <br>
    017 * Parameters:
    018 * <table border='1'>
    019 * <tr>
    020 * <th>Parameter</th>
    021 * <th>Type</th>
    022 * <th>Comment</th>
    023 * </tr>
    024 * <tr>
    025 * <td>OnFlySectioning.Enabled</td>
    026 * <td>{@link Boolean}</td>
    027 * <td>Enable on fly sectioning (if enabled, students will be resectioned after
    028 * each iteration)</td>
    029 * </tr>
    030 * <tr>
    031 * <td>OnFlySectioning.Recursive</td>
    032 * <td>{@link Boolean}</td>
    033 * <td>Recursively resection lectures affected by a student swap</td>
    034 * </tr>
    035 * <tr>
    036 * <td>OnFlySectioning.ConfigAsWell</td>
    037 * <td>{@link Boolean}</td>
    038 * <td>Resection students between configurations as well</td>
    039 * </tr>
    040 * </table>
    041 *
    042 * @version CourseTT 1.2 (University Course Timetabling)<br>
    043 *          Copyright (C) 2006 - 2010 Tomáš Müller<br>
    044 *          <a href="mailto:muller@unitime.org">muller@unitime.org</a><br>
    045 *          <a href="http://muller.unitime.org">http://muller.unitime.org</a><br>
    046 * <br>
    047 *          This library is free software; you can redistribute it and/or modify
    048 *          it under the terms of the GNU Lesser General Public License as
    049 *          published by the Free Software Foundation; either version 3 of the
    050 *          License, or (at your option) any later version. <br>
    051 * <br>
    052 *          This library is distributed in the hope that it will be useful, but
    053 *          WITHOUT ANY WARRANTY; without even the implied warranty of
    054 *          MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
    055 *          Lesser General Public License for more details. <br>
    056 * <br>
    057 *          You should have received a copy of the GNU Lesser General Public
    058 *          License along with this library; if not see
    059 *          <a href='http://www.gnu.org/licenses/'>http://www.gnu.org/licenses/</a>.
    060 */
    061
    062public class OnFlySectioning implements ModelListener<Lecture, Placement> {
    063    private TimetableModel iModel;
    064    private boolean iRecursive = true;
    065    private boolean iConfigAsWell = false;
    066
    067    /**
    068     * Constructor
    069     *
    070     * @param model
    071     *            timetabling model
    072     */
    073    public OnFlySectioning(TimetableModel model) {
    074        iModel = model;
    075    }
    076
    077    @Override
    078    public void variableAdded(Lecture variable) {
    079    }
    080
    081    @Override
    082    public void variableRemoved(Lecture variable) {
    083    }
    084
    085    @Override
    086    public void constraintAdded(Constraint<Lecture, Placement> constraint) {
    087    }
    088
    089    @Override
    090    public void constraintRemoved(Constraint<Lecture, Placement> constraint) {
    091    }
    092
    093    @Override
    094    public void beforeAssigned(long iteration, Placement value) {
    095    }
    096
    097    @Override
    098    public void beforeUnassigned(long iteration, Placement value) {
    099    }
    100
    101    /**
    102     * {@link FinalSectioning#resection(Lecture, boolean, boolean)} is called
    103     * when given iteration number is greater than zero.
    104     */
    105    @Override
    106    public void afterAssigned(long iteration, Placement value) {
    107        if (iteration > 0)
    108            iModel.getStudentSectioning().resection(value.variable(), iRecursive, iConfigAsWell);
    109    }
    110
    111    @Override
    112    public void afterUnassigned(long iteration, Placement value) {
    113    }
    114
    115    /**
    116     * Initialization
    117     */
    118    @Override
    119    public boolean init(Solver<Lecture, Placement> solver) {
    120        iRecursive = solver.getProperties().getPropertyBoolean("OnFlySectioning.Recursive", true);
    121        iConfigAsWell = solver.getProperties().getPropertyBoolean("OnFlySectioning.ConfigAsWell", false);
    122        return true;
    123    }
    124}