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.SameLinkConstraint;
011import org.cpsolver.instructor.model.TeachingAssignment;
012import org.cpsolver.instructor.model.TeachingRequest;
013
014/**
015 * Same Link. This criterion counts how well are the soft Same Link Constraints (see {@link SameLinkConstraint}) met.
016 * 
017 * @author  Tomáš Müller
018 * @version IFS 1.3 (Instructor Sectioning)<br>
019 *          Copyright (C) 2016 Tomáš Müller<br>
020 *          <a href="mailto:muller@unitime.org">muller@unitime.org</a><br>
021 *          <a href="http://muller.unitime.org">http://muller.unitime.org</a><br>
022 * <br>
023 *          This library is free software; you can redistribute it and/or modify
024 *          it under the terms of the GNU Lesser General Public License as
025 *          published by the Free Software Foundation; either version 3 of the
026 *          License, or (at your option) any later version. <br>
027 * <br>
028 *          This library is distributed in the hope that it will be useful, but
029 *          WITHOUT ANY WARRANTY; without even the implied warranty of
030 *          MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
031 *          Lesser General Public License for more details. <br>
032 * <br>
033 *          You should have received a copy of the GNU Lesser General Public
034 *          License along with this library; if not see
035 *          <a href='http://www.gnu.org/licenses/'>http://www.gnu.org/licenses/</a>.
036 */
037public class SameLink extends InstructorSchedulingCriterion {
038
039    public SameLink() {
040        setValueUpdateType(ValueUpdateType.NoUpdate);
041    }
042
043    @Override
044    public double getWeightDefault(DataProperties config) {
045        return 100.0;
046    }
047
048    @Override
049    public double getValue(Assignment<TeachingRequest.Variable, TeachingAssignment> assignment, TeachingAssignment value, Set<TeachingAssignment> conflicts) {
050        double ret = 0.0;
051        for (Constraint<TeachingRequest.Variable, TeachingAssignment> c : value.variable().constraints())
052            if (c instanceof SameLinkConstraint)
053                ret += ((SameLinkConstraint)c).getCurrentPreference(assignment, value);
054        return ret;
055    }
056
057    @Override
058    public double getValue(Assignment<TeachingRequest.Variable, TeachingAssignment> assignment, Collection<TeachingRequest.Variable> variables) {
059        double ret = 0;
060        Set<Constraint<TeachingRequest.Variable, TeachingAssignment>> constraints = new HashSet<Constraint<TeachingRequest.Variable, TeachingAssignment>>();
061        for (TeachingRequest.Variable req: variables) {
062            for (Constraint<TeachingRequest.Variable, TeachingAssignment> c : req.constraints()) {
063                if (c instanceof SameLinkConstraint && constraints.add(c))
064                    ret += ((SameLinkConstraint)c).getContext(assignment).getPreference();
065            }
066        }
067        return ret;
068    }
069    
070    @Override
071    protected double[] computeBounds(Assignment<TeachingRequest.Variable, TeachingAssignment> assignment) {
072        double[] bounds = new double[] { 0.0, 0.0 };
073        for (Constraint<TeachingRequest.Variable, TeachingAssignment> c: getModel().constraints()) {
074            if (c instanceof SameLinkConstraint && !c.isHard())
075                bounds[1] += Math.abs(((SameLinkConstraint)c).getPreference()) * (c.variables().size() - 1);
076        }
077        return bounds;
078    }
079    
080    @Override
081    public double[] getBounds(Assignment<TeachingRequest.Variable, TeachingAssignment> assignment, Collection<TeachingRequest.Variable> variables) {
082        double[] bounds = new double[] { 0.0, 0.0 };
083        Set<Constraint<TeachingRequest.Variable, TeachingAssignment>> constraints = new HashSet<Constraint<TeachingRequest.Variable, TeachingAssignment>>();
084        for (TeachingRequest.Variable req: variables) {
085            for (Constraint<TeachingRequest.Variable, TeachingAssignment> c : req.constraints()) {
086                if (c instanceof SameLinkConstraint && !c.isHard() && constraints.add(c))
087                    bounds[1] += Math.abs(((SameLinkConstraint)c).getPreference()) * (c.variables().size() - 1);
088            }
089        }
090        return bounds;
091    }
092
093    @Override
094    public String getAbbreviation() {
095        return "SameLink";
096    }
097}