    001package org.cpsolver.coursett.criteria;
    002
    003import java.util.Collection;
    004import java.util.HashSet;
    005import java.util.Set;
    006
    007import org.cpsolver.coursett.constraint.GroupConstraint;
    008import org.cpsolver.coursett.model.Lecture;
    009import org.cpsolver.coursett.model.Placement;
    010import org.cpsolver.coursett.model.TimetableModel;
    011import org.cpsolver.ifs.assignment.Assignment;
    012import org.cpsolver.ifs.util.DataProperties;
    013
    014
    015/**
    016 * Distribution preferences. This criterion counts how well the soft distribution preferences
    017 * are met. This is a sum of {@link GroupConstraint#getPreference()} of all soft distribution
    018 * (group) constraints.
    019 * <br>
    020 *
    021 * @author  Tomáš Müller
    022 * @version CourseTT 1.3 (University Course Timetabling)<br>
    023 *          Copyright (C) 2006 - 2014 Tomáš Müller<br>
    024 *          <a href="mailto:muller@unitime.org">muller@unitime.org</a><br>
    025 *          <a href="http://muller.unitime.org">http://muller.unitime.org</a><br>
    026 * <br>
    027 *          This library is free software; you can redistribute it and/or modify
    028 *          it under the terms of the GNU Lesser General Public License as
    029 *          published by the Free Software Foundation; either version 3 of the
    030 *          License, or (at your option) any later version. <br>
    031 * <br>
    032 *          This library is distributed in the hope that it will be useful, but
    033 *          WITHOUT ANY WARRANTY; without even the implied warranty of
    034 *          MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
    035 *          Lesser General Public License for more details. <br>
    036 * <br>
    037 *          You should have received a copy of the GNU Lesser General Public
    038 *          License along with this library; if not see
    039 *          <a href='http://www.gnu.org/licenses/'>http://www.gnu.org/licenses/</a>.
    040 */
    041public class DistributionPreferences extends TimetablingCriterion {
    042
    043    public DistributionPreferences() {
    044        setValueUpdateType(ValueUpdateType.NoUpdate);
    045    }
    046
    047    @Override
    048    public double getWeightDefault(DataProperties config) {
    049        return config.getPropertyDouble("Comparator.ContrPreferenceWeight", 1.0);
    050    }
    051
    052    @Override
    053    public String getPlacementSelectionWeightName() {
    054        return "Placement.ConstrPreferenceWeight";
    055    }
    056
    057
    058    @Override
    059    public double getValue(Assignment<Lecture, Placement> assignment, Placement value, Set<Placement> conflicts) {
    060        double ret = 0.0;
    061        for (GroupConstraint gc : value.variable().groupConstraints())
    062            ret += gc.getCurrentPreference(assignment, value);
    063        return ret;
    064    }
    065
    066    @Override
    067    public double getValue(Assignment<Lecture, Placement> assignment, Collection<Lecture> variables) {
    068        double ret = 0;
    069        Set<GroupConstraint> constraints = new HashSet<GroupConstraint>();
    070        for (Lecture lect: variables) {
    071            for (GroupConstraint gc: lect.groupConstraints()) {
    072                if (!constraints.add(gc)) continue;
    073                ret += gc.getContext(assignment).getPreference();
    074            }
    075        }
    076        return ret;
    077    }
    078
    079    @Override
    080    protected double[] computeBounds(Assignment<Lecture, Placement> assignment) {
    081        double[] bounds = new double[] { 0.0, 0.0 };
    082        for (GroupConstraint gc: ((TimetableModel)getModel()).getGroupConstraints()) {
    083            if (!gc.isHard()) {
    084                bounds[1] += Math.abs(gc.getPreference()) * (1 + (gc.variables().size() * (gc.variables().size() - 1)) / 2);
    085            }
    086        }
    087        return bounds;
    088    }
    089
    090    @Override
    091    public double[] getBounds(Assignment<Lecture, Placement> assignment, Collection<Lecture> variables) {
    092        double[] bounds = new double[] { 0.0, 0.0 };
    093        Set<GroupConstraint> constraints = new HashSet<GroupConstraint>();
    094        for (Lecture lect: variables) {
    095            for (GroupConstraint gc: lect.groupConstraints()) {
    096                if (!gc.isHard() && constraints.add(gc)) {
    097                    bounds[1] += Math.abs(gc.getPreference()) * (1 + (gc.variables().size() * (gc.variables().size() - 1)) / 2);
    098                }
    099            }
    100        }
    101        return bounds;
    102    }
    103}