001package org.cpsolver.instructor.criteria;
002
003import java.util.Collection;
004import java.util.HashSet;
005import java.util.Set;
006
007import org.cpsolver.ifs.assignment.Assignment;
008import org.cpsolver.ifs.solver.Solver;
009import org.cpsolver.ifs.util.DataProperties;
010import org.cpsolver.instructor.model.Instructor;
011import org.cpsolver.instructor.model.InstructorSchedulingModel;
012import org.cpsolver.instructor.model.TeachingAssignment;
013import org.cpsolver.instructor.model.TeachingRequest;
014
015/**
016 * Back to Back. This criterion counts how well are the back-to-back preferences that are set on an {@link Instructor} met
017 * (counting {@link Instructor#countBackToBacks(Assignment, TeachingAssignment, double, double)}).
018 * 
019 * @version IFS 1.3 (Instructor Sectioning)<br>
020 *          Copyright (C) 2016 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 */
038public class BackToBack extends InstructorSchedulingCriterion {
039    private double iDiffRoomWeight = 0.8, iDiffTypeWeight = 0.5;
040
041    public BackToBack() {
042        setValueUpdateType(ValueUpdateType.NoUpdate);
043    }
044    
045    @Override
046    public boolean init(Solver<TeachingRequest, TeachingAssignment> solver) {
047        iDiffRoomWeight = solver.getProperties().getPropertyDouble("BackToBack.DifferentRoomWeight", 0.8);
048        iDiffTypeWeight = solver.getProperties().getPropertyDouble("BackToBack.DifferentTypeWeight", 0.5);
049        return super.init(solver);
050    }
051
052    @Override
053    public double getWeightDefault(DataProperties config) {
054        return 1.0;
055    }
056    
057    /**
058     * Different room weight
059     * @return penalty for teaching two back-to-back that are in different rooms
060     */
061    public double getDifferentRoomWeight() { return iDiffRoomWeight; }
062
063    /**
064     * Different instructional type weight
065     * @return penalty for teaching two back-to-back that are of different instructional type
066     */
067    public double getDifferentTypeWeight() { return iDiffTypeWeight; }
068
069    @Override
070    public double getValue(Assignment<TeachingRequest, TeachingAssignment> assignment, TeachingAssignment value, Set<TeachingAssignment> conflicts) {
071        return value.getInstructor().countBackToBacks(assignment, value, iDiffRoomWeight, iDiffTypeWeight);
072    }
073    
074    @Override
075    protected double[] computeBounds(Assignment<TeachingRequest, TeachingAssignment> assignment) {
076        double[] bounds = new double[] { 0.0, 0.0 };
077        for (Instructor instructor: ((InstructorSchedulingModel)getModel()).getInstructors()) {
078            bounds[1] += Math.abs(instructor.getBackToBackPreference());
079        }
080        return bounds;
081    }
082    
083    @Override
084    public double[] getBounds(Assignment<TeachingRequest, TeachingAssignment> assignment, Collection<TeachingRequest> variables) {
085        double[] bounds = new double[] { 0.0, 0.0 };
086        for (Instructor instructor: getInstructors(assignment, variables)) {
087            bounds[1] += Math.abs(instructor.getBackToBackPreference());
088        }
089        return bounds;
090    }
091
092    @Override
093    public double getValue(Assignment<TeachingRequest, TeachingAssignment> assignment, Collection<TeachingRequest> variables) {
094        double value = 0.0;
095        Set<Instructor> instructors = new HashSet<Instructor>();
096        for (Instructor instructor: ((InstructorSchedulingModel)getModel()).getInstructors()) {
097            if (instructors.add(instructor)) {
098                value += instructor.getContext(assignment).countBackToBackPreference(iDiffRoomWeight, iDiffTypeWeight);
099            }
100        }
101        return value;
102    }
103
104    @Override
105    public String getAbbreviation() {
106        return "Back2Back";
107    }
108}