use of org.cpsolver.studentsct.model.FreeTimeRequest in project cpsolver by UniTime.
the class SuggestionsBranchAndBound method backtrack.
/**
* Main branch and bound rutine
* @param requests2resolve remaining requests to assign
* @param altRequests2resolve alternative requests to assign
* @param idx current depth
* @param depth remaining depth
* @param alt can leave a request unassigned
*/
protected void backtrack(ArrayList<Request> requests2resolve, TreeSet<Request> altRequests2resolve, int idx, int depth, boolean alt) {
if (!iTimeoutReached && iTimeout > 0 && System.currentTimeMillis() - iT0 > iTimeout)
iTimeoutReached = true;
int nrUnassigned = requests2resolve.size() - idx;
if (nrUnassigned == 0) {
List<FreeTimeRequest> okFreeTimes = new ArrayList<FreeTimeRequest>();
double sectionsWithPenalty = 0;
for (Request r : iStudent.getRequests()) {
Enrollment e = iAssignment.getValue(r);
if (iMaxSectionsWithPenalty >= 0 && e != null && r instanceof CourseRequest) {
for (Section s : e.getSections()) sectionsWithPenalty += iModel.getOverExpected(iAssignment, s, r);
}
if (e == null && r instanceof FreeTimeRequest) {
FreeTimeRequest ft = (FreeTimeRequest) r;
Enrollment enrollment = ft.createEnrollment();
if (iModel.conflictValues(iAssignment, enrollment).isEmpty()) {
iAssignment.assign(0, enrollment);
okFreeTimes.add(ft);
}
}
}
if (iMaxSectionsWithPenalty >= 0 && sectionsWithPenalty > iMaxSectionsWithPenalty)
return;
Suggestion s = new Suggestion(requests2resolve);
if (iSuggestions.size() >= iMaxSuggestions && iSuggestions.last().compareTo(s) <= 0)
return;
if (iMatched != 1) {
for (Iterator<Suggestion> i = iSuggestions.iterator(); i.hasNext(); ) {
Suggestion x = i.next();
if (x.sameSelectedSection()) {
if (x.compareTo(s) <= 0)
return;
i.remove();
}
}
}
s.init();
iSuggestions.add(s);
if (iSuggestions.size() > iMaxSuggestions)
iSuggestions.remove(iSuggestions.last());
for (FreeTimeRequest ft : okFreeTimes) iAssignment.unassign(0, ft);
return;
}
if (!canContinue(requests2resolve, idx, depth))
return;
Request request = requests2resolve.get(idx);
for (Enrollment enrollment : values(request)) {
if (!canContinueEvaluation())
break;
if (!isAllowed(enrollment))
continue;
if (enrollment.equals(iAssignment.getValue(request)))
continue;
if (enrollment.getAssignments().isEmpty() && alt)
continue;
Set<Enrollment> conflicts = iModel.conflictValues(iAssignment, enrollment);
if (!checkBound(requests2resolve, idx, depth, enrollment, conflicts))
continue;
Enrollment current = iAssignment.getValue(request);
ArrayList<Request> newVariables2resolve = new ArrayList<Request>(requests2resolve);
for (Iterator<Enrollment> i = conflicts.iterator(); i.hasNext(); ) {
Enrollment conflict = i.next();
iAssignment.unassign(0, conflict.variable());
if (!newVariables2resolve.contains(conflict.variable()))
newVariables2resolve.add(conflict.variable());
}
if (current != null)
iAssignment.unassign(0, current.variable());
iAssignment.assign(0, enrollment);
if (enrollment.getAssignments().isEmpty()) {
if (altRequests2resolve != null && !altRequests2resolve.isEmpty()) {
Suggestion lastBefore = (iSuggestions.isEmpty() ? null : iSuggestions.last());
int sizeBefore = iSuggestions.size();
for (Request r : altRequests2resolve) {
newVariables2resolve.add(r);
backtrack(newVariables2resolve, null, idx + 1, depth, true);
newVariables2resolve.remove(r);
}
Suggestion lastAfter = (iSuggestions.isEmpty() ? null : iSuggestions.last());
int sizeAfter = iSuggestions.size();
// did not succeeded with an alternative -> try without it
if (sizeBefore == sizeAfter && (sizeAfter < iMaxSuggestions || sizeAfter == 0 || lastAfter.compareTo(lastBefore) == 0))
backtrack(newVariables2resolve, altRequests2resolve, idx + 1, depth - 1, alt);
} else {
backtrack(newVariables2resolve, altRequests2resolve, idx + 1, depth - 1, alt);
}
} else {
backtrack(newVariables2resolve, altRequests2resolve, idx + 1, depth - 1, alt);
}
if (current == null)
iAssignment.unassign(0, request);
else
iAssignment.assign(0, current);
for (Iterator<Enrollment> i = conflicts.iterator(); i.hasNext(); ) {
Enrollment conflict = i.next();
iAssignment.assign(0, conflict);
}
}
}
use of org.cpsolver.studentsct.model.FreeTimeRequest in project cpsolver by UniTime.
the class EqualWeightCriterion method canImprove.
@Override
public boolean canImprove(Assignment<Request, Enrollment> assignment, int maxIdx, Enrollment[] current, Enrollment[] best) {
// 0. best number of assigned course requests (including alternativity &
// priority)
int currentAssignedCourseReq = 0, bestAssignedCourseReq = 0;
int currentAssignedRequests = 0, bestAssignedRequests = 0;
int currentAssignedPriority = 0, bestAssignedPriority = 0;
int currentAssignedAlternativity = 0, bestAssignedAlternativity = 0;
int alt = 0;
for (int idx = 0; idx < current.length; idx++) {
if (idx < maxIdx) {
if (current[idx] != null && current[idx].getAssignments() != null) {
currentAssignedRequests++;
if (current[idx].isCourseRequest())
currentAssignedCourseReq++;
currentAssignedPriority += current[idx].getPriority() * current[idx].getPriority();
currentAssignedAlternativity += (current[idx].getRequest().isAlternative() ? 1 : 0);
} else if (!isFreeTime(idx) && !getRequest(idx).isAlternative()) {
alt++;
}
} else {
if (!getRequest(idx).isAlternative()) {
currentAssignedRequests++;
if (!isFreeTime(idx))
currentAssignedCourseReq++;
} else if (alt > 0) {
currentAssignedRequests++;
currentAssignedCourseReq++;
alt--;
currentAssignedAlternativity++;
}
}
if (best[idx] != null && best[idx].getAssignments() != null) {
bestAssignedRequests++;
if (best[idx].isCourseRequest())
bestAssignedCourseReq++;
bestAssignedPriority += best[idx].getPriority() * best[idx].getPriority();
bestAssignedAlternativity += (best[idx].getRequest().isAlternative() ? 1 : 0);
}
}
if (currentAssignedCourseReq > bestAssignedCourseReq)
return true;
if (bestAssignedCourseReq > currentAssignedCourseReq)
return false;
if (currentAssignedPriority < bestAssignedPriority)
return true;
if (bestAssignedPriority < currentAssignedPriority)
return false;
if (currentAssignedAlternativity < bestAssignedAlternativity)
return true;
if (bestAssignedAlternativity < currentAssignedAlternativity)
return false;
// 0.5. avoid course time overlaps & unavailabilities
if (getModel().getTimeOverlaps() != null) {
int bestTimeOverlaps = 0, currentTimeOverlaps = 0;
for (int idx = 0; idx < current.length; idx++) {
if (best[idx] != null && best[idx].getRequest() instanceof CourseRequest) {
for (int x = 0; x < idx; x++) {
if (best[x] != null && best[x].getRequest() instanceof CourseRequest)
bestTimeOverlaps += getModel().getTimeOverlaps().nrConflicts(best[x], best[idx]);
}
}
if (current[idx] != null && idx < maxIdx && current[idx].getRequest() instanceof CourseRequest) {
for (int x = 0; x < idx; x++) {
if (current[x] != null && current[x].getRequest() instanceof CourseRequest)
currentTimeOverlaps += getModel().getTimeOverlaps().nrConflicts(current[x], current[idx]);
}
}
}
for (int idx = 0; idx < current.length; idx++) {
if (best[idx] != null && best[idx].getAssignments() != null && best[idx].isCourseRequest()) {
bestTimeOverlaps += getModel().getTimeOverlaps().nrNotAvailableTimeConflicts(best[idx]);
}
if (current[idx] != null && idx < maxIdx && current[idx].getAssignments() != null && current[idx].isCourseRequest()) {
currentTimeOverlaps += getModel().getTimeOverlaps().nrNotAvailableTimeConflicts(current[idx]);
}
}
if (currentTimeOverlaps < bestTimeOverlaps)
return true;
if (bestTimeOverlaps < currentTimeOverlaps)
return false;
}
// 1. maximize number of penalties
double bestPenalties = 0, currentPenalties = 0;
for (int idx = 0; idx < current.length; idx++) {
if (best[idx] != null) {
for (Section section : best[idx].getSections()) bestPenalties += getModel().getOverExpected(assignment, section, best[idx].getRequest());
}
if (current[idx] != null && idx < maxIdx) {
for (Section section : current[idx].getSections()) currentPenalties += getModel().getOverExpected(assignment, section, current[idx].getRequest());
}
}
if (currentPenalties < bestPenalties)
return true;
if (bestPenalties < currentPenalties)
return false;
// 2. best number of assigned requests (including free time requests)
if (currentAssignedRequests > bestAssignedRequests)
return true;
if (bestAssignedRequests > currentAssignedRequests)
return false;
// 3. maximize selection
int bestSelected = 0, currentSelected = 0;
for (int idx = 0; idx < current.length; idx++) {
if (best[idx] != null && best[idx].isCourseRequest()) {
Set<Section> preferred = getPreferredSections(best[idx].getRequest());
if (preferred != null && !preferred.isEmpty()) {
for (Section section : best[idx].getSections()) if (preferred.contains(section)) {
if (idx < maxIdx)
bestSelected++;
} else if (idx >= maxIdx)
bestSelected--;
}
}
if (current[idx] != null && idx < maxIdx && current[idx].isCourseRequest()) {
Set<Section> preferred = getPreferredSections(current[idx].getRequest());
if (preferred != null && !preferred.isEmpty()) {
for (Section section : current[idx].getSections()) if (preferred.contains(section))
currentSelected++;
}
}
}
if (currentSelected > bestSelected)
return true;
if (bestSelected > currentSelected)
return false;
// 3.5 maximize preferences
double bestSelectedConfigs = 0, currentSelectedConfigs = 0;
double bestSelectedSections = 0, currentSelectedSections = 0;
for (int idx = 0; idx < current.length; idx++) {
if (best[idx] != null && best[idx].getAssignments() != null && best[idx].isCourseRequest()) {
bestSelectedSections += best[idx].percentSelectedSameSection();
bestSelectedConfigs += best[idx].percentSelectedSameConfig();
if (idx >= maxIdx) {
bestSelectedSections -= 1.0;
bestSelectedConfigs -= 1.0;
}
}
if (current[idx] != null && idx < maxIdx && current[idx].getAssignments() != null && current[idx].isCourseRequest()) {
currentSelectedSections += current[idx].percentSelectedSameSection();
currentSelectedConfigs += current[idx].percentSelectedSameConfig();
}
}
if (0.3 * currentSelectedConfigs + 0.7 * currentSelectedSections > 0.3 * bestSelectedConfigs + 0.7 * bestSelectedSections)
return true;
if (0.3 * bestSelectedConfigs + 0.7 * bestSelectedSections > 0.3 * currentSelectedConfigs + 0.7 * currentSelectedSections)
return false;
// 4. avoid time overlaps
if (getModel().getTimeOverlaps() != null) {
int bestTimeOverlaps = 0, currentTimeOverlaps = 0;
for (int idx = 0; idx < current.length; idx++) {
if (best[idx] != null) {
for (int x = 0; x < idx; x++) {
if (best[x] != null)
bestTimeOverlaps += getModel().getTimeOverlaps().nrConflicts(best[x], best[idx]);
else if (getStudent().getRequests().get(x) instanceof FreeTimeRequest)
bestTimeOverlaps += getModel().getTimeOverlaps().nrConflicts(((FreeTimeRequest) getStudent().getRequests().get(x)).createEnrollment(), best[idx]);
}
}
if (current[idx] != null && idx < maxIdx) {
for (int x = 0; x < idx; x++) {
if (current[x] != null)
currentTimeOverlaps += getModel().getTimeOverlaps().nrConflicts(current[x], current[idx]);
else if (getStudent().getRequests().get(x) instanceof FreeTimeRequest)
currentTimeOverlaps += getModel().getTimeOverlaps().nrConflicts(((FreeTimeRequest) getStudent().getRequests().get(x)).createEnrollment(), current[idx]);
}
}
}
for (int idx = 0; idx < current.length; idx++) {
if (best[idx] != null && best[idx].getAssignments() != null && best[idx].isCourseRequest()) {
bestTimeOverlaps += getModel().getTimeOverlaps().nrNotAvailableTimeConflicts(best[idx]);
}
if (current[idx] != null && idx < maxIdx && current[idx].getAssignments() != null && current[idx].isCourseRequest()) {
currentTimeOverlaps += getModel().getTimeOverlaps().nrNotAvailableTimeConflicts(current[idx]);
}
}
if (currentTimeOverlaps < bestTimeOverlaps)
return true;
if (bestTimeOverlaps < currentTimeOverlaps)
return false;
}
// 5. avoid distance conflicts
if (getModel().getDistanceConflict() != null) {
int bestDistanceConf = 0, currentDistanceConf = 0;
for (int idx = 0; idx < current.length; idx++) {
if (best[idx] != null) {
for (int x = 0; x < idx; x++) {
if (best[x] != null)
bestDistanceConf += getModel().getDistanceConflict().nrConflicts(best[x], best[idx]);
}
}
if (current[idx] != null && idx < maxIdx) {
for (int x = 0; x < idx; x++) {
if (current[x] != null)
currentDistanceConf += getModel().getDistanceConflict().nrConflicts(current[x], current[idx]);
}
}
}
if (currentDistanceConf < bestDistanceConf)
return true;
if (bestDistanceConf < currentDistanceConf)
return false;
}
// 6. avoid no-time sections
int bestNoTime = 0, currentNoTime = 0;
for (int idx = 0; idx < current.length; idx++) {
if (best[idx] != null) {
for (Section section : best[idx].getSections()) if (section.getTime() == null)
bestNoTime++;
}
if (current[idx] != null && idx < maxIdx) {
for (Section section : current[idx].getSections()) if (section.getTime() == null)
currentNoTime++;
}
}
if (currentNoTime < bestNoTime)
return true;
if (bestNoTime < currentNoTime)
return false;
// 7. balance sections
double bestUnavailableSize = 0.0, currentUnavailableSize = 0.0;
int bestAltSectionsWithLimit = 0, currentAltSectionsWithLimit = 0;
for (int idx = 0; idx < current.length; idx++) {
if (best[idx] != null) {
for (Section section : best[idx].getSections()) {
Subpart subpart = section.getSubpart();
// skip unlimited and single section subparts
if (subpart.getSections().size() <= 1 || subpart.getLimit() <= 0)
continue;
// average size
double averageSize = ((double) subpart.getLimit()) / subpart.getSections().size();
// section is below average
if (section.getLimit() < averageSize)
bestUnavailableSize += (averageSize - section.getLimit()) / averageSize;
bestAltSectionsWithLimit++;
}
}
if (current[idx] != null && idx < maxIdx) {
for (Section section : current[idx].getSections()) {
Subpart subpart = section.getSubpart();
// skip unlimited and single section subparts
if (subpart.getSections().size() <= 1 || subpart.getLimit() <= 0)
continue;
// average size
double averageSize = ((double) subpart.getLimit()) / subpart.getSections().size();
// section is below average
if (section.getLimit() < averageSize)
currentUnavailableSize += (averageSize - section.getLimit()) / averageSize;
currentAltSectionsWithLimit++;
}
}
}
double bestUnavailableSizeFraction = (bestUnavailableSize > 0 ? bestUnavailableSize / bestAltSectionsWithLimit : 0.0);
double currentUnavailableSizeFraction = (currentUnavailableSize > 0 ? currentUnavailableSize / currentAltSectionsWithLimit : 0.0);
if (currentUnavailableSizeFraction < bestUnavailableSizeFraction)
return true;
if (bestUnavailableSizeFraction < currentUnavailableSizeFraction)
return false;
// 8. average penalty sections
double bestPenalty = 0.0, currentPenalty = 0.0;
for (int idx = 0; idx < current.length; idx++) {
if (best[idx] != null) {
for (Section section : best[idx].getSections()) bestPenalty += section.getPenalty();
if (idx >= maxIdx && best[idx].isCourseRequest())
bestPenalty -= ((CourseRequest) best[idx].getRequest()).getMinPenalty();
}
if (current[idx] != null && idx < maxIdx) {
for (Section section : current[idx].getSections()) currentPenalty += section.getPenalty();
}
}
if (currentPenalty < bestPenalty)
return true;
if (bestPenalty < currentPenalty)
return false;
return true;
}
use of org.cpsolver.studentsct.model.FreeTimeRequest in project cpsolver by UniTime.
the class StudentRequestXml method exportModel.
public static Document exportModel(Assignment<Request, Enrollment> assignment, StudentSectioningModel model) {
Document document = DocumentHelper.createDocument();
Element requestElement = document.addElement("request");
requestElement.addAttribute("campus", model.getProperties().getProperty("Data.Initiative"));
requestElement.addAttribute("year", model.getProperties().getProperty("Data.Year"));
requestElement.addAttribute("term", model.getProperties().getProperty("Data.Term"));
for (Student student : model.getStudents()) {
Element studentElement = requestElement.addElement("student");
studentElement.addAttribute("key", String.valueOf(student.getId()));
Element courseRequestsElement = studentElement.addElement("updateCourseRequests");
courseRequestsElement.addAttribute("commit", "true");
Collections.sort(student.getRequests(), new Comparator<Request>() {
@Override
public int compare(Request r1, Request r2) {
if (r1.isAlternative() != r2.isAlternative()) {
return (r1.isAlternative() ? 1 : -1);
}
return Double.compare(r1.getPriority(), r2.getPriority());
}
});
boolean hasSchedule = false;
for (Request request : student.getRequests()) {
if (assignment.getValue(request) != null)
hasSchedule = true;
if (request instanceof FreeTimeRequest) {
FreeTimeRequest ftReq = (FreeTimeRequest) request;
Element ftReqElement = courseRequestsElement.addElement("freeTime");
requestElement.addAttribute("days", ftReq.getTime().getDayHeader());
int startSlot = ftReq.getTime().getStartSlot();
int startTime = startSlot * Constants.SLOT_LENGTH_MIN + Constants.FIRST_SLOT_TIME_MIN;
ftReqElement.addAttribute("startTime", s2zDF.format(startTime / 60) + s2zDF.format(startTime % 60));
int endTime = startTime + ftReq.getTime().getLength() * Constants.SLOT_LENGTH_MIN - ftReq.getTime().getBreakTime();
ftReqElement.addAttribute("endTime", s2zDF.format(endTime / 60) + s2zDF.format(endTime % 60));
ftReqElement.addAttribute("length", String.valueOf(ftReq.getTime().getLength() * Constants.SLOT_LENGTH_MIN));
} else {
CourseRequest crReq = (CourseRequest) request;
Element crReqElement = courseRequestsElement.addElement("courseOffering");
Course course = crReq.getCourses().get(0);
crReqElement.addAttribute("subjectArea", course.getSubjectArea());
crReqElement.addAttribute("courseNumber", course.getCourseNumber());
crReqElement.addAttribute("waitlist", crReq.isWaitlist() ? "true" : "false");
crReqElement.addAttribute("alternative", crReq.isAlternative() ? "true" : "false");
for (int i = 1; i < crReq.getCourses().size(); i++) {
Course altCourse = crReq.getCourses().get(i);
Element altCourseElement = crReqElement.addElement("alternative");
altCourseElement.addAttribute("subjectArea", altCourse.getSubjectArea());
altCourseElement.addAttribute("courseNumber", altCourse.getCourseNumber());
}
}
}
if (hasSchedule) {
Element requestScheduleElement = studentElement.addElement("requestSchedule");
requestScheduleElement.addAttribute("type", "commit");
for (Request request : student.getRequests()) {
if (request instanceof CourseRequest) {
CourseRequest crReq = (CourseRequest) request;
Enrollment enrollment = assignment.getValue(crReq);
if (enrollment == null)
continue;
Element crReqElement = requestScheduleElement.addElement("courseOffering");
Course course = enrollment.getCourse();
crReqElement.addAttribute("subjectArea", course.getSubjectArea());
crReqElement.addAttribute("courseNumber", course.getCourseNumber());
for (Section section : enrollment.getSections()) {
Element classEl = crReqElement.addElement("class");
classEl.addAttribute("id", section.getSubpart().getInstructionalType());
classEl.addAttribute("assignmentId", String.valueOf(section.getId()));
}
}
}
}
}
return document;
}
use of org.cpsolver.studentsct.model.FreeTimeRequest in project cpsolver by UniTime.
the class SuggestionsBranchAndBound method computeSuggestions.
/**
* Perform the search
* @return an ordered set of possible suggestions
*/
public TreeSet<Suggestion> computeSuggestions() {
iT0 = System.currentTimeMillis();
iTimeoutReached = false;
iNrSolutionsSeen = 0;
iSuggestions.clear();
ArrayList<Request> requests2resolve = new ArrayList<Request>();
requests2resolve.add(iSelectedRequest);
TreeSet<Request> altRequests2resolve = new TreeSet<Request>();
for (Map.Entry<CourseRequest, Set<Section>> entry : iPreferredSections.entrySet()) {
CourseRequest request = entry.getKey();
Set<Section> sections = entry.getValue();
if (!sections.isEmpty() && sections.size() == sections.iterator().next().getSubpart().getConfig().getSubparts().size())
iAssignment.assign(0, request.createEnrollment(iAssignment, sections));
else if (!request.equals(iSelectedRequest)) {
if (sections.isEmpty())
altRequests2resolve.add(request);
else
requests2resolve.add(request);
}
}
for (Request request : iStudent.getRequests()) {
if (iAssignment.getValue(request) == null && request instanceof FreeTimeRequest) {
FreeTimeRequest ft = (FreeTimeRequest) request;
Enrollment enrollment = ft.createEnrollment();
if (iModel.conflictValues(iAssignment, enrollment).isEmpty())
iAssignment.assign(0, enrollment);
}
}
for (Request request : iStudent.getRequests()) {
request.setInitialAssignment(iAssignment.getValue(request));
}
backtrack(requests2resolve, altRequests2resolve, 0, iMaxDepth, false);
iT1 = System.currentTimeMillis();
return iSuggestions;
}
use of org.cpsolver.studentsct.model.FreeTimeRequest in project cpsolver by UniTime.
the class SectionConflictTable method createTable.
/**
* Create report
*
* @param assignment current assignment
* @param includeLastLikeStudents
* true, if last-like students should be included (i.e.,
* {@link Student#isDummy()} is true)
* @param includeRealStudents
* true, if real students should be included (i.e.,
* {@link Student#isDummy()} is false)
* @param useAmPm use 12-hour format
* @return report as comma separated text file
*/
public CSVFile createTable(Assignment<Request, Enrollment> assignment, boolean includeLastLikeStudents, boolean includeRealStudents, boolean useAmPm) {
HashMap<Course, Map<Section, Double[]>> unavailabilities = new HashMap<Course, Map<Section, Double[]>>();
HashMap<Course, Set<Long>> totals = new HashMap<Course, Set<Long>>();
HashMap<CourseSection, Map<CourseSection, Double>> conflictingPairs = new HashMap<CourseSection, Map<CourseSection, Double>>();
HashMap<CourseSection, Double> sectionOverlaps = new HashMap<CourseSection, Double>();
for (Request request : new ArrayList<Request>(getModel().unassignedVariables(assignment))) {
if (request.getStudent().isDummy() && !includeLastLikeStudents)
continue;
if (!request.getStudent().isDummy() && !includeRealStudents)
continue;
if (request instanceof CourseRequest) {
CourseRequest courseRequest = (CourseRequest) request;
if (courseRequest.getStudent().isComplete(assignment))
continue;
List<Enrollment> values = courseRequest.values(assignment);
SectionLimit limitConstraint = null;
for (GlobalConstraint<Request, Enrollment> c : getModel().globalConstraints()) {
if (c instanceof SectionLimit) {
limitConstraint = (SectionLimit) c;
break;
}
}
if (limitConstraint == null) {
limitConstraint = new SectionLimit(new DataProperties());
limitConstraint.setModel(getModel());
}
List<Enrollment> notAvailableValues = new ArrayList<Enrollment>(values.size());
List<Enrollment> availableValues = new ArrayList<Enrollment>(values.size());
for (Enrollment enrollment : values) {
if (limitConstraint.inConflict(assignment, enrollment))
notAvailableValues.add(enrollment);
else
availableValues.add(enrollment);
}
if (!notAvailableValues.isEmpty() && iType.hasUnavailabilities()) {
List<Enrollment> notOverlappingEnrollments = new ArrayList<Enrollment>(values.size());
enrollments: for (Enrollment enrollment : notAvailableValues) {
for (Request other : request.getStudent().getRequests()) {
if (other.equals(request) || assignment.getValue(other) == null || other instanceof FreeTimeRequest)
continue;
if (assignment.getValue(other).isOverlapping(enrollment))
continue enrollments;
}
// not overlapping
notOverlappingEnrollments.add(enrollment);
}
if (notOverlappingEnrollments.isEmpty() && availableValues.isEmpty() && iOverlapsAllEnrollments) {
double fraction = request.getWeight() / notAvailableValues.size();
Set<CourseSection> ones = new HashSet<CourseSection>();
for (Enrollment enrollment : notAvailableValues) {
boolean hasConflict = false;
for (Section s : enrollment.getSections()) {
if (s.getLimit() >= 0 && s.getEnrollmentWeight(assignment, request) + request.getWeight() > s.getLimit()) {
hasConflict = true;
break;
}
}
Map<Section, Double[]> sections = unavailabilities.get(enrollment.getCourse());
if (sections == null) {
sections = new HashMap<Section, Double[]>();
unavailabilities.put(enrollment.getCourse(), sections);
}
for (Section s : enrollment.getSections()) {
if (hasConflict && s.getLimit() < 0 || s.getEnrollmentWeight(assignment, request) + request.getWeight() <= s.getLimit())
continue;
Double[] total = sections.get(s);
sections.put(s, new Double[] { fraction + (total == null ? 0.0 : total[0].doubleValue()), (total == null ? 0.0 : total[1].doubleValue()) });
ones.add(new CourseSection(enrollment.getCourse(), s));
}
Set<Long> total = totals.get(enrollment.getCourse());
if (total == null) {
total = new HashSet<Long>();
totals.put(enrollment.getCourse(), total);
}
total.add(enrollment.getStudent().getId());
}
} else if (!notOverlappingEnrollments.isEmpty()) {
double fraction = request.getWeight() / notOverlappingEnrollments.size();
Set<CourseSection> ones = new HashSet<CourseSection>();
for (Enrollment enrollment : notOverlappingEnrollments) {
boolean hasConflict = false;
for (Section s : enrollment.getSections()) {
if (s.getLimit() >= 0 && s.getEnrollmentWeight(assignment, request) + request.getWeight() > s.getLimit()) {
hasConflict = true;
break;
}
}
Map<Section, Double[]> sections = unavailabilities.get(enrollment.getCourse());
if (sections == null) {
sections = new HashMap<Section, Double[]>();
unavailabilities.put(enrollment.getCourse(), sections);
}
for (Section s : enrollment.getSections()) {
if (hasConflict && s.getLimit() < 0 || s.getEnrollmentWeight(assignment, request) + request.getWeight() <= s.getLimit())
continue;
Double[] total = sections.get(s);
sections.put(s, new Double[] { fraction + (total == null ? 0.0 : total[0].doubleValue()), (total == null ? 0.0 : total[1].doubleValue()) });
ones.add(new CourseSection(enrollment.getCourse(), s));
}
Set<Long> total = totals.get(enrollment.getCourse());
if (total == null) {
total = new HashSet<Long>();
totals.put(enrollment.getCourse(), total);
}
total.add(enrollment.getStudent().getId());
}
for (CourseSection section : ones) {
Map<Section, Double[]> sections = unavailabilities.get(section.getCourse());
Double[] total = sections.get(section.getSection());
sections.put(section.getSection(), new Double[] { (total == null ? 0.0 : total[0].doubleValue()), request.getWeight() + (total == null ? 0.0 : total[1].doubleValue()) });
}
}
}
if (iOverlapsAllEnrollments)
availableValues = values;
if (!availableValues.isEmpty() && iType.hasOverlaps()) {
List<Map<CourseSection, List<CourseSection>>> conflicts = new ArrayList<Map<CourseSection, List<CourseSection>>>();
for (Enrollment enrollment : availableValues) {
Map<CourseSection, List<CourseSection>> overlaps = new HashMap<CourseSection, List<CourseSection>>();
for (Request other : request.getStudent().getRequests()) {
Enrollment otherEnrollment = assignment.getValue(other);
if (other.equals(request) || otherEnrollment == null || other instanceof FreeTimeRequest)
continue;
if (enrollment.isOverlapping(otherEnrollment))
for (Section a : enrollment.getSections()) for (Section b : otherEnrollment.getSections()) if (a.getTime() != null && b.getTime() != null && !a.isAllowOverlap() && !b.isAllowOverlap() && !a.isToIgnoreStudentConflictsWith(b.getId()) && a.getTime().hasIntersection(b.getTime()) && !canIgnore(assignment, enrollment, a, availableValues)) {
List<CourseSection> x = overlaps.get(new CourseSection(enrollment.getCourse(), a));
if (x == null) {
x = new ArrayList<CourseSection>();
overlaps.put(new CourseSection(enrollment.getCourse(), a), x);
}
x.add(new CourseSection(otherEnrollment.getCourse(), b));
}
}
if (!overlaps.isEmpty()) {
conflicts.add(overlaps);
Set<Long> total = totals.get(enrollment.getCourse());
if (total == null) {
total = new HashSet<Long>();
totals.put(enrollment.getCourse(), total);
}
total.add(enrollment.getStudent().getId());
}
}
double fraction = request.getWeight() / conflicts.size();
for (Map<CourseSection, List<CourseSection>> overlaps : conflicts) {
for (Map.Entry<CourseSection, List<CourseSection>> entry : overlaps.entrySet()) {
CourseSection a = entry.getKey();
Double total = sectionOverlaps.get(a);
sectionOverlaps.put(a, fraction + (total == null ? 0.0 : total.doubleValue()));
Map<CourseSection, Double> pair = conflictingPairs.get(a);
if (pair == null) {
pair = new HashMap<CourseSection, Double>();
conflictingPairs.put(a, pair);
}
for (CourseSection b : entry.getValue()) {
Double prev = pair.get(b);
pair.put(b, fraction + (prev == null ? 0.0 : prev.doubleValue()));
}
}
}
}
}
}
Comparator<Course> courseComparator = new Comparator<Course>() {
@Override
public int compare(Course a, Course b) {
int cmp = a.getName().compareTo(b.getName());
if (cmp != 0)
return cmp;
return a.getId() < b.getId() ? -1 : a.getId() == b.getId() ? 0 : 1;
}
};
Comparator<Section> sectionComparator = new Comparator<Section>() {
@Override
public int compare(Section a, Section b) {
int cmp = a.getSubpart().getConfig().getOffering().getName().compareTo(b.getSubpart().getConfig().getOffering().getName());
if (cmp != 0)
return cmp;
cmp = a.getSubpart().getInstructionalType().compareTo(b.getSubpart().getInstructionalType());
// cmp = a.getName().compareTo(b.getName());
if (cmp != 0)
return cmp;
return a.getId() < b.getId() ? -1 : a.getId() == b.getId() ? 0 : 1;
}
};
CSVFile csv = new CSVFile();
List<CSVFile.CSVField> headers = new ArrayList<CSVFile.CSVField>();
headers.add(new CSVFile.CSVField("Course"));
headers.add(new CSVFile.CSVField("Total\nConflicts"));
if (iType.hasUnavailabilities()) {
headers.add(new CSVFile.CSVField("Course\nEnrollment"));
headers.add(new CSVFile.CSVField("Course\nLimit"));
}
headers.add(new CSVFile.CSVField("Class"));
headers.add(new CSVFile.CSVField("Meeting Time"));
if (iType.hasUnavailabilities()) {
headers.add(new CSVFile.CSVField("Availability\nConflicts"));
headers.add(new CSVFile.CSVField("% of Total\nConflicts"));
}
if (iType.hasOverlaps()) {
headers.add(new CSVFile.CSVField("Time\nConflicts"));
headers.add(new CSVFile.CSVField("% of Total\nConflicts"));
}
if (iType.hasUnavailabilities()) {
headers.add(new CSVFile.CSVField("Class\nEnrollment"));
headers.add(new CSVFile.CSVField("Class\nLimit"));
if (!iType.hasOverlaps())
headers.add(new CSVFile.CSVField("Class\nPotential"));
}
if (iType.hasOverlaps()) {
headers.add(new CSVFile.CSVField("Conflicting\nClass"));
headers.add(new CSVFile.CSVField("Conflicting\nMeeting Time"));
headers.add(new CSVFile.CSVField("Joined\nConflicts"));
headers.add(new CSVFile.CSVField("% of Total\nConflicts"));
}
csv.setHeader(headers);
TreeSet<Course> courses = new TreeSet<Course>(courseComparator);
courses.addAll(totals.keySet());
for (Course course : courses) {
Map<Section, Double[]> sectionUnavailability = unavailabilities.get(course);
Set<Long> total = totals.get(course);
TreeSet<Section> sections = new TreeSet<Section>(sectionComparator);
if (sectionUnavailability != null)
sections.addAll(sectionUnavailability.keySet());
for (Map.Entry<CourseSection, Double> entry : sectionOverlaps.entrySet()) if (course.equals(entry.getKey().getCourse()))
sections.add(entry.getKey().getSection());
boolean firstCourse = true;
for (Section section : sections) {
Double[] sectionUnavailable = (sectionUnavailability == null ? null : sectionUnavailability.get(section));
Double sectionOverlap = sectionOverlaps.get(new CourseSection(course, section));
Map<CourseSection, Double> pair = conflictingPairs.get(new CourseSection(course, section));
if (pair == null) {
List<CSVFile.CSVField> line = new ArrayList<CSVFile.CSVField>();
line.add(new CSVFile.CSVField(firstCourse ? course.getName() : ""));
line.add(new CSVFile.CSVField(firstCourse ? total.size() : ""));
if (iType.hasUnavailabilities()) {
line.add(new CSVFile.CSVField(firstCourse ? sDF1.format(course.getEnrollmentWeight(assignment, null)) : ""));
line.add(new CSVFile.CSVField(firstCourse ? course.getLimit() < 0 ? "" : String.valueOf(course.getLimit()) : ""));
}
line.add(new CSVFile.CSVField(section.getSubpart().getName() + " " + section.getName(course.getId())));
line.add(new CSVFile.CSVField(section.getTime() == null ? "" : section.getTime().getDayHeader() + " " + section.getTime().getStartTimeHeader(useAmPm) + " - " + section.getTime().getEndTimeHeader(useAmPm)));
if (iType.hasUnavailabilities()) {
line.add(new CSVFile.CSVField(sectionUnavailable != null ? sDF2.format(sectionUnavailable[0]) : ""));
line.add(new CSVFile.CSVField(sectionUnavailable != null ? sDF2.format(sectionUnavailable[0] / total.size()) : ""));
}
if (iType.hasOverlaps()) {
line.add(new CSVFile.CSVField(sectionOverlap != null ? sDF2.format(sectionOverlap) : ""));
line.add(new CSVFile.CSVField(sectionOverlap != null ? sDF2.format(sectionOverlap / total.size()) : ""));
}
if (iType.hasUnavailabilities()) {
line.add(new CSVFile.CSVField(sDF1.format(section.getEnrollmentWeight(assignment, null))));
line.add(new CSVFile.CSVField(section.getLimit() < 0 ? "" : String.valueOf(section.getLimit())));
if (!iType.hasOverlaps())
line.add(new CSVFile.CSVField(sectionUnavailable != null ? sDF1.format(sectionUnavailable[1]) : ""));
}
csv.addLine(line);
} else {
boolean firstClass = true;
for (CourseSection other : new TreeSet<CourseSection>(pair.keySet())) {
List<CSVFile.CSVField> line = new ArrayList<CSVFile.CSVField>();
line.add(new CSVFile.CSVField(firstCourse && firstClass ? course.getName() : ""));
line.add(new CSVFile.CSVField(firstCourse && firstClass ? total.size() : ""));
if (iType.hasUnavailabilities()) {
line.add(new CSVFile.CSVField(firstCourse && firstClass ? sDF1.format(course.getEnrollmentWeight(assignment, null)) : ""));
line.add(new CSVFile.CSVField(firstCourse && firstClass ? course.getLimit() < 0 ? "" : String.valueOf(course.getLimit()) : ""));
}
line.add(new CSVFile.CSVField(firstClass ? section.getSubpart().getName() + " " + section.getName(course.getId()) : ""));
line.add(new CSVFile.CSVField(firstClass ? section.getTime() == null ? "" : section.getTime().getDayHeader() + " " + section.getTime().getStartTimeHeader(useAmPm) + " - " + section.getTime().getEndTimeHeader(useAmPm) : ""));
if (iType.hasUnavailabilities()) {
line.add(new CSVFile.CSVField(firstClass && sectionUnavailable != null ? sDF2.format(sectionUnavailable[0]) : ""));
line.add(new CSVFile.CSVField(sectionUnavailable != null ? sDF2.format(sectionUnavailable[0] / total.size()) : ""));
}
line.add(new CSVFile.CSVField(firstClass && sectionOverlap != null ? sDF2.format(sectionOverlap) : ""));
line.add(new CSVFile.CSVField(firstClass && sectionOverlap != null ? sDF2.format(sectionOverlap / total.size()) : ""));
if (iType.hasUnavailabilities()) {
line.add(new CSVFile.CSVField(firstClass ? sDF1.format(section.getEnrollmentWeight(assignment, null)) : ""));
line.add(new CSVFile.CSVField(firstClass ? section.getLimit() < 0 ? "" : String.valueOf(section.getLimit()) : ""));
}
line.add(new CSVFile.CSVField(other.getCourse().getName() + " " + other.getSection().getSubpart().getName() + " " + other.getSection().getName(other.getCourse().getId())));
line.add(new CSVFile.CSVField(other.getSection().getTime().getDayHeader() + " " + other.getSection().getTime().getStartTimeHeader(useAmPm) + " - " + other.getSection().getTime().getEndTimeHeader(useAmPm)));
line.add(new CSVFile.CSVField(sDF2.format(pair.get(other))));
line.add(new CSVFile.CSVField(sDF2.format(pair.get(other) / total.size())));
csv.addLine(line);
firstClass = false;
}
}
firstCourse = false;
}
csv.addLine();
}
return csv;
}
Aggregations