use of org.cpsolver.studentsct.heuristics.selection.OnlineSelection 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;
}
Aggregations