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