001    package net.sf.cpsolver.coursett.criteria;
002    
003    import net.sf.cpsolver.coursett.heuristics.PlacementSelection;
004    import net.sf.cpsolver.coursett.model.Lecture;
005    import net.sf.cpsolver.coursett.model.Placement;
006    import net.sf.cpsolver.ifs.criteria.AbstractCriterion;
007    import net.sf.cpsolver.ifs.solver.Solver;
008    
009    /**
010     * Abstract class for all timetabling criteria. On top of the {@link AbstractCriterion}, it provides
011     * weights for the {@link PlacementSelection} heuristics.
012     * <br>
013     * 
014     * @version CourseTT 1.2 (University Course Timetabling)<br>
015     *          Copyright (C) 2006 - 2011 Tomas Muller<br>
016     *          <a href="mailto:muller@unitime.org">muller@unitime.org</a><br>
017     *          <a href="http://muller.unitime.org">http://muller.unitime.org</a><br>
018     * <br>
019     *          This library is free software; you can redistribute it and/or modify
020     *          it under the terms of the GNU Lesser General Public License as
021     *          published by the Free Software Foundation; either version 3 of the
022     *          License, or (at your option) any later version. <br>
023     * <br>
024     *          This library is distributed in the hope that it will be useful, but
025     *          WITHOUT ANY WARRANTY; without even the implied warranty of
026     *          MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
027     *          Lesser General Public License for more details. <br>
028     * <br>
029     *          You should have received a copy of the GNU Lesser General Public
030     *          License along with this library; if not see
031     *          <a href='http://www.gnu.org/licenses/'>http://www.gnu.org/licenses/</a>.
032     */
033    public abstract class TimetablingCriterion extends AbstractCriterion<Lecture, Placement> {
034        private double[] iPlacementSelectionWeight = null;
035    
036        @Override
037        public boolean init(Solver<Lecture, Placement> solver) {
038            super.init(solver);
039            if (getPlacementSelectionWeightName() != null) {
040                iPlacementSelectionWeight = new double[] { 0.0, 0.0, 0.0 };
041                for (int i = 0; i < 3; i++)
042                    iPlacementSelectionWeight[i] = solver.getProperties().getPropertyDouble(getPlacementSelectionWeightName() + (1 + i), getPlacementSelectionWeightDefault(i));
043            }
044            return true;
045        }
046        
047        public String getPlacementSelectionWeightName() {
048            return "Placement." + getClass().getName().substring(1 + getClass().getName().lastIndexOf('.')) + "Weight";
049        }
050        
051        public double getPlacementSelectionWeight(int level) {
052            return iPlacementSelectionWeight == null ? 0.0 : iPlacementSelectionWeight[level];
053        }
054        
055        public double getPlacementSelectionWeightDefault(int level) {
056            return (level <= 1 ? getWeight() : 0.0);
057        }
058    }