use of org.cpsolver.studentsct.model.Student in project cpsolver by UniTime.
the class Test method fixPriorities.
public static void fixPriorities(StudentSectioningModel model) {
for (Student student : model.getStudents()) {
Collections.sort(student.getRequests(), new Comparator<Request>() {
@Override
public int compare(Request r1, Request r2) {
int cmp = Double.compare(r1.getPriority(), r2.getPriority());
if (cmp != 0)
return cmp;
return Double.compare(r1.getId(), r2.getId());
}
});
int priority = 0;
for (Request request : student.getRequests()) {
if (priority != request.getPriority()) {
sLog.debug("Change priority of " + request + " to " + priority);
request.setPriority(priority);
}
}
}
}
use of org.cpsolver.studentsct.model.Student in project cpsolver by UniTime.
the class Test method onlineSectioning.
/** Online sectioning test
* @param cfg solver configuration
* @return resultant solution
* @throws Exception thrown when the sectioning fails
**/
public static Solution<Request, Enrollment> onlineSectioning(DataProperties cfg) throws Exception {
Solution<Request, Enrollment> solution = load(cfg);
if (solution == null)
return null;
StudentSectioningModel model = (StudentSectioningModel) solution.getModel();
Assignment<Request, Enrollment> assignment = solution.getAssignment();
solution.addSolutionListener(new TestSolutionListener());
double startTime = JProf.currentTimeSec();
Solver<Request, Enrollment> solver = new Solver<Request, Enrollment>(cfg);
solver.setInitalSolution(solution);
solver.initSolver();
OnlineSelection onlineSelection = new OnlineSelection(cfg);
onlineSelection.init(solver);
double totalPenalty = 0, minPenalty = 0, maxPenalty = 0;
double minAvEnrlPenalty = 0, maxAvEnrlPenalty = 0;
double totalPrefPenalty = 0, minPrefPenalty = 0, maxPrefPenalty = 0;
double minAvEnrlPrefPenalty = 0, maxAvEnrlPrefPenalty = 0;
int nrChoices = 0, nrEnrollments = 0, nrCourseRequests = 0;
int chChoices = 0, chCourseRequests = 0, chStudents = 0;
int choiceLimit = model.getProperties().getPropertyInt("Test.ChoicesLimit", -1);
File outDir = new File(model.getProperties().getProperty("General.Output", "."));
outDir.mkdirs();
PrintWriter pw = new PrintWriter(new FileWriter(new File(outDir, "choices.csv")));
List<Student> students = model.getStudents();
try {
@SuppressWarnings("rawtypes") Class studentOrdClass = Class.forName(model.getProperties().getProperty("Test.StudentOrder", StudentRandomOrder.class.getName()));
@SuppressWarnings("unchecked") StudentOrder studentOrd = (StudentOrder) studentOrdClass.getConstructor(new Class[] { DataProperties.class }).newInstance(new Object[] { model.getProperties() });
students = studentOrd.order(model.getStudents());
} catch (Exception e) {
sLog.error("Unable to reorder students, reason: " + e.getMessage(), e);
}
ShutdownHook hook = new ShutdownHook(solver);
Runtime.getRuntime().addShutdownHook(hook);
for (Student student : students) {
if (student.nrAssignedRequests(assignment) > 0)
// skip students with assigned courses (i.e., students
continue;
// already assigned by a batch sectioning process)
sLog.info("Sectioning student: " + student);
BranchBoundSelection.Selection selection = onlineSelection.getSelection(assignment, student);
BranchBoundNeighbour neighbour = selection.select();
if (neighbour != null) {
StudentPreferencePenalties penalties = null;
if (selection instanceof OnlineSelection.EpsilonSelection) {
OnlineSelection.EpsilonSelection epsSelection = (OnlineSelection.EpsilonSelection) selection;
penalties = epsSelection.getPenalties();
for (int i = 0; i < neighbour.getAssignment().length; i++) {
Request r = student.getRequests().get(i);
if (r instanceof CourseRequest) {
nrCourseRequests++;
chCourseRequests++;
int chChoicesThisRq = 0;
CourseRequest request = (CourseRequest) r;
for (Enrollment x : request.getAvaiableEnrollments(assignment)) {
nrEnrollments++;
if (epsSelection.isAllowed(i, x)) {
nrChoices++;
if (choiceLimit <= 0 || chChoicesThisRq < choiceLimit) {
chChoices++;
chChoicesThisRq++;
}
}
}
}
}
chStudents++;
if (chStudents == 100) {
pw.println(sDF.format(((double) chChoices) / chCourseRequests));
pw.flush();
chStudents = 0;
chChoices = 0;
chCourseRequests = 0;
}
}
for (int i = 0; i < neighbour.getAssignment().length; i++) {
if (neighbour.getAssignment()[i] == null)
continue;
Enrollment enrollment = neighbour.getAssignment()[i];
if (enrollment.getRequest() instanceof CourseRequest) {
CourseRequest request = (CourseRequest) enrollment.getRequest();
double[] avEnrlMinMax = getMinMaxAvailableEnrollmentPenalty(assignment, request);
minAvEnrlPenalty += avEnrlMinMax[0];
maxAvEnrlPenalty += avEnrlMinMax[1];
totalPenalty += enrollment.getPenalty();
minPenalty += request.getMinPenalty();
maxPenalty += request.getMaxPenalty();
if (penalties != null) {
double[] avEnrlPrefMinMax = penalties.getMinMaxAvailableEnrollmentPenalty(assignment, enrollment.getRequest());
minAvEnrlPrefPenalty += avEnrlPrefMinMax[0];
maxAvEnrlPrefPenalty += avEnrlPrefMinMax[1];
totalPrefPenalty += penalties.getPenalty(enrollment);
minPrefPenalty += penalties.getMinPenalty(enrollment.getRequest());
maxPrefPenalty += penalties.getMaxPenalty(enrollment.getRequest());
}
}
}
neighbour.assign(assignment, solution.getIteration());
sLog.info("Student " + student + " enrolls into " + neighbour);
onlineSelection.updateSpace(assignment, student);
} else {
sLog.warn("No solution found.");
}
solution.update(JProf.currentTimeSec() - startTime);
}
if (chCourseRequests > 0)
pw.println(sDF.format(((double) chChoices) / chCourseRequests));
pw.flush();
pw.close();
HashMap<String, String> extra = new HashMap<String, String>();
sLog.info("Overall penalty is " + getPerc(totalPenalty, minPenalty, maxPenalty) + "% (" + sDF.format(totalPenalty) + "/" + sDF.format(minPenalty) + ".." + sDF.format(maxPenalty) + ")");
extra.put("Overall penalty", getPerc(totalPenalty, minPenalty, maxPenalty) + "% (" + sDF.format(totalPenalty) + "/" + sDF.format(minPenalty) + ".." + sDF.format(maxPenalty) + ")");
extra.put("Overall available enrollment penalty", getPerc(totalPenalty, minAvEnrlPenalty, maxAvEnrlPenalty) + "% (" + sDF.format(totalPenalty) + "/" + sDF.format(minAvEnrlPenalty) + ".." + sDF.format(maxAvEnrlPenalty) + ")");
if (onlineSelection.isUseStudentPrefPenalties()) {
sLog.info("Overall preference penalty is " + getPerc(totalPrefPenalty, minPrefPenalty, maxPrefPenalty) + "% (" + sDF.format(totalPrefPenalty) + "/" + sDF.format(minPrefPenalty) + ".." + sDF.format(maxPrefPenalty) + ")");
extra.put("Overall preference penalty", getPerc(totalPrefPenalty, minPrefPenalty, maxPrefPenalty) + "% (" + sDF.format(totalPrefPenalty) + "/" + sDF.format(minPrefPenalty) + ".." + sDF.format(maxPrefPenalty) + ")");
extra.put("Overall preference available enrollment penalty", getPerc(totalPrefPenalty, minAvEnrlPrefPenalty, maxAvEnrlPrefPenalty) + "% (" + sDF.format(totalPrefPenalty) + "/" + sDF.format(minAvEnrlPrefPenalty) + ".." + sDF.format(maxAvEnrlPrefPenalty) + ")");
extra.put("Average number of choices", sDF.format(((double) nrChoices) / nrCourseRequests) + " (" + nrChoices + "/" + nrCourseRequests + ")");
extra.put("Average number of enrollments", sDF.format(((double) nrEnrollments) / nrCourseRequests) + " (" + nrEnrollments + "/" + nrCourseRequests + ")");
}
hook.setExtra(extra);
return solution;
}
use of org.cpsolver.studentsct.model.Student in project cpsolver by UniTime.
the class Test method fixWeights.
private static void fixWeights(StudentSectioningModel model) {
HashMap<Course, Integer> lastLike = new HashMap<Course, Integer>();
HashMap<Course, Integer> real = new HashMap<Course, Integer>();
HashSet<Long> lastLikeIds = new HashSet<Long>();
HashSet<Long> realIds = new HashSet<Long>();
for (Student student : model.getStudents()) {
if (student.isDummy()) {
if (!lastLikeIds.add(new Long(student.getId()))) {
sLog.error("Two last-like student with id " + student.getId());
}
} else {
if (!realIds.add(new Long(student.getId()))) {
sLog.error("Two real student with id " + student.getId());
}
}
for (Request request : student.getRequests()) {
if (request instanceof CourseRequest) {
CourseRequest courseRequest = (CourseRequest) request;
Course course = courseRequest.getCourses().get(0);
Integer cnt = (student.isDummy() ? lastLike : real).get(course);
(student.isDummy() ? lastLike : real).put(course, new Integer((cnt == null ? 0 : cnt.intValue()) + 1));
}
}
}
for (Student student : new ArrayList<Student>(model.getStudents())) {
if (student.isDummy() && realIds.contains(new Long(student.getId()))) {
sLog.warn("There is both last-like and real student with id " + student.getId());
long newId = -1;
while (true) {
newId = 1 + (long) (999999999L * Math.random());
if (!realIds.contains(new Long(newId)) && !lastLikeIds.contains(new Long(newId)))
break;
}
lastLikeIds.remove(new Long(student.getId()));
lastLikeIds.add(new Long(newId));
student.setId(newId);
sLog.warn(" -- last-like student id changed to " + student.getId());
}
for (Request request : new ArrayList<Request>(student.getRequests())) {
if (!student.isDummy()) {
request.setWeight(1.0);
continue;
}
if (request instanceof CourseRequest) {
CourseRequest courseRequest = (CourseRequest) request;
Course course = courseRequest.getCourses().get(0);
Integer lastLikeCnt = lastLike.get(course);
Integer realCnt = real.get(course);
courseRequest.setWeight(getLastLikeStudentWeight(course, realCnt == null ? 0 : realCnt.intValue(), lastLikeCnt == null ? 0 : lastLikeCnt.intValue()));
} else
request.setWeight(1.0);
if (request.getWeight() <= 0.0) {
model.removeVariable(request);
student.getRequests().remove(request);
}
}
if (student.getRequests().isEmpty()) {
model.getStudents().remove(student);
}
}
}
use of org.cpsolver.studentsct.model.Student in project cpsolver by UniTime.
the class StudentSectioningModel method avgNrRequests.
/**
* Average number of requests per student (see {@link Student#getRequests()}
* )
* @return average number of requests per student
*/
public double avgNrRequests() {
double totalRequests = 0.0;
int totalStudents = 0;
for (Student student : getStudents()) {
if (student.nrRequests() == 0)
continue;
totalRequests += student.nrRequests();
totalStudents++;
}
return totalRequests / totalStudents;
}
use of org.cpsolver.studentsct.model.Student in project cpsolver by UniTime.
the class StudentSectioningXMLLoader method loadStudent.
/**
* Load student
* @param studentEl student element
* @param offeringTable offering table
* @return loaded student
*/
protected Student loadStudent(Element studentEl, Map<Long, Offering> offeringTable) {
Student student = new Student(Long.parseLong(studentEl.attributeValue("id")), "true".equals(studentEl.attributeValue("dummy")));
student.setExternalId(studentEl.attributeValue("externalId"));
student.setName(studentEl.attributeValue("name"));
student.setStatus(studentEl.attributeValue("status"));
for (Iterator<?> j = studentEl.elementIterator(); j.hasNext(); ) {
Element requestEl = (Element) j.next();
if ("classification".equals(requestEl.getName())) {
student.getAcademicAreaClasiffications().add(new AcademicAreaCode(requestEl.attributeValue("area"), requestEl.attributeValue("code")));
} else if ("major".equals(requestEl.getName())) {
student.getMajors().add(new AcademicAreaCode(requestEl.attributeValue("area"), requestEl.attributeValue("code")));
} else if ("minor".equals(requestEl.getName())) {
student.getMinors().add(new AcademicAreaCode(requestEl.attributeValue("area"), requestEl.attributeValue("code")));
} else if ("unavailability".equals(requestEl.getName())) {
Offering offering = offeringTable.get(Long.parseLong(requestEl.attributeValue("offering")));
Section section = (offering == null ? null : offering.getSection(Long.parseLong(requestEl.attributeValue("section"))));
if (section != null)
new Unavailability(student, section, "true".equals(requestEl.attributeValue("allowOverlap")));
} else if ("acm".equals(requestEl.getName())) {
student.getAreaClassificationMajors().add(new AreaClassificationMajor(requestEl.attributeValue("area"), requestEl.attributeValue("classification"), requestEl.attributeValue("major")));
}
}
return student;
}
Aggregations