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.Config; 012import org.cpsolver.studentsct.model.Enrollment; 013import org.cpsolver.studentsct.model.Request; 014 015 016/** 017 * Configuration limit constraint. This global constraint ensures that a limit of each 018 * configuration is not exceeded. This means that the total sum of weights of course 019 * requests (see {@link Request#getWeight()}) enrolled into a configuration is below 020 * the configuration's limit (see {@link Config#getLimit()}). 021 * 022 * <br> 023 * <br> 024 * Configurations 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>ConfigLimit.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 ConfigLimit extends GlobalConstraint<Request, Enrollment> { 066 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 ConfigLimit(DataProperties cfg) { 077 super(); 078 iPreferDummyStudents = cfg.getPropertyBoolean("ConfigLimit.PreferDummyStudents", false); 079 } 080 081 082 /** 083 * Enrollment weight of a config 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 configs 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 config is changed to a small nominal weight. 088 * 089 * @param assignment current assignment 090 * @param config 091 * a config 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, Config config, Request request) { 098 return config.getEnrollmentWeight(assignment, request) + request.getWeight() - Math.max(config.getMaxEnrollmentWeight(assignment), request.getWeight()) + sNominalWeight; 099 } 100 101 /** 102 * A given enrollment is conflicting, if the config's enrollment 103 * (computed by {@link ConfigLimit#getEnrollmentWeight(Assignment, Config, 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 (enrollment.getReservation() != null && enrollment.getReservation().canBatchAssignOverLimit()) 118 return; 119 120 // enrollment's config 121 Config config = enrollment.getConfig(); 122 123 // exclude free time requests 124 if (config == null) 125 return; 126 127 128 // unlimited config 129 if (config.getLimit() < 0) 130 return; 131 132 // new enrollment weight 133 double enrlWeight = getEnrollmentWeight(assignment, config, enrollment.getRequest()); 134 135 // below limit -> ok 136 if (enrlWeight <= config.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>(config.getEnrollments(assignment).size()); 143 for (Enrollment e : config.getEnrollments(assignment)) { 144 if (e.getRequest().equals(enrollment.getRequest())) 145 continue; 146 if (e.getReservation() != null && e.getReservation().canBatchAssignOverLimit()) 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 (enrollment.getReservation() != null && enrollment.getReservation().canBatchAssignOverLimit()) 226 return false; 227 228 // enrollment's config 229 Config config = enrollment.getConfig(); 230 231 // exclude free time requests 232 if (config == null) 233 return false; 234 235 // unlimited config 236 if (config.getLimit() < 0) 237 return false; 238 239 240 // new enrollment weight 241 double enrlWeight = getEnrollmentWeight(assignment, config, enrollment.getRequest()); 242 243 // above limit -> conflict 244 return (enrlWeight > config.getLimit()); 245 } 246 247 @Override 248 public String toString() { 249 return "ConfigLimit"; 250 } 251 252}