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.model.Course; 012import org.cpsolver.studentsct.model.Enrollment; 013import org.cpsolver.studentsct.model.Request; 014 015 016/** 017 * Course limit constraint. This global constraint ensures that a limit of each 018 * course is not exceeded. This means that the total sum of weights of course 019 * requests (see {@link Request#getWeight()}) enrolled into a course is below 020 * the course's limit (see {@link Course#getLimit()}). 021 * 022 * <br> 023 * <br> 024 * Sections with negative limit are considered unlimited, and therefore 025 * completely ignored by this constraint. 026 * 027 * <br> 028 * <br> 029 * Parameters: 030 * <table border='1' summary='Related Solver Parameters'> 031 * <tr> 032 * <th>Parameter</th> 033 * <th>Type</th> 034 * <th>Comment</th> 035 * </tr> 036 * <tr> 037 * <td>CourseLimit.PreferDummyStudents</td> 038 * <td>{@link Boolean}</td> 039 * <td>If true, requests of dummy (last-like) students are preferred to be 040 * selected as conflicting.</td> 041 * </tr> 042 * </table> 043 * <br> 044 * <br> 045 * 046 * @version StudentSct 1.3 (Student Sectioning)<br> 047 * Copyright (C) 2007 - 2014 Tomas Muller<br> 048 * <a href="mailto:muller@unitime.org">muller@unitime.org</a><br> 049 * <a href="http://muller.unitime.org">http://muller.unitime.org</a><br> 050 * <br> 051 * This library is free software; you can redistribute it and/or modify 052 * it under the terms of the GNU Lesser General Public License as 053 * published by the Free Software Foundation; either version 3 of the 054 * License, or (at your option) any later version. <br> 055 * <br> 056 * This library is distributed in the hope that it will be useful, but 057 * WITHOUT ANY WARRANTY; without even the implied warranty of 058 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 059 * Lesser General Public License for more details. <br> 060 * <br> 061 * You should have received a copy of the GNU Lesser General Public 062 * License along with this library; if not see 063 * <a href='http://www.gnu.org/licenses/'>http://www.gnu.org/licenses/</a>. 064 */ 065public class CourseLimit extends GlobalConstraint<Request, Enrollment> { 066 private static double sNominalWeight = 0.00001; 067 private boolean iPreferDummyStudents = false; 068 069 /** 070 * Constructor 071 * 072 * @param cfg 073 * solver configuration 074 */ 075 public CourseLimit(DataProperties cfg) { 076 super(); 077 iPreferDummyStudents = cfg.getPropertyBoolean("CourseLimit.PreferDummyStudents", false); 078 } 079 080 081 /** 082 * Enrollment weight of a course if the given request is assigned. In order 083 * to overcome rounding problems with last-like students ( e.g., 5 students 084 * are projected to two sections of limit 2 -- each section can have up to 3 085 * of these last-like students), the weight of the request with the highest 086 * weight in the section is changed to a small nominal weight. 087 * 088 * @param assignment current assignment 089 * @param course 090 * a course that is of concern 091 * @param request 092 * a request of a student to be assigned containing the given 093 * section 094 * @return section's new weight 095 */ 096 public static double getEnrollmentWeight(Assignment<Request, Enrollment> assignment, Course course, Request request) { 097 return course.getEnrollmentWeight(assignment, request) + request.getWeight() - Math.max(course.getMaxEnrollmentWeight(assignment), request.getWeight()) + sNominalWeight; 098 } 099 100 /** 101 * A given enrollment is conflicting, if the course's enrollment 102 * (computed by {@link CourseLimit#getEnrollmentWeight(Assignment, Course, Request)}) 103 * exceeds the limit. <br> 104 * If the limit is breached, one or more existing enrollments are 105 * (randomly) selected as conflicting until the overall weight is under the 106 * limit. 107 * 108 * @param enrollment 109 * {@link Enrollment} that is being considered 110 * @param conflicts 111 * all computed conflicting requests are added into this set 112 */ 113 @Override 114 public void computeConflicts(Assignment<Request, Enrollment> assignment, Enrollment enrollment, Set<Enrollment> conflicts) { 115 // check reservation can assign over the limit 116 if (enrollment.getReservation() != null && enrollment.getReservation().canBatchAssignOverLimit()) 117 return; 118 119 // enrollment's course 120 Course course = enrollment.getCourse(); 121 122 // exclude free time requests 123 if (course == null) 124 return; 125 126 // unlimited course 127 if (course.getLimit() < 0) 128 return; 129 130 // new enrollment weight 131 double enrlWeight = getEnrollmentWeight(assignment, course, enrollment.getRequest()); 132 133 // below limit -> ok 134 if (enrlWeight <= course.getLimit()) 135 return; 136 137 // above limit -> compute adepts (current assignments that are not 138 // yet conflicting) 139 // exclude all conflicts as well 140 List<Enrollment> adepts = new ArrayList<Enrollment>(course.getEnrollments(assignment).size()); 141 for (Enrollment e : course.getEnrollments(assignment)) { 142 if (e.getRequest().equals(enrollment.getRequest())) 143 continue; 144 if (e.getReservation() != null && e.getReservation().canBatchAssignOverLimit()) 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 (enrollment.getReservation() != null && enrollment.getReservation().canBatchAssignOverLimit()) 209 return false; 210 211 // enrollment's course 212 Course course = enrollment.getCourse(); 213 214 // exclude free time requests 215 if (course == null) 216 return false; 217 218 // unlimited course 219 if (course.getLimit() < 0) 220 return false; 221 222 223 // new enrollment weight 224 double enrlWeight = getEnrollmentWeight(assignment, course, enrollment.getRequest()); 225 226 // above limit -> conflict 227 return (enrlWeight > course.getLimit()); 228 } 229 230 @Override 231 public String toString() { 232 return "CourseLimit"; 233 } 234 235}