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