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.solver.Solver;
013import org.cpsolver.ifs.util.DataProperties;
014
015
016/**
017 * Number of back-to-back distance student conflicts. I.e., number of
018 * cases when an exam is attended by a student that attends some other
019 * exam at the previous {@link ExamPeriod#prev()} or following
020 * {@link ExamPeriod#next()} period and the distance
021 * {@link ExamPlacement#getDistanceInMeters(ExamPlacement)} between these two exams
022 * is greater than {@link ExamModel#getBackToBackDistance()}. Distance
023 * back-to-back conflicts are only considered between consecutive periods
024 * that are of the same day.
025 * <br><br>
026 * Distance back-to-back student conflict weight can be set by problem
027 * property Exams.DistanceBackToBackConflictWeight, or in the
028 * input xml file, property distanceBackToBackConflictWeight.
029 * 
030 * <br>
031 * 
032 * @version ExamTT 1.3 (Examination Timetabling)<br>
033 *          Copyright (C) 2008 - 2014 Tomas Muller<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 boolean init(Solver<Exam, ExamPlacement> solver) {
056        boolean ret = super.init(solver);
057        iBackToBackDistance = solver.getProperties().getPropertyDouble("Exams.BackToBackDistance", iBackToBackDistance);
058        return ret;
059    }
060    
061    @Override
062    public String getWeightName() {
063        return "Exams.DistanceBackToBackConflictWeight";
064    }
065    
066    @Override
067    public String getXmlWeightName() {
068        return "distanceBackToBackConflictWeight";
069    }
070    
071    @Override
072    public double getWeightDefault(DataProperties config) {
073        return 25.0;
074    }
075    
076    /**
077     * Back-to-back distance. Can be set by
078     * problem property Exams.BackToBackDistance, or in the input xml file,
079     * property backToBackDistance)
080     * @return back-to-back distance in meters
081     */
082    public double getBackToBackDistance() {
083        return iBackToBackDistance;
084    }
085    
086    /**
087     * Back-to-back distance. Can be set by
088     * problem property Exams.BackToBackDistance, or in the input xml file,
089     * property backToBackDistance)
090     * @param backToBackDistance back-to-back distance in meters
091     */
092    public void setBackToBackDistance(double backToBackDistance) {
093        iBackToBackDistance = backToBackDistance;
094    }
095
096    @Override
097    public void getXmlParameters(Map<String, String> params) {
098        params.put(getXmlWeightName(), String.valueOf(getWeight()));
099        params.put("backToBackDistance", String.valueOf(getBackToBackDistance()));
100    }
101    
102    @Override
103    public void setXmlParameters(Map<String, String> params) {
104        try {
105            setWeight(Double.valueOf(params.get(getXmlWeightName())));
106        } catch (NumberFormatException e) {} catch (NullPointerException e) {}
107        try {
108            setBackToBackDistance(Double.valueOf(params.get("backToBackDistance")));
109        } catch (NumberFormatException e) {} catch (NullPointerException e) {}
110    }
111    
112    @Override
113    public double getValue(Assignment<Exam, ExamPlacement> assignment, ExamPlacement value, Set<ExamPlacement> conflicts) {
114        Exam exam = value.variable();
115        if (getBackToBackDistance() < 0) return 0;
116        int penalty = 0;
117        ExamPeriod period = value.getPeriod();
118        Map<ExamStudent, Set<Exam>> prev = (period.prev() != null && period.prev().getDay() == period.getDay() ? ((ExamModel)getModel()).getStudentsOfPeriod(assignment, period.prev()) : null);
119        Map<ExamStudent, Set<Exam>> next = (period.next() != null && period.next().getDay() == period.getDay() ? ((ExamModel)getModel()).getStudentsOfPeriod(assignment, period.next()) : null);
120        for (ExamStudent s : exam.getStudents()) {
121            if (prev != null) {
122                Set<Exam> exams = prev.get(s);
123                if (exams != null)
124                    for (Exam x : exams) {
125                        if (x.equals(exam))
126                            continue;
127                        if (value.getDistanceInMeters(assignment.getValue(x)) > getBackToBackDistance())
128                            penalty++;
129                    }
130            }
131            if (next != null) {
132                Set<Exam> exams = next.get(s);
133                if (exams != null)
134                    for (Exam x : exams) {
135                        if (x.equals(exam))
136                            continue;
137                        if (value.getDistanceInMeters(assignment.getValue(x)) > getBackToBackDistance())
138                            penalty++;
139                    }
140            }
141        }
142        /*
143        for (ExamStudent s : exam.getStudents()) {
144            if (period.prev() != null) {
145                if (period.prev().getDay() == period.getDay()) {
146                    for (Exam x : s.getExams(assignment, period.prev())) {
147                        if (x.equals(exam))
148                            continue;
149                        if (value.getDistanceInMeters(assignment.getValue(x)) > getBackToBackDistance())
150                            penalty++;
151                    }
152                }
153            }
154            if (period.next() != null) {
155                if (period.next().getDay() == period.getDay()) {
156                    for (Exam x : s.getExams(assignment, period.next())) {
157                        if (x.equals(exam))
158                            continue;
159                        if (value.getDistanceInMeters(assignment.getValue(x)) > getBackToBackDistance())
160                            penalty++;
161                    }
162                }
163            }
164        }
165        */
166        return penalty;
167    }
168    
169    @Override
170    public String getName() {
171        return "Distance Back-To-Back Conflicts";
172    }
173    
174    @Override
175    public void getInfo(Assignment<Exam, ExamPlacement> assignment, Map<String, String> info) {
176        if (getBackToBackDistance() >= 0.0 && getValue(assignment) != 0.0)
177            info.put(getName(), sDoubleFormat.format(getValue(assignment)));
178    }
179    
180    @Override
181    public String toString(Assignment<Exam, ExamPlacement> assignment) {
182        return (getValue(assignment) <= 0.0 ? "" : "BTBd:" + sDoubleFormat.format(getValue(assignment)));
183    }
184
185    @Override
186    public boolean isPeriodCriterion() { return false; }
187}