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 * @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 StudentDistanceBackToBackConflicts extends ExamCriterion {
051    private double iBackToBackDistance = -1;
052    
053    @Override
054    public void configure(DataProperties properties) {   
055        super.configure(properties);
056        iBackToBackDistance = properties.getPropertyDouble("Exams.BackToBackDistance", iBackToBackDistance);
057    }
058    
059    @Override
060    public String getWeightName() {
061        return "Exams.DistanceBackToBackConflictWeight";
062    }
063    
064    @Override
065    public String getXmlWeightName() {
066        return "distanceBackToBackConflictWeight";
067    }
068    
069    @Override
070    public double getWeightDefault(DataProperties config) {
071        return 25.0;
072    }
073    
074    /**
075     * Back-to-back distance. Can be set by
076     * problem property Exams.BackToBackDistance, or in the input xml file,
077     * property backToBackDistance)
078     * @return back-to-back distance in meters
079     */
080    public double getBackToBackDistance() {
081        return iBackToBackDistance;
082    }
083    
084    /**
085     * Back-to-back distance. Can be set by
086     * problem property Exams.BackToBackDistance, or in the input xml file,
087     * property backToBackDistance)
088     * @param backToBackDistance back-to-back distance in meters
089     */
090    public void setBackToBackDistance(double backToBackDistance) {
091        iBackToBackDistance = backToBackDistance;
092    }
093
094    @Override
095    public void getXmlParameters(Map<String, String> params) {
096        params.put(getXmlWeightName(), String.valueOf(getWeight()));
097        params.put("backToBackDistance", String.valueOf(getBackToBackDistance()));
098    }
099    
100    @Override
101    public void setXmlParameters(Map<String, String> params) {
102        try {
103            setWeight(Double.valueOf(params.get(getXmlWeightName())));
104        } catch (NumberFormatException e) {} catch (NullPointerException e) {}
105        try {
106            setBackToBackDistance(Double.valueOf(params.get("backToBackDistance")));
107        } catch (NumberFormatException e) {} catch (NullPointerException e) {}
108    }
109    
110    @Override
111    public double getValue(Assignment<Exam, ExamPlacement> assignment, ExamPlacement value, Set<ExamPlacement> conflicts) {
112        Exam exam = value.variable();
113        if (getBackToBackDistance() < 0) return 0;
114        int penalty = 0;
115        ExamPeriod period = value.getPeriod();
116        Map<ExamStudent, Set<Exam>> prev = (period.prev() != null && period.prev().getDay() == period.getDay() ? ((ExamModel)getModel()).getStudentsOfPeriod(assignment, period.prev()) : null);
117        Map<ExamStudent, Set<Exam>> next = (period.next() != null && period.next().getDay() == period.getDay() ? ((ExamModel)getModel()).getStudentsOfPeriod(assignment, period.next()) : null);
118        for (ExamStudent s : exam.getStudents()) {
119            if (prev != null) {
120                Set<Exam> exams = prev.get(s);
121                if (exams != null)
122                    for (Exam x : exams) {
123                        if (x.equals(exam))
124                            continue;
125                        if (value.getDistanceInMeters(assignment.getValue(x)) > getBackToBackDistance())
126                            penalty++;
127                    }
128            }
129            if (next != null) {
130                Set<Exam> exams = next.get(s);
131                if (exams != null)
132                    for (Exam x : exams) {
133                        if (x.equals(exam))
134                            continue;
135                        if (value.getDistanceInMeters(assignment.getValue(x)) > getBackToBackDistance())
136                            penalty++;
137                    }
138            }
139        }
140        /*
141        for (ExamStudent s : exam.getStudents()) {
142            if (period.prev() != null) {
143                if (period.prev().getDay() == period.getDay()) {
144                    for (Exam x : s.getExams(assignment, period.prev())) {
145                        if (x.equals(exam))
146                            continue;
147                        if (value.getDistanceInMeters(assignment.getValue(x)) > getBackToBackDistance())
148                            penalty++;
149                    }
150                }
151            }
152            if (period.next() != null) {
153                if (period.next().getDay() == period.getDay()) {
154                    for (Exam x : s.getExams(assignment, period.next())) {
155                        if (x.equals(exam))
156                            continue;
157                        if (value.getDistanceInMeters(assignment.getValue(x)) > getBackToBackDistance())
158                            penalty++;
159                    }
160                }
161            }
162        }
163        */
164        return penalty;
165    }
166    
167    @Override
168    public String getName() {
169        return "Distance Back-To-Back Conflicts";
170    }
171    
172    @Override
173    public void getInfo(Assignment<Exam, ExamPlacement> assignment, Map<String, String> info) {
174        if (getBackToBackDistance() >= 0.0 && getValue(assignment) != 0.0)
175            info.put(getName(), sDoubleFormat.format(getValue(assignment)));
176    }
177    
178    @Override
179    public String toString(Assignment<Exam, ExamPlacement> assignment) {
180        return (getValue(assignment) <= 0.0 ? "" : "BTBd:" + sDoubleFormat.format(getValue(assignment)));
181    }
182
183    @Override
184    public boolean isPeriodCriterion() { return false; }
185}