use of org.cpsolver.studentsct.model.Student 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.Student in project cpsolver by UniTime.
the class StudentSectioningXMLLoader method loadStudents.
/**
* Load students
* @param studentsEl students element
* @param offeringTable offering table
* @param courseTable course table
*/
protected void loadStudents(Element studentsEl, Map<Long, Offering> offeringTable, Map<Long, Course> courseTable) {
List<Enrollment> bestEnrollments = new ArrayList<Enrollment>();
List<Enrollment> currentEnrollments = new ArrayList<Enrollment>();
for (Iterator<?> i = studentsEl.elementIterator("student"); i.hasNext(); ) {
Element studentEl = (Element) i.next();
Student student = loadStudent(studentEl, offeringTable);
if (iStudentFilter != null && !iStudentFilter.accept(student))
continue;
for (Iterator<?> j = studentEl.elementIterator(); j.hasNext(); ) {
Element requestEl = (Element) j.next();
Request request = loadRequest(requestEl, student, offeringTable, courseTable);
if (request == null)
continue;
Element initialEl = requestEl.element("initial");
if (iLoadInitial && initialEl != null) {
Enrollment enrollment = loadEnrollment(initialEl, request);
if (enrollment != null)
request.setInitialAssignment(enrollment);
}
Element currentEl = requestEl.element("current");
if (iLoadCurrent && currentEl != null) {
Enrollment enrollment = loadEnrollment(currentEl, request);
if (enrollment != null)
currentEnrollments.add(enrollment);
}
Element bestEl = requestEl.element("best");
if (iLoadBest && bestEl != null) {
Enrollment enrollment = loadEnrollment(bestEl, request);
if (enrollment != null)
bestEnrollments.add(enrollment);
}
}
getModel().addStudent(student);
}
if (!bestEnrollments.isEmpty()) {
// Enrollments with a reservation must go first
for (Enrollment enrollment : bestEnrollments) {
if (enrollment.getReservation() == null)
continue;
if (!enrollment.getStudent().isAvailable(enrollment)) {
sLogger.warn("Enrollment " + enrollment + " is conflicting: student not available.");
continue;
}
Map<Constraint<Request, Enrollment>, Set<Enrollment>> conflicts = getModel().conflictConstraints(getAssignment(), enrollment);
if (conflicts.isEmpty())
getAssignment().assign(0, enrollment);
else
sLogger.warn("Enrollment " + enrollment + " conflicts with " + conflicts);
}
for (Enrollment enrollment : bestEnrollments) {
if (enrollment.getReservation() != null)
continue;
if (!enrollment.getStudent().isAvailable(enrollment)) {
sLogger.warn("Enrollment " + enrollment + " is conflicting: student not available.");
continue;
}
Map<Constraint<Request, Enrollment>, Set<Enrollment>> conflicts = getModel().conflictConstraints(getAssignment(), enrollment);
if (conflicts.isEmpty())
getAssignment().assign(0, enrollment);
else
sLogger.warn("Enrollment " + enrollment + " conflicts with " + conflicts);
}
getModel().saveBest(getAssignment());
}
if (!currentEnrollments.isEmpty()) {
for (Request request : getModel().variables()) getAssignment().unassign(0, request);
// Enrollments with a reservation must go first
for (Enrollment enrollment : currentEnrollments) {
if (enrollment.getReservation() == null)
continue;
if (!enrollment.getStudent().isAvailable(enrollment)) {
sLogger.warn("Enrollment " + enrollment + " is conflicting: student not available.");
continue;
}
Map<Constraint<Request, Enrollment>, Set<Enrollment>> conflicts = getModel().conflictConstraints(getAssignment(), enrollment);
if (conflicts.isEmpty())
getAssignment().assign(0, enrollment);
else
sLogger.warn("Enrollment " + enrollment + " conflicts with " + conflicts);
}
for (Enrollment enrollment : currentEnrollments) {
if (enrollment.getReservation() != null)
continue;
if (!enrollment.getStudent().isAvailable(enrollment)) {
sLogger.warn("Enrollment " + enrollment + " is conflicting: student not available.");
continue;
}
Map<Constraint<Request, Enrollment>, Set<Enrollment>> conflicts = getModel().conflictConstraints(getAssignment(), enrollment);
if (conflicts.isEmpty())
getAssignment().assign(0, enrollment);
else
sLogger.warn("Enrollment " + enrollment + " conflicts with " + conflicts);
}
}
}
use of org.cpsolver.studentsct.model.Student in project cpsolver by UniTime.
the class StudentSectioningModel method computeOnlineSectioningInfos.
/**
* Compute online student sectioning infos for all sections (see
* {@link Section#getSpaceExpected()} and {@link Section#getSpaceHeld()}).
* @param assignment current assignment
*/
public void computeOnlineSectioningInfos(Assignment<Request, Enrollment> assignment) {
clearOnlineSectioningInfos();
for (Student student : getStudents()) {
if (!student.isDummy())
continue;
for (Request request : student.getRequests()) {
if (!(request instanceof CourseRequest))
continue;
CourseRequest courseRequest = (CourseRequest) request;
Enrollment enrollment = assignment.getValue(courseRequest);
if (enrollment != null) {
for (Section section : enrollment.getSections()) {
section.setSpaceHeld(courseRequest.getWeight() + section.getSpaceHeld());
}
}
List<Enrollment> feasibleEnrollments = new ArrayList<Enrollment>();
int totalLimit = 0;
for (Enrollment enrl : courseRequest.values(assignment)) {
boolean overlaps = false;
for (Request otherRequest : student.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);
if (totalLimit >= 0) {
int limit = enrl.getLimit();
if (limit < 0)
totalLimit = -1;
else
totalLimit += limit;
}
}
}
double increment = courseRequest.getWeight() / (totalLimit > 0 ? totalLimit : feasibleEnrollments.size());
for (Enrollment feasibleEnrollment : feasibleEnrollments) {
for (Section section : feasibleEnrollment.getSections()) {
if (totalLimit > 0) {
section.setSpaceExpected(section.getSpaceExpected() + increment * feasibleEnrollment.getLimit());
} else {
section.setSpaceExpected(section.getSpaceExpected() + increment);
}
}
}
}
}
}
use of org.cpsolver.studentsct.model.Student in project cpsolver by UniTime.
the class OriginalStudentWeights method main.
/**
* Test case -- run to see the weights for a few courses
* @param args program arguments
*/
public static void main(String[] args) {
OriginalStudentWeights pw = new OriginalStudentWeights(new DataProperties());
DecimalFormat df = new DecimalFormat("0.000");
Student s = new Student(0l);
new CourseRequest(1l, 0, false, s, ToolBox.toList(new Course(1, "A", "1", new Offering(0, "A")), new Course(1, "A", "2", new Offering(0, "A")), new Course(1, "A", "3", new Offering(0, "A"))), false, null);
new CourseRequest(2l, 1, false, s, ToolBox.toList(new Course(1, "B", "1", new Offering(0, "B")), new Course(1, "B", "2", new Offering(0, "B")), new Course(1, "B", "3", new Offering(0, "B"))), false, null);
new CourseRequest(3l, 2, false, s, ToolBox.toList(new Course(1, "C", "1", new Offering(0, "C")), new Course(1, "C", "2", new Offering(0, "C")), new Course(1, "C", "3", new Offering(0, "C"))), false, null);
new CourseRequest(4l, 3, false, s, ToolBox.toList(new Course(1, "D", "1", new Offering(0, "D")), new Course(1, "D", "2", new Offering(0, "D")), new Course(1, "D", "3", new Offering(0, "D"))), false, null);
new CourseRequest(5l, 4, false, s, ToolBox.toList(new Course(1, "E", "1", new Offering(0, "E")), new Course(1, "E", "2", new Offering(0, "E")), new Course(1, "E", "3", new Offering(0, "E"))), false, null);
new CourseRequest(6l, 5, true, s, ToolBox.toList(new Course(1, "F", "1", new Offering(0, "F")), new Course(1, "F", "2", new Offering(0, "F")), new Course(1, "F", "3", new Offering(0, "F"))), false, null);
new CourseRequest(7l, 6, true, s, ToolBox.toList(new Course(1, "G", "1", new Offering(0, "G")), new Course(1, "G", "2", new Offering(0, "G")), new Course(1, "G", "3", new Offering(0, "G"))), false, null);
Assignment<Request, Enrollment> assignment = new DefaultSingleAssignment<Request, Enrollment>();
Placement p = new Placement(null, new TimeLocation(1, 90, 12, 0, 0, null, null, new BitSet(), 10), new ArrayList<RoomLocation>());
for (Request r : s.getRequests()) {
CourseRequest cr = (CourseRequest) r;
double[] w = new double[] { 0.0, 0.0, 0.0 };
for (int i = 0; i < cr.getCourses().size(); i++) {
Config cfg = new Config(0l, -1, "", cr.getCourses().get(i).getOffering());
Set<SctAssignment> sections = new HashSet<SctAssignment>();
sections.add(new Section(0, 1, "x", new Subpart(0, "Lec", "Lec", cfg, null), p, null));
Enrollment e = new Enrollment(cr, i, cfg, sections, assignment);
w[i] = pw.getWeight(assignment, e, null, null);
}
System.out.println(cr + ": " + df.format(w[0]) + " " + df.format(w[1]) + " " + df.format(w[2]));
}
System.out.println("With one distance conflict:");
for (Request r : s.getRequests()) {
CourseRequest cr = (CourseRequest) r;
double[] w = new double[] { 0.0, 0.0, 0.0 };
for (int i = 0; i < cr.getCourses().size(); i++) {
Config cfg = new Config(0l, -1, "", cr.getCourses().get(i).getOffering());
Set<SctAssignment> sections = new HashSet<SctAssignment>();
sections.add(new Section(0, 1, "x", new Subpart(0, "Lec", "Lec", cfg, null), p, null));
Enrollment e = new Enrollment(cr, i, cfg, sections, assignment);
Set<DistanceConflict.Conflict> dc = new HashSet<DistanceConflict.Conflict>();
dc.add(new DistanceConflict.Conflict(s, e, (Section) sections.iterator().next(), e, (Section) sections.iterator().next()));
w[i] = pw.getWeight(assignment, e, dc, null);
}
System.out.println(cr + ": " + df.format(w[0]) + " " + df.format(w[1]) + " " + df.format(w[2]));
}
System.out.println("With two distance conflicts:");
for (Request r : s.getRequests()) {
CourseRequest cr = (CourseRequest) r;
double[] w = new double[] { 0.0, 0.0, 0.0 };
for (int i = 0; i < cr.getCourses().size(); i++) {
Config cfg = new Config(0l, -1, "", cr.getCourses().get(i).getOffering());
Set<SctAssignment> sections = new HashSet<SctAssignment>();
sections.add(new Section(0, 1, "x", new Subpart(0, "Lec", "Lec", cfg, null), p, null));
Enrollment e = new Enrollment(cr, i, cfg, sections, assignment);
Set<DistanceConflict.Conflict> dc = new HashSet<DistanceConflict.Conflict>();
dc.add(new DistanceConflict.Conflict(s, e, (Section) sections.iterator().next(), e, (Section) sections.iterator().next()));
dc.add(new DistanceConflict.Conflict(s, e, (Section) sections.iterator().next(), e, new Section(1, 1, "x", new Subpart(0, "Lec", "Lec", cfg, null), p, null)));
w[i] = pw.getWeight(assignment, e, dc, null);
}
System.out.println(cr + ": " + df.format(w[0]) + " " + df.format(w[1]) + " " + df.format(w[2]));
}
System.out.println("With 25% time overlapping conflicts:");
for (Request r : s.getRequests()) {
CourseRequest cr = (CourseRequest) r;
double[] w = new double[] { 0.0, 0.0, 0.0 };
for (int i = 0; i < cr.getCourses().size(); i++) {
Config cfg = new Config(0l, -1, "", cr.getCourses().get(i).getOffering());
Set<SctAssignment> sections = new HashSet<SctAssignment>();
sections.add(new Section(0, 1, "x", new Subpart(0, "Lec", "Lec", cfg, null), p, null));
Enrollment e = new Enrollment(cr, i, cfg, sections, assignment);
Set<TimeOverlapsCounter.Conflict> toc = new HashSet<TimeOverlapsCounter.Conflict>();
toc.add(new TimeOverlapsCounter.Conflict(s, 3, e, sections.iterator().next(), e, sections.iterator().next()));
w[i] = pw.getWeight(assignment, e, null, toc);
}
System.out.println(cr + ": " + df.format(w[0]) + " " + df.format(w[1]) + " " + df.format(w[2]));
}
}
use of org.cpsolver.studentsct.model.Student in project cpsolver by UniTime.
the class Test method loadLastLikeCourseDemandsXml.
/**
* Load last-like students from an XML file (the one that is used to load
* last like course demands table in the timetabling application)
* @param model problem model
* @param xml an XML file
*/
public static void loadLastLikeCourseDemandsXml(StudentSectioningModel model, File xml) {
try {
Document document = (new SAXReader()).read(xml);
Element root = document.getRootElement();
HashMap<Course, List<Request>> requests = new HashMap<Course, List<Request>>();
long reqId = 0;
for (Iterator<?> i = root.elementIterator("student"); i.hasNext(); ) {
Element studentEl = (Element) i.next();
Student student = new Student(Long.parseLong(studentEl.attributeValue("externalId")));
student.setDummy(true);
int priority = 0;
HashSet<Course> reqCourses = new HashSet<Course>();
for (Iterator<?> j = studentEl.elementIterator("studentCourse"); j.hasNext(); ) {
Element courseEl = (Element) j.next();
String subjectArea = courseEl.attributeValue("subject");
String courseNbr = courseEl.attributeValue("courseNumber");
Course course = null;
offerings: for (Offering offering : model.getOfferings()) {
for (Course c : offering.getCourses()) {
if (c.getSubjectArea().equals(subjectArea) && c.getCourseNumber().equals(courseNbr)) {
course = c;
break offerings;
}
}
}
if (course == null && courseNbr.charAt(courseNbr.length() - 1) >= 'A' && courseNbr.charAt(courseNbr.length() - 1) <= 'Z') {
String courseNbrNoSfx = courseNbr.substring(0, courseNbr.length() - 1);
offerings: for (Offering offering : model.getOfferings()) {
for (Course c : offering.getCourses()) {
if (c.getSubjectArea().equals(subjectArea) && c.getCourseNumber().equals(courseNbrNoSfx)) {
course = c;
break offerings;
}
}
}
}
if (course == null) {
sLog.warn("Course " + subjectArea + " " + courseNbr + " not found.");
} else {
if (!reqCourses.add(course)) {
sLog.warn("Course " + subjectArea + " " + courseNbr + " already requested.");
} else {
List<Course> courses = new ArrayList<Course>(1);
courses.add(course);
CourseRequest request = new CourseRequest(reqId++, priority++, false, student, courses, false, null);
List<Request> requestsThisCourse = requests.get(course);
if (requestsThisCourse == null) {
requestsThisCourse = new ArrayList<Request>();
requests.put(course, requestsThisCourse);
}
requestsThisCourse.add(request);
}
}
}
if (!student.getRequests().isEmpty())
model.addStudent(student);
}
for (Map.Entry<Course, List<Request>> entry : requests.entrySet()) {
Course course = entry.getKey();
List<Request> requestsThisCourse = entry.getValue();
double weight = getLastLikeStudentWeight(course, 0, requestsThisCourse.size());
for (Request request : requestsThisCourse) {
request.setWeight(weight);
}
}
} catch (Exception e) {
sLog.error(e.getMessage(), e);
}
}
Aggregations