001package org.cpsolver.coursett.custom; 002 003import java.util.ArrayList; 004import java.util.Collection; 005import java.util.Comparator; 006import java.util.Iterator; 007import java.util.Set; 008import java.util.TreeSet; 009 010import org.cpsolver.coursett.model.Configuration; 011import org.cpsolver.coursett.model.DefaultStudentSectioning; 012import org.cpsolver.coursett.model.InitialSectioning; 013import org.cpsolver.coursett.model.Lecture; 014import org.cpsolver.coursett.model.Placement; 015import org.cpsolver.coursett.model.Student; 016import org.cpsolver.coursett.model.StudentSectioning; 017import org.cpsolver.coursett.model.TimetableModel; 018import org.cpsolver.coursett.model.InitialSectioning.Group; 019import org.cpsolver.ifs.assignment.Assignment; 020import org.cpsolver.ifs.solution.Solution; 021import org.cpsolver.ifs.termination.TerminationCondition; 022import org.cpsolver.ifs.util.Progress; 023 024 025/** 026 * Deterministic implementation of the initial student sectioning. This class assign students to groups in 027 * a deterministic way. Students are ordered by their academic information (curriculum) and unique ids and 028 * assigned in this order to the first available group (configuration or lecture). See {@link StudentSectioning} 029 * and {@link DefaultStudentSectioning} for more details about sectioning students during course timetabling. 030 * <br><br> 031 * This deterministic sectioning can be enabled by setting the following parameter:<pre> 032 * <code>StudentSectioning.Class=org.cpsolver.coursett.custom.DeterministicStudentSectioning</code> 033 * </pre> 034 * 035 * @author Tomáš Müller 036 * @version CourseTT 1.3 (University Course Timetabling)<br> 037 * Copyright (C) 2014 Tomáš Müller<br> 038 * <a href="mailto:muller@unitime.org">muller@unitime.org</a><br> 039 * <a href="http://muller.unitime.org">http://muller.unitime.org</a><br> 040 * This customization has been implemented for <a href='http://www.agh.pl'>AGH, Poland</a>.<br> 041 * <br> 042 * This library is free software; you can redistribute it and/or modify 043 * it under the terms of the GNU Lesser General Public License as 044 * published by the Free Software Foundation; either version 3 of the 045 * License, or (at your option) any later version. <br> 046 * <br> 047 * This library is distributed in the hope that it will be useful, but 048 * WITHOUT ANY WARRANTY; without even the implied warranty of 049 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 050 * Lesser General Public License for more details. <br> 051 * <br> 052 * You should have received a copy of the GNU Lesser General Public 053 * License along with this library; if not see 054 * <a href='http://www.gnu.org/licenses/'>http://www.gnu.org/licenses/</a>. 055 */ 056public class DeterministicStudentSectioning extends DefaultStudentSectioning { 057 public DeterministicStudentSectioning(TimetableModel model) { 058 super(model); 059 } 060 061 @Override 062 protected Group[] studentsToConfigurations(Long offeringId, Collection<Student> students, Collection<Configuration> configurations) { 063 DeterministicInitialSectioning sect = new DeterministicInitialSectioning(getProgress(), offeringId, configurations, students); 064 return sect.getGroups(); 065 } 066 067 @Override 068 protected Group[] studentsToLectures(Long offeringId, Collection<Student> students, Collection<Lecture> lectures) { 069 Set<Lecture> sortedLectures = new TreeSet<Lecture>(new Comparator<Lecture>() { 070 @Override 071 public int compare(Lecture l1, Lecture l2) { 072 return l1.getClassId().compareTo(l2.getClassId()); 073 } 074 }); 075 sortedLectures.addAll(lectures); 076 DeterministicInitialSectioning sect = new DeterministicInitialSectioning(getProgress(), offeringId, sortedLectures, students); 077 return sect.getGroups(); 078 } 079 080 /** 081 * No re-sectioning (final sectioning) during deterministic student sectioning. 082 */ 083 @Override 084 public boolean hasFinalSectioning() { 085 return false; 086 } 087 088 /** 089 * No re-sectioning (final sectioning) during deterministic student sectioning. 090 */ 091 @Override 092 public void switchStudents(Solution<Lecture, Placement> solution, TerminationCondition<Lecture, Placement> termination) { 093 } 094 095 /** 096 * No re-sectioning (final sectioning) during deterministic student sectioning. 097 */ 098 @Override 099 public void resection(Assignment<Lecture, Placement> assignment, Lecture lecture, boolean recursive, boolean configAsWell) { 100 } 101 102 /** 103 * Assign students to groups in a deterministic way, i.e., first student to first available group etc. 104 * @author Tomáš Müller 105 */ 106 public static class DeterministicInitialSectioning extends InitialSectioning implements Comparator<Student> { 107 108 public DeterministicInitialSectioning(Progress progress, Long offeringId, Collection<?> lectureOrConfigurations, Collection<Student> students) { 109 super(progress, offeringId, lectureOrConfigurations, students); 110 111 // Sort students by their curriculum information first 112 iStudents = new TreeSet<Student>(this); iStudents.addAll(students); 113 } 114 115 @Override 116 protected void tweakSizes(double total) { 117 double studentsWeight = 0; 118 for (Student s : iStudents) { 119 studentsWeight += s.getOfferingWeight(iOfferingId); 120 } 121 122 // if there is not enough space for the given students 123 if (studentsWeight > total) { 124 if (total == 0) { 125 // all limits are zero -> spread students equally 126 for (int idx = 0; idx < iGroups.length; idx++) 127 iGroups[idx].setMaxSize(total / iGroups.length); 128 } else { 129 // enlarge sections proportionally 130 double factor = studentsWeight / total; 131 for (int idx = 0; idx < iGroups.length; idx++) { 132 iGroups[idx].setMaxSize(factor * iGroups[idx].getMaxSize()); 133 iGroups[idx].setMinSize(Math.min(iGroups[idx].getMinSize(), 0.9 * iGroups[idx].getMaxSize())); 134 } 135 } 136 } 137 } 138 139 @Override 140 public Group[] getGroups() { 141 // Assign already enrolled students first 142 students: for (Iterator<Student> i = iStudents.iterator(); i.hasNext();) { 143 Student student = i.next(); 144 for (int idx = 0; idx < iGroups.length; idx++) { 145 if (iGroups[idx].isEnrolled(student)) { 146 iGroups[idx].addStudent(student); 147 i.remove(); 148 continue students; 149 } 150 } 151 } 152 153 // For all other (not enrolled) students 154 students: for (Student student : iStudents) { 155 double studentWeight = student.getOfferingWeight(iOfferingId); 156 157 // enroll into first available group with enough space 158 for (int idx = 0; idx < iGroups.length; idx++) { 159 Group g = iGroups[idx]; 160 if (!g.canEnroll(student) || g.size() >= g.getMaxSize()) continue; 161 // group found -> enroll and continue with the next student 162 g.addStudent(student); 163 continue students; 164 } 165 166 // disobey max size, but prefer sections with smallest excess 167 Group group = null; int excess = 0; 168 for (int idx = 0; idx < iGroups.length; idx++) { 169 Group g = iGroups[idx]; 170 if (!g.canEnroll(student)) continue; 171 int ex = (int)Math.round(g.size() + studentWeight - g.getMaxSize()); 172 if (group == null || ex < excess) { 173 group = g; 174 excess = ex; 175 } 176 } 177 178 if (group != null) { 179 group.addStudent(student); 180 continue; 181 } 182 183 // put the student into the first group 184 getProgress().debug("Unable to find a valid section for student " + student.getId() + ", enrolling to " + (iGroups[0].getLecture() != null ? iGroups[0].getLecture().getName() : iGroups[0].getConfiguration().getConfigId().toString())); 185 iGroups[0].addStudent(student); 186 } 187 188 // try to move students away from groups with an excess of more than a student 189 for (int idx = 0; idx < iGroups.length; idx++) { 190 Group group = iGroups[idx]; 191 if (group.size() > group.getMaxSize()) { 192 // for each student of a group that is not enrolled in it 193 for (Student student : new ArrayList<Student>(group.getStudents())) { 194 if (group.isEnrolled(student)) continue; 195 // excess of a fraction of a student is allowed 196 if (group.size() - student.getOfferingWeight(iOfferingId) < group.getMaxSize()) continue; 197 // look for an available group with enough space 198 for (int x = 0; x < iGroups.length; x++) { 199 if (idx == x) continue; 200 if (!iGroups[x].canEnroll(student) || iGroups[x].size() >= iGroups[x].getMaxSize()) continue; 201 // group found, move the student away 202 group.removeStudent(student); 203 iGroups[x].addStudent(student); 204 break; 205 } 206 if (group.size() <= group.getMaxSize()) break; 207 } 208 } 209 } 210 211 return iGroups; 212 } 213 214 /** 215 * Sort students by their curriculum information and id 216 */ 217 @Override 218 public int compare(Student s1, Student s2) { 219 int cmp = (s1.getCurriculum() == null ? "" : s1.getCurriculum()).compareToIgnoreCase(s2.getCurriculum() == null ? "" : s2.getCurriculum()); 220 if (cmp != 0) return cmp; 221 cmp = (s1.getAcademicArea() == null ? "" : s1.getAcademicArea()).compareToIgnoreCase(s2.getAcademicArea() == null ? "" : s2.getAcademicArea()); 222 if (cmp != 0) return cmp; 223 cmp = (s1.getMajor() == null ? "" : s1.getMajor()).compareToIgnoreCase(s2.getMajor() == null ? "" : s2.getMajor()); 224 if (cmp != 0) return cmp; 225 cmp = (s1.getAcademicClassification() == null ? "" : s1.getAcademicClassification()).compareToIgnoreCase(s2.getAcademicClassification() == null ? "" : s2.getAcademicClassification()); 226 if (cmp != 0) return cmp; 227 return s1.getId().compareTo(s2.getId()); 228 } 229 } 230}