001package org.cpsolver.exam.criteria;
002
003import java.util.Collection;
004import java.util.Set;
005
006import org.cpsolver.exam.criteria.additional.PeriodViolation;
007import org.cpsolver.exam.model.Exam;
008import org.cpsolver.exam.model.ExamPeriodPlacement;
009import org.cpsolver.exam.model.ExamPlacement;
010import org.cpsolver.ifs.assignment.Assignment;
011import org.cpsolver.ifs.solver.Solver;
012import org.cpsolver.ifs.util.DataProperties;
013
014
015/**
016 * Cost for using a period. See {@link ExamPeriodPlacement#getPenalty()} for more details.
017 * <br><br>
018 * A weight for period penalty can be set by problem property
019 * Exams.PeriodWeight, or in the input xml file, property periodWeight.
020 * 
021 * <br>
022 * 
023 * @author  Tomáš Müller
024 * @version ExamTT 1.3 (Examination Timetabling)<br>
025 *          Copyright (C) 2008 - 2014 Tomáš Müller<br>
026 *          <a href="mailto:muller@unitime.org">muller@unitime.org</a><br>
027 *          <a href="http://muller.unitime.org">http://muller.unitime.org</a><br>
028 * <br>
029 *          This library is free software; you can redistribute it and/or modify
030 *          it under the terms of the GNU Lesser General Public License as
031 *          published by the Free Software Foundation; either version 3 of the
032 *          License, or (at your option) any later version. <br>
033 * <br>
034 *          This library is distributed in the hope that it will be useful, but
035 *          WITHOUT ANY WARRANTY; without even the implied warranty of
036 *          MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
037 *          Lesser General Public License for more details. <br>
038 * <br>
039 *          You should have received a copy of the GNU Lesser General Public
040 *          License along with this library; if not see
041 *          <a href='http://www.gnu.org/licenses/'>http://www.gnu.org/licenses/</a>.
042 */
043public class PeriodPenalty extends ExamCriterion {
044    protected Integer iSoftPeriods = null;
045    
046    @Override
047    public boolean init(Solver<Exam, ExamPlacement> solver) {
048        if (super.init(solver)) {
049            iSoftPeriods = solver.getProperties().getPropertyInteger("Exam.SoftPeriods", null);
050            if (iSoftPeriods != null) {
051                PeriodViolation pv = new PeriodViolation();
052                getModel().addCriterion(pv);
053                return pv.init(solver);
054            }
055        }
056        return true;
057    }
058    
059    @Override
060    public String getWeightName() {
061        return "Exams.PeriodWeight";
062    }
063    
064    @Override
065    public String getXmlWeightName() {
066        return "periodWeight";
067    }
068    
069    @Override
070    public double getWeightDefault(DataProperties config) {
071        return 1.0;
072    }
073    
074    @Override
075    public double getValue(Assignment<Exam, ExamPlacement> assignment, ExamPlacement value, Set<ExamPlacement> conflicts) {
076        return (iSoftPeriods == null || (value.getPeriodPlacement().getExamPenalty() != iSoftPeriods &&  value.getPeriodPlacement().getPeriod().getPenalty() != iSoftPeriods) ? value.getPeriodPlacement().getPenalty() : 0.0);
077    }
078
079    @Override
080    public double[] getBounds(Assignment<Exam, ExamPlacement> assignment, Collection<Exam> variables) {
081        double[] bounds = new double[] { 0.0, 0.0 };
082        for (Exam exam : variables) {
083            if (!exam.getPeriodPlacements().isEmpty()) {
084                int minPenalty = Integer.MAX_VALUE, maxPenalty = Integer.MIN_VALUE;
085                for (ExamPeriodPlacement periodPlacement : exam.getPeriodPlacements()) {
086                    if (iSoftPeriods != null && (periodPlacement.getExamPenalty() == iSoftPeriods || periodPlacement.getPeriod().getPenalty() == iSoftPeriods)) continue;
087                    minPenalty = Math.min(minPenalty, periodPlacement.getPenalty());
088                    maxPenalty = Math.max(maxPenalty, periodPlacement.getPenalty());
089                }
090                bounds[0] += minPenalty;
091                bounds[1] += maxPenalty;
092            }
093        }
094        return bounds;
095    }
096    
097    @Override
098    public String toString(Assignment<Exam, ExamPlacement> assignment) {
099        return "PP:" + sDoubleFormat.format(getValue(assignment));
100    }
101}