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.util.DataProperties;
009import org.cpsolver.instructor.model.Instructor;
010import org.cpsolver.instructor.model.TeachingAssignment;
011import org.cpsolver.instructor.model.TeachingRequest;
012
013/**
014 * Unused Instructor Load. If an instructor is being used (has at least one teaching assignment),
015 * this criterion can penalize the remaining (unused) load of the instructor. That is the difference
016 * between instructor maximal load and the assigned load of the instructor.
017 * 
018 * @author  Tomáš Müller
019 * @version IFS 1.3 (Instructor Sectioning)<br>
020 *          Copyright (C) 2016 Tomáš Müller<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 UnusedInstructorLoad extends InstructorSchedulingCriterion {
039    
040    public UnusedInstructorLoad() {
041        setValueUpdateType(ValueUpdateType.NoUpdate);
042    }
043    
044    @Override
045    public double getWeightDefault(DataProperties config) {
046        return 100000.0;
047    }
048
049    @Override
050    public double getValue(Assignment<TeachingRequest.Variable, TeachingAssignment> assignment, TeachingAssignment value, Set<TeachingAssignment> conflicts) {
051        Instructor.Context cx = value.getInstructor().getContext(assignment);
052        
053        float load = cx.getLoad();
054        if (!cx.getAssignments().contains(value)) load += value.variable().getRequest().getLoad();
055        if (conflicts != null)
056            for (TeachingAssignment ta: conflicts)
057                if (ta.getInstructor().equals(value.getInstructor())) load -= ta.variable().getRequest().getLoad();
058        
059        return Math.max(0, value.getInstructor().getMaxLoad() - load);
060    }
061    
062    @Override
063    public double getValue(Assignment<TeachingRequest.Variable, TeachingAssignment> assignment, Collection<TeachingRequest.Variable> variables) {
064        double unused = 0.0;
065        Set<Instructor> instructors = new HashSet<Instructor>();
066        for (TeachingRequest.Variable req: variables) {
067            TeachingAssignment ta = assignment.getValue(req);
068            if (ta != null &&instructors.add(ta.getInstructor())) {
069                unused += ta.getInstructor().getMaxLoad() - ta.getInstructor().getContext(assignment).getLoad();
070            }
071        }
072        return unused;
073    }
074
075    @Override
076    public String getAbbreviation() {
077        return "UnusedLoad";
078    }
079}