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