001package org.cpsolver.studentsct.model; 002 003import java.text.DecimalFormat; 004import java.util.ArrayList; 005import java.util.Collection; 006import java.util.Collections; 007import java.util.HashMap; 008import java.util.HashSet; 009import java.util.List; 010import java.util.Map; 011import java.util.Set; 012import java.util.TreeSet; 013 014import org.cpsolver.coursett.model.TimeLocation; 015import org.cpsolver.ifs.assignment.Assignment; 016import org.cpsolver.ifs.assignment.AssignmentComparator; 017import org.cpsolver.ifs.util.ToolBox; 018import org.cpsolver.studentsct.StudentSectioningModel; 019import org.cpsolver.studentsct.constraint.ConfigLimit; 020import org.cpsolver.studentsct.constraint.CourseLimit; 021import org.cpsolver.studentsct.constraint.LinkedSections; 022import org.cpsolver.studentsct.constraint.SectionLimit; 023import org.cpsolver.studentsct.reservation.Reservation; 024import org.cpsolver.studentsct.reservation.Restriction; 025 026 027/** 028 * Representation of a request of a student for one or more course. A student 029 * requests one of the given courses, preferably the first one. <br> 030 * <br> 031 * 032 * @author Tomáš Müller 033 * @version StudentSct 1.3 (Student Sectioning)<br> 034 * Copyright (C) 2007 - 2014 Tomáš Müller<br> 035 * <a href="mailto:muller@unitime.org">muller@unitime.org</a><br> 036 * <a href="http://muller.unitime.org">http://muller.unitime.org</a><br> 037 * <br> 038 * This library is free software; you can redistribute it and/or modify 039 * it under the terms of the GNU Lesser General Public License as 040 * published by the Free Software Foundation; either version 3 of the 041 * License, or (at your option) any later version. <br> 042 * <br> 043 * This library is distributed in the hope that it will be useful, but 044 * WITHOUT ANY WARRANTY; without even the implied warranty of 045 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 046 * Lesser General Public License for more details. <br> 047 * <br> 048 * You should have received a copy of the GNU Lesser General Public 049 * License along with this library; if not see 050 * <a href='http://www.gnu.org/licenses/'>http://www.gnu.org/licenses/</a>. 051 */ 052public class CourseRequest extends Request { 053 private static DecimalFormat sDF = new DecimalFormat("0.000"); 054 private List<Course> iCourses = null; 055 private Set<Choice> iWaitlistedChoices = new HashSet<Choice>(); 056 private Set<Choice> iSelectedChoices = new HashSet<Choice>(); 057 private Set<Choice> iRequiredChoices = new HashSet<Choice>(); 058 private boolean iWaitlist = false; 059 private Long iTimeStamp = null; 060 private Double iCachedMinPenalty = null, iCachedMaxPenalty = null; 061 public static boolean sSameTimePrecise = false; 062 private Set<RequestGroup> iRequestGroups = new HashSet<RequestGroup>(); 063 private RequestPriority iPriority = RequestPriority.Normal; 064 private Enrollment iFixed = null; 065 066 /** 067 * Constructor 068 * 069 * @param id 070 * request unique id 071 * @param priority 072 * request priority 073 * @param alternative 074 * true if the request is alternative (alternative request can be 075 * assigned instead of a non-alternative course requests, if it 076 * is left unassigned) 077 * @param student 078 * appropriate student 079 * @param courses 080 * list of requested courses (in the correct order -- first is 081 * the requested course, second is the first alternative, etc.) 082 * @param waitlist 083 * time stamp of the request if the student can be put on a wait-list (no alternative 084 * course request will be given instead) 085 * @param critical 086 * is the course request is critical for the student in order to move forward in their degree 087 * @param timeStamp request time stamp 088 */ 089 public CourseRequest(long id, int priority, boolean alternative, Student student, java.util.List<Course> courses, boolean waitlist, boolean critical, Long timeStamp) { 090 super(id, priority, alternative, student); 091 iCourses = new ArrayList<Course>(courses); 092 for (Course course: iCourses) 093 course.getRequests().add(this); 094 iWaitlist = waitlist; 095 iPriority = (critical ? RequestPriority.Critical : RequestPriority.Normal); 096 iTimeStamp = timeStamp; 097 } 098 099 /** 100 * Constructor 101 * 102 * @param id 103 * request unique id 104 * @param priority 105 * request priority 106 * @param alternative 107 * true if the request is alternative (alternative request can be 108 * assigned instead of a non-alternative course requests, if it 109 * is left unassigned) 110 * @param student 111 * appropriate student 112 * @param courses 113 * list of requested courses (in the correct order -- first is 114 * the requested course, second is the first alternative, etc.) 115 * @param waitlist 116 * time stamp of the request if the student can be put on a wait-list (no alternative 117 * course request will be given instead) 118 * @param importance 119 * request priority 120 * @param timeStamp request time stamp 121 */ 122 public CourseRequest(long id, int priority, boolean alternative, Student student, java.util.List<Course> courses, boolean waitlist, RequestPriority importance, Long timeStamp) { 123 super(id, priority, alternative, student); 124 iCourses = new ArrayList<Course>(courses); 125 for (Course course: iCourses) 126 course.getRequests().add(this); 127 iWaitlist = waitlist; 128 iPriority = importance; 129 iTimeStamp = timeStamp; 130 } 131 132 /** 133 * Constructor 134 * 135 * @param id 136 * request unique id 137 * @param priority 138 * request priority 139 * @param alternative 140 * true if the request is alternative (alternative request can be 141 * assigned instead of a non-alternative course requests, if it 142 * is left unassigned) 143 * @param student 144 * appropriate student 145 * @param courses 146 * list of requested courses (in the correct order -- first is 147 * the requested course, second is the first alternative, etc.) 148 * @param waitlist 149 * time stamp of the request if the student can be put on a wait-list (no alternative 150 * course request will be given instead) 151 * @param timeStamp request time stamp 152 */ 153 public CourseRequest(long id, int priority, boolean alternative, Student student, java.util.List<Course> courses, boolean waitlist, Long timeStamp) { 154 this(id, priority, alternative, student, courses, waitlist, false, timeStamp); 155 } 156 157 /** 158 * List of requested courses (in the correct order -- first is the requested 159 * course, second is the first alternative, etc.) 160 * @return requested course offerings 161 */ 162 public List<Course> getCourses() { 163 return iCourses; 164 } 165 166 /** 167 * Create enrollment for the given list of sections. The list of sections 168 * needs to be correct, i.e., a section for each subpart of a configuration 169 * of one of the requested courses. 170 * @param sections selected sections 171 * @param reservation selected reservation 172 * @return enrollment 173 */ 174 public Enrollment createEnrollment(Set<? extends SctAssignment> sections, Reservation reservation) { 175 if (sections == null || sections.isEmpty()) 176 return null; 177 Config config = ((Section) sections.iterator().next()).getSubpart().getConfig(); 178 Course course = null; 179 for (Course c: iCourses) { 180 if (c.getOffering().getConfigs().contains(config)) { 181 course = c; 182 break; 183 } 184 } 185 return new Enrollment(this, iCourses.indexOf(course), course, config, sections, reservation); 186 } 187 188 /** 189 * Create enrollment for the given list of sections. The list of sections 190 * needs to be correct, i.e., a section for each subpart of a configuration 191 * of one of the requested courses. 192 * @param course selected course 193 * @param sections selected sections 194 * @param reservation selected reservation 195 * @return enrollment 196 */ 197 public Enrollment createEnrollment(Course course, Set<? extends SctAssignment> sections, Reservation reservation) { 198 if (sections == null || sections.isEmpty()) 199 return null; 200 Config config = ((Section) sections.iterator().next()).getSubpart().getConfig(); 201 return new Enrollment(this, iCourses.indexOf(course), course, config, sections, reservation); 202 } 203 204 /** 205 * Create enrollment for the given list of sections. The list of sections 206 * needs to be correct, i.e., a section for each subpart of a configuration 207 * of one of the requested courses. 208 * @param assignment current assignment (to guess the reservation) 209 * @param sections selected sections 210 * @return enrollment 211 */ 212 public Enrollment createEnrollment(Assignment<Request, Enrollment> assignment, Set<? extends SctAssignment> sections) { 213 Enrollment ret = createEnrollment(sections, null); 214 ret.guessReservation(assignment, true); 215 return ret; 216 217 } 218 219 /** 220 * Maximal domain size (i.e., number of enrollments of a course request), -1 if there is no limit. 221 * @return maximal domain size, -1 if unlimited 222 */ 223 protected int getMaxDomainSize() { 224 StudentSectioningModel model = (StudentSectioningModel) getModel(); 225 return model == null ? -1 : model.getMaxDomainSize(); 226 } 227 228 /** 229 * Return all possible enrollments. 230 */ 231 @Override 232 public List<Enrollment> computeEnrollments(Assignment<Request, Enrollment> assignment) { 233 List<Enrollment> ret = new ArrayList<Enrollment>(); 234 if (isFixed()) { 235 ret.add(getFixedValue()); 236 return ret; 237 } 238 if (getInitialAssignment() != null && getModel() != null && ((StudentSectioningModel)getModel()).isMPP() && ((StudentSectioningModel)getModel()).getKeepInitialAssignments()) { 239 ret.add(getInitialAssignment()); 240 return ret; 241 } 242 int idx = 0; 243 for (Course course : iCourses) { 244 for (Config config : course.getOffering().getConfigs()) { 245 computeEnrollments(assignment, ret, idx, 0, course, config, new HashSet<Section>(), 0, false, false, 246 false, false, getMaxDomainSize() <= 0 ? -1 : ret.size() + getMaxDomainSize()); 247 } 248 idx++; 249 } 250 return ret; 251 } 252 253 /** 254 * Return a subset of all enrollments -- randomly select only up to 255 * limitEachConfig enrollments of each config. 256 * @param assignment current assignment 257 * @param limitEachConfig maximal number of enrollments in each configuration 258 * @return computed enrollments 259 */ 260 public List<Enrollment> computeRandomEnrollments(Assignment<Request, Enrollment> assignment, int limitEachConfig) { 261 List<Enrollment> ret = new ArrayList<Enrollment>(); 262 if (isFixed()) { 263 ret.add(getFixedValue()); 264 return ret; 265 } 266 if (getInitialAssignment() != null && getModel() != null && ((StudentSectioningModel)getModel()).isMPP() && ((StudentSectioningModel)getModel()).getKeepInitialAssignments()) { 267 ret.add(getInitialAssignment()); 268 return ret; 269 } 270 int idx = 0; 271 for (Course course : iCourses) { 272 for (Config config : course.getOffering().getConfigs()) { 273 computeEnrollments(assignment, ret, idx, 0, course, config, new HashSet<Section>(), 0, false, false, 274 false, true, (limitEachConfig <= 0 ? limitEachConfig : ret.size() + limitEachConfig)); 275 } 276 idx++; 277 } 278 return ret; 279 } 280 281 /** 282 * Return true if the both sets of sections contain sections of the same 283 * subparts, and each pair of sections of the same subpart is offered at the 284 * same time. 285 */ 286 private boolean sameTimes(Set<Section> sections1, Set<Section> sections2) { 287 for (Section s1 : sections1) { 288 Section s2 = null; 289 for (Section s : sections2) { 290 if (s.getSubpart().equals(s1.getSubpart())) { 291 s2 = s; 292 break; 293 } 294 } 295 if (s2 == null) 296 return false; 297 if (!ToolBox.equals(s1.getTime(), s2.getTime())) 298 return false; 299 } 300 return true; 301 } 302 303 /** 304 * Recursive computation of enrollments 305 * 306 * @param enrollments 307 * list of enrollments to be returned 308 * @param priority 309 * zero for the course, one for the first alternative, two for the second alternative 310 * @param penalty 311 * penalty of the selected sections 312 * @param course 313 * selected course 314 * @param config 315 * selected configuration 316 * @param sections 317 * sections selected so far 318 * @param idx 319 * index of the subparts (a section of 0..idx-1 subparts has been 320 * already selected) 321 * @param availableOnly 322 * only use available sections 323 * @param skipSameTime 324 * for each possible times, pick only one section 325 * @param selectedOnly 326 * select only sections that are selected ( 327 * {@link CourseRequest#isSelected(Section)} is true) 328 * @param random 329 * pick sections in a random order (useful when limit is used) 330 * @param limit 331 * when above zero, limit the number of selected enrollments to 332 * this limit 333 * @param ignoreDisabled 334 * are sections that are disabled for student scheduling allowed to be used 335 * @param reservations 336 * list of applicable reservations 337 */ 338 private void computeEnrollments(Assignment<Request, Enrollment> assignment, Collection<Enrollment> enrollments, int priority, double penalty, Course course, Config config, 339 HashSet<Section> sections, int idx, boolean availableOnly, boolean skipSameTime, boolean selectedOnly, 340 boolean random, int limit) { 341 if (limit > 0 && enrollments.size() >= limit) 342 return; 343 if (idx == 0) { // run only once for each configuration 344 if (isNotAllowed(course, config)) return; 345 boolean canOverLimit = false; 346 if (availableOnly) { 347 for (Reservation r: getReservations(course)) { 348 if (!r.canBatchAssignOverLimit()) continue; 349 if (r.neverIncluded()) continue; 350 if (!r.getConfigs().isEmpty() && !r.getConfigs().contains(config)) continue; 351 if (r.getReservedAvailableSpace(assignment, config, this) < getWeight()) continue; 352 canOverLimit = true; break; 353 } 354 } 355 if (!canOverLimit) { 356 if (availableOnly && config.getLimit() >= 0 && ConfigLimit.getEnrollmentWeight(assignment, config, this) > config.getLimit()) 357 return; 358 if (availableOnly && course.getLimit() >= 0 && CourseLimit.getEnrollmentWeight(assignment, course, this) > course.getLimit()) 359 return; 360 if (config.getOffering().hasReservations()) { 361 boolean hasReservation = false, hasConfigReservation = false, reservationMustBeUsed = false; 362 for (Reservation r: getReservations(course)) { 363 if (r.mustBeUsed()) reservationMustBeUsed = true; 364 if (availableOnly && r.getReservedAvailableSpace(assignment, config, this) < getWeight()) continue; 365 if (r.neverIncluded()) { 366 } else if (r.getConfigs().isEmpty()) { 367 hasReservation = true; 368 } else if (r.getConfigs().contains(config)) { 369 hasReservation = true; 370 hasConfigReservation = true; 371 } else if (!r.areRestrictionsInclusive()) { 372 hasReservation = true; 373 } 374 } 375 if (!hasConfigReservation && config.getTotalUnreservedSpace() < getWeight()) 376 return; 377 if (!hasReservation && config.getOffering().getTotalUnreservedSpace() < getWeight()) 378 return; 379 if (availableOnly && !hasReservation && config.getOffering().getUnreservedSpace(assignment, this) < getWeight()) 380 return; 381 if (availableOnly && !hasConfigReservation && config.getUnreservedSpace(assignment, this) < getWeight()) 382 return; 383 if (!hasReservation && reservationMustBeUsed) 384 return; 385 } 386 } 387 } 388 if (config.getSubparts().size() == idx) { 389 if (skipSameTime && sSameTimePrecise) { 390 boolean waitListedOrSelected = false; 391 if (!getSelectedChoices().isEmpty() || !getWaitlistedChoices().isEmpty()) { 392 for (Section section : sections) { 393 if (isWaitlisted(section) || isSelected(section)) { 394 waitListedOrSelected = true; 395 break; 396 } 397 } 398 } 399 if (!waitListedOrSelected) { 400 for (Enrollment enrollment : enrollments) { 401 if (sameTimes(enrollment.getSections(), sections)) 402 return; 403 } 404 } 405 } 406 Enrollment e = new Enrollment(this, priority, course, config, new HashSet<SctAssignment>(sections), null); 407 if (isNotAllowed(e)) { 408 } else if (!config.getOffering().hasReservations()) { 409 enrollments.add(e); 410 } else { 411 boolean mustHaveReservation = config.getOffering().getTotalUnreservedSpace() < getWeight(); 412 boolean mustHaveConfigReservation = config.getTotalUnreservedSpace() < getWeight(); 413 boolean mustHaveSectionReservation = false; 414 boolean containDisabledSection = false; 415 for (Section s: sections) { 416 if (s.getTotalUnreservedSpace() < getWeight()) { 417 mustHaveSectionReservation = true; 418 } 419 if (!getStudent().isAllowDisabled() && !s.isEnabled(getStudent())) { 420 containDisabledSection = true; 421 } 422 } 423 boolean canOverLimit = false; 424 if (availableOnly) { 425 for (Reservation r: getReservations(course)) { 426 if (!r.canBatchAssignOverLimit() || !r.isIncluded(e)) continue; 427 if (r.getReservedAvailableSpace(assignment, config, this) < getWeight()) continue; 428 if (containDisabledSection && !r.isAllowDisabled()) continue; 429 enrollments.add(new Enrollment(this, priority, null, config, new HashSet<SctAssignment>(sections), r)); 430 canOverLimit = true; 431 } 432 } 433 if (!canOverLimit) { 434 boolean reservationMustBeUsed = false; 435 reservations: for (Reservation r: (availableOnly ? getSortedReservations(assignment, course) : getReservations(course))) { 436 if (r.mustBeUsed()) reservationMustBeUsed = true; 437 if (!r.isIncluded(e)) continue; 438 if (availableOnly && r.getReservedAvailableSpace(assignment, config, this) < getWeight()) continue; 439 if (mustHaveConfigReservation && r.getConfigs().isEmpty()) continue; 440 if (mustHaveSectionReservation) 441 for (Section s: sections) 442 if (r.getSections(s.getSubpart()) == null && s.getTotalUnreservedSpace() < getWeight()) continue reservations; 443 if (containDisabledSection && !r.isAllowDisabled()) continue; 444 enrollments.add(new Enrollment(this, priority, null, config, new HashSet<SctAssignment>(sections), r)); 445 if (availableOnly) return; // only one available reservation suffice (the best matching one) 446 } 447 // a case w/o reservation 448 if (!(mustHaveReservation || mustHaveConfigReservation || mustHaveSectionReservation) && 449 !(availableOnly && config.getOffering().getUnreservedSpace(assignment, this) < getWeight()) && 450 !reservationMustBeUsed && !containDisabledSection) { 451 enrollments.add(new Enrollment(this, priority, !getReservations(course).isEmpty(), null, config, new HashSet<SctAssignment>(sections), null)); 452 } 453 } 454 } 455 } else { 456 Subpart subpart = config.getSubparts().get(idx); 457 HashSet<TimeLocation> times = (skipSameTime ? new HashSet<TimeLocation>() : null); 458 List<Section> sectionsThisSubpart = subpart.getSections(); 459 if (skipSameTime) { 460 sectionsThisSubpart = new ArrayList<Section>(subpart.getSections()); 461 Collections.sort(sectionsThisSubpart, new AssignmentComparator<Section, Request, Enrollment>(assignment)); 462 } 463 List<Section> matchingSectionsThisSubpart = new ArrayList<Section>(subpart.getSections().size()); 464 boolean hasChildren = !subpart.getChildren().isEmpty(); 465 for (Section section : sectionsThisSubpart) { 466 if (section.isCancelled()) 467 continue; 468 if (!isRequired(section)) 469 continue; 470 if (getInitialAssignment() != null && (getModel() != null && ((StudentSectioningModel)getModel()).getKeepInitialAssignments()) && 471 !getInitialAssignment().getAssignments().contains(section)) 472 continue; 473 if (isFixed() && !getFixedValue().getAssignments().contains(section)) 474 continue; 475 if (section.getParent() != null && !sections.contains(section.getParent())) 476 continue; 477 if (section.isOverlapping(sections)) 478 continue; 479 if (selectedOnly && hasSelection(section) && !isSelected(section)) 480 continue; 481 if (isNotAllowed(course, section)) 482 continue; 483 if (!getStudent().isAvailable(section)) { 484 boolean canOverlap = false; 485 for (Reservation r: getReservations(course)) { 486 if (!r.isAllowOverlap()) continue; 487 if (r.getSections(subpart) != null && !r.getSections(subpart).contains(section)) continue; 488 if (r.getReservedAvailableSpace(assignment, config, this) < getWeight()) continue; 489 canOverlap = true; break; 490 } 491 if (!canOverlap) continue; 492 } 493 boolean canOverLimit = false; 494 if (availableOnly) { 495 for (Reservation r: getReservations(course)) { 496 if (!r.canBatchAssignOverLimit()) continue; 497 if (r.getSections(subpart) != null && !r.getSections(subpart).contains(section)) continue; 498 if (r.getReservedAvailableSpace(assignment, config, this) < getWeight()) continue; 499 canOverLimit = true; break; 500 } 501 } 502 if (!canOverLimit) { 503 if (availableOnly && section.getLimit() >= 0 504 && SectionLimit.getEnrollmentWeight(assignment, section, this) > section.getLimit()) 505 continue; 506 if (config.getOffering().hasReservations()) { 507 boolean hasReservation = false, hasSectionReservation = false, reservationMustBeUsed = false; 508 for (Reservation r: getReservations(course)) { 509 if (r.mustBeUsed()) reservationMustBeUsed = true; 510 if (availableOnly && r.getReservedAvailableSpace(assignment, config, this) < getWeight()) continue; 511 if (r.getSections(subpart) == null) { 512 hasReservation = true; 513 } else if (r.getSections(subpart).contains(section)) { 514 hasReservation = true; 515 hasSectionReservation = true; 516 } 517 } 518 if (!hasSectionReservation && section.getTotalUnreservedSpace() < getWeight()) 519 continue; 520 if (availableOnly && !hasSectionReservation && section.getUnreservedSpace(assignment, this) < getWeight()) 521 continue; 522 if (!hasReservation && reservationMustBeUsed) 523 continue; 524 } 525 } 526 if (!getStudent().isAllowDisabled() && !section.isEnabled(getStudent())) { 527 boolean allowDisabled = false; 528 for (Reservation r: getReservations(course)) { 529 if (!r.isAllowDisabled()) continue; 530 if (r.getSections(subpart) != null && !r.getSections(subpart).contains(section)) continue; 531 if (!r.getConfigs().isEmpty() && !r.getConfigs().contains(config)) continue; 532 allowDisabled = true; break; 533 } 534 if (!allowDisabled) continue; 535 } 536 if (skipSameTime && section.getTime() != null && !hasChildren && !times.add(section.getTime()) && !isSelected(section) && !isWaitlisted(section) && 537 (section.getIgnoreConflictWithSectionIds() == null || section.getIgnoreConflictWithSectionIds().isEmpty())) 538 continue; 539 matchingSectionsThisSubpart.add(section); 540 } 541 if (random || limit > 0) { 542 sectionsThisSubpart = new ArrayList<Section>(sectionsThisSubpart); 543 Collections.shuffle(sectionsThisSubpart); 544 } 545 int i = 0; 546 for (Section section: matchingSectionsThisSubpart) { 547 sections.add(section); 548 computeEnrollments(assignment, enrollments, priority, penalty + section.getPenalty(), course, config, sections, idx + 1, 549 availableOnly, skipSameTime, selectedOnly, random, 550 limit < 0 ? limit : Math.max(1, limit * (1 + i) / matchingSectionsThisSubpart.size())); 551 sections.remove(section); 552 i++; 553 } 554 } 555 } 556 557 /** Return all enrollments that are available 558 * @param assignment current assignment 559 * @return all available enrollments 560 **/ 561 public List<Enrollment> getAvaiableEnrollments(Assignment<Request, Enrollment> assignment) { 562 List<Enrollment> ret = new ArrayList<Enrollment>(); 563 if (isFixed()) { 564 ret.add(getFixedValue()); 565 return ret; 566 } 567 if (getInitialAssignment() != null && getModel() != null && ((StudentSectioningModel)getModel()).isMPP() && ((StudentSectioningModel)getModel()).getKeepInitialAssignments()) { 568 ret.add(getInitialAssignment()); 569 return ret; 570 } 571 int idx = 0; 572 for (Course course : iCourses) { 573 for (Config config : course.getOffering().getConfigs()) { 574 computeEnrollments(assignment, ret, idx, 0, course, config, new HashSet<Section>(), 0, true, false, false, false, 575 getMaxDomainSize() <= 0 ? -1 : ret.size() + getMaxDomainSize()); 576 } 577 idx++; 578 } 579 return ret; 580 } 581 582 /** 583 * Return all enrollments of the first course that are selected ( 584 * {@link CourseRequest#isSelected(Section)} is true) 585 * 586 * @param assignment current assignment 587 * @param availableOnly 588 * pick only available sections 589 * @return selected enrollments 590 */ 591 public List<Enrollment> getSelectedEnrollments(Assignment<Request, Enrollment> assignment, boolean availableOnly) { 592 if (getSelectedChoices().isEmpty()) 593 return null; 594 List<Enrollment> enrollments = new ArrayList<Enrollment>(); 595 if (isFixed()) return enrollments; 596 if (getInitialAssignment() != null && getModel() != null && ((StudentSectioningModel)getModel()).isMPP() && ((StudentSectioningModel)getModel()).getKeepInitialAssignments()) 597 return enrollments; 598 for (Course course : iCourses) { 599 boolean hasChoice = false; 600 for (Choice choice: getSelectedChoices()) 601 if (course.getOffering().equals(choice.getOffering())) { hasChoice = true; break; } 602 if (hasChoice) 603 for (Config config : course.getOffering().getConfigs()) { 604 computeEnrollments(assignment, enrollments, 0, 0, course, config, new HashSet<Section>(), 0, availableOnly, false, true, false, -1); 605 } 606 break; 607 } 608 return enrollments; 609 } 610 611 /** 612 * Return all enrollments that are available, pick only the first section of 613 * the sections with the same time (of each subpart, {@link Section} 614 * comparator is used) 615 * @param assignment current assignment 616 * @return available enrollments 617 */ 618 public List<Enrollment> getAvaiableEnrollmentsSkipSameTime(Assignment<Request, Enrollment> assignment) { 619 List<Enrollment> ret = new ArrayList<Enrollment>(); 620 if (isFixed()) { 621 ret.add(getFixedValue()); 622 return ret; 623 } 624 if (getInitialAssignment() != null) { 625 ret.add(getInitialAssignment()); 626 if (getModel() != null && ((StudentSectioningModel)getModel()).isMPP() && ((StudentSectioningModel)getModel()).getKeepInitialAssignments()) 627 return ret; 628 } 629 int idx = 0; 630 for (Course course : iCourses) { 631 boolean skipSameTime = true; 632 for (LinkedSections link: getStudent().getLinkedSections()) 633 if (link.getOfferings().contains(course.getOffering())) { skipSameTime = false; break; } 634 for (Config config : course.getOffering().getConfigs()) { 635 computeEnrollments(assignment, ret, idx, 0, course, config, new HashSet<Section>(), 0, true, skipSameTime, false, false, 636 getMaxDomainSize() <= 0 ? -1 : ret.size() + getMaxDomainSize()); 637 } 638 idx++; 639 } 640 return ret; 641 } 642 643 /** 644 * Return all possible enrollments, but pick only the first section of 645 * the sections with the same time (of each subpart, {@link Section} 646 * comparator is used). 647 * @param assignment current assignment 648 * @return computed enrollments 649 */ 650 public List<Enrollment> getEnrollmentsSkipSameTime(Assignment<Request, Enrollment> assignment) { 651 List<Enrollment> ret = new ArrayList<Enrollment>(); 652 if (isFixed()) { 653 ret.add(getFixedValue()); 654 return ret; 655 } 656 if (getInitialAssignment() != null) { 657 ret.add(getInitialAssignment()); 658 if (getModel() != null && ((StudentSectioningModel)getModel()).isMPP() && ((StudentSectioningModel)getModel()).getKeepInitialAssignments()) 659 return ret; 660 } 661 int idx = 0; 662 for (Course course : iCourses) { 663 for (Config config : course.getOffering().getConfigs()) { 664 boolean skipSameTime = true; 665 for (LinkedSections link: getStudent().getLinkedSections()) 666 if (link.getOfferings().contains(course.getOffering())) { skipSameTime = false; break; } 667 computeEnrollments(assignment, ret, idx, 0, course, config, new HashSet<Section>(), 0, false, skipSameTime, false, false, 668 getMaxDomainSize() <= 0 ? -1 : ret.size() + getMaxDomainSize()); 669 } 670 idx++; 671 } 672 return ret; 673 } 674 675 /** Wait-listed choices 676 * @return wait-listed choices 677 **/ 678 public Set<Choice> getWaitlistedChoices() { 679 return iWaitlistedChoices; 680 } 681 682 /** 683 * Return true when the given section is wait-listed (i.e., its choice is 684 * among wait-listed choices) 685 * @param section given section 686 * @return true if the given section matches the wait-listed choices 687 */ 688 public boolean isWaitlisted(Section section) { 689 for (Choice choice: iWaitlistedChoices) 690 if (choice.sameChoice(section)) return true; 691 return false; 692 } 693 694 /** Selected choices 695 * @return selected choices 696 **/ 697 public Set<Choice> getSelectedChoices() { 698 return iSelectedChoices; 699 } 700 701 /** 702 * Return true when the given section is selected (i.e., its choice is among 703 * selected choices) 704 * @param section given section 705 * @return true if the given section matches the selected choices 706 */ 707 public boolean isSelected(Section section) { 708 for (Choice choice: iSelectedChoices) 709 if (choice.sameSection(section) || choice.sameConfiguration(section)) return true; 710 return false; 711 } 712 713 /** 714 * Return true when the given section has a preference (i.e., there is a matching selection), 715 * or when there is a section preference for a different configuration 716 * @param section given section 717 * @return true if the there is a matching choice for the given section that is of the same offering 718 */ 719 public boolean hasSelection(Section section) { 720 boolean hasSectionChoices = false, hasSectionChoicesThisConfig = false; 721 for (Choice choice: iSelectedChoices) { 722 if (choice.sameOffering(section)) { 723 if (choice.isMatching(section)) return true; 724 if (choice.getSubpartId() != null) { 725 hasSectionChoices = true; 726 for (Subpart subpart: section.getSubpart().getConfig().getSubparts()) { 727 if (choice.getSubpartId().equals(subpart.getId())) { hasSectionChoicesThisConfig = true; } 728 } 729 } 730 } 731 } 732 return (hasSectionChoices && !hasSectionChoicesThisConfig); 733 } 734 735 /** 736 * Required choices 737 * @return required choices 738 */ 739 public Set<Choice> getRequiredChoices() { 740 return iRequiredChoices; 741 } 742 743 /** 744 * Return true when the given section is required (i.e., its choice is among required choices, or there are no requirements) 745 * @param section given section 746 * @return true if the given section matches the required choices 747 */ 748 public boolean isRequired(Section section) { 749 if (iRequiredChoices.isEmpty()) return true; 750 boolean hasConfig = false, hasMatchingConfig = false; 751 boolean hasSubpart = false, hasMatchingSection = false; 752 boolean hasSectionReq = false; 753 for (Choice choice: iRequiredChoices) { 754 // different offering -> skip 755 if (!choice.getOffering().equals(section.getSubpart().getConfig().getOffering())) continue; 756 // has config -> check config 757 if (choice.getConfigId() != null) { 758 hasConfig = true; 759 if (choice.sameConfiguration(section)) hasMatchingConfig = true; 760 } 761 // has section of the matching subpart -> check section 762 if (choice.getSubpartId() != null) { 763 hasSectionReq = true; 764 if (choice.getSubpartId().equals(section.getSubpart().getId())) { 765 hasSubpart = true; 766 if (choice.sameSection(section)) hasMatchingSection = true; 767 } else if (!hasMatchingConfig) { 768 for (Subpart subpart: section.getSubpart().getConfig().getSubparts()) { 769 if (choice.getSubpartId().equals(subpart.getId())) { 770 hasMatchingConfig = true; 771 break; 772 } 773 } 774 } 775 } 776 } 777 if (hasConfig && !hasMatchingConfig) return false; 778 if (hasSubpart && !hasMatchingSection) return false; 779 // no match, but there are section requirements for a different config -> not satisfied 780 if (!hasMatchingConfig && !hasMatchingSection && hasSectionReq) return false; 781 return true; 782 } 783 784 /** 785 * Request name: A for alternative, 1 + priority, (w) when wait-list, list of 786 * course names 787 */ 788 @Override 789 public String getName() { 790 String ret = (isAlternative() ? "A" : "") 791 + (1 + getPriority() + (isAlternative() ? -getStudent().nrRequests() : 0)) + ". " 792 + (getRequestPriority() != RequestPriority.Normal ? 793 (isWaitlist() ? "(" + getRequestPriority().getAbbreviation() + "w) " : "(" + getRequestPriority().getAbbreviation() + ") ") 794 : isWaitlist() ? "(w) " : ""); 795 int idx = 0; 796 for (Course course : iCourses) { 797 if (idx == 0) 798 ret += course.getName(); 799 else 800 ret += ", " + idx + ". alt " + course.getName(); 801 idx++; 802 } 803 return ret; 804 } 805 806 /** 807 * True if the student can be put on a wait-list (no alternative course 808 * request will be given instead) 809 * @return true if the request can be wait-listed 810 */ 811 public boolean isWaitlist() { 812 return iWaitlist; 813 } 814 815 /** 816 * True if the student can be put on a wait-list (no alternative course 817 * request will be given instead) 818 * @param waitlist true if the request can be wait-listed 819 */ 820 public void setWaitlist(boolean waitlist) { 821 iWaitlist = waitlist; 822 } 823 824 /** 825 * True if the course request is critical for the student in order to move forward in their degree 826 * @param critical true if the request is critical 827 */ 828 @Deprecated 829 public void setCritical(boolean critical) { 830 iPriority = (critical ? RequestPriority.Critical : RequestPriority.Normal); 831 } 832 833 /** 834 * Time stamp of the request 835 * @return request time stamp 836 */ 837 public Long getTimeStamp() { 838 return iTimeStamp; 839 } 840 841 @Override 842 public String toString() { 843 return getName() + (getWeight() != 1.0 ? " (W:" + sDF.format(getWeight()) + ")" : ""); 844 } 845 846 /** Return course of the requested courses with the given id 847 * @param courseId course offering id 848 * @return course of the given id 849 **/ 850 public Course getCourse(long courseId) { 851 for (Course course : iCourses) { 852 if (course.getId() == courseId) 853 return course; 854 } 855 return null; 856 } 857 858 /** Return configuration of the requested courses with the given id 859 * @param configId instructional offering configuration unique id 860 * @return config of the given id 861 **/ 862 public Config getConfig(long configId) { 863 for (Course course : iCourses) { 864 for (Config config : course.getOffering().getConfigs()) { 865 if (config.getId() == configId) 866 return config; 867 } 868 } 869 return null; 870 } 871 872 /** Return subpart of the requested courses with the given id 873 * @param subpartId scheduling subpart unique id 874 * @return subpart of the given id 875 **/ 876 public Subpart getSubpart(long subpartId) { 877 for (Course course : iCourses) { 878 for (Config config : course.getOffering().getConfigs()) { 879 for (Subpart subpart : config.getSubparts()) { 880 if (subpart.getId() == subpartId) 881 return subpart; 882 } 883 } 884 } 885 return null; 886 } 887 888 /** Return section of the requested courses with the given id 889 * @param sectionId class unique id 890 * @return section of the given id 891 **/ 892 public Section getSection(long sectionId) { 893 for (Course course : iCourses) { 894 for (Config config : course.getOffering().getConfigs()) { 895 for (Subpart subpart : config.getSubparts()) { 896 for (Section section : subpart.getSections()) { 897 if (section.getId() == sectionId) 898 return section; 899 } 900 } 901 } 902 } 903 return null; 904 } 905 906 /** 907 * Minimal penalty (minimum of {@link Offering#getMinPenalty()} among 908 * requested courses) 909 * @return minimal penalty 910 */ 911 public double getMinPenalty() { 912 if (iCachedMinPenalty == null) { 913 double min = Double.MAX_VALUE; 914 for (Course course : iCourses) { 915 min = Math.min(min, course.getOffering().getMinPenalty()); 916 } 917 iCachedMinPenalty = Double.valueOf(min); 918 } 919 return iCachedMinPenalty.doubleValue(); 920 } 921 922 /** 923 * Maximal penalty (maximum of {@link Offering#getMaxPenalty()} among 924 * requested courses) 925 * @return maximal penalty 926 */ 927 public double getMaxPenalty() { 928 if (iCachedMaxPenalty == null) { 929 double max = Double.MIN_VALUE; 930 for (Course course : iCourses) { 931 max = Math.max(max, course.getOffering().getMaxPenalty()); 932 } 933 iCachedMaxPenalty = Double.valueOf(max); 934 } 935 return iCachedMaxPenalty.doubleValue(); 936 } 937 938 /** Clear cached min/max penalties and cached bound */ 939 public void clearCache() { 940 iCachedMaxPenalty = null; 941 iCachedMinPenalty = null; 942 } 943 944 /** 945 * Estimated bound for this request -- it estimates the smallest value among 946 * all possible enrollments 947 */ 948 @Override 949 public double getBound() { 950 return - getWeight() * ((StudentSectioningModel)getModel()).getStudentWeights().getBound(this); 951 /* 952 if (iCachedBound == null) { 953 iCachedBound = Double.valueOf(-Math.pow(Enrollment.sPriorityWeight, getPriority()) 954 * (isAlternative() ? Enrollment.sAlterativeWeight : 1.0) 955 * Math.pow(Enrollment.sInitialWeight, (getInitialAssignment() == null ? 0 : 1)) 956 * Math.pow(Enrollment.sSelectedWeight, (iSelectedChoices.isEmpty() ? 0 : 1)) 957 * Math.pow(Enrollment.sWaitlistedWeight, (iWaitlistedChoices.isEmpty() ? 0 : 1)) 958 * 959 // Math.max(Enrollment.sMinWeight,getWeight()) * 960 (getStudent().isDummy() ? Student.sDummyStudentWeight : 1.0) 961 * Enrollment.normalizePenalty(getMinPenalty())); 962 } 963 return iCachedBound.doubleValue(); 964 */ 965 } 966 967 /** Return true if request is assigned. */ 968 @Override 969 public boolean isAssigned(Assignment<Request, Enrollment> assignment) { 970 Enrollment e = assignment.getValue(this); 971 return e != null && !e.getAssignments().isEmpty(); 972 } 973 974 @Override 975 public boolean equals(Object o) { 976 return super.equals(o) && (o instanceof CourseRequest); 977 } 978 979 /** 980 * Get reservations for this course requests 981 * @param course given course 982 * @return reservations for this course requests and the given course 983 */ 984 public synchronized List<Reservation> getReservations(Course course) { 985 if (iReservations == null) 986 iReservations = new HashMap<Course, List<Reservation>>(); 987 List<Reservation> reservations = iReservations.get(course); 988 if (reservations == null) { 989 reservations = new ArrayList<Reservation>(); 990 boolean mustBeUsed = false; 991 for (Reservation r: course.getOffering().getReservations()) { 992 if (!r.isApplicable(getStudent())) continue; 993 if (!mustBeUsed && r.mustBeUsed()) { reservations.clear(); mustBeUsed = true; } 994 if (mustBeUsed && !r.mustBeUsed()) continue; 995 reservations.add(r); 996 } 997 iReservations.put(course, reservations); 998 } 999 return reservations; 1000 } 1001 private Map<Course, List<Reservation>> iReservations = null; 1002 1003 /** 1004 * Get reservations for this course requests ordered using {@link Reservation#compareTo(Assignment, Reservation)} 1005 * @param course given course 1006 * @return reservations for this course requests and the given course 1007 */ 1008 public TreeSet<Reservation> getSortedReservations(Assignment<Request, Enrollment> assignment, Course course) { 1009 TreeSet<Reservation> reservations = new TreeSet<Reservation>(new AssignmentComparator<Reservation, Request, Enrollment>(assignment)); 1010 reservations.addAll(getReservations(course)); 1011 return reservations; 1012 } 1013 1014 /** 1015 * Return true if there is a reservation for a course of this request 1016 * @return true if there is a reservation for a course of this request 1017 */ 1018 public boolean hasReservations() { 1019 for (Course course: getCourses()) 1020 if (!getReservations(course).isEmpty()) 1021 return true; 1022 return false; 1023 } 1024 1025 /** 1026 * Clear reservation information that was cached on this section 1027 */ 1028 public synchronized void clearReservationCache() { 1029 if (iReservations != null) iReservations.clear(); 1030 } 1031 1032 /** 1033 * Get restrictions for this course requests 1034 * @param course given course 1035 * @return restrictions for this course requests and the given course 1036 */ 1037 public synchronized List<Restriction> getRestrictions(Course course) { 1038 if (iRestrictions == null) 1039 iRestrictions = new HashMap<Course, List<Restriction>>(); 1040 List<Restriction> restrictions = iRestrictions.get(course); 1041 if (restrictions == null) { 1042 restrictions = new ArrayList<Restriction>(); 1043 for (Restriction r: course.getOffering().getRestrictions()) { 1044 if (r.isApplicable(getStudent())) 1045 restrictions.add(r); 1046 } 1047 iRestrictions.put(course, restrictions); 1048 } 1049 return restrictions; 1050 } 1051 private Map<Course, List<Restriction>> iRestrictions = null; 1052 1053 /** 1054 * Return true if there is a restriction for a course of this request 1055 * @return true if there is a restriction for a course of this request 1056 */ 1057 public boolean hasRestrictions(Course course) { 1058 return !getRestrictions(course).isEmpty(); 1059 } 1060 1061 /** 1062 * Return true when there are restrictions for a course of this course request and the given config does not meet any of them 1063 */ 1064 public boolean isNotAllowed(Course course, Config config) { 1065 List<Restriction> restrictions = getRestrictions(course); 1066 if (restrictions.isEmpty()) return false; 1067 for (Restriction r: restrictions) 1068 if (r.isIncluded(config)) return false; 1069 return true; 1070 } 1071 1072 /** 1073 * Return true when there are restrictions for a course of this course request and the given section does not meet any of them 1074 */ 1075 public boolean isNotAllowed(Course course, Section section) { 1076 List<Restriction> restrictions = getRestrictions(course); 1077 if (restrictions.isEmpty()) return false; 1078 for (Restriction r: restrictions) 1079 if (r.isIncluded(section)) return false; 1080 return true; 1081 } 1082 1083 /** 1084 * Return true when there are restrictions for a course of this course request and the given enrollment does not meet any of them 1085 */ 1086 public boolean isNotAllowed(Enrollment e) { 1087 List<Restriction> restrictions = getRestrictions(e.getCourse()); 1088 if (restrictions.isEmpty()) return false; 1089 for (Restriction r: restrictions) 1090 if (r.isIncluded(e)) return false; 1091 return true; 1092 } 1093 1094 /** 1095 * Clear restriction information that was cached on this request 1096 */ 1097 public synchronized void clearRestrictionCache() { 1098 if (iRestrictions != null) iRestrictions.clear(); 1099 } 1100 1101 /** 1102 * Return true if this request can track MPP 1103 * @return true if the request is course request and it either has an initial enrollment 1104 */ 1105 @Override 1106 public boolean isMPP() { 1107 StudentSectioningModel model = (StudentSectioningModel) getModel(); 1108 if (model == null || !model.isMPP()) return false; 1109 return !getStudent().isDummy() && getInitialAssignment() != null; 1110 } 1111 1112 /** 1113 * Return true if this request has any selection 1114 * @return true if the request is course request and has some selected choices. 1115 */ 1116 @Override 1117 public boolean hasSelection() { 1118 if (getStudent().isDummy() || getSelectedChoices().isEmpty()) return false; 1119 for (Choice choice: getSelectedChoices()) 1120 if (choice.getSectionId() != null || choice.getConfigId() != null) return true; 1121 return false; 1122 } 1123 1124 /** 1125 * Add request group to this request. 1126 * @param group request group to be added 1127 */ 1128 public void addRequestGroup(RequestGroup group) { 1129 iRequestGroups.add(group); 1130 group.addRequest(this); 1131 } 1132 1133 /** 1134 * Removed request group from this request. 1135 * @param group request group to be removed 1136 */ 1137 public void removeRequestGroup(RequestGroup group) { 1138 iRequestGroups.remove(group); 1139 group.removeRequest(this); 1140 } 1141 1142 /** 1143 * Lists request groups of this request 1144 * @return request groups of this course requests 1145 */ 1146 public Set<RequestGroup> getRequestGroups() { 1147 return iRequestGroups; 1148 } 1149 1150 @Override 1151 public void variableAssigned(Assignment<Request, Enrollment> assignment, long iteration, Enrollment enrollment) { 1152 super.variableAssigned(assignment, iteration, enrollment); 1153 for (RequestGroup g: getRequestGroups()) 1154 if (g.getCourse().equals(enrollment.getCourse())) 1155 g.assigned(assignment, enrollment); 1156 } 1157 1158 @Override 1159 public void variableUnassigned(Assignment<Request, Enrollment> assignment, long iteration, Enrollment enrollment) { 1160 super.variableUnassigned(assignment, iteration, enrollment); 1161 for (RequestGroup g: getRequestGroups()) 1162 if (g.getCourse().equals(enrollment.getCourse())) 1163 g.unassigned(assignment, enrollment); 1164 } 1165 1166 @Override 1167 public float getMinCredit() { 1168 Float credit = null; 1169 for (Course course: getCourses()) { 1170 if (course.hasCreditValue() && (credit == null || credit > course.getCreditValue())) 1171 credit = course.getCreditValue(); 1172 for (Config config: course.getOffering().getConfigs()) { 1173 Float configCredit = config.getCreditValue(); 1174 if (configCredit != null && (credit == null || credit > configCredit)) 1175 credit = configCredit; 1176 } 1177 } 1178 return (credit == null ? 0 : credit.floatValue()); 1179 } 1180 1181 @Override 1182 public RequestPriority getRequestPriority() { 1183 return iPriority; 1184 } 1185 1186 public void setRequestPriority(RequestPriority priority) { 1187 iPriority = priority; 1188 } 1189 1190 public boolean isFixed() { return iFixed != null; } 1191 public Enrollment getFixedValue() { return iFixed; } 1192 public void setFixedValue(Enrollment constant) { iFixed = constant; } 1193}