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