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