001package org.cpsolver.exam.criteria; 002 003import java.util.Collection; 004import java.util.HashSet; 005import java.util.Map; 006import java.util.Set; 007 008import org.cpsolver.exam.model.Exam; 009import org.cpsolver.exam.model.ExamModel; 010import org.cpsolver.exam.model.ExamPeriod; 011import org.cpsolver.exam.model.ExamPlacement; 012import org.cpsolver.exam.model.ExamStudent; 013import org.cpsolver.ifs.assignment.Assignment; 014import org.cpsolver.ifs.util.DataProperties; 015 016 017/** 018 * Number of more than two exams a day student conflicts. I.e., when an 019 * exam is attended by a student student that attends two or more other 020 * exams at the same day. 021 * <br><br> 022 * More than two exams a day student conflict weight can be set by 023 * problem property Exams.MoreThanTwoADayWeight, or in the input 024 * xml file, property moreThanTwoADayWeight. 025 * 026 * <br> 027 * 028 * @version ExamTT 1.3 (Examination Timetabling)<br> 029 * Copyright (C) 2008 - 2014 Tomáš Müller<br> 030 * <a href="mailto:muller@unitime.org">muller@unitime.org</a><br> 031 * <a href="http://muller.unitime.org">http://muller.unitime.org</a><br> 032 * <br> 033 * This library is free software; you can redistribute it and/or modify 034 * it under the terms of the GNU Lesser General Public License as 035 * published by the Free Software Foundation; either version 3 of the 036 * License, or (at your option) any later version. <br> 037 * <br> 038 * This library is distributed in the hope that it will be useful, but 039 * WITHOUT ANY WARRANTY; without even the implied warranty of 040 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 041 * Lesser General Public License for more details. <br> 042 * <br> 043 * You should have received a copy of the GNU Lesser General Public 044 * License along with this library; if not see 045 * <a href='http://www.gnu.org/licenses/'>http://www.gnu.org/licenses/</a>. 046 */ 047public class StudentMoreThan2ADayConflicts extends ExamCriterion { 048 049 @Override 050 public String getWeightName() { 051 return "Exams.MoreThanTwoADayWeight"; 052 } 053 054 @Override 055 public double getWeightDefault(DataProperties config) { 056 return 100.0; 057 } 058 059 @Override 060 public double getValue(Assignment<Exam, ExamPlacement> assignment, ExamPlacement value, Set<ExamPlacement> conflicts) { 061 Exam exam = value.variable(); 062 int penalty = 0; 063 ExamPeriod period = value.getPeriod(); 064 Map<ExamStudent, Set<Exam>> students = ((ExamModel)getModel()).getStudentsOfDay(assignment, period); 065 for (ExamStudent s : exam.getStudents()) { 066 Set<Exam> exams = students.get(s); 067 if (exams == null || exams.size() < 2) continue; 068 int nrExams = exams.size() + (exams.contains(exam) ? 0 : 1); 069 if (nrExams > 2) 070 penalty++; 071 } 072 /* 073 for (ExamStudent s : exam.getStudents()) { 074 Set<Exam> exams = s.getExamsADay(assignment, period); 075 int nrExams = exams.size() + (exams.contains(exam) ? 0 : 1); 076 if (nrExams > 2) 077 penalty++; 078 } 079 */ 080 return penalty; 081 } 082 083 @Override 084 public String getName() { 085 return "More Than 2 A Day Conflicts"; 086 } 087 088 @Override 089 public String getXmlWeightName() { 090 return "moreThanTwoADayWeight"; 091 } 092 093 @Override 094 public void getInfo(Assignment<Exam, ExamPlacement> assignment, Map<String, String> info) { 095 if (getValue(assignment) != 0.0) 096 info.put(getName(), sDoubleFormat.format(getValue(assignment))); 097 } 098 099 @Override 100 public double getValue(Assignment<Exam, ExamPlacement> assignment, Collection<Exam> variables) { 101 int ret = 0; 102 ExamModel m = (ExamModel)getModel(); 103 Set<Integer> days = new HashSet<Integer>(); 104 for (ExamPeriod p: m.getPeriods()) { 105 if (days.add(p.getDay())) { 106 Map<ExamStudent, Set<Exam>> students = ((ExamModel)getModel()).getStudentsOfDay(assignment, p); 107 for (Set<Exam> exams: students.values()) { 108 int nrExams = exams.size(); 109 if (nrExams > 2) 110 ret += nrExams - 2; 111 } 112 } 113 } 114 return ret; 115 } 116 117 @Override 118 public String toString(Assignment<Exam, ExamPlacement> assignment) { 119 return "M2D:" + sDoubleFormat.format(getValue(assignment)); 120 } 121}