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' summary='Related Solver Parameters'>
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 * @version CourseTT 1.3 (University Course Timetabling)<br>
045 *          Copyright (C) 2006 - 2014 Tomáš Müller<br>
046 *          <a href="mailto:muller@unitime.org">muller@unitime.org</a><br>
047 *          <a href="http://muller.unitime.org">http://muller.unitime.org</a><br>
048 * <br>
049 *          This library is free software; you can redistribute it and/or modify
050 *          it under the terms of the GNU Lesser General Public License as
051 *          published by the Free Software Foundation; either version 3 of the
052 *          License, or (at your option) any later version. <br>
053 * <br>
054 *          This library is distributed in the hope that it will be useful, but
055 *          WITHOUT ANY WARRANTY; without even the implied warranty of
056 *          MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
057 *          Lesser General Public License for more details. <br>
058 * <br>
059 *          You should have received a copy of the GNU Lesser General Public
060 *          License along with this library; if not see
061 *          <a href='http://www.gnu.org/licenses/'>http://www.gnu.org/licenses/</a>.
062 */
063
064public class OnFlySectioning implements ModelListener<Lecture, Placement> {
065    private TimetableModel iModel;
066    private boolean iRecursive = true;
067    private boolean iConfigAsWell = false;
068
069    /**
070     * Constructor
071     * 
072     * @param model
073     *            timetabling model
074     */
075    public OnFlySectioning(TimetableModel model) {
076        iModel = model;
077    }
078
079    @Override
080    public void variableAdded(Lecture variable) {
081    }
082
083    @Override
084    public void variableRemoved(Lecture variable) {
085    }
086
087    @Override
088    public void constraintAdded(Constraint<Lecture, Placement> constraint) {
089    }
090
091    @Override
092    public void constraintRemoved(Constraint<Lecture, Placement> constraint) {
093    }
094
095    @Override
096    public void beforeAssigned(Assignment<Lecture, Placement> assignment, long iteration, Placement value) {
097    }
098
099    @Override
100    public void beforeUnassigned(Assignment<Lecture, Placement> assignment, long iteration, Placement value) {
101    }
102
103    /**
104     * {@link FinalSectioning#resection(Assignment, Lecture, boolean, boolean)} is called
105     * when given iteration number is greater than zero.
106     */
107    @Override
108    public void afterAssigned(Assignment<Lecture, Placement> assignment, long iteration, Placement value) {
109        if (iteration > 0 && assignment instanceof DefaultSingleAssignment)
110            iModel.getStudentSectioning().resection(assignment, value.variable(), iRecursive, iConfigAsWell);
111    }
112
113    @Override
114    public void afterUnassigned(Assignment<Lecture, Placement> assignment, long iteration, Placement value) {
115    }
116
117    /**
118     * Initialization
119     */
120    @Override
121    public boolean init(Solver<Lecture, Placement> solver) {
122        iRecursive = solver.getProperties().getPropertyBoolean("OnFlySectioning.Recursive", true);
123        iConfigAsWell = solver.getProperties().getPropertyBoolean("OnFlySectioning.ConfigAsWell", false);
124        return true;
125    }
126}