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