001package org.cpsolver.studentsct.online; 002 003import java.util.Set; 004 005import org.cpsolver.ifs.assignment.Assignment; 006import org.cpsolver.ifs.heuristics.RouletteWheelSelection; 007import org.cpsolver.ifs.model.GlobalConstraint; 008import org.cpsolver.studentsct.model.CourseRequest; 009import org.cpsolver.studentsct.model.Enrollment; 010import org.cpsolver.studentsct.model.Request; 011import org.cpsolver.studentsct.model.Section; 012import org.cpsolver.studentsct.online.expectations.OverExpectedCriterion; 013 014/** 015 * A global constraint limiting the over-expected penalization for a student. 016 * Only to be used during online scheduling (with the {@link OnlineSectioningModel}). 017 * 018 * @version StudentSct 1.3 (Student Sectioning)<br> 019 * Copyright (C) 2014 Tomáš Müller<br> 020 * <a href="mailto:muller@unitime.org">muller@unitime.org</a><br> 021 * <a href="http://muller.unitime.org">http://muller.unitime.org</a><br> 022 * <br> 023 * This library is free software; you can redistribute it and/or modify 024 * it under the terms of the GNU Lesser General Public License as 025 * published by the Free Software Foundation; either version 3 of the 026 * License, or (at your option) any later version. <br> 027 * <br> 028 * This library is distributed in the hope that it will be useful, but 029 * WITHOUT ANY WARRANTY; without even the implied warranty of 030 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 031 * Lesser General Public License for more details. <br> 032 * <br> 033 * You should have received a copy of the GNU Lesser General Public 034 * License along with this library; if not see <a href='http://www.gnu.org/licenses'>http://www.gnu.org/licenses</a>. 035 * 036 */ 037public class MaxOverExpectedConstraint extends GlobalConstraint<Request, Enrollment> { 038 private double iMaxOverExpected; 039 040 public MaxOverExpectedConstraint(double limit) { 041 iMaxOverExpected = limit; 042 } 043 044 @Override 045 public void computeConflicts(Assignment<Request, Enrollment> assignment, Enrollment value, Set<Enrollment> conflicts) { 046 if (!value.isCourseRequest()) return; 047 CourseRequest cr = (CourseRequest) value.variable(); 048 OnlineSectioningModel model = (OnlineSectioningModel) getModel(); 049 050 double basePenalty = model.getOverExpected(assignment, value, value, conflicts); 051 052 if (basePenalty > iMaxOverExpected) { 053 conflicts.add(value); 054 return; 055 } 056 057 RouletteWheelSelection<Enrollment> selection = new RouletteWheelSelection<Enrollment>(); 058 059 for (Request r : cr.getStudent().getRequests()) { 060 Enrollment e = assignment.getValue(r); 061 if (e != null && !r.equals(value.variable()) && !conflicts.contains(e) && e.isCourseRequest()) { 062 double penalty = model.getOverExpected(assignment, e, value, conflicts); 063 if (penalty > 0.0) 064 selection.add(e, penalty); 065 } 066 } 067 068 while (selection.getRemainingPoints() + basePenalty > iMaxOverExpected && selection.hasMoreElements()) { 069 conflicts.add(selection.nextElement()); 070 } 071 } 072 073 @Override 074 public boolean inConflict(Assignment<Request, Enrollment> assignment, Enrollment value) { 075 if (!value.isCourseRequest()) return false; 076 OverExpectedCriterion over = ((OnlineSectioningModel)getModel()).getOverExpectedCriterion(); 077 if (over == null) return false; 078 079 double penalty = 0.0; 080 for (Request r : value.getRequest().getStudent().getRequests()) { 081 Enrollment e = (r.equals(value.variable()) ? value : assignment.getValue(r)); 082 if (e != null && e.isCourseRequest()) 083 for (Section s : e.getSections()) 084 penalty += over.getOverExpected(assignment, s, e.getRequest()); 085 } 086 return penalty > iMaxOverExpected; 087 } 088 089}