001package org.cpsolver.exam.criteria;
002
003import java.util.Set;
004
005import org.cpsolver.exam.model.Exam;
006import org.cpsolver.exam.model.ExamPlacement;
007import org.cpsolver.exam.model.ExamRoomPlacement;
008import org.cpsolver.ifs.assignment.Assignment;
009import org.cpsolver.ifs.util.DataProperties;
010
011
012/**
013 * Room perturbation penalty. I.e., number of assigned rooms different from
014 * initial. Only applicable when {@link PerturbationPenalty#isMPP()} is true (minimal
015 * perturbation problem).
016 * <br><br>
017 * A weight of room perturbations (i.e., a penalty for
018 * an assignment of an exam to a room different from the initial one) can be
019 * set by problem property Exams.RoomPerturbationWeight, or in the input xml
020 * file, property roomPerturbationWeight).
021 * 
022 * <br>
023 * 
024 * @author  Tomáš Müller
025 * @version ExamTT 1.3 (Examination Timetabling)<br>
026 *          Copyright (C) 2008 - 2014 Tomáš Müller<br>
027 *          <a href="mailto:muller@unitime.org">muller@unitime.org</a><br>
028 *          <a href="http://muller.unitime.org">http://muller.unitime.org</a><br>
029 * <br>
030 *          This library is free software; you can redistribute it and/or modify
031 *          it under the terms of the GNU Lesser General Public License as
032 *          published by the Free Software Foundation; either version 3 of the
033 *          License, or (at your option) any later version. <br>
034 * <br>
035 *          This library is distributed in the hope that it will be useful, but
036 *          WITHOUT ANY WARRANTY; without even the implied warranty of
037 *          MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
038 *          Lesser General Public License for more details. <br>
039 * <br>
040 *          You should have received a copy of the GNU Lesser General Public
041 *          License along with this library; if not see
042 *          <a href='http://www.gnu.org/licenses/'>http://www.gnu.org/licenses/</a>.
043 */
044public class RoomPerturbationPenalty extends PerturbationPenalty {
045    
046    @Override
047    public String getWeightName() {
048        return "Exams.RoomPerturbationWeight";
049    }
050    
051    @Override
052    public String getXmlWeightName() {
053        return "roomPerturbationWeight";
054    }
055    
056    @Override
057    public double getWeightDefault(DataProperties config) {
058        return 0.01;
059    }
060
061    @Override
062    public double getValue(Assignment<Exam, ExamPlacement> assignment, ExamPlacement value, Set<ExamPlacement> conflicts) {
063        if (!isMPP()) return 0;
064        Exam exam = value.variable();
065        ExamPlacement initial = exam.getInitialAssignment();
066        if (initial == null) return 0;
067        int penalty = 0;
068        if (value.getRoomPlacements() != null)
069            for (ExamRoomPlacement rp : value.getRoomPlacements()) {
070                if (initial.getRoomPlacements() == null || !initial.getRoomPlacements().contains(rp))
071                    penalty++;
072            }
073        return penalty;
074    }
075
076
077    @Override
078    public String toString(Assignment<Exam, ExamPlacement> assignment) {
079        return (isMPP() ? "IRP:" + sDoubleFormat.format(getValue(assignment)) : "");
080    }
081
082    @Override
083    public boolean isPeriodCriterion() { return false; }
084}