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 * @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 StudentBackToBackConflicts extends ExamCriterion { 052 private boolean iDayBreakBackToBack = false; 053 054 @Override 055 public void configure(DataProperties properties) { 056 super.configure(properties); 057 iDayBreakBackToBack = properties.getPropertyBoolean("Exams.IsDayBreakBackToBack", iDayBreakBackToBack); 058 } 059 060 @Override 061 public String getWeightName() { 062 return "Exams.BackToBackConflictWeight"; 063 } 064 065 @Override 066 public String getXmlWeightName() { 067 return "backToBackConflictWeight"; 068 } 069 070 @Override 071 public double getWeightDefault(DataProperties config) { 072 return 10.0; 073 } 074 075 /** 076 * True when back-to-back student conflict is to be encountered when a 077 * student is enrolled into an exam that is on the last period of one day 078 * and another exam that is on the first period of the consecutive day. It 079 * can be set by problem property Exams.IsDayBreakBackToBack, or in the 080 * input xml file, property isDayBreakBackToBack) 081 * @return true if last exam on one day is back-to-back to the first exam of the following day 082 */ 083 public boolean isDayBreakBackToBack() { 084 return iDayBreakBackToBack; 085 } 086 087 /** 088 * True when back-to-back student conflict is to be encountered when a 089 * student is enrolled into an exam that is on the last period of one day 090 * and another exam that is on the first period of the consecutive day. It 091 * can be set by problem property Exams.IsDayBreakBackToBack, or in the 092 * input xml file, property isDayBreakBackToBack) 093 * @param dayBreakBackToBack true if last exam on one day is back-to-back to the first exam of the following day 094 * 095 */ 096 public void setDayBreakBackToBack(boolean dayBreakBackToBack) { 097 iDayBreakBackToBack = dayBreakBackToBack; 098 } 099 100 @Override 101 public void getXmlParameters(Map<String, String> params) { 102 params.put(getXmlWeightName(), String.valueOf(getWeight())); 103 params.put("isDayBreakBackToBack", isDayBreakBackToBack() ? "true" : "false"); 104 } 105 106 @Override 107 public void setXmlParameters(Map<String, String> params) { 108 try { 109 setWeight(Double.valueOf(params.get(getXmlWeightName()))); 110 } catch (NumberFormatException e) {} catch (NullPointerException e) {} 111 try { 112 setDayBreakBackToBack("true".equals(params.get("isDayBreakBackToBack"))); 113 } catch (NumberFormatException e) {} catch (NullPointerException e) {} 114 } 115 116 @Override 117 public double getValue(Assignment<Exam, ExamPlacement> assignment, ExamPlacement value, Set<ExamPlacement> conflicts) { 118 Exam exam = value.variable(); 119 int penalty = 0; 120 ExamPeriod period = value.getPeriod(); 121 Map<ExamStudent, Set<Exam>> prev = (period.prev() != null && (isDayBreakBackToBack() || period.prev().getDay() == period.getDay()) ? ((ExamModel)getModel()).getStudentsOfPeriod(assignment, period.prev()) : null); 122 Map<ExamStudent, Set<Exam>> next = (period.next() != null && (isDayBreakBackToBack() || period.next().getDay() == period.getDay()) ? ((ExamModel)getModel()).getStudentsOfPeriod(assignment, period.next()) : null); 123 for (ExamStudent s : exam.getStudents()) { 124 if (prev != null) { 125 Set<Exam> exams = prev.get(s); 126 if (exams != null) { 127 int nrExams = exams.size() + (exams.contains(exam) ? -1 : 0); 128 penalty += nrExams; 129 } 130 } 131 if (next != null) { 132 Set<Exam> exams = next.get(s); 133 if (exams != null) { 134 int nrExams = exams.size() + (exams.contains(exam) ? -1 : 0); 135 penalty += nrExams; 136 } 137 } 138 } 139 /* 140 for (ExamStudent s : exam.getStudents()) { 141 if (period.prev() != null) { 142 if (isDayBreakBackToBack() || period.prev().getDay() == period.getDay()) { 143 Set<Exam> exams = s.getExams(assignment, period.prev()); 144 int nrExams = exams.size() + (exams.contains(exam) ? -1 : 0); 145 penalty += nrExams; 146 } 147 } 148 if (period.next() != null) { 149 if (isDayBreakBackToBack() || period.next().getDay() == period.getDay()) { 150 Set<Exam> exams = s.getExams(assignment, period.next()); 151 int nrExams = exams.size() + (exams.contains(exam) ? -1 : 0); 152 penalty += nrExams; 153 } 154 } 155 } 156 */ 157 return penalty; 158 } 159 160 @Override 161 public double getValue(Assignment<Exam, ExamPlacement> assignment, Collection<Exam> variables) { 162 return super.getValue(assignment, variables) / 2.0; 163 } 164 165 @Override 166 public String getName() { 167 return "Back-To-Back Conflicts"; 168 } 169 170 @Override 171 public void getInfo(Assignment<Exam, ExamPlacement> assignment, Map<String, String> info) { 172 if (getValue(assignment) != 0.0) 173 info.put(getName(), sDoubleFormat.format(getValue(assignment))); 174 } 175 176 @Override 177 public String toString(Assignment<Exam, ExamPlacement> assignment) { 178 return "BTB:" + sDoubleFormat.format(getValue(assignment)); 179 } 180}