001package org.cpsolver.studentsct.constraint; 002 003import java.util.ArrayList; 004import java.util.List; 005import java.util.Set; 006 007import org.cpsolver.ifs.assignment.Assignment; 008import org.cpsolver.ifs.model.GlobalConstraint; 009import org.cpsolver.ifs.util.DataProperties; 010import org.cpsolver.ifs.util.ToolBox; 011import org.cpsolver.studentsct.StudentSectioningModel; 012import org.cpsolver.studentsct.model.Course; 013import org.cpsolver.studentsct.model.Enrollment; 014import org.cpsolver.studentsct.model.Request; 015 016 017/** 018 * Course limit constraint. This global constraint ensures that a limit of each 019 * course is not exceeded. This means that the total sum of weights of course 020 * requests (see {@link Request#getWeight()}) enrolled into a course is below 021 * the course's limit (see {@link Course#getLimit()}). 022 * 023 * <br> 024 * <br> 025 * Sections with negative limit are considered unlimited, and therefore 026 * completely ignored by this constraint. 027 * 028 * <br> 029 * <br> 030 * Parameters: 031 * <table border='1' summary='Related Solver Parameters'> 032 * <tr> 033 * <th>Parameter</th> 034 * <th>Type</th> 035 * <th>Comment</th> 036 * </tr> 037 * <tr> 038 * <td>CourseLimit.PreferDummyStudents</td> 039 * <td>{@link Boolean}</td> 040 * <td>If true, requests of dummy (last-like) students are preferred to be 041 * selected as conflicting.</td> 042 * </tr> 043 * </table> 044 * <br> 045 * <br> 046 * 047 * @version StudentSct 1.3 (Student Sectioning)<br> 048 * Copyright (C) 2007 - 2014 Tomas Muller<br> 049 * <a href="mailto:muller@unitime.org">muller@unitime.org</a><br> 050 * <a href="http://muller.unitime.org">http://muller.unitime.org</a><br> 051 * <br> 052 * This library is free software; you can redistribute it and/or modify 053 * it under the terms of the GNU Lesser General Public License as 054 * published by the Free Software Foundation; either version 3 of the 055 * License, or (at your option) any later version. <br> 056 * <br> 057 * This library is distributed in the hope that it will be useful, but 058 * WITHOUT ANY WARRANTY; without even the implied warranty of 059 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 060 * Lesser General Public License for more details. <br> 061 * <br> 062 * You should have received a copy of the GNU Lesser General Public 063 * License along with this library; if not see 064 * <a href='http://www.gnu.org/licenses/'>http://www.gnu.org/licenses/</a>. 065 */ 066public class CourseLimit extends GlobalConstraint<Request, Enrollment> { 067 private static double sNominalWeight = 0.00001; 068 private boolean iPreferDummyStudents = false; 069 070 /** 071 * Constructor 072 * 073 * @param cfg 074 * solver configuration 075 */ 076 public CourseLimit(DataProperties cfg) { 077 super(); 078 iPreferDummyStudents = cfg.getPropertyBoolean("CourseLimit.PreferDummyStudents", false); 079 } 080 081 082 /** 083 * Enrollment weight of a course if the given request is assigned. In order 084 * to overcome rounding problems with last-like students ( e.g., 5 students 085 * are projected to two sections of limit 2 -- each section can have up to 3 086 * of these last-like students), the weight of the request with the highest 087 * weight in the section is changed to a small nominal weight. 088 * 089 * @param assignment current assignment 090 * @param course 091 * a course that is of concern 092 * @param request 093 * a request of a student to be assigned containing the given 094 * section 095 * @return section's new weight 096 */ 097 public static double getEnrollmentWeight(Assignment<Request, Enrollment> assignment, Course course, Request request) { 098 return course.getEnrollmentWeight(assignment, request) + request.getWeight() - Math.max(course.getMaxEnrollmentWeight(assignment), request.getWeight()) + sNominalWeight; 099 } 100 101 /** 102 * A given enrollment is conflicting, if the course's enrollment 103 * (computed by {@link CourseLimit#getEnrollmentWeight(Assignment, Course, Request)}) 104 * exceeds the limit. <br> 105 * If the limit is breached, one or more existing enrollments are 106 * (randomly) selected as conflicting until the overall weight is under the 107 * limit. 108 * 109 * @param enrollment 110 * {@link Enrollment} that is being considered 111 * @param conflicts 112 * all computed conflicting requests are added into this set 113 */ 114 @Override 115 public void computeConflicts(Assignment<Request, Enrollment> assignment, Enrollment enrollment, Set<Enrollment> conflicts) { 116 // check reservation can assign over the limit 117 if (((StudentSectioningModel)getModel()).getReservationCanAssignOverTheLimit() && 118 enrollment.getReservation() != null && enrollment.getReservation().canAssignOverLimit()) 119 return; 120 121 // enrollment's course 122 Course course = enrollment.getCourse(); 123 124 // exclude free time requests 125 if (course == null) 126 return; 127 128 // unlimited course 129 if (course.getLimit() < 0) 130 return; 131 132 // new enrollment weight 133 double enrlWeight = getEnrollmentWeight(assignment, course, enrollment.getRequest()); 134 135 // below limit -> ok 136 if (enrlWeight <= course.getLimit()) 137 return; 138 139 // above limit -> compute adepts (current assignments that are not 140 // yet conflicting) 141 // exclude all conflicts as well 142 List<Enrollment> adepts = new ArrayList<Enrollment>(course.getEnrollments(assignment).size()); 143 for (Enrollment e : course.getEnrollments(assignment)) { 144 if (e.getRequest().equals(enrollment.getRequest())) 145 continue; 146 if (conflicts.contains(e)) 147 enrlWeight -= e.getRequest().getWeight(); 148 else 149 adepts.add(e); 150 } 151 152 // while above limit -> pick an adept and make it conflicting 153 while (enrlWeight > course.getLimit()) { 154 if (adepts.isEmpty()) { 155 // no adepts -> enrollment cannot be assigned 156 conflicts.add(enrollment); 157 break; 158 } 159 160 // pick adept (prefer dummy students), decrease unreserved space, 161 // make conflict 162 List<Enrollment> best = new ArrayList<Enrollment>(); 163 boolean bestDummy = false; 164 double bestValue = 0; 165 for (Enrollment adept: adepts) { 166 boolean dummy = adept.getStudent().isDummy(); 167 double value = adept.toDouble(assignment, false); 168 169 if (iPreferDummyStudents && dummy != bestDummy) { 170 if (dummy) { 171 best.clear(); 172 best.add(adept); 173 bestDummy = dummy; 174 bestValue = value; 175 } 176 continue; 177 } 178 179 if (best.isEmpty() || value > bestValue) { 180 if (best.isEmpty()) best.clear(); 181 best.add(adept); 182 bestDummy = dummy; 183 bestValue = value; 184 } else if (bestValue == value) { 185 best.add(adept); 186 } 187 } 188 189 Enrollment conflict = ToolBox.random(best); 190 adepts.remove(conflict); 191 enrlWeight -= conflict.getRequest().getWeight(); 192 conflicts.add(conflict); 193 } 194 } 195 196 /** 197 * A given enrollment is conflicting, if the course's enrollment (computed by 198 * {@link CourseLimit#getEnrollmentWeight(Assignment, Course, Request)}) exceeds the 199 * limit. 200 * 201 * @param enrollment 202 * {@link Enrollment} that is being considered 203 * @return true, if the enrollment cannot be assigned without exceeding the limit 204 */ 205 @Override 206 public boolean inConflict(Assignment<Request, Enrollment> assignment, Enrollment enrollment) { 207 // check reservation can assign over the limit 208 if (((StudentSectioningModel)getModel()).getReservationCanAssignOverTheLimit() && 209 enrollment.getReservation() != null && enrollment.getReservation().canAssignOverLimit()) 210 return false; 211 212 // enrollment's course 213 Course course = enrollment.getCourse(); 214 215 // exclude free time requests 216 if (course == null) 217 return false; 218 219 // unlimited course 220 if (course.getLimit() < 0) 221 return false; 222 223 224 // new enrollment weight 225 double enrlWeight = getEnrollmentWeight(assignment, course, enrollment.getRequest()); 226 227 // above limit -> conflict 228 return (enrlWeight > course.getLimit() + sNominalWeight); 229 } 230 231 @Override 232 public String toString() { 233 return "CourseLimit"; 234 } 235 236}