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; 024 025 026/** 027 * Representation of a request of a student for one or more course. A student 028 * requests one of the given courses, preferably the first one. <br> 029 * <br> 030 * 031 * @version StudentSct 1.3 (Student Sectioning)<br> 032 * Copyright (C) 2007 - 2014 Tomas Muller<br> 033 * <a href="mailto:muller@unitime.org">muller@unitime.org</a><br> 034 * <a href="http://muller.unitime.org">http://muller.unitime.org</a><br> 035 * <br> 036 * This library is free software; you can redistribute it and/or modify 037 * it under the terms of the GNU Lesser General Public License as 038 * published by the Free Software Foundation; either version 3 of the 039 * License, or (at your option) any later version. <br> 040 * <br> 041 * This library is distributed in the hope that it will be useful, but 042 * WITHOUT ANY WARRANTY; without even the implied warranty of 043 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 044 * Lesser General Public License for more details. <br> 045 * <br> 046 * You should have received a copy of the GNU Lesser General Public 047 * License along with this library; if not see 048 * <a href='http://www.gnu.org/licenses/'>http://www.gnu.org/licenses/</a>. 049 */ 050public class CourseRequest extends Request { 051 private static DecimalFormat sDF = new DecimalFormat("0.000"); 052 private List<Course> iCourses = null; 053 private Set<Choice> iWaitlistedChoices = new HashSet<Choice>(); 054 private Set<Choice> iSelectedChoices = new HashSet<Choice>(); 055 private boolean iWaitlist = false; 056 private Long iTimeStamp = null; 057 private Double iCachedMinPenalty = null, iCachedMaxPenalty = null; 058 public static boolean sSameTimePrecise = false; 059 private Set<RequestGroup> iRequestGroups = new HashSet<RequestGroup>(); 060 061 /** 062 * Constructor 063 * 064 * @param id 065 * request unique id 066 * @param priority 067 * request priority 068 * @param alternative 069 * true if the request is alternative (alternative request can be 070 * assigned instead of a non-alternative course requests, if it 071 * is left unassigned) 072 * @param student 073 * appropriate student 074 * @param courses 075 * list of requested courses (in the correct order -- first is 076 * the requested course, second is the first alternative, etc.) 077 * @param waitlist 078 * time stamp of the request if the student can be put on a wait-list (no alternative 079 * course request will be given instead) 080 * @param timeStamp request time stamp 081 */ 082 public CourseRequest(long id, int priority, boolean alternative, Student student, java.util.List<Course> courses, 083 boolean waitlist, Long timeStamp) { 084 super(id, priority, alternative, student); 085 iCourses = new ArrayList<Course>(courses); 086 for (Course course: iCourses) 087 course.getRequests().add(this); 088 iWaitlist = waitlist; 089 iTimeStamp = timeStamp; 090 } 091 092 /** 093 * List of requested courses (in the correct order -- first is the requested 094 * course, second is the first alternative, etc.) 095 * @return requested course offerings 096 */ 097 public List<Course> getCourses() { 098 return iCourses; 099 } 100 101 /** 102 * Create enrollment for the given list of sections. The list of sections 103 * needs to be correct, i.e., a section for each subpart of a configuration 104 * of one of the requested courses. 105 * @param sections selected sections 106 * @param reservation selected reservation 107 * @return enrollment 108 */ 109 public Enrollment createEnrollment(Set<? extends SctAssignment> sections, Reservation reservation) { 110 if (sections == null || sections.isEmpty()) 111 return null; 112 Config config = ((Section) sections.iterator().next()).getSubpart().getConfig(); 113 Course course = null; 114 for (Course c: iCourses) { 115 if (c.getOffering().getConfigs().contains(config)) { 116 course = c; 117 break; 118 } 119 } 120 return new Enrollment(this, iCourses.indexOf(course), course, config, sections, reservation); 121 } 122 123 /** 124 * Create enrollment for the given list of sections. The list of sections 125 * needs to be correct, i.e., a section for each subpart of a configuration 126 * of one of the requested courses. 127 * @param assignment current assignment (to guess the reservation) 128 * @param sections selected sections 129 * @return enrollment 130 */ 131 public Enrollment createEnrollment(Assignment<Request, Enrollment> assignment, Set<? extends SctAssignment> sections) { 132 Enrollment ret = createEnrollment(sections, null); 133 ret.guessReservation(assignment, true); 134 return ret; 135 136 } 137 138 /** 139 * Maximal domain size (i.e., number of enrollments of a course request), -1 if there is no limit. 140 * @return maximal domain size, -1 if unlimited 141 */ 142 protected int getMaxDomainSize() { 143 StudentSectioningModel model = (StudentSectioningModel) getModel(); 144 return model == null ? -1 : model.getMaxDomainSize(); 145 } 146 147 /** 148 * Return all possible enrollments. 149 */ 150 @Override 151 public List<Enrollment> computeEnrollments(Assignment<Request, Enrollment> assignment) { 152 List<Enrollment> ret = new ArrayList<Enrollment>(); 153 int idx = 0; 154 for (Course course : iCourses) { 155 for (Config config : course.getOffering().getConfigs()) { 156 computeEnrollments(assignment, ret, idx, 0, course, config, new HashSet<Section>(), 0, false, false, 157 false, false, getMaxDomainSize() <= 0 ? -1 : ret.size() + getMaxDomainSize()); 158 } 159 idx++; 160 } 161 return ret; 162 } 163 164 /** 165 * Return a subset of all enrollments -- randomly select only up to 166 * limitEachConfig enrollments of each config. 167 * @param assignment current assignment 168 * @param limitEachConfig maximal number of enrollments in each configuration 169 * @return computed enrollments 170 */ 171 public List<Enrollment> computeRandomEnrollments(Assignment<Request, Enrollment> assignment, int limitEachConfig) { 172 List<Enrollment> ret = new ArrayList<Enrollment>(); 173 int idx = 0; 174 for (Course course : iCourses) { 175 for (Config config : course.getOffering().getConfigs()) { 176 computeEnrollments(assignment, ret, idx, 0, course, config, new HashSet<Section>(), 0, false, false, 177 false, true, (limitEachConfig <= 0 ? limitEachConfig : ret.size() + limitEachConfig)); 178 } 179 idx++; 180 } 181 return ret; 182 } 183 184 /** 185 * Return true if the both sets of sections contain sections of the same 186 * subparts, and each pair of sections of the same subpart is offered at the 187 * same time. 188 */ 189 private boolean sameTimes(Set<Section> sections1, Set<Section> sections2) { 190 for (Section s1 : sections1) { 191 Section s2 = null; 192 for (Section s : sections2) { 193 if (s.getSubpart().equals(s1.getSubpart())) { 194 s2 = s; 195 break; 196 } 197 } 198 if (s2 == null) 199 return false; 200 if (!ToolBox.equals(s1.getTime(), s2.getTime())) 201 return false; 202 } 203 return true; 204 } 205 206 /** 207 * Recursive computation of enrollments 208 * 209 * @param enrollments 210 * list of enrollments to be returned 211 * @param priority 212 * zero for the course, one for the first alternative, two for the second alternative 213 * @param penalty 214 * penalty of the selected sections 215 * @param course 216 * selected course 217 * @param config 218 * selected configuration 219 * @param sections 220 * sections selected so far 221 * @param idx 222 * index of the subparts (a section of 0..idx-1 subparts has been 223 * already selected) 224 * @param availableOnly 225 * only use available sections 226 * @param skipSameTime 227 * for each possible times, pick only one section 228 * @param selectedOnly 229 * select only sections that are selected ( 230 * {@link CourseRequest#isSelected(Section)} is true) 231 * @param random 232 * pick sections in a random order (useful when limit is used) 233 * @param limit 234 * when above zero, limit the number of selected enrollments to 235 * this limit 236 * @param reservations 237 * list of applicable reservations 238 */ 239 private void computeEnrollments(Assignment<Request, Enrollment> assignment, Collection<Enrollment> enrollments, int priority, double penalty, Course course, Config config, 240 HashSet<Section> sections, int idx, boolean availableOnly, boolean skipSameTime, boolean selectedOnly, 241 boolean random, int limit) { 242 if (limit > 0 && enrollments.size() >= limit) 243 return; 244 if (idx == 0) { // run only once for each configuration 245 boolean canOverLimit = false; 246 if (availableOnly) { 247 for (Reservation r: getReservations(course)) { 248 if (!r.canBatchAssignOverLimit()) continue; 249 if (!r.getConfigs().isEmpty() && !r.getConfigs().contains(config)) continue; 250 if (r.getReservedAvailableSpace(assignment, this) < getWeight()) continue; 251 canOverLimit = true; break; 252 } 253 } 254 if (!canOverLimit) { 255 if (availableOnly && config.getLimit() >= 0 && ConfigLimit.getEnrollmentWeight(assignment, config, this) > config.getLimit()) 256 return; 257 if (availableOnly && course.getLimit() >= 0 && CourseLimit.getEnrollmentWeight(assignment, course, this) > course.getLimit()) 258 return; 259 if (config.getOffering().hasReservations()) { 260 boolean hasReservation = false, hasConfigReservation = false, reservationMustBeUsed = false; 261 for (Reservation r: getReservations(course)) { 262 if (r.mustBeUsed()) reservationMustBeUsed = true; 263 if (availableOnly && r.getReservedAvailableSpace(assignment, this) < getWeight()) continue; 264 if (r.getConfigs().isEmpty()) { 265 hasReservation = true; 266 } else if (r.getConfigs().contains(config)) { 267 hasReservation = true; 268 hasConfigReservation = true; 269 } 270 } 271 if (!hasConfigReservation && config.getTotalUnreservedSpace() < getWeight()) 272 return; 273 if (!hasReservation && config.getOffering().getTotalUnreservedSpace() < getWeight()) 274 return; 275 if (availableOnly && !hasReservation && config.getOffering().getUnreservedSpace(assignment, this) < getWeight()) 276 return; 277 if (availableOnly && !hasConfigReservation && config.getUnreservedSpace(assignment, this) < getWeight()) 278 return; 279 if (!hasReservation && reservationMustBeUsed) 280 return; 281 } 282 } 283 } 284 if (config.getSubparts().size() == idx) { 285 if (skipSameTime && sSameTimePrecise) { 286 boolean waitListedOrSelected = false; 287 if (!getSelectedChoices().isEmpty() || !getWaitlistedChoices().isEmpty()) { 288 for (Section section : sections) { 289 if (isWaitlisted(section) || isSelected(section)) { 290 waitListedOrSelected = true; 291 break; 292 } 293 } 294 } 295 if (!waitListedOrSelected) { 296 for (Enrollment enrollment : enrollments) { 297 if (sameTimes(enrollment.getSections(), sections)) 298 return; 299 } 300 } 301 } 302 if (!config.getOffering().hasReservations()) { 303 enrollments.add(new Enrollment(this, priority, null, config, new HashSet<SctAssignment>(sections), null)); 304 } else { 305 Enrollment e = new Enrollment(this, priority, null, config, new HashSet<SctAssignment>(sections), null); 306 boolean mustHaveReservation = config.getOffering().getTotalUnreservedSpace() < getWeight(); 307 boolean mustHaveConfigReservation = config.getTotalUnreservedSpace() < getWeight(); 308 boolean mustHaveSectionReservation = false; 309 for (Section s: sections) { 310 if (s.getTotalUnreservedSpace() < getWeight()) { 311 mustHaveSectionReservation = true; 312 break; 313 } 314 } 315 boolean canOverLimit = false; 316 if (availableOnly) { 317 for (Reservation r: getReservations(course)) { 318 if (!r.canBatchAssignOverLimit() || !r.isIncluded(e)) continue; 319 if (r.getReservedAvailableSpace(assignment, this) < getWeight()) continue; 320 enrollments.add(new Enrollment(this, priority, null, config, new HashSet<SctAssignment>(sections), r)); 321 canOverLimit = true; 322 } 323 } 324 if (!canOverLimit) { 325 boolean reservationMustBeUsed = false; 326 reservations: for (Reservation r: (availableOnly ? getSortedReservations(assignment, course) : getReservations(course))) { 327 if (r.mustBeUsed()) reservationMustBeUsed = true; 328 if (!r.isIncluded(e)) continue; 329 if (availableOnly && r.getReservedAvailableSpace(assignment, this) < getWeight()) continue; 330 if (mustHaveConfigReservation && r.getConfigs().isEmpty()) continue; 331 if (mustHaveSectionReservation) 332 for (Section s: sections) 333 if (r.getSections(s.getSubpart()) == null && s.getTotalUnreservedSpace() < getWeight()) continue reservations; 334 enrollments.add(new Enrollment(this, priority, null, config, new HashSet<SctAssignment>(sections), r)); 335 if (availableOnly) return; // only one available reservation suffice (the best matching one) 336 } 337 // a case w/o reservation 338 if (!(mustHaveReservation || mustHaveConfigReservation || mustHaveSectionReservation) && 339 !(availableOnly && config.getOffering().getUnreservedSpace(assignment, this) < getWeight()) && 340 !reservationMustBeUsed) { 341 enrollments.add(new Enrollment(this, (getReservations(course).isEmpty() ? 0 : 1) + priority, null, config, new HashSet<SctAssignment>(sections), null)); 342 } 343 } 344 } 345 } else { 346 Subpart subpart = config.getSubparts().get(idx); 347 HashSet<TimeLocation> times = (skipSameTime ? new HashSet<TimeLocation>() : null); 348 List<Section> sectionsThisSubpart = subpart.getSections(); 349 if (skipSameTime) { 350 sectionsThisSubpart = new ArrayList<Section>(subpart.getSections()); 351 Collections.sort(sectionsThisSubpart, new AssignmentComparator<Section, Request, Enrollment>(assignment)); 352 } 353 List<Section> matchingSectionsThisSubpart = new ArrayList<Section>(subpart.getSections().size()); 354 boolean hasChildren = !subpart.getChildren().isEmpty(); 355 for (Section section : sectionsThisSubpart) { 356 if (section.isCancelled()) 357 continue; 358 if (getInitialAssignment() != null && (getModel() != null && ((StudentSectioningModel)getModel()).getKeepInitialAssignments()) && 359 !getInitialAssignment().getAssignments().contains(section)) 360 continue; 361 if (section.getParent() != null && !sections.contains(section.getParent())) 362 continue; 363 if (section.isOverlapping(sections)) 364 continue; 365 if (selectedOnly && !isSelected(section)) 366 continue; 367 boolean canOverLimit = false; 368 if (availableOnly) { 369 for (Reservation r: getReservations(course)) { 370 if (!r.canBatchAssignOverLimit()) continue; 371 if (r.getSections(subpart) != null && !r.getSections(subpart).contains(section)) continue; 372 if (r.getReservedAvailableSpace(assignment, this) < getWeight()) continue; 373 canOverLimit = true; break; 374 } 375 } 376 if (!canOverLimit) { 377 if (availableOnly && section.getLimit() >= 0 378 && SectionLimit.getEnrollmentWeight(assignment, section, this) > section.getLimit()) 379 continue; 380 if (config.getOffering().hasReservations()) { 381 boolean hasReservation = false, hasSectionReservation = false, reservationMustBeUsed = false; 382 for (Reservation r: getReservations(course)) { 383 if (r.mustBeUsed()) reservationMustBeUsed = true; 384 if (availableOnly && r.getReservedAvailableSpace(assignment, this) < getWeight()) continue; 385 if (r.getSections(subpart) == null) { 386 hasReservation = true; 387 } else if (r.getSections(subpart).contains(section)) { 388 hasReservation = true; 389 hasSectionReservation = true; 390 } 391 } 392 if (!hasSectionReservation && section.getTotalUnreservedSpace() < getWeight()) 393 continue; 394 if (availableOnly && !hasSectionReservation && section.getUnreservedSpace(assignment, this) < getWeight()) 395 continue; 396 if (!hasReservation && reservationMustBeUsed) 397 continue; 398 } 399 } 400 if (skipSameTime && section.getTime() != null && !hasChildren && !times.add(section.getTime()) && !isSelected(section) && !isWaitlisted(section) && 401 (section.getIgnoreConflictWithSectionIds() == null || section.getIgnoreConflictWithSectionIds().isEmpty())) 402 continue; 403 matchingSectionsThisSubpart.add(section); 404 } 405 if (random || limit > 0) { 406 sectionsThisSubpart = new ArrayList<Section>(sectionsThisSubpart); 407 Collections.shuffle(sectionsThisSubpart); 408 } 409 int i = 0; 410 for (Section section: matchingSectionsThisSubpart) { 411 sections.add(section); 412 computeEnrollments(assignment, enrollments, priority, penalty + section.getPenalty(), course, config, sections, idx + 1, 413 availableOnly, skipSameTime, selectedOnly, random, 414 limit < 0 ? limit : Math.max(1, limit * (1 + i) / matchingSectionsThisSubpart.size()) 415 ); 416 sections.remove(section); 417 i++; 418 } 419 } 420 } 421 422 /** Return all enrollments that are available 423 * @param assignment current assignment 424 * @return all available enrollments 425 **/ 426 public List<Enrollment> getAvaiableEnrollments(Assignment<Request, Enrollment> assignment) { 427 List<Enrollment> ret = new ArrayList<Enrollment>(); 428 int idx = 0; 429 for (Course course : iCourses) { 430 for (Config config : course.getOffering().getConfigs()) { 431 computeEnrollments(assignment, ret, idx, 0, course, config, new HashSet<Section>(), 0, true, false, false, false, 432 getMaxDomainSize() <= 0 ? -1 : ret.size() + getMaxDomainSize()); 433 } 434 idx++; 435 } 436 return ret; 437 } 438 439 /** 440 * Return all enrollments that are selected ( 441 * {@link CourseRequest#isSelected(Section)} is true) 442 * 443 * @param assignment current assignment 444 * @param availableOnly 445 * pick only available sections 446 * @return selected enrollments 447 */ 448 public List<Enrollment> getSelectedEnrollments(Assignment<Request, Enrollment> assignment, boolean availableOnly) { 449 if (getSelectedChoices().isEmpty()) 450 return null; 451 Choice firstChoice = getSelectedChoices().iterator().next(); 452 List<Enrollment> enrollments = new ArrayList<Enrollment>(); 453 for (Course course : iCourses) { 454 if (!course.getOffering().equals(firstChoice.getOffering())) 455 continue; 456 for (Config config : course.getOffering().getConfigs()) { 457 computeEnrollments(assignment, enrollments, 0, 0, course, config, new HashSet<Section>(), 0, availableOnly, false, true, false, -1); 458 } 459 } 460 return enrollments; 461 } 462 463 /** 464 * Return all enrollments that are available, pick only the first section of 465 * the sections with the same time (of each subpart, {@link Section} 466 * comparator is used) 467 * @param assignment current assignment 468 * @return available enrollments 469 */ 470 public List<Enrollment> getAvaiableEnrollmentsSkipSameTime(Assignment<Request, Enrollment> assignment) { 471 List<Enrollment> ret = new ArrayList<Enrollment>(); 472 if (getInitialAssignment() != null) 473 ret.add(getInitialAssignment()); 474 int idx = 0; 475 for (Course course : iCourses) { 476 boolean skipSameTime = true; 477 for (LinkedSections link: getStudent().getLinkedSections()) 478 if (link.getOfferings().contains(course.getOffering())) { skipSameTime = false; break; } 479 for (Config config : course.getOffering().getConfigs()) { 480 computeEnrollments(assignment, ret, idx, 0, course, config, new HashSet<Section>(), 0, true, skipSameTime, false, false, 481 getMaxDomainSize() <= 0 ? -1 : ret.size() + getMaxDomainSize()); 482 } 483 idx++; 484 } 485 return ret; 486 } 487 488 /** 489 * Return all possible enrollments, but pick only the first section of 490 * the sections with the same time (of each subpart, {@link Section} 491 * comparator is used). 492 * @param assignment current assignment 493 * @return computed enrollments 494 */ 495 public List<Enrollment> getEnrollmentsSkipSameTime(Assignment<Request, Enrollment> assignment) { 496 List<Enrollment> ret = new ArrayList<Enrollment>(); 497 int idx = 0; 498 for (Course course : iCourses) { 499 for (Config config : course.getOffering().getConfigs()) { 500 boolean skipSameTime = true; 501 for (LinkedSections link: getStudent().getLinkedSections()) 502 if (link.getOfferings().contains(course.getOffering())) { skipSameTime = false; break; } 503 computeEnrollments(assignment, ret, idx, 0, course, config, new HashSet<Section>(), 0, false, skipSameTime, false, false, 504 getMaxDomainSize() <= 0 ? -1 : ret.size() + getMaxDomainSize()); 505 } 506 idx++; 507 } 508 return ret; 509 } 510 511 /** Wait-listed choices 512 * @return wait-listed choices 513 **/ 514 public Set<Choice> getWaitlistedChoices() { 515 return iWaitlistedChoices; 516 } 517 518 /** 519 * Return true when the given section is wait-listed (i.e., its choice is 520 * among wait-listed choices) 521 * @param section given section 522 * @return true if the given section matches the wait-listed choices 523 */ 524 public boolean isWaitlisted(Section section) { 525 for (Choice choice: iWaitlistedChoices) 526 if (choice.sameChoice(section)) return true; 527 return false; 528 } 529 530 /** Selected choices 531 * @return selected choices 532 **/ 533 public Set<Choice> getSelectedChoices() { 534 return iSelectedChoices; 535 } 536 537 /** 538 * Return true when the given section is selected (i.e., its choice is among 539 * selected choices) 540 * @param section given section 541 * @return true if the given section matches the selected choices 542 */ 543 public boolean isSelected(Section section) { 544 boolean hasMatch = false; 545 for (Choice choice: iSelectedChoices) { 546 if (choice.sameChoice(section) || choice.sameConfiguration(section)) return true; 547 if (choice.isMatching(section)) hasMatch = true; 548 } 549 return !iSelectedChoices.isEmpty() && !hasMatch; 550 } 551 552 /** 553 * Request name: A for alternative, 1 + priority, (w) when wait-list, list of 554 * course names 555 */ 556 @Override 557 public String getName() { 558 String ret = (isAlternative() ? "A" : "") 559 + (1 + getPriority() + (isAlternative() ? -getStudent().nrRequests() : 0)) + ". " 560 + (isWaitlist() ? "(w) " : ""); 561 int idx = 0; 562 for (Course course : iCourses) { 563 if (idx == 0) 564 ret += course.getName(); 565 else 566 ret += ", " + idx + ". alt " + course.getName(); 567 idx++; 568 } 569 return ret; 570 } 571 572 /** 573 * True if the student can be put on a wait-list (no alternative course 574 * request will be given instead) 575 * @return true if the request can be wait-listed 576 */ 577 public boolean isWaitlist() { 578 return iWaitlist; 579 } 580 581 /** 582 * True if the student can be put on a wait-list (no alternative course 583 * request will be given instead) 584 * @param waitlist true if the request can be wait-listed 585 */ 586 public void setWaitlist(boolean waitlist) { 587 iWaitlist = waitlist; 588 } 589 590 /** 591 * Time stamp of the request 592 * @return request time stamp 593 */ 594 public Long getTimeStamp() { 595 return iTimeStamp; 596 } 597 598 @Override 599 public String toString() { 600 return getName() + (getWeight() != 1.0 ? " (W:" + sDF.format(getWeight()) + ")" : ""); 601 } 602 603 /** Return course of the requested courses with the given id 604 * @param courseId course offering id 605 * @return course of the given id 606 **/ 607 public Course getCourse(long courseId) { 608 for (Course course : iCourses) { 609 if (course.getId() == courseId) 610 return course; 611 } 612 return null; 613 } 614 615 /** Return configuration of the requested courses with the given id 616 * @param configId instructional offering configuration unique id 617 * @return config of the given id 618 **/ 619 public Config getConfig(long configId) { 620 for (Course course : iCourses) { 621 for (Config config : course.getOffering().getConfigs()) { 622 if (config.getId() == configId) 623 return config; 624 } 625 } 626 return null; 627 } 628 629 /** Return subpart of the requested courses with the given id 630 * @param subpartId scheduling subpart unique id 631 * @return subpart of the given id 632 **/ 633 public Subpart getSubpart(long subpartId) { 634 for (Course course : iCourses) { 635 for (Config config : course.getOffering().getConfigs()) { 636 for (Subpart subpart : config.getSubparts()) { 637 if (subpart.getId() == subpartId) 638 return subpart; 639 } 640 } 641 } 642 return null; 643 } 644 645 /** Return section of the requested courses with the given id 646 * @param sectionId class unique id 647 * @return section of the given id 648 **/ 649 public Section getSection(long sectionId) { 650 for (Course course : iCourses) { 651 for (Config config : course.getOffering().getConfigs()) { 652 for (Subpart subpart : config.getSubparts()) { 653 for (Section section : subpart.getSections()) { 654 if (section.getId() == sectionId) 655 return section; 656 } 657 } 658 } 659 } 660 return null; 661 } 662 663 /** 664 * Minimal penalty (minimum of {@link Offering#getMinPenalty()} among 665 * requested courses) 666 * @return minimal penalty 667 */ 668 public double getMinPenalty() { 669 if (iCachedMinPenalty == null) { 670 double min = Double.MAX_VALUE; 671 for (Course course : iCourses) { 672 min = Math.min(min, course.getOffering().getMinPenalty()); 673 } 674 iCachedMinPenalty = new Double(min); 675 } 676 return iCachedMinPenalty.doubleValue(); 677 } 678 679 /** 680 * Maximal penalty (maximum of {@link Offering#getMaxPenalty()} among 681 * requested courses) 682 * @return maximal penalty 683 */ 684 public double getMaxPenalty() { 685 if (iCachedMaxPenalty == null) { 686 double max = Double.MIN_VALUE; 687 for (Course course : iCourses) { 688 max = Math.max(max, course.getOffering().getMaxPenalty()); 689 } 690 iCachedMaxPenalty = new Double(max); 691 } 692 return iCachedMaxPenalty.doubleValue(); 693 } 694 695 /** Clear cached min/max penalties and cached bound */ 696 public void clearCache() { 697 iCachedMaxPenalty = null; 698 iCachedMinPenalty = null; 699 } 700 701 /** 702 * Estimated bound for this request -- it estimates the smallest value among 703 * all possible enrollments 704 */ 705 @Override 706 public double getBound() { 707 return - getWeight() * ((StudentSectioningModel)getModel()).getStudentWeights().getBound(this); 708 /* 709 if (iCachedBound == null) { 710 iCachedBound = new Double(-Math.pow(Enrollment.sPriorityWeight, getPriority()) 711 * (isAlternative() ? Enrollment.sAlterativeWeight : 1.0) 712 * Math.pow(Enrollment.sInitialWeight, (getInitialAssignment() == null ? 0 : 1)) 713 * Math.pow(Enrollment.sSelectedWeight, (iSelectedChoices.isEmpty() ? 0 : 1)) 714 * Math.pow(Enrollment.sWaitlistedWeight, (iWaitlistedChoices.isEmpty() ? 0 : 1)) 715 * 716 // Math.max(Enrollment.sMinWeight,getWeight()) * 717 (getStudent().isDummy() ? Student.sDummyStudentWeight : 1.0) 718 * Enrollment.normalizePenalty(getMinPenalty())); 719 } 720 return iCachedBound.doubleValue(); 721 */ 722 } 723 724 /** Return true if request is assigned. */ 725 @Override 726 public boolean isAssigned(Assignment<Request, Enrollment> assignment) { 727 Enrollment e = assignment.getValue(this); 728 return e != null && !e.getAssignments().isEmpty(); 729 } 730 731 @Override 732 public boolean equals(Object o) { 733 return super.equals(o) && (o instanceof CourseRequest); 734 } 735 736 /** 737 * Get reservations for this course requests 738 * @param course given course 739 * @return reservations for this course requests and the given course 740 */ 741 public synchronized List<Reservation> getReservations(Course course) { 742 if (iReservations == null) 743 iReservations = new HashMap<Course, List<Reservation>>(); 744 List<Reservation> reservations = iReservations.get(course); 745 if (reservations == null) { 746 reservations = new ArrayList<Reservation>(); 747 boolean mustBeUsed = false; 748 for (Reservation r: course.getOffering().getReservations()) { 749 if (!r.isApplicable(getStudent())) continue; 750 if (!mustBeUsed && r.mustBeUsed()) { reservations.clear(); mustBeUsed = true; } 751 if (mustBeUsed && !r.mustBeUsed()) continue; 752 reservations.add(r); 753 } 754 iReservations.put(course, reservations); 755 } 756 return reservations; 757 } 758 private Map<Course, List<Reservation>> iReservations = null; 759 760 /** 761 * Get reservations for this course requests ordered using {@link Reservation#compareTo(Assignment, Reservation)} 762 * @param course given course 763 * @return reservations for this course requests and the given course 764 */ 765 public TreeSet<Reservation> getSortedReservations(Assignment<Request, Enrollment> assignment, Course course) { 766 TreeSet<Reservation> reservations = new TreeSet<Reservation>(new AssignmentComparator<Reservation, Request, Enrollment>(assignment)); 767 reservations.addAll(getReservations(course)); 768 return reservations; 769 } 770 771 /** 772 * Return true if there is a reservation for a course of this request 773 * @return true if there is a reservation for a course of this request 774 */ 775 public boolean hasReservations() { 776 for (Course course: getCourses()) 777 if (!getReservations(course).isEmpty()) 778 return true; 779 return false; 780 } 781 782 /** 783 * Clear reservation information that was cached on this section 784 */ 785 public synchronized void clearReservationCache() { 786 if (iReservations != null) iReservations.clear(); 787 } 788 789 /** 790 * Return true if this request can track MPP 791 * @return true if the request is course request and it either has an initial enrollment or some selected choices. 792 */ 793 @Override 794 public boolean isMPP() { 795 StudentSectioningModel model = (StudentSectioningModel) getModel(); 796 if (model == null || !model.isMPP()) return false; 797 return !getStudent().isDummy() && (getInitialAssignment() != null || !getSelectedChoices().isEmpty()); 798 } 799 800 /** 801 * Return true if this request has any selection 802 * @return true if the request is course request and has some selected choices. 803 */ 804 @Override 805 public boolean hasSelection() { 806 if (getStudent().isDummy() || getSelectedChoices().isEmpty()) return false; 807 for (Choice choice: getSelectedChoices()) 808 if (choice.getSectionId() != null || choice.getConfigId() != null) return true; 809 return false; 810 } 811 812 /** 813 * Add request group to this request. 814 * @param group request group to be added 815 */ 816 public void addRequestGroup(RequestGroup group) { 817 iRequestGroups.add(group); 818 group.addRequest(this); 819 } 820 821 /** 822 * Removed request group from this request. 823 * @param group request group to be removed 824 */ 825 public void removeRequestGroup(RequestGroup group) { 826 iRequestGroups.remove(group); 827 group.removeRequest(this); 828 } 829 830 /** 831 * Lists request groups of this request 832 * @return request groups of this course requests 833 */ 834 public Set<RequestGroup> getRequestGroups() { 835 return iRequestGroups; 836 } 837 838 @Override 839 public void variableAssigned(Assignment<Request, Enrollment> assignment, long iteration, Enrollment enrollment) { 840 super.variableAssigned(assignment, iteration, enrollment); 841 for (RequestGroup g: getRequestGroups()) 842 if (g.getCourse().equals(enrollment.getCourse())) 843 g.assigned(assignment, enrollment); 844 } 845 846 @Override 847 public void variableUnassigned(Assignment<Request, Enrollment> assignment, long iteration, Enrollment enrollment) { 848 super.variableUnassigned(assignment, iteration, enrollment); 849 for (RequestGroup g: getRequestGroups()) 850 if (g.getCourse().equals(enrollment.getCourse())) 851 g.unassigned(assignment, enrollment); 852 } 853}