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 if (!getStudent().isAvailable(section)) { 368 boolean canOverlap = false; 369 for (Reservation r: getReservations(course)) { 370 if (!r.isAllowOverlap()) continue; 371 if (r.getSections(subpart) != null && !r.getSections(subpart).contains(section)) continue; 372 if (r.getReservedAvailableSpace(assignment, this) < getWeight()) continue; 373 canOverlap = true; break; 374 } 375 if (!canOverlap) continue; 376 } 377 boolean canOverLimit = false; 378 if (availableOnly) { 379 for (Reservation r: getReservations(course)) { 380 if (!r.canBatchAssignOverLimit()) continue; 381 if (r.getSections(subpart) != null && !r.getSections(subpart).contains(section)) continue; 382 if (r.getReservedAvailableSpace(assignment, this) < getWeight()) continue; 383 canOverLimit = true; break; 384 } 385 } 386 if (!canOverLimit) { 387 if (availableOnly && section.getLimit() >= 0 388 && SectionLimit.getEnrollmentWeight(assignment, section, this) > section.getLimit()) 389 continue; 390 if (config.getOffering().hasReservations()) { 391 boolean hasReservation = false, hasSectionReservation = false, reservationMustBeUsed = false; 392 for (Reservation r: getReservations(course)) { 393 if (r.mustBeUsed()) reservationMustBeUsed = true; 394 if (availableOnly && r.getReservedAvailableSpace(assignment, this) < getWeight()) continue; 395 if (r.getSections(subpart) == null) { 396 hasReservation = true; 397 } else if (r.getSections(subpart).contains(section)) { 398 hasReservation = true; 399 hasSectionReservation = true; 400 } 401 } 402 if (!hasSectionReservation && section.getTotalUnreservedSpace() < getWeight()) 403 continue; 404 if (availableOnly && !hasSectionReservation && section.getUnreservedSpace(assignment, this) < getWeight()) 405 continue; 406 if (!hasReservation && reservationMustBeUsed) 407 continue; 408 } 409 } 410 if (skipSameTime && section.getTime() != null && !hasChildren && !times.add(section.getTime()) && !isSelected(section) && !isWaitlisted(section) && 411 (section.getIgnoreConflictWithSectionIds() == null || section.getIgnoreConflictWithSectionIds().isEmpty())) 412 continue; 413 matchingSectionsThisSubpart.add(section); 414 } 415 if (random || limit > 0) { 416 sectionsThisSubpart = new ArrayList<Section>(sectionsThisSubpart); 417 Collections.shuffle(sectionsThisSubpart); 418 } 419 int i = 0; 420 for (Section section: matchingSectionsThisSubpart) { 421 sections.add(section); 422 computeEnrollments(assignment, enrollments, priority, penalty + section.getPenalty(), course, config, sections, idx + 1, 423 availableOnly, skipSameTime, selectedOnly, random, 424 limit < 0 ? limit : Math.max(1, limit * (1 + i) / matchingSectionsThisSubpart.size()) 425 ); 426 sections.remove(section); 427 i++; 428 } 429 } 430 } 431 432 /** Return all enrollments that are available 433 * @param assignment current assignment 434 * @return all available enrollments 435 **/ 436 public List<Enrollment> getAvaiableEnrollments(Assignment<Request, Enrollment> assignment) { 437 List<Enrollment> ret = new ArrayList<Enrollment>(); 438 int idx = 0; 439 for (Course course : iCourses) { 440 for (Config config : course.getOffering().getConfigs()) { 441 computeEnrollments(assignment, ret, idx, 0, course, config, new HashSet<Section>(), 0, true, false, false, false, 442 getMaxDomainSize() <= 0 ? -1 : ret.size() + getMaxDomainSize()); 443 } 444 idx++; 445 } 446 return ret; 447 } 448 449 /** 450 * Return all enrollments that are selected ( 451 * {@link CourseRequest#isSelected(Section)} is true) 452 * 453 * @param assignment current assignment 454 * @param availableOnly 455 * pick only available sections 456 * @return selected enrollments 457 */ 458 public List<Enrollment> getSelectedEnrollments(Assignment<Request, Enrollment> assignment, boolean availableOnly) { 459 if (getSelectedChoices().isEmpty()) 460 return null; 461 Choice firstChoice = getSelectedChoices().iterator().next(); 462 List<Enrollment> enrollments = new ArrayList<Enrollment>(); 463 for (Course course : iCourses) { 464 if (!course.getOffering().equals(firstChoice.getOffering())) 465 continue; 466 for (Config config : course.getOffering().getConfigs()) { 467 computeEnrollments(assignment, enrollments, 0, 0, course, config, new HashSet<Section>(), 0, availableOnly, false, true, false, -1); 468 } 469 } 470 return enrollments; 471 } 472 473 /** 474 * Return all enrollments that are available, pick only the first section of 475 * the sections with the same time (of each subpart, {@link Section} 476 * comparator is used) 477 * @param assignment current assignment 478 * @return available enrollments 479 */ 480 public List<Enrollment> getAvaiableEnrollmentsSkipSameTime(Assignment<Request, Enrollment> assignment) { 481 List<Enrollment> ret = new ArrayList<Enrollment>(); 482 if (getInitialAssignment() != null) 483 ret.add(getInitialAssignment()); 484 int idx = 0; 485 for (Course course : iCourses) { 486 boolean skipSameTime = true; 487 for (LinkedSections link: getStudent().getLinkedSections()) 488 if (link.getOfferings().contains(course.getOffering())) { skipSameTime = false; break; } 489 for (Config config : course.getOffering().getConfigs()) { 490 computeEnrollments(assignment, ret, idx, 0, course, config, new HashSet<Section>(), 0, true, skipSameTime, false, false, 491 getMaxDomainSize() <= 0 ? -1 : ret.size() + getMaxDomainSize()); 492 } 493 idx++; 494 } 495 return ret; 496 } 497 498 /** 499 * Return all possible enrollments, but pick only the first section of 500 * the sections with the same time (of each subpart, {@link Section} 501 * comparator is used). 502 * @param assignment current assignment 503 * @return computed enrollments 504 */ 505 public List<Enrollment> getEnrollmentsSkipSameTime(Assignment<Request, Enrollment> assignment) { 506 List<Enrollment> ret = new ArrayList<Enrollment>(); 507 int idx = 0; 508 for (Course course : iCourses) { 509 for (Config config : course.getOffering().getConfigs()) { 510 boolean skipSameTime = true; 511 for (LinkedSections link: getStudent().getLinkedSections()) 512 if (link.getOfferings().contains(course.getOffering())) { skipSameTime = false; break; } 513 computeEnrollments(assignment, ret, idx, 0, course, config, new HashSet<Section>(), 0, false, skipSameTime, false, false, 514 getMaxDomainSize() <= 0 ? -1 : ret.size() + getMaxDomainSize()); 515 } 516 idx++; 517 } 518 return ret; 519 } 520 521 /** Wait-listed choices 522 * @return wait-listed choices 523 **/ 524 public Set<Choice> getWaitlistedChoices() { 525 return iWaitlistedChoices; 526 } 527 528 /** 529 * Return true when the given section is wait-listed (i.e., its choice is 530 * among wait-listed choices) 531 * @param section given section 532 * @return true if the given section matches the wait-listed choices 533 */ 534 public boolean isWaitlisted(Section section) { 535 for (Choice choice: iWaitlistedChoices) 536 if (choice.sameChoice(section)) return true; 537 return false; 538 } 539 540 /** Selected choices 541 * @return selected choices 542 **/ 543 public Set<Choice> getSelectedChoices() { 544 return iSelectedChoices; 545 } 546 547 /** 548 * Return true when the given section is selected (i.e., its choice is among 549 * selected choices) 550 * @param section given section 551 * @return true if the given section matches the selected choices 552 */ 553 public boolean isSelected(Section section) { 554 boolean hasMatch = false; 555 for (Choice choice: iSelectedChoices) { 556 if (choice.sameChoice(section) || choice.sameConfiguration(section)) return true; 557 if (choice.isMatching(section)) hasMatch = true; 558 } 559 return !iSelectedChoices.isEmpty() && !hasMatch; 560 } 561 562 /** 563 * Request name: A for alternative, 1 + priority, (w) when wait-list, list of 564 * course names 565 */ 566 @Override 567 public String getName() { 568 String ret = (isAlternative() ? "A" : "") 569 + (1 + getPriority() + (isAlternative() ? -getStudent().nrRequests() : 0)) + ". " 570 + (isWaitlist() ? "(w) " : ""); 571 int idx = 0; 572 for (Course course : iCourses) { 573 if (idx == 0) 574 ret += course.getName(); 575 else 576 ret += ", " + idx + ". alt " + course.getName(); 577 idx++; 578 } 579 return ret; 580 } 581 582 /** 583 * True if the student can be put on a wait-list (no alternative course 584 * request will be given instead) 585 * @return true if the request can be wait-listed 586 */ 587 public boolean isWaitlist() { 588 return iWaitlist; 589 } 590 591 /** 592 * True if the student can be put on a wait-list (no alternative course 593 * request will be given instead) 594 * @param waitlist true if the request can be wait-listed 595 */ 596 public void setWaitlist(boolean waitlist) { 597 iWaitlist = waitlist; 598 } 599 600 /** 601 * Time stamp of the request 602 * @return request time stamp 603 */ 604 public Long getTimeStamp() { 605 return iTimeStamp; 606 } 607 608 @Override 609 public String toString() { 610 return getName() + (getWeight() != 1.0 ? " (W:" + sDF.format(getWeight()) + ")" : ""); 611 } 612 613 /** Return course of the requested courses with the given id 614 * @param courseId course offering id 615 * @return course of the given id 616 **/ 617 public Course getCourse(long courseId) { 618 for (Course course : iCourses) { 619 if (course.getId() == courseId) 620 return course; 621 } 622 return null; 623 } 624 625 /** Return configuration of the requested courses with the given id 626 * @param configId instructional offering configuration unique id 627 * @return config of the given id 628 **/ 629 public Config getConfig(long configId) { 630 for (Course course : iCourses) { 631 for (Config config : course.getOffering().getConfigs()) { 632 if (config.getId() == configId) 633 return config; 634 } 635 } 636 return null; 637 } 638 639 /** Return subpart of the requested courses with the given id 640 * @param subpartId scheduling subpart unique id 641 * @return subpart of the given id 642 **/ 643 public Subpart getSubpart(long subpartId) { 644 for (Course course : iCourses) { 645 for (Config config : course.getOffering().getConfigs()) { 646 for (Subpart subpart : config.getSubparts()) { 647 if (subpart.getId() == subpartId) 648 return subpart; 649 } 650 } 651 } 652 return null; 653 } 654 655 /** Return section of the requested courses with the given id 656 * @param sectionId class unique id 657 * @return section of the given id 658 **/ 659 public Section getSection(long sectionId) { 660 for (Course course : iCourses) { 661 for (Config config : course.getOffering().getConfigs()) { 662 for (Subpart subpart : config.getSubparts()) { 663 for (Section section : subpart.getSections()) { 664 if (section.getId() == sectionId) 665 return section; 666 } 667 } 668 } 669 } 670 return null; 671 } 672 673 /** 674 * Minimal penalty (minimum of {@link Offering#getMinPenalty()} among 675 * requested courses) 676 * @return minimal penalty 677 */ 678 public double getMinPenalty() { 679 if (iCachedMinPenalty == null) { 680 double min = Double.MAX_VALUE; 681 for (Course course : iCourses) { 682 min = Math.min(min, course.getOffering().getMinPenalty()); 683 } 684 iCachedMinPenalty = new Double(min); 685 } 686 return iCachedMinPenalty.doubleValue(); 687 } 688 689 /** 690 * Maximal penalty (maximum of {@link Offering#getMaxPenalty()} among 691 * requested courses) 692 * @return maximal penalty 693 */ 694 public double getMaxPenalty() { 695 if (iCachedMaxPenalty == null) { 696 double max = Double.MIN_VALUE; 697 for (Course course : iCourses) { 698 max = Math.max(max, course.getOffering().getMaxPenalty()); 699 } 700 iCachedMaxPenalty = new Double(max); 701 } 702 return iCachedMaxPenalty.doubleValue(); 703 } 704 705 /** Clear cached min/max penalties and cached bound */ 706 public void clearCache() { 707 iCachedMaxPenalty = null; 708 iCachedMinPenalty = null; 709 } 710 711 /** 712 * Estimated bound for this request -- it estimates the smallest value among 713 * all possible enrollments 714 */ 715 @Override 716 public double getBound() { 717 return - getWeight() * ((StudentSectioningModel)getModel()).getStudentWeights().getBound(this); 718 /* 719 if (iCachedBound == null) { 720 iCachedBound = new Double(-Math.pow(Enrollment.sPriorityWeight, getPriority()) 721 * (isAlternative() ? Enrollment.sAlterativeWeight : 1.0) 722 * Math.pow(Enrollment.sInitialWeight, (getInitialAssignment() == null ? 0 : 1)) 723 * Math.pow(Enrollment.sSelectedWeight, (iSelectedChoices.isEmpty() ? 0 : 1)) 724 * Math.pow(Enrollment.sWaitlistedWeight, (iWaitlistedChoices.isEmpty() ? 0 : 1)) 725 * 726 // Math.max(Enrollment.sMinWeight,getWeight()) * 727 (getStudent().isDummy() ? Student.sDummyStudentWeight : 1.0) 728 * Enrollment.normalizePenalty(getMinPenalty())); 729 } 730 return iCachedBound.doubleValue(); 731 */ 732 } 733 734 /** Return true if request is assigned. */ 735 @Override 736 public boolean isAssigned(Assignment<Request, Enrollment> assignment) { 737 Enrollment e = assignment.getValue(this); 738 return e != null && !e.getAssignments().isEmpty(); 739 } 740 741 @Override 742 public boolean equals(Object o) { 743 return super.equals(o) && (o instanceof CourseRequest); 744 } 745 746 /** 747 * Get reservations for this course requests 748 * @param course given course 749 * @return reservations for this course requests and the given course 750 */ 751 public synchronized List<Reservation> getReservations(Course course) { 752 if (iReservations == null) 753 iReservations = new HashMap<Course, List<Reservation>>(); 754 List<Reservation> reservations = iReservations.get(course); 755 if (reservations == null) { 756 reservations = new ArrayList<Reservation>(); 757 boolean mustBeUsed = false; 758 for (Reservation r: course.getOffering().getReservations()) { 759 if (!r.isApplicable(getStudent())) continue; 760 if (!mustBeUsed && r.mustBeUsed()) { reservations.clear(); mustBeUsed = true; } 761 if (mustBeUsed && !r.mustBeUsed()) continue; 762 reservations.add(r); 763 } 764 iReservations.put(course, reservations); 765 } 766 return reservations; 767 } 768 private Map<Course, List<Reservation>> iReservations = null; 769 770 /** 771 * Get reservations for this course requests ordered using {@link Reservation#compareTo(Assignment, Reservation)} 772 * @param course given course 773 * @return reservations for this course requests and the given course 774 */ 775 public TreeSet<Reservation> getSortedReservations(Assignment<Request, Enrollment> assignment, Course course) { 776 TreeSet<Reservation> reservations = new TreeSet<Reservation>(new AssignmentComparator<Reservation, Request, Enrollment>(assignment)); 777 reservations.addAll(getReservations(course)); 778 return reservations; 779 } 780 781 /** 782 * Return true if there is a reservation for a course of this request 783 * @return true if there is a reservation for a course of this request 784 */ 785 public boolean hasReservations() { 786 for (Course course: getCourses()) 787 if (!getReservations(course).isEmpty()) 788 return true; 789 return false; 790 } 791 792 /** 793 * Clear reservation information that was cached on this section 794 */ 795 public synchronized void clearReservationCache() { 796 if (iReservations != null) iReservations.clear(); 797 } 798 799 /** 800 * Return true if this request can track MPP 801 * @return true if the request is course request and it either has an initial enrollment or some selected choices. 802 */ 803 @Override 804 public boolean isMPP() { 805 StudentSectioningModel model = (StudentSectioningModel) getModel(); 806 if (model == null || !model.isMPP()) return false; 807 return !getStudent().isDummy() && (getInitialAssignment() != null || !getSelectedChoices().isEmpty()); 808 } 809 810 /** 811 * Return true if this request has any selection 812 * @return true if the request is course request and has some selected choices. 813 */ 814 @Override 815 public boolean hasSelection() { 816 if (getStudent().isDummy() || getSelectedChoices().isEmpty()) return false; 817 for (Choice choice: getSelectedChoices()) 818 if (choice.getSectionId() != null || choice.getConfigId() != null) return true; 819 return false; 820 } 821 822 /** 823 * Add request group to this request. 824 * @param group request group to be added 825 */ 826 public void addRequestGroup(RequestGroup group) { 827 iRequestGroups.add(group); 828 group.addRequest(this); 829 } 830 831 /** 832 * Removed request group from this request. 833 * @param group request group to be removed 834 */ 835 public void removeRequestGroup(RequestGroup group) { 836 iRequestGroups.remove(group); 837 group.removeRequest(this); 838 } 839 840 /** 841 * Lists request groups of this request 842 * @return request groups of this course requests 843 */ 844 public Set<RequestGroup> getRequestGroups() { 845 return iRequestGroups; 846 } 847 848 @Override 849 public void variableAssigned(Assignment<Request, Enrollment> assignment, long iteration, Enrollment enrollment) { 850 super.variableAssigned(assignment, iteration, enrollment); 851 for (RequestGroup g: getRequestGroups()) 852 if (g.getCourse().equals(enrollment.getCourse())) 853 g.assigned(assignment, enrollment); 854 } 855 856 @Override 857 public void variableUnassigned(Assignment<Request, Enrollment> assignment, long iteration, Enrollment enrollment) { 858 super.variableUnassigned(assignment, iteration, enrollment); 859 for (RequestGroup g: getRequestGroups()) 860 if (g.getCourse().equals(enrollment.getCourse())) 861 g.unassigned(assignment, enrollment); 862 } 863}