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