001package org.cpsolver.studentsct.online.expectations; 002 003import org.cpsolver.ifs.assignment.Assignment; 004import org.cpsolver.ifs.util.DataProperties; 005import org.cpsolver.studentsct.model.Enrollment; 006import org.cpsolver.studentsct.model.Request; 007import org.cpsolver.studentsct.model.Section; 008import org.cpsolver.studentsct.model.Subpart; 009 010/** 011 * Avoid unbalanced sections when there are no expectations. When there are 012 * expectations, the {@link PercentageOverExpected} is used. If there are no 013 * expectations, sections that would have more students than given by the balance 014 * are marked as over-expected. The target fill ratio is proportional to the section 015 * size, a section is considered unbalanced, when the target fill is exceeded by 016 * more than OverExpected.Disbalance percentage (defaults to 0.1). Unlimited 017 * sections are also balanced, when General.BalanceUnlimited parameter is set 018 * to true (defaults to false).<br><br> 019 * A class of an offering with no expectations is over-expected when the number 020 * of enrolled students (including the student in question) minus the target 021 * fill is over the OverExpected.Disbalance portion of the section limit. 022 * 023 * @author Tomáš Müller 024 * @version StudentSct 1.3 (Student Sectioning)<br> 025 * Copyright (C) 2014 Tomáš Müller<br> 026 * <a href="mailto:muller@unitime.org">muller@unitime.org</a><br> 027 * <a href="http://muller.unitime.org">http://muller.unitime.org</a><br> 028 * <br> 029 * This library is free software; you can redistribute it and/or modify 030 * it under the terms of the GNU Lesser General Public License as 031 * published by the Free Software Foundation; either version 3 of the 032 * License, or (at your option) any later version. <br> 033 * <br> 034 * This library is distributed in the hope that it will be useful, but 035 * WITHOUT ANY WARRANTY; without even the implied warranty of 036 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 037 * Lesser General Public License for more details. <br> 038 * <br> 039 * You should have received a copy of the GNU Lesser General Public 040 * License along with this library; if not see <a 041 * href='http://www.gnu.org/licenses'>http://www.gnu.org/licenses</a>. 042 * 043 */ 044public class AvoidUnbalancedWhenNoExpectations extends PercentageOverExpected { 045 private Double iDisbalance = 0.1; 046 private boolean iBalanceUnlimited = false; 047 048 public AvoidUnbalancedWhenNoExpectations(DataProperties config) { 049 super(config); 050 iDisbalance = config.getPropertyDouble("OverExpected.Disbalance", iDisbalance); 051 iBalanceUnlimited = config.getPropertyBoolean("General.BalanceUnlimited", iBalanceUnlimited); 052 } 053 054 public AvoidUnbalancedWhenNoExpectations(Double percentage, Double disbalance) { 055 super(percentage); 056 iDisbalance = disbalance; 057 } 058 059 public AvoidUnbalancedWhenNoExpectations(Double percentage) { 060 this(percentage, null); 061 } 062 063 public AvoidUnbalancedWhenNoExpectations() { 064 this(null, null); 065 } 066 067 /** 068 * Return allowed disbalance, defaults to 0.1 (parameter OverExpected.Disbalance) 069 * @return allowed disbalance 070 */ 071 public Double getDisbalance() { 072 return iDisbalance; 073 } 074 075 /** 076 * Is balancing of unlimited sections enabled (parameter General.BalanceUnlimited) 077 * @return true if balancing of unlimited sections is enabled 078 */ 079 public boolean isBalanceUnlimited() { 080 return iBalanceUnlimited; 081 } 082 083 @Override 084 public double getOverExpected(Assignment<Request, Enrollment> assignment, Section section, Request request) { 085 Subpart subpart = section.getSubpart(); 086 087 if (hasExpectations(subpart) && section.getLimit() > 0) 088 return super.getOverExpected(assignment, section, request); 089 090 if (getDisbalance() == null || getDisbalance() < 0.0) 091 return 0.0; 092 093 double enrlConfig = request.getWeight() + getEnrollment(assignment, subpart.getConfig(), request); 094 int subparts = section.getSubpart().getConfig().getSubparts().size(); 095 int limit = getLimit(section); 096 double enrl = request.getWeight() + getEnrollment(assignment, section, request); 097 098 if (limit > 0) { 099 // sections have limits -> desired size is section limit x (total 100 // enrollment / total limit) 101 double desired = (enrlConfig / getLimit(subpart)) * limit; 102 if (enrl - desired >= Math.max(1.0, getDisbalance() * limit)) 103 return 1.0 / subparts; 104 } else if (isBalanceUnlimited()) { 105 // unlimited sections -> desired size is total enrollment / number 106 // of sections 107 double desired = enrlConfig / subpart.getSections().size(); 108 if (enrl - desired >= Math.max(1.0, getDisbalance() * desired)) 109 return 1.0 / subparts; 110 } 111 112 return 0.0; 113 } 114 115 @Override 116 public String toString() { 117 return "bal(" + getPercentage() + "," + getDisbalance() + ")"; 118 } 119 120}