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 (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 > config.getLimit()) { 154 if (adepts.isEmpty()) { 155 // no adepts -> enrollment cannot be assigned 156 conflicts.add(enrollment); 157 return; 158 } 159 160 // pick adept (prefer dummy students & students w/o reservation), decrease enrollment 161 // weight, make conflict 162 List<Enrollment> best = new ArrayList<Enrollment>(); 163 boolean bestDummy = false; 164 double bestValue = 0; 165 boolean bestRes = true; 166 for (Enrollment adept: adepts) { 167 boolean dummy = adept.getStudent().isDummy(); 168 double value = adept.toDouble(assignment, false); 169 boolean res = (adept.getReservation() != null); 170 171 if (iPreferDummyStudents && dummy != bestDummy) { 172 if (dummy) { 173 best.clear(); 174 best.add(adept); 175 bestDummy = dummy; 176 bestValue = value; 177 bestRes = res; 178 } 179 continue; 180 } 181 182 if (bestRes != res) { 183 if (!res) { 184 best.clear(); 185 best.add(adept); 186 bestDummy = dummy; 187 bestValue = value; 188 bestRes = res; 189 } 190 continue; 191 } 192 193 if (best.isEmpty() || value > bestValue) { 194 if (best.isEmpty()) best.clear(); 195 best.add(adept); 196 bestDummy = dummy; 197 bestValue = value; 198 bestRes = res; 199 } else if (bestValue == value) { 200 best.add(adept); 201 } 202 } 203 204 Enrollment conflict = ToolBox.random(best); 205 adepts.remove(conflict); 206 enrlWeight -= conflict.getRequest().getWeight(); 207 conflicts.add(conflict); 208 } 209 } 210 211 /** 212 * A given enrollment is conflicting, if the config's enrollment (computed by 213 * {@link ConfigLimit#getEnrollmentWeight(Assignment, Config, Request)}) exceeds the 214 * limit. 215 * 216 * @param enrollment 217 * {@link Enrollment} that is being considered 218 * @return true, if the enrollment cannot be assigned without exceeding the limit 219 */ 220 @Override 221 public boolean inConflict(Assignment<Request, Enrollment> assignment, Enrollment enrollment) { 222 // check reservation can assign over the limit 223 if (enrollment.getReservation() != null && enrollment.getReservation().canBatchAssignOverLimit()) 224 return false; 225 226 // enrollment's config 227 Config config = enrollment.getConfig(); 228 229 // exclude free time requests 230 if (config == null) 231 return false; 232 233 // unlimited config 234 if (config.getLimit() < 0) 235 return false; 236 237 238 // new enrollment weight 239 double enrlWeight = getEnrollmentWeight(assignment, config, enrollment.getRequest()); 240 241 // above limit -> conflict 242 return (enrlWeight > config.getLimit()); 243 } 244 245 @Override 246 public String toString() { 247 return "ConfigLimit"; 248 } 249 250}