001package org.cpsolver.instructor.constraints;
002
003import java.util.ArrayList;
004import java.util.List;
005import java.util.Set;
006
007import org.cpsolver.ifs.assignment.Assignment;
008import org.cpsolver.ifs.model.GlobalConstraint;
009import org.cpsolver.ifs.util.ToolBox;
010import org.cpsolver.instructor.model.Instructor.Context;
011import org.cpsolver.instructor.model.TeachingAssignment;
012import org.cpsolver.instructor.model.TeachingRequest;
013
014/**
015 * Instructor Constraint. This is the main constraint of the problem, ensuring that an instructor gets a consistent list of 
016 * assignments. It checks for instructor availability, maximal load, time conflicts, and whether the given assignments are of the same
017 * course (if desired).
018 * 
019 * @author  Tomáš Müller
020 * @version IFS 1.3 (Instructor Sectioning)<br>
021 *          Copyright (C) 2016 Tomáš Müller<br>
022 *          <a href="mailto:muller@unitime.org">muller@unitime.org</a><br>
023 *          <a href="http://muller.unitime.org">http://muller.unitime.org</a><br>
024 * <br>
025 *          This library is free software; you can redistribute it and/or modify
026 *          it under the terms of the GNU Lesser General Public License as
027 *          published by the Free Software Foundation; either version 3 of the
028 *          License, or (at your option) any later version. <br>
029 * <br>
030 *          This library is distributed in the hope that it will be useful, but
031 *          WITHOUT ANY WARRANTY; without even the implied warranty of
032 *          MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
033 *          Lesser General Public License for more details. <br>
034 * <br>
035 *          You should have received a copy of the GNU Lesser General Public
036 *          License along with this library; if not see
037 *          <a href='http://www.gnu.org/licenses/'>http://www.gnu.org/licenses/</a>.
038 */
039public class InstructorConstraint extends GlobalConstraint<TeachingRequest.Variable, TeachingAssignment> {
040    
041    /**
042     * Constructor
043     */
044    public InstructorConstraint() {}
045    
046    @Override
047    public void computeConflicts(Assignment<TeachingRequest.Variable, TeachingAssignment> assignment, TeachingAssignment value, Set<TeachingAssignment> conflicts) {
048        Context context = value.getInstructor().getContext(assignment);
049
050        // Check availability
051        if (context.getInstructor().getTimePreference(value.variable().getRequest()).isProhibited()) {
052            conflicts.add(value);
053            return;
054        }
055
056        // Check for overlaps
057        for (TeachingAssignment ta : context.getAssignments()) {
058            if (ta.variable().equals(value.variable()) || conflicts.contains(ta))
059                continue;
060
061            if (ta.variable().getRequest().overlaps(value.variable().getRequest()))
062                conflicts.add(ta);
063        }
064
065        // Same course and/or common
066        for (TeachingAssignment ta : context.getAssignments()) {
067            if (ta.variable().equals(value.variable()) || conflicts.contains(ta))
068                continue;
069            if (ta.variable().getRequest().isSameCourseViolated(value.variable().getRequest()) || ta.variable().getRequest().isSameCommonViolated(value.variable().getRequest()))
070                conflicts.add(ta);
071        }
072        
073        // Check load
074        float load = value.variable().getRequest().getLoad();
075        List<TeachingAssignment> adepts = new ArrayList<TeachingAssignment>();
076        for (TeachingAssignment ta : context.getAssignments()) {
077            if (ta.variable().equals(value.variable()) || conflicts.contains(ta))
078                continue;
079
080            adepts.add(ta);
081            load += ta.variable().getRequest().getLoad();
082        }
083        while (load > context.getInstructor().getMaxLoad()) {
084            if (adepts.isEmpty()) {
085                conflicts.add(value);
086                break;
087            }
088            TeachingAssignment conflict = ToolBox.random(adepts);
089            load -= conflict.variable().getRequest().getLoad();
090            adepts.remove(conflict);
091            conflicts.add(conflict);
092        }
093    }
094    
095    @Override
096    public String getName() {
097        return "Instructor Constraint";
098    }
099    
100    @Override
101    public String toString() {
102        return getName();
103    }
104}