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 (conflicts.contains(e)) 145 enrlWeight -= e.getRequest().getWeight(); 146 else 147 adepts.add(e); 148 } 149 150 // while above limit -> pick an adept and make it conflicting 151 while (enrlWeight > course.getLimit()) { 152 if (adepts.isEmpty()) { 153 // no adepts -> enrollment cannot be assigned 154 conflicts.add(enrollment); 155 break; 156 } 157 158 // pick adept (prefer dummy students), decrease unreserved space, 159 // make conflict 160 List<Enrollment> best = new ArrayList<Enrollment>(); 161 boolean bestDummy = false; 162 double bestValue = 0; 163 for (Enrollment adept: adepts) { 164 boolean dummy = adept.getStudent().isDummy(); 165 double value = adept.toDouble(assignment, false); 166 167 if (iPreferDummyStudents && dummy != bestDummy) { 168 if (dummy) { 169 best.clear(); 170 best.add(adept); 171 bestDummy = dummy; 172 bestValue = value; 173 } 174 continue; 175 } 176 177 if (best.isEmpty() || value > bestValue) { 178 if (best.isEmpty()) best.clear(); 179 best.add(adept); 180 bestDummy = dummy; 181 bestValue = value; 182 } else if (bestValue == value) { 183 best.add(adept); 184 } 185 } 186 187 Enrollment conflict = ToolBox.random(best); 188 adepts.remove(conflict); 189 enrlWeight -= conflict.getRequest().getWeight(); 190 conflicts.add(conflict); 191 } 192 } 193 194 /** 195 * A given enrollment is conflicting, if the course's enrollment (computed by 196 * {@link CourseLimit#getEnrollmentWeight(Assignment, Course, Request)}) exceeds the 197 * limit. 198 * 199 * @param enrollment 200 * {@link Enrollment} that is being considered 201 * @return true, if the enrollment cannot be assigned without exceeding the limit 202 */ 203 @Override 204 public boolean inConflict(Assignment<Request, Enrollment> assignment, Enrollment enrollment) { 205 // check reservation can assign over the limit 206 if (enrollment.getReservation() != null && enrollment.getReservation().canBatchAssignOverLimit()) 207 return false; 208 209 // enrollment's course 210 Course course = enrollment.getCourse(); 211 212 // exclude free time requests 213 if (course == null) 214 return false; 215 216 // unlimited course 217 if (course.getLimit() < 0) 218 return false; 219 220 221 // new enrollment weight 222 double enrlWeight = getEnrollmentWeight(assignment, course, enrollment.getRequest()); 223 224 // above limit -> conflict 225 return (enrlWeight > course.getLimit() + sNominalWeight); 226 } 227 228 @Override 229 public String toString() { 230 return "CourseLimit"; 231 } 232 233}