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 * @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 abstract class InstructorSchedulingCriterion extends AbstractCriterion<TeachingRequest.Variable, TeachingAssignment> {
039
040    @Override
041    public void getInfo(Assignment<TeachingRequest.Variable, TeachingAssignment> assignment, Map<String, String> info) {
042        double val = getValue(assignment);
043        double[] bounds = getBounds(assignment);
044        if (bounds[0] <= val && val <= bounds[1] && bounds[0] < bounds[1])
045            info.put(getName(), getPerc(val, bounds[0], bounds[1]) + "% (" + sDoubleFormat.format(val) + ")");
046        else if (bounds[1] <= val && val <= bounds[0] && bounds[1] < bounds[0])
047            info.put(getName(), getPercRev(val, bounds[1], bounds[0]) + "% (" + sDoubleFormat.format(val) + ")");
048        else if (bounds[0] != val || val != bounds[1])
049            info.put(getName(), sDoubleFormat.format(val));
050    }
051    
052    @Override
053    public void getInfo(Assignment<TeachingRequest.Variable, TeachingAssignment> assignment, Map<String, String> info, Collection<TeachingRequest.Variable> variables) {
054        double val = getValue(assignment, variables);
055        double[] bounds = getBounds(assignment, variables);
056        if (bounds[0] <= val && val <= bounds[1] && bounds[0] < bounds[1])
057            info.put(getName(), getPerc(val, bounds[0], bounds[1]) + "% (" + sDoubleFormat.format(val) + ")");
058        else if (bounds[1] <= val && val <= bounds[0] && bounds[1] < bounds[0])
059            info.put(getName(), getPercRev(val, bounds[1], bounds[0]) + "% (" + sDoubleFormat.format(val) + ")");
060        else if (bounds[0] != val || val != bounds[1])
061            info.put(getName(), sDoubleFormat.format(val));
062    }
063    
064    /**
065     * Instructor of a sub-problem
066     * @param assignment current instructors
067     * @param variables sub-problem
068     * @return instructors that can be used by the given teaching requests
069     */
070    public Set<Instructor> getInstructors(Assignment<TeachingRequest.Variable, TeachingAssignment> assignment, Collection<TeachingRequest.Variable> variables) {
071        Set<Instructor> instructors = new HashSet<Instructor>();
072        for (TeachingRequest.Variable req: variables)
073            for (TeachingAssignment ta: req.values(assignment))
074                instructors.add(ta.getInstructor());
075        return instructors;
076    }
077    
078    /**
079     * Assigned instructors of a sub-problem
080     * @param assignment current instructors
081     * @param variables sub-problem
082     * @return instructors that can be used by the given teaching requests
083     */
084    public Set<Instructor> getAssignedInstructors(Assignment<TeachingRequest.Variable, TeachingAssignment> assignment, Collection<TeachingRequest.Variable> variables) {
085        Set<Instructor> instructors = new HashSet<Instructor>();
086        for (TeachingRequest.Variable req: variables) {
087            TeachingAssignment ta = assignment.getValue(req);
088            if (ta != null)
089                instructors.add(ta.getInstructor());
090        }
091        return instructors;
092    }
093}