001    package net.sf.cpsolver.coursett.criteria;
002    
003    import java.util.Collection;
004    import java.util.HashSet;
005    import java.util.Set;
006    
007    import net.sf.cpsolver.coursett.constraint.GroupConstraint;
008    import net.sf.cpsolver.coursett.model.Lecture;
009    import net.sf.cpsolver.coursett.model.Placement;
010    import net.sf.cpsolver.coursett.model.TimetableModel;
011    import 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 Tomas Muller<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     */
038    public 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 += Math.min(0, 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            }
083            return bounds;
084        }
085        
086        @Override
087        public double[] getBounds(Collection<Lecture> variables) {
088            double[] bounds = new double[] { 0.0, 0.0 };
089            Set<GroupConstraint> constraints = new HashSet<GroupConstraint>();
090            for (Lecture lect: variables) {
091                for (GroupConstraint gc: lect.groupConstraints()) {
092                    if (!gc.isHard() && constraints.add(gc))
093                        bounds[0] -= Math.abs(gc.getPreference());
094                }
095            }
096            return bounds;
097        }
098    }