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.Config; 013import org.cpsolver.studentsct.model.Enrollment; 014import org.cpsolver.studentsct.model.Request; 015 016 017/** 018 * Configuration limit constraint. This global constraint ensures that a limit of each 019 * configuration is not exceeded. This means that the total sum of weights of course 020 * requests (see {@link Request#getWeight()}) enrolled into a configuration is below 021 * the configuration's limit (see {@link Config#getLimit()}). 022 * 023 * <br> 024 * <br> 025 * Configurations 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>ConfigLimit.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 ConfigLimit extends GlobalConstraint<Request, Enrollment> { 067 068 private static double sNominalWeight = 0.00001; 069 private boolean iPreferDummyStudents = false; 070 071 /** 072 * Constructor 073 * 074 * @param cfg 075 * solver configuration 076 */ 077 public ConfigLimit(DataProperties cfg) { 078 super(); 079 iPreferDummyStudents = cfg.getPropertyBoolean("ConfigLimit.PreferDummyStudents", false); 080 } 081 082 083 /** 084 * Enrollment weight of a config if the given request is assigned. In order 085 * to overcome rounding problems with last-like students ( e.g., 5 students 086 * are projected to two configs of limit 2 -- each section can have up to 3 087 * of these last-like students), the weight of the request with the highest 088 * weight in the config is changed to a small nominal weight. 089 * 090 * @param assignment current assignment 091 * @param config 092 * a config that is of concern 093 * @param request 094 * a request of a student to be assigned containing the given 095 * section 096 * @return section's new weight 097 */ 098 public static double getEnrollmentWeight(Assignment<Request, Enrollment> assignment, Config config, Request request) { 099 return config.getEnrollmentWeight(assignment, request) + request.getWeight() - Math.max(config.getMaxEnrollmentWeight(assignment), request.getWeight()) + sNominalWeight; 100 } 101 102 /** 103 * A given enrollment is conflicting, if the config's enrollment 104 * (computed by {@link ConfigLimit#getEnrollmentWeight(Assignment, Config, Request)}) 105 * exceeds the limit. <br> 106 * If the limit is breached, one or more existing enrollments are 107 * (randomly) selected as conflicting until the overall weight is under the 108 * limit. 109 * 110 * @param enrollment 111 * {@link Enrollment} that is being considered 112 * @param conflicts 113 * all computed conflicting requests are added into this set 114 */ 115 @Override 116 public void computeConflicts(Assignment<Request, Enrollment> assignment, Enrollment enrollment, Set<Enrollment> conflicts) { 117 // check reservation can assign over the limit 118 if (((StudentSectioningModel)getModel()).getReservationCanAssignOverTheLimit() && 119 enrollment.getReservation() != null && enrollment.getReservation().canAssignOverLimit()) 120 return; 121 122 // enrollment's config 123 Config config = enrollment.getConfig(); 124 125 // exclude free time requests 126 if (config == null) 127 return; 128 129 130 // unlimited config 131 if (config.getLimit() < 0) 132 return; 133 134 // new enrollment weight 135 double enrlWeight = getEnrollmentWeight(assignment, config, enrollment.getRequest()); 136 137 // below limit -> ok 138 if (enrlWeight <= config.getLimit()) 139 return; 140 141 // above limit -> compute adepts (current assignments that are not 142 // yet conflicting) 143 // exclude all conflicts as well 144 List<Enrollment> adepts = new ArrayList<Enrollment>(config.getEnrollments(assignment).size()); 145 for (Enrollment e : config.getEnrollments(assignment)) { 146 if (e.getRequest().equals(enrollment.getRequest())) 147 continue; 148 if (conflicts.contains(e)) 149 enrlWeight -= e.getRequest().getWeight(); 150 else 151 adepts.add(e); 152 } 153 154 // while above limit -> pick an adept and make it conflicting 155 while (enrlWeight > config.getLimit()) { 156 if (adepts.isEmpty()) { 157 // no adepts -> enrollment cannot be assigned 158 conflicts.add(enrollment); 159 return; 160 } 161 162 // pick adept (prefer dummy students & students w/o reservation), decrease enrollment 163 // weight, make conflict 164 List<Enrollment> best = new ArrayList<Enrollment>(); 165 boolean bestDummy = false; 166 double bestValue = 0; 167 boolean bestRes = true; 168 for (Enrollment adept: adepts) { 169 boolean dummy = adept.getStudent().isDummy(); 170 double value = adept.toDouble(assignment, false); 171 boolean res = (adept.getReservation() != null); 172 173 if (iPreferDummyStudents && dummy != bestDummy) { 174 if (dummy) { 175 best.clear(); 176 best.add(adept); 177 bestDummy = dummy; 178 bestValue = value; 179 bestRes = res; 180 } 181 continue; 182 } 183 184 if (bestRes != res) { 185 if (!res) { 186 best.clear(); 187 best.add(adept); 188 bestDummy = dummy; 189 bestValue = value; 190 bestRes = res; 191 } 192 continue; 193 } 194 195 if (best.isEmpty() || value > bestValue) { 196 if (best.isEmpty()) best.clear(); 197 best.add(adept); 198 bestDummy = dummy; 199 bestValue = value; 200 bestRes = res; 201 } else if (bestValue == value) { 202 best.add(adept); 203 } 204 } 205 206 Enrollment conflict = ToolBox.random(best); 207 adepts.remove(conflict); 208 enrlWeight -= conflict.getRequest().getWeight(); 209 conflicts.add(conflict); 210 } 211 } 212 213 /** 214 * A given enrollment is conflicting, if the config's enrollment (computed by 215 * {@link ConfigLimit#getEnrollmentWeight(Assignment, Config, Request)}) exceeds the 216 * limit. 217 * 218 * @param enrollment 219 * {@link Enrollment} that is being considered 220 * @return true, if the enrollment cannot be assigned without exceeding the limit 221 */ 222 @Override 223 public boolean inConflict(Assignment<Request, Enrollment> assignment, Enrollment enrollment) { 224 // check reservation can assign over the limit 225 if (((StudentSectioningModel)getModel()).getReservationCanAssignOverTheLimit() && 226 enrollment.getReservation() != null && enrollment.getReservation().canAssignOverLimit()) 227 return false; 228 229 // enrollment's config 230 Config config = enrollment.getConfig(); 231 232 // exclude free time requests 233 if (config == null) 234 return false; 235 236 // unlimited config 237 if (config.getLimit() < 0) 238 return false; 239 240 241 // new enrollment weight 242 double enrlWeight = getEnrollmentWeight(assignment, config, enrollment.getRequest()); 243 244 // above limit -> conflict 245 return (enrlWeight > config.getLimit()); 246 } 247 248 @Override 249 public String toString() { 250 return "ConfigLimit"; 251 } 252 253}