001package net.sf.cpsolver.coursett.criteria;
002
003import java.util.Collection;
004import java.util.Set;
005
006import net.sf.cpsolver.coursett.Constants;
007import net.sf.cpsolver.coursett.model.Lecture;
008import net.sf.cpsolver.coursett.model.Placement;
009import net.sf.cpsolver.coursett.model.TimeLocation;
010import net.sf.cpsolver.ifs.util.DataProperties;
011
012/**
013 * Time preferences. This criterion counts how well the time preferences are met. This is
014 * a sum of {@link TimeLocation#getNormalizedPreference()} of the assigned classes.
015 * <br>
016 * 
017 * @version CourseTT 1.2 (University Course Timetabling)<br>
018 *          Copyright (C) 2006 - 2011 Tomáš Müller<br>
019 *          <a href="mailto:muller@unitime.org">muller@unitime.org</a><br>
020 *          <a href="http://muller.unitime.org">http://muller.unitime.org</a><br>
021 * <br>
022 *          This library is free software; you can redistribute it and/or modify
023 *          it under the terms of the GNU Lesser General Public License as
024 *          published by the Free Software Foundation; either version 3 of the
025 *          License, or (at your option) any later version. <br>
026 * <br>
027 *          This library is distributed in the hope that it will be useful, but
028 *          WITHOUT ANY WARRANTY; without even the implied warranty of
029 *          MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
030 *          Lesser General Public License for more details. <br>
031 * <br>
032 *          You should have received a copy of the GNU Lesser General Public
033 *          License along with this library; if not see
034 *          <a href='http://www.gnu.org/licenses/'>http://www.gnu.org/licenses/</a>.
035 */
036public class TimePreferences extends TimetablingCriterion {
037
038    @Override
039    public double getWeightDefault(DataProperties config) {
040        return config.getPropertyDouble("Comparator.TimePreferenceWeight", 1.0);
041    }
042
043    @Override
044    public String getPlacementSelectionWeightName() {
045        return "Placement.TimePreferenceWeight";
046    }
047    
048    private double preference(Placement value) {
049        int pref = value.getTimeLocation().getPreference();
050        if (pref < Constants.sPreferenceLevelRequired / 2)
051            return value.variable().getMinMaxTimePreference()[0];
052        else if (pref > Constants.sPreferenceLevelProhibited / 2)
053            return value.variable().getMinMaxTimePreference()[1];
054        else
055            return value.getTimeLocation().getNormalizedPreference();
056    }
057
058    @Override
059    public double getValue(Placement value, Set<Placement> conflicts) {
060        if (value.variable().isCommitted()) return 0.0;
061        double ret = value.variable().getWeight() * preference(value);
062        if (conflicts != null)
063            for (Placement conflict: conflicts)
064                ret -= conflict.variable().getWeight() * preference(conflict);
065        return ret;
066    }
067        
068    @Override
069    public double[] getBounds(Collection<Lecture> variables) {
070        double[] bounds = new double[] { 0.0, 0.0 };
071        for (Lecture lect: variables) {
072            if (lect.isCommitted()) continue;
073            double[] p = lect.getMinMaxTimePreference();
074            bounds[0] += lect.getWeight() * p[0];
075            bounds[1] += lect.getWeight() * p[1];
076        }
077        return bounds;
078    }
079
080}