001package org.cpsolver.instructor.criteria;
002
003import java.util.Collection;
004import java.util.Set;
005
006import org.cpsolver.ifs.assignment.Assignment;
007import org.cpsolver.ifs.util.DataProperties;
008import org.cpsolver.instructor.model.Instructor;
009import org.cpsolver.instructor.model.TeachingAssignment;
010import org.cpsolver.instructor.model.TeachingRequest;
011
012/**
013 * Same Course Preferences. This criterion counts how well are the same course preferences that are set on a {@link TeachingRequest} met
014 * (counting {@link TeachingRequest#getSameCoursePenalty(TeachingRequest)}).
015 * 
016 * @version IFS 1.3 (Instructor Sectioning)<br>
017 *          Copyright (C) 2016 Tomáš Müller<br>
018 *          <a href="mailto:muller@unitime.org">muller@unitime.org</a><br>
019 *          <a href="http://muller.unitime.org">http://muller.unitime.org</a><br>
020 * <br>
021 *          This library is free software; you can redistribute it and/or modify
022 *          it under the terms of the GNU Lesser General Public License as
023 *          published by the Free Software Foundation; either version 3 of the
024 *          License, or (at your option) any later version. <br>
025 * <br>
026 *          This library is distributed in the hope that it will be useful, but
027 *          WITHOUT ANY WARRANTY; without even the implied warranty of
028 *          MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
029 *          Lesser General Public License for more details. <br>
030 * <br>
031 *          You should have received a copy of the GNU Lesser General Public
032 *          License along with this library; if not see
033 *          <a href='http://www.gnu.org/licenses/'>http://www.gnu.org/licenses/</a>.
034 */
035public class SameCourse extends InstructorSchedulingCriterion {
036    
037    public SameCourse() {
038        setValueUpdateType(ValueUpdateType.NoUpdate);
039    }
040
041    @Override
042    public double getWeightDefault(DataProperties config) {
043        return 1000000.0;
044    }
045
046    @Override
047    public double getValue(Assignment<TeachingRequest.Variable, TeachingAssignment> assignment, TeachingAssignment value, Set<TeachingAssignment> conflicts) {
048        Instructor.Context context = value.getInstructor().getContext(assignment);
049        double penalty = 0.0;
050        int pairs = 0;
051        for (TeachingAssignment ta : context.getAssignments()) {
052            if (ta.variable().equals(value.variable()))
053                continue;
054            penalty += value.variable().getRequest().getSameCoursePenalty(ta.variable().getRequest());
055            pairs ++;
056        }
057        return (pairs == 0 ? 0.0 : penalty / pairs);
058    }
059    
060    @Override
061    public double[] getBounds(Assignment<TeachingRequest.Variable, TeachingAssignment> assignment, Collection<TeachingRequest.Variable> variables) {
062        double[] bounds = new double[] { 0.0, 0.0 };
063        for (TeachingRequest.Variable req: variables) {
064            if (!req.getRequest().isSameCourseProhibited() && !req.getRequest().isSameCourseRequired())
065                if (req.getRequest().getSameCoursePreference() < 0) {
066                    bounds[0] += req.getRequest().getSameCoursePreference();
067                } else {
068                    bounds[1] += req.getRequest().getSameCoursePreference();
069                }
070        }
071        return bounds;
072    }
073    
074    @Override
075    public double getValue(Assignment<TeachingRequest.Variable, TeachingAssignment> assignment, Collection<TeachingRequest.Variable> variables) {
076        return 0.5 * super.getValue(assignment, variables);
077    }
078
079    @Override
080    public String getAbbreviation() {
081        return "SameCourse";
082    }
083}