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.Course;
012import org.cpsolver.studentsct.model.Enrollment;
013import org.cpsolver.studentsct.model.Request;
014
015
016/**
017 * Course limit constraint. This global constraint ensures that a limit of each
018 * course is not exceeded. This means that the total sum of weights of course
019 * requests (see {@link Request#getWeight()}) enrolled into a course is below
020 * the course's limit (see {@link Course#getLimit()}).
021 * 
022 * <br>
023 * <br>
024 * Sections 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>CourseLimit.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 CourseLimit extends GlobalConstraint<Request, Enrollment> {
066    private static double sNominalWeight = 0.00001;
067    private boolean iPreferDummyStudents = false;
068
069    /**
070     * Constructor
071     * 
072     * @param cfg
073     *            solver configuration
074     */
075    public CourseLimit(DataProperties cfg) {
076        super();
077        iPreferDummyStudents = cfg.getPropertyBoolean("CourseLimit.PreferDummyStudents", false);
078    }
079
080
081    /**
082     * Enrollment weight of a course if the given request is assigned. In order
083     * to overcome rounding problems with last-like students ( e.g., 5 students
084     * are projected to two sections of limit 2 -- each section can have up to 3
085     * of these last-like students), the weight of the request with the highest
086     * weight in the section is changed to a small nominal weight.
087     * 
088     * @param assignment current assignment
089     * @param course
090     *            a course that is of concern
091     * @param request
092     *            a request of a student to be assigned containing the given
093     *            section
094     * @return section's new weight
095     */
096    public static double getEnrollmentWeight(Assignment<Request, Enrollment> assignment, Course course, Request request) {
097        return course.getEnrollmentWeight(assignment, request) + request.getWeight() - Math.max(course.getMaxEnrollmentWeight(assignment), request.getWeight()) + sNominalWeight;
098    }
099
100    /**
101     * A given enrollment is conflicting, if the course's enrollment
102     * (computed by {@link CourseLimit#getEnrollmentWeight(Assignment, Course, Request)})
103     * exceeds the limit. <br>
104     * If the limit is breached, one or more existing enrollments are
105     * (randomly) selected as conflicting until the overall weight is under the
106     * limit.
107     * 
108     * @param enrollment
109     *            {@link Enrollment} that is being considered
110     * @param conflicts
111     *            all computed conflicting requests are added into this set
112     */
113    @Override
114    public void computeConflicts(Assignment<Request, Enrollment> assignment, Enrollment enrollment, Set<Enrollment> conflicts) {
115        // check reservation can assign over the limit
116        if (enrollment.getReservation() != null && enrollment.getReservation().canBatchAssignOverLimit())
117            return;
118
119        // enrollment's course
120        Course course = enrollment.getCourse();
121
122        // exclude free time requests
123        if (course == null)
124            return;
125        
126        // exclude empty enrollmens
127        if (enrollment.getSections() == null || enrollment.getSections().isEmpty())
128            return;
129
130        // unlimited course
131        if (course.getLimit() < 0)
132            return;
133        
134        // new enrollment weight
135        double enrlWeight = getEnrollmentWeight(assignment, course, enrollment.getRequest());
136
137        // below limit -> ok
138        if (enrlWeight <= course.getLimit())
139            return;
140
141        // above limit -> compute adepts (current assignments that are not
142        // yet conflicting)
143        // exclude all conflicts as well
144        List<Enrollment> adepts = new ArrayList<Enrollment>(course.getEnrollments(assignment).size());
145        for (Enrollment e : course.getEnrollments(assignment)) {
146            if (e.getRequest().equals(enrollment.getRequest()))
147                continue;
148            if (e.getReservation() != null && e.getReservation().canBatchAssignOverLimit())
149                continue;
150            if (conflicts.contains(e))
151                enrlWeight -= e.getRequest().getWeight();
152            else
153                adepts.add(e);
154        }
155
156        // while above limit -> pick an adept and make it conflicting
157        while (enrlWeight > course.getLimit()) {
158            if (adepts.isEmpty()) {
159                // no adepts -> enrollment cannot be assigned
160                conflicts.add(enrollment);
161                break;
162            }
163            
164            // pick adept (prefer dummy students), decrease unreserved space,
165            // make conflict
166            List<Enrollment> best = new ArrayList<Enrollment>();
167            boolean bestDummy = false;
168            double bestValue = 0;
169            for (Enrollment adept: adepts) {
170                boolean dummy = adept.getStudent().isDummy();
171                double value = adept.toDouble(assignment, false);
172                
173                if (iPreferDummyStudents && dummy != bestDummy) {
174                    if (dummy) {
175                        best.clear();
176                        best.add(adept);
177                        bestDummy = dummy;
178                        bestValue = value;
179                    }
180                    continue;
181                }
182                
183                if (best.isEmpty() || value > bestValue) {
184                    if (best.isEmpty()) best.clear();
185                    best.add(adept);
186                    bestDummy = dummy;
187                    bestValue = value;
188                } else if (bestValue == value) {
189                    best.add(adept);
190                }
191            }
192            
193            Enrollment conflict = ToolBox.random(best);
194            adepts.remove(conflict);
195            enrlWeight -= conflict.getRequest().getWeight();
196            conflicts.add(conflict);
197        }
198    }
199
200    /**
201     * A given enrollment is conflicting, if the course's enrollment (computed by
202     * {@link CourseLimit#getEnrollmentWeight(Assignment, Course, Request)}) exceeds the
203     * limit.
204     * 
205     * @param enrollment
206     *            {@link Enrollment} that is being considered
207     * @return true, if the enrollment cannot be assigned without exceeding the limit
208     */
209    @Override
210    public boolean inConflict(Assignment<Request, Enrollment> assignment, Enrollment enrollment) {
211        // check reservation can assign over the limit
212        if (enrollment.getReservation() != null && enrollment.getReservation().canBatchAssignOverLimit())
213            return false;
214
215        // enrollment's course
216        Course course = enrollment.getCourse();
217
218        // exclude free time requests
219        if (course == null)
220            return false;
221        
222        // exclude empty enrollmens
223        if (enrollment.getSections() == null || enrollment.getSections().isEmpty())
224            return false;
225
226        // unlimited course
227        if (course.getLimit() < 0)
228            return false;
229
230
231        // new enrollment weight
232        double enrlWeight = getEnrollmentWeight(assignment, course, enrollment.getRequest());
233        
234        // above limit -> conflict
235        return (enrlWeight > course.getLimit());
236    }
237    
238    @Override
239    public String toString() {
240        return "CourseLimit";
241    }
242
243}