    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.constraints.GroupConstraint;
    009import org.cpsolver.instructor.constraints.GroupConstraint.Distribution;
    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 * Distributions criterion. Counting violated soft group constraints. See {@link GroupConstraint}
    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 Distributions extends InstructorSchedulingCriterion {
    039
    040    public Distributions() {
    041        setValueUpdateType(ValueUpdateType.NoUpdate);
    042    }
    043
    044    @Override
    045    public double getWeightDefault(DataProperties config) {
    046        return 1.0;
    047    }
    048
    049    @Override
    050    public double getValue(Assignment<TeachingRequest.Variable, TeachingAssignment> assignment, TeachingAssignment value, Set<TeachingAssignment> conflicts) {
    051        double ret = 0.0;
    052        for (Distribution d: value.getInstructor().getDistributions()) {
    053            if (!d.isHard()) {
    054                ret += Math.abs(d.getPenalty()) * d.getType().getValue(d, assignment, value.getInstructor(), value);
    055            }
    056        }
    057        return ret;
    058    }
    059
    060    @Override
    061    public double getValue(Assignment<TeachingRequest.Variable, TeachingAssignment> assignment, Collection<TeachingRequest.Variable> variables) {
    062        double value = 0.0;
    063        for (Instructor instructor: getAssignedInstructors(assignment, variables))
    064            for (Distribution d: instructor.getDistributions())
    065                if (!d.isHard())
    066                    value += Math.abs(d.getPenalty()) * d.getType().getValue(d, assignment, instructor, null);
    067        return value;
    068    }
    069
    070    @Override
    071    protected double[] computeBounds(Assignment<TeachingRequest.Variable, TeachingAssignment> assignment) {
    072        double[] bounds = new double[] { 0.0, 0.0 };
    073        for (Instructor instructor: ((InstructorSchedulingModel)getModel()).getInstructors()) {
    074            for (Distribution d: instructor.getDistributions())
    075                if (!d.isHard())
    076                    bounds[1] += Math.abs(d.getPenalty());
    077        }
    078        return bounds;
    079    }
    080
    081    @Override
    082    public double[] getBounds(Assignment<TeachingRequest.Variable, TeachingAssignment> assignment, Collection<TeachingRequest.Variable> variables) {
    083        double[] bounds = new double[] { 0.0, 0.0 };
    084        for (Instructor instructor: getInstructors(assignment, variables)) {
    085            for (Distribution d: instructor.getDistributions())
    086                if (!d.isHard())
    087                    bounds[1] += Math.abs(d.getPenalty());
    088        }
    089        return bounds;
    090    }
    091}