use of org.cpsolver.studentsct.model.CourseRequest in project cpsolver by UniTime.
the class OnlineSelection method updateSpace.
/**
* Update online sectioning info after the given student is sectioned
* @param assignment current assignment
* @param student student in question
*/
public void updateSpace(Assignment<Request, Enrollment> assignment, Student student) {
for (Request request : student.getRequests()) {
if (!(request instanceof CourseRequest))
continue;
CourseRequest courseRequest = (CourseRequest) request;
Enrollment enrollment = assignment.getValue(courseRequest);
if (enrollment == null)
// not enrolled --> no update
return;
for (Section section : enrollment.getSections()) {
section.setSpaceHeld(section.getSpaceHeld() - courseRequest.getWeight());
// sLog.debug(" -- space held for "+section+" decreased by 1 (to "+section.getSpaceHeld()+")");
}
List<Enrollment> feasibleEnrollments = new ArrayList<Enrollment>();
for (Enrollment enrl : courseRequest.values(assignment)) {
boolean overlaps = false;
for (Request otherRequest : courseRequest.getStudent().getRequests()) {
if (otherRequest.equals(courseRequest) || !(otherRequest instanceof CourseRequest))
continue;
Enrollment otherErollment = assignment.getValue(otherRequest);
if (otherErollment == null)
continue;
if (enrl.isOverlapping(otherErollment)) {
overlaps = true;
break;
}
}
if (!overlaps)
feasibleEnrollments.add(enrl);
}
double decrement = courseRequest.getWeight() / feasibleEnrollments.size();
for (Enrollment feasibleEnrollment : feasibleEnrollments) {
for (Section section : feasibleEnrollment.getSections()) {
section.setSpaceExpected(section.getSpaceExpected() - decrement);
// sLog.debug(" -- space expected for "+section+" decreased by "+decrement+" (to "+section.getSpaceExpected()+")");
}
}
}
}
use of org.cpsolver.studentsct.model.CourseRequest in project cpsolver by UniTime.
the class RequestPriorityTable method create.
@Override
public CSVFile create(Assignment<Request, Enrollment> assignment, DataProperties properties) {
CSVFile csv = new CSVFile();
csv.setHeader(new CSVFile.CSVField[] { new CSVFile.CSVField("__Student"), new CSVFile.CSVField("Student"), new CSVFile.CSVField("Course"), new CSVFile.CSVField("Alternative"), new CSVFile.CSVField("Enrolled"), new CSVFile.CSVField("Primary"), new CSVFile.CSVField("Priority"), new CSVFile.CSVField("Alternativity") });
for (Student student : getModel().getStudents()) {
if (student.isDummy())
continue;
int regPriority = 1, altPriority = 1;
for (Request r : student.getRequests()) {
if (r instanceof CourseRequest) {
CourseRequest cr = (CourseRequest) r;
Enrollment e = cr.getAssignment(assignment);
int primary = (cr.isAlternative() ? 0 : 1);
int priority = 0;
if (cr.isAlternative())
priority = altPriority++;
else
priority = regPriority++;
int alternativity = 0;
for (Course course : cr.getCourses()) {
int alternative = (primary == 0 || alternativity > 0 ? 1 : 0);
int enrolled = (e != null && e.getCourse().equals(course) ? 1 : 0);
csv.addLine(new CSVFile.CSVField[] { new CSVFile.CSVField(student.getId()), new CSVFile.CSVField(student.getExternalId()), new CSVFile.CSVField(cr.getCourses().get(alternativity).getName()), new CSVFile.CSVField(alternative), new CSVFile.CSVField(enrolled), new CSVFile.CSVField(primary), new CSVFile.CSVField(priority), new CSVFile.CSVField(alternativity) });
alternativity++;
}
}
}
}
return csv;
}
use of org.cpsolver.studentsct.model.CourseRequest 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;
}
use of org.cpsolver.studentsct.model.CourseRequest in project cpsolver by UniTime.
the class MultiCriteriaBranchAndBoundSelection method isAllowed.
public boolean isAllowed(int idx, Enrollment enrollment) {
if (enrollment.isCourseRequest()) {
CourseRequest request = (CourseRequest) enrollment.getRequest();
if (iRequiredUnassinged != null && iRequiredUnassinged.contains(request))
return false;
Config reqConfig = iRequiredConfig.get(request);
if (reqConfig != null) {
if (!reqConfig.equals(enrollment.getConfig()))
return false;
Hashtable<Subpart, Section> reqSections = iRequiredSection.get(request);
for (Section section : enrollment.getSections()) {
Section reqSection = reqSections.get(section.getSubpart());
if (reqSection == null)
continue;
if (!section.equals(reqSection))
return false;
}
}
} else if (iRequiredFreeTimes.contains(enrollment.getRequest())) {
if (enrollment.getAssignments() == null || enrollment.getAssignments().isEmpty())
return false;
}
return true;
}
use of org.cpsolver.studentsct.model.CourseRequest in project cpsolver by UniTime.
the class MultiCriteriaBranchAndBoundSelection method setRequiredSections.
@Override
public void setRequiredSections(Hashtable<CourseRequest, Set<Section>> requiredSections) {
if (requiredSections != null) {
for (Map.Entry<CourseRequest, Set<Section>> entry : requiredSections.entrySet()) {
Hashtable<Subpart, Section> subSection = new Hashtable<Subpart, Section>();
iRequiredSection.put(entry.getKey(), subSection);
for (Section section : entry.getValue()) {
if (subSection.isEmpty())
iRequiredConfig.put(entry.getKey(), section.getSubpart().getConfig());
subSection.put(section.getSubpart(), section);
}
}
}
}
Aggregations