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 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 * @version ExamTT 1.3 (Examination Timetabling)<br> 032 * Copyright (C) 2008 - 2014 Tomas Muller<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 StudentBackToBackConflicts extends ExamCriterion { 051 private boolean iDayBreakBackToBack = false; 052 053 @Override 054 public boolean init(Solver<Exam, ExamPlacement> solver) { 055 boolean ret = super.init(solver); 056 iDayBreakBackToBack = solver.getProperties().getPropertyBoolean("Exams.IsDayBreakBackToBack", iDayBreakBackToBack); 057 return ret; 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 String getName() { 162 return "Back-To-Back Conflicts"; 163 } 164 165 @Override 166 public void getInfo(Assignment<Exam, ExamPlacement> assignment, Map<String, String> info) { 167 if (getValue(assignment) != 0.0) 168 info.put(getName(), sDoubleFormat.format(getValue(assignment))); 169 } 170 171 @Override 172 public String toString(Assignment<Exam, ExamPlacement> assignment) { 173 return "BTB:" + sDoubleFormat.format(getValue(assignment)); 174 } 175}