    001package org.cpsolver.exam.criteria;
    002
    003import java.util.Collection;
    004import java.util.Map;
    005import java.util.Set;
    006
    007import org.cpsolver.exam.model.Exam;
    008import org.cpsolver.exam.model.ExamModel;
    009import org.cpsolver.exam.model.ExamPeriod;
    010import org.cpsolver.exam.model.ExamPlacement;
    011import org.cpsolver.exam.model.ExamStudent;
    012import org.cpsolver.ifs.assignment.Assignment;
    013import org.cpsolver.ifs.util.DataProperties;
    014
    015
    016/**
    017 * Number of back-to-back student conflicts. I.e., number of cases when
    018 * an exam is attended by a student that attends some other exam at
    019 * the previous {@link ExamPeriod#prev()} or following
    020 * {@link ExamPeriod#next()} period. If
    021 * {@link StudentBackToBackConflicts#isDayBreakBackToBack()} is false, back-to-back conflicts
    022 * are only considered between consecutive periods that are of the same day.
    023 * <br><br>
    024 * Back-to-back student conflict weight can be set by problem property
    025 * Exams.BackToBackConflictWeight, or in the input xml file,
    026 * property backToBackConflictWeight.
    027 *
    028 *
    029 * <br>
    030 *
    031 * @version ExamTT 1.3 (Examination Timetabling)<br>
    032 *          Copyright (C) 2008 - 2014 Tomáš Müller<br>
    033 *          <a href="mailto:muller@unitime.org">muller@unitime.org</a><br>
    034 *          <a href="http://muller.unitime.org">http://muller.unitime.org</a><br>
    035 * <br>
    036 *          This library is free software; you can redistribute it and/or modify
    037 *          it under the terms of the GNU Lesser General Public License as
    038 *          published by the Free Software Foundation; either version 3 of the
    039 *          License, or (at your option) any later version. <br>
    040 * <br>
    041 *          This library is distributed in the hope that it will be useful, but
    042 *          WITHOUT ANY WARRANTY; without even the implied warranty of
    043 *          MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
    044 *          Lesser General Public License for more details. <br>
    045 * <br>
    046 *          You should have received a copy of the GNU Lesser General Public
    047 *          License along with this library; if not see
    048 *          <a href='http://www.gnu.org/licenses/'>http://www.gnu.org/licenses/</a>.
    049 */
    050public class StudentBackToBackConflicts extends ExamCriterion {
    051    private boolean iDayBreakBackToBack = false;
    052
    053    @Override
    054    public void configure(DataProperties properties) {
    055        super.configure(properties);
    056        iDayBreakBackToBack = properties.getPropertyBoolean("Exams.IsDayBreakBackToBack", iDayBreakBackToBack);
    057    }
    058
    059    @Override
    060    public String getWeightName() {
    061        return "Exams.BackToBackConflictWeight";
    062    }
    063
    064    @Override
    065    public String getXmlWeightName() {
    066        return "backToBackConflictWeight";
    067    }
    068
    069    @Override
    070    public double getWeightDefault(DataProperties config) {
    071        return 10.0;
    072    }
    073
    074    /**
    075     * True when back-to-back student conflict is to be encountered when a
    076     * student is enrolled into an exam that is on the last period of one day
    077     * and another exam that is on the first period of the consecutive day. It
    078     * can be set by problem property Exams.IsDayBreakBackToBack, or in the
    079     * input xml file, property isDayBreakBackToBack)
    080     * @return true if last exam on one day is back-to-back to the first exam of the following day
    081     */
    082    public boolean isDayBreakBackToBack() {
    083        return iDayBreakBackToBack;
    084    }
    085
    086    /**
    087     * True when back-to-back student conflict is to be encountered when a
    088     * student is enrolled into an exam that is on the last period of one day
    089     * and another exam that is on the first period of the consecutive day. It
    090     * can be set by problem property Exams.IsDayBreakBackToBack, or in the
    091     * input xml file, property isDayBreakBackToBack)
    092     * @param dayBreakBackToBack true if last exam on one day is back-to-back to the first exam of the following day
    093     *
    094     */
    095    public void setDayBreakBackToBack(boolean dayBreakBackToBack) {
    096        iDayBreakBackToBack = dayBreakBackToBack;
    097    }
    098
    099    @Override
    100    public void getXmlParameters(Map<String, String> params) {
    101        params.put(getXmlWeightName(), String.valueOf(getWeight()));
    102        params.put("isDayBreakBackToBack", isDayBreakBackToBack() ? "true" : "false");
    103    }
    104
    105    @Override
    106    public void setXmlParameters(Map<String, String> params) {
    107        try {
    108            setWeight(Double.valueOf(params.get(getXmlWeightName())));
    109        } catch (NumberFormatException e) {} catch (NullPointerException e) {}
    110        try {
    111            setDayBreakBackToBack("true".equals(params.get("isDayBreakBackToBack")));
    112        } catch (NumberFormatException e) {} catch (NullPointerException e) {}
    113    }
    114
    115    @Override
    116    public double getValue(Assignment<Exam, ExamPlacement> assignment, ExamPlacement value, Set<ExamPlacement> conflicts) {
    117        Exam exam = value.variable();
    118        int penalty = 0;
    119        ExamPeriod period = value.getPeriod();
    120        Map<ExamStudent, Set<Exam>> prev = (period.prev() != null && (isDayBreakBackToBack() || period.prev().getDay() == period.getDay()) ? ((ExamModel)getModel()).getStudentsOfPeriod(assignment, period.prev()) : null);
    121        Map<ExamStudent, Set<Exam>> next = (period.next() != null && (isDayBreakBackToBack() || period.next().getDay() == period.getDay()) ? ((ExamModel)getModel()).getStudentsOfPeriod(assignment, period.next()) : null);
    122        for (ExamStudent s : exam.getStudents()) {
    123            if (prev != null) {
    124                Set<Exam> exams = prev.get(s);
    125                if (exams != null) {
    126                    int nrExams = exams.size() + (exams.contains(exam) ? -1 : 0);
    127                    penalty += nrExams;
    128                }
    129            }
    130            if (next != null) {
    131                Set<Exam> exams = next.get(s);
    132                if (exams != null) {
    133                    int nrExams = exams.size() + (exams.contains(exam) ? -1 : 0);
    134                    penalty += nrExams;
    135                }
    136            }
    137        }
    138        /*
    139        for (ExamStudent s : exam.getStudents()) {
    140            if (period.prev() != null) {
    141                if (isDayBreakBackToBack() || period.prev().getDay() == period.getDay()) {
    142                    Set<Exam> exams = s.getExams(assignment, period.prev());
    143                    int nrExams = exams.size() + (exams.contains(exam) ? -1 : 0);
    144                    penalty += nrExams;
    145                }
    146            }
    147            if (period.next() != null) {
    148                if (isDayBreakBackToBack() || period.next().getDay() == period.getDay()) {
    149                    Set<Exam> exams = s.getExams(assignment, period.next());
    150                    int nrExams = exams.size() + (exams.contains(exam) ? -1 : 0);
    151                    penalty += nrExams;
    152                }
    153            }
    154        }
    155        */
    156        return penalty;
    157    }
    158
    159    @Override
    160    public double getValue(Assignment<Exam, ExamPlacement> assignment, Collection<Exam> variables) {
    161        return super.getValue(assignment, variables) / 2.0;
    162    }
    163
    164    @Override
    165    public String getName() {
    166        return "Back-To-Back Conflicts";
    167    }
    168
    169    @Override
    170    public void getInfo(Assignment<Exam, ExamPlacement> assignment, Map<String, String> info) {
    171        if (getValue(assignment) != 0.0)
    172            info.put(getName(), sDoubleFormat.format(getValue(assignment)));
    173    }
    174
    175    @Override
    176    public String toString(Assignment<Exam, ExamPlacement> assignment) {
    177        return "BTB:" + sDoubleFormat.format(getValue(assignment));
    178    }
    179}