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