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