001package org.cpsolver.instructor.criteria;
002
003import java.util.Collection;
004import java.util.HashSet;
005import java.util.Map;
006import java.util.Set;
007
008import org.cpsolver.ifs.assignment.Assignment;
009import org.cpsolver.ifs.criteria.AbstractCriterion;
010import org.cpsolver.ifs.criteria.Criterion;
011import org.cpsolver.instructor.model.Instructor;
012import org.cpsolver.instructor.model.TeachingAssignment;
013import org.cpsolver.instructor.model.TeachingRequest;
014
015/**
016 * Abstract instructor scheduling criterion. Implementing {@link Criterion#getInfo(Assignment, Map)} and
017 * a few other methods.
018 * 
019 * @author  Tomáš Müller
020 * @version IFS 1.3 (Instructor Sectioning)<br>
021 *          Copyright (C) 2016 Tomáš Müller<br>
022 *          <a href="mailto:muller@unitime.org">muller@unitime.org</a><br>
023 *          <a href="http://muller.unitime.org">http://muller.unitime.org</a><br>
024 * <br>
025 *          This library is free software; you can redistribute it and/or modify
026 *          it under the terms of the GNU Lesser General Public License as
027 *          published by the Free Software Foundation; either version 3 of the
028 *          License, or (at your option) any later version. <br>
029 * <br>
030 *          This library is distributed in the hope that it will be useful, but
031 *          WITHOUT ANY WARRANTY; without even the implied warranty of
032 *          MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
033 *          Lesser General Public License for more details. <br>
034 * <br>
035 *          You should have received a copy of the GNU Lesser General Public
036 *          License along with this library; if not see
037 *          <a href='http://www.gnu.org/licenses/'>http://www.gnu.org/licenses/</a>.
038 */
039public abstract class InstructorSchedulingCriterion extends AbstractCriterion<TeachingRequest.Variable, TeachingAssignment> {
040
041    @Override
042    public void getInfo(Assignment<TeachingRequest.Variable, TeachingAssignment> assignment, Map<String, String> info) {
043        double val = getValue(assignment);
044        double[] bounds = getBounds(assignment);
045        if (bounds[0] <= val && val <= bounds[1] && bounds[0] < bounds[1])
046            info.put(getName(), getPerc(val, bounds[0], bounds[1]) + "% (" + sDoubleFormat.format(val) + ")");
047        else if (bounds[1] <= val && val <= bounds[0] && bounds[1] < bounds[0])
048            info.put(getName(), getPercRev(val, bounds[1], bounds[0]) + "% (" + sDoubleFormat.format(val) + ")");
049        else if (bounds[0] != val || val != bounds[1])
050            info.put(getName(), sDoubleFormat.format(val));
051    }
052    
053    @Override
054    public void getInfo(Assignment<TeachingRequest.Variable, TeachingAssignment> assignment, Map<String, String> info, Collection<TeachingRequest.Variable> variables) {
055        double val = getValue(assignment, variables);
056        double[] bounds = getBounds(assignment, variables);
057        if (bounds[0] <= val && val <= bounds[1] && bounds[0] < bounds[1])
058            info.put(getName(), getPerc(val, bounds[0], bounds[1]) + "% (" + sDoubleFormat.format(val) + ")");
059        else if (bounds[1] <= val && val <= bounds[0] && bounds[1] < bounds[0])
060            info.put(getName(), getPercRev(val, bounds[1], bounds[0]) + "% (" + sDoubleFormat.format(val) + ")");
061        else if (bounds[0] != val || val != bounds[1])
062            info.put(getName(), sDoubleFormat.format(val));
063    }
064    
065    /**
066     * Instructor of a sub-problem
067     * @param assignment current instructors
068     * @param variables sub-problem
069     * @return instructors that can be used by the given teaching requests
070     */
071    public Set<Instructor> getInstructors(Assignment<TeachingRequest.Variable, TeachingAssignment> assignment, Collection<TeachingRequest.Variable> variables) {
072        Set<Instructor> instructors = new HashSet<Instructor>();
073        for (TeachingRequest.Variable req: variables)
074            for (TeachingAssignment ta: req.values(assignment))
075                instructors.add(ta.getInstructor());
076        return instructors;
077    }
078    
079    /**
080     * Assigned instructors of a sub-problem
081     * @param assignment current instructors
082     * @param variables sub-problem
083     * @return instructors that can be used by the given teaching requests
084     */
085    public Set<Instructor> getAssignedInstructors(Assignment<TeachingRequest.Variable, TeachingAssignment> assignment, Collection<TeachingRequest.Variable> variables) {
086        Set<Instructor> instructors = new HashSet<Instructor>();
087        for (TeachingRequest.Variable req: variables) {
088            TeachingAssignment ta = assignment.getValue(req);
089            if (ta != null)
090                instructors.add(ta.getInstructor());
091        }
092        return instructors;
093    }
094}