001package net.sf.cpsolver.coursett.criteria;
002
003import net.sf.cpsolver.coursett.heuristics.PlacementSelection;
004import net.sf.cpsolver.coursett.model.Lecture;
005import net.sf.cpsolver.coursett.model.Placement;
006import net.sf.cpsolver.ifs.criteria.AbstractCriterion;
007import 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 Tomáš Müller<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 */
033public 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
059    /** Abbreviated name of the criterion for {@link TimetablingCriterion#toString()}. */
060    public String getAbbreviation() {
061        return getName().replaceAll("[a-z ]","");
062    }
063    
064    @Override
065    public String toString() {
066        double val = getValue();
067        if (Math.abs(val) < 0.005 || getWeight() <= 0.01) return "";
068        double[] bounds = getBounds();
069        if (bounds[0] <= val && val <= bounds[1] && bounds[0] < bounds[1] && getName().endsWith(" Preferences"))
070            return getAbbreviation() + ":" + getPerc(val, bounds[0], bounds[1]) + "%";
071        else if (bounds[1] <= val && val <= bounds[0] && bounds[1] < bounds[0] && getName().endsWith(" Preferences"))
072            return getAbbreviation() + ":" + getPercRev(val, bounds[1], bounds[0]) + "%";
073        else if (bounds[0] != val || val != bounds[1])
074            return getAbbreviation() + ":" + sDoubleFormat.format(getValue());
075        else
076            return "";
077    }
078}