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