    001package org.cpsolver.coursett.criteria;
    002
    003import org.cpsolver.coursett.heuristics.PlacementSelection;
    004import org.cpsolver.coursett.model.Lecture;
    005import org.cpsolver.coursett.model.Placement;
    006import org.cpsolver.ifs.criteria.AbstractCriterion;
    007import org.cpsolver.ifs.util.DataProperties;
    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.3 (University Course Timetabling)<br>
    015 *          Copyright (C) 2006 - 2014 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    private Double[][] iPlacementSelectionAdjusts = null;
    036
    037    @Override
    038    public void configure(DataProperties properties) {
    039        super.configure(properties);
    040        if (getPlacementSelectionWeightName() != null) {
    041            iPlacementSelectionWeight = new double[] { 0.0, 0.0, 0.0 };
    042            for (int i = 0; i < 3; i++)
    043                iPlacementSelectionWeight[i] = properties.getPropertyDouble(getPlacementSelectionWeightName() + (1 + i), getPlacementSelectionWeightDefault(i));
    044            iPlacementSelectionAdjusts = new Double[][] { null, null, null};
    045            for (int i = 0; i < 3; i++) {
    046                iPlacementSelectionAdjusts[i] = properties.getPropertyDoubleArry(getPlacementSelectionAdjustmentsName() + (1 + i), properties.getPropertyDoubleArry(getPlacementSelectionAdjustmentsName(), null));
    047            }
    048        }
    049    }
    050
    051    public String getPlacementSelectionWeightName() {
    052        return "Placement." + getClass().getName().substring(1 + getClass().getName().lastIndexOf('.')) + "Weight";
    053    }
    054
    055    public String getPlacementSelectionAdjustmentsName() {
    056        return getPlacementSelectionWeightName() + "Adjustments";
    057    }
    058
    059    public double getPlacementSelectionWeight(int level, int idx) {
    060        double w = (iPlacementSelectionWeight == null ? 0.0 : iPlacementSelectionWeight[level]);
    061        if (idx < 0 || iPlacementSelectionAdjusts == null || iPlacementSelectionAdjusts[level] == null || idx >= iPlacementSelectionAdjusts[level].length || iPlacementSelectionAdjusts[level][idx] == null) return w;
    062        return w * iPlacementSelectionAdjusts[level][idx];
    063    }
    064
    065    public double getPlacementSelectionWeightDefault(int level) {
    066        return (level <= 1 ? getWeight() : 0.0);
    067    }
    068}