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