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 // exclude empty enrollmens 128 if (enrollment.getSections() == null || enrollment.getSections().isEmpty()) 129 return; 130 131 // unlimited config 132 if (config.getLimit() < 0) 133 return; 134 135 // new enrollment weight 136 double enrlWeight = getEnrollmentWeight(assignment, config, enrollment.getRequest()); 137 138 // below limit -> ok 139 if (enrlWeight <= config.getLimit()) 140 return; 141 142 // above limit -> compute adepts (current assignments that are not 143 // yet conflicting) 144 // exclude all conflicts as well 145 List<Enrollment> adepts = new ArrayList<Enrollment>(config.getEnrollments(assignment).size()); 146 for (Enrollment e : config.getEnrollments(assignment)) { 147 if (e.getRequest().equals(enrollment.getRequest())) 148 continue; 149 if (e.getReservation() != null && e.getReservation().canBatchAssignOverLimit()) 150 continue; 151 if (conflicts.contains(e)) 152 enrlWeight -= e.getRequest().getWeight(); 153 else 154 adepts.add(e); 155 } 156 157 // while above limit -> pick an adept and make it conflicting 158 while (enrlWeight > config.getLimit()) { 159 if (adepts.isEmpty()) { 160 // no adepts -> enrollment cannot be assigned 161 conflicts.add(enrollment); 162 return; 163 } 164 165 // pick adept (prefer dummy students & students w/o reservation), decrease enrollment 166 // weight, make conflict 167 List<Enrollment> best = new ArrayList<Enrollment>(); 168 boolean bestDummy = false; 169 double bestValue = 0; 170 boolean bestRes = true; 171 for (Enrollment adept: adepts) { 172 boolean dummy = adept.getStudent().isDummy(); 173 double value = adept.toDouble(assignment, false); 174 boolean res = (adept.getReservation() != null); 175 176 if (iPreferDummyStudents && dummy != bestDummy) { 177 if (dummy) { 178 best.clear(); 179 best.add(adept); 180 bestDummy = dummy; 181 bestValue = value; 182 bestRes = res; 183 } 184 continue; 185 } 186 187 if (bestRes != res) { 188 if (!res) { 189 best.clear(); 190 best.add(adept); 191 bestDummy = dummy; 192 bestValue = value; 193 bestRes = res; 194 } 195 continue; 196 } 197 198 if (best.isEmpty() || value > bestValue) { 199 if (best.isEmpty()) best.clear(); 200 best.add(adept); 201 bestDummy = dummy; 202 bestValue = value; 203 bestRes = res; 204 } else if (bestValue == value) { 205 best.add(adept); 206 } 207 } 208 209 Enrollment conflict = ToolBox.random(best); 210 adepts.remove(conflict); 211 enrlWeight -= conflict.getRequest().getWeight(); 212 conflicts.add(conflict); 213 } 214 } 215 216 /** 217 * A given enrollment is conflicting, if the config's enrollment (computed by 218 * {@link ConfigLimit#getEnrollmentWeight(Assignment, Config, Request)}) exceeds the 219 * limit. 220 * 221 * @param enrollment 222 * {@link Enrollment} that is being considered 223 * @return true, if the enrollment cannot be assigned without exceeding the limit 224 */ 225 @Override 226 public boolean inConflict(Assignment<Request, Enrollment> assignment, Enrollment enrollment) { 227 // check reservation can assign over the limit 228 if (enrollment.getReservation() != null && enrollment.getReservation().canBatchAssignOverLimit()) 229 return false; 230 231 // enrollment's config 232 Config config = enrollment.getConfig(); 233 234 // exclude free time requests 235 if (config == null) 236 return false; 237 238 // exclude empty enrollmens 239 if (enrollment.getSections() == null || enrollment.getSections().isEmpty()) 240 return false; 241 242 // unlimited config 243 if (config.getLimit() < 0) 244 return false; 245 246 // new enrollment weight 247 double enrlWeight = getEnrollmentWeight(assignment, config, enrollment.getRequest()); 248 249 // above limit -> conflict 250 return (enrlWeight > config.getLimit()); 251 } 252 253 @Override 254 public String toString() { 255 return "ConfigLimit"; 256 } 257 258}