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.model.Constraint;
009import org.cpsolver.ifs.util.DataProperties;
010import org.cpsolver.instructor.constraints.SameInstructorConstraint;
011import org.cpsolver.instructor.model.TeachingAssignment;
012import org.cpsolver.instructor.model.TeachingRequest;
013
014/**
015 * Same Instructor. This criterion counts how well are the soft Same Instructor Constraints (see {@link SameInstructorConstraint}) met.
016 * 
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 SameInstructor extends InstructorSchedulingCriterion {
037
038    public SameInstructor() {
039        setValueUpdateType(ValueUpdateType.NoUpdate);
040    }
041
042    @Override
043    public double getWeightDefault(DataProperties config) {
044        return 10.0;
045    }
046
047    @Override
048    public double getValue(Assignment<TeachingRequest.Variable, TeachingAssignment> assignment, TeachingAssignment value, Set<TeachingAssignment> conflicts) {
049        double ret = 0.0;
050        for (Constraint<TeachingRequest.Variable, TeachingAssignment> c : value.variable().constraints())
051            if (c instanceof SameInstructorConstraint)
052                ret += ((SameInstructorConstraint)c).getCurrentPreference(assignment, value);
053        return ret;
054    }
055
056    @Override
057    public double getValue(Assignment<TeachingRequest.Variable, TeachingAssignment> assignment, Collection<TeachingRequest.Variable> variables) {
058        double ret = 0;
059        Set<Constraint<TeachingRequest.Variable, TeachingAssignment>> constraints = new HashSet<Constraint<TeachingRequest.Variable, TeachingAssignment>>();
060        for (TeachingRequest.Variable req: variables) {
061            for (Constraint<TeachingRequest.Variable, TeachingAssignment> c : req.constraints()) {
062                if (c instanceof SameInstructorConstraint && constraints.add(c))
063                    ret += ((SameInstructorConstraint)c).getContext(assignment).getPreference();
064            }
065        }
066        return ret;
067    }
068    
069    @Override
070    protected double[] computeBounds(Assignment<TeachingRequest.Variable, TeachingAssignment> assignment) {
071        double[] bounds = new double[] { 0.0, 0.0 };
072        for (Constraint<TeachingRequest.Variable, TeachingAssignment> c: getModel().constraints()) {
073            if (c instanceof SameInstructorConstraint && !c.isHard())
074                bounds[1] += Math.abs(((SameInstructorConstraint)c).getPreference()) * (1 + (c.variables().size() * (c.variables().size() - 1)) / 2);
075        }
076        return bounds;
077    }
078    
079    @Override
080    public double[] getBounds(Assignment<TeachingRequest.Variable, TeachingAssignment> assignment, Collection<TeachingRequest.Variable> variables) {
081        double[] bounds = new double[] { 0.0, 0.0 };
082        Set<Constraint<TeachingRequest.Variable, TeachingAssignment>> constraints = new HashSet<Constraint<TeachingRequest.Variable, TeachingAssignment>>();
083        for (TeachingRequest.Variable req: variables) {
084            for (Constraint<TeachingRequest.Variable, TeachingAssignment> c : req.constraints()) {
085                if (c instanceof SameInstructorConstraint && !c.isHard() && constraints.add(c))
086                    bounds[1] += Math.abs(((SameInstructorConstraint)c).getPreference()) * ((c.variables().size() * (c.variables().size() - 1)) / 2);
087            }
088        }
089        return bounds;
090    }
091
092    @Override
093    public String getAbbreviation() {
094        return "SameInstructor";
095    }
096}