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