use of org.cpsolver.studentsct.model.Enrollment in project cpsolver by UniTime.
the class SwapStudentSelection method selectNeighbour.
/**
* For each student that does not have a complete schedule, try to find a
* request and a student that can be moved out of an enrollment so that the
* selected student can be assigned to the selected request.
*/
@Override
public Neighbour<Request, Enrollment> selectNeighbour(Solution<Request, Enrollment> solution) {
Student student = null;
while ((student = nextStudent()) != null) {
Progress.getInstance(solution.getModel()).incProgress();
if (student.isComplete(solution.getAssignment()) || student.nrAssignedRequests(solution.getAssignment()) == 0)
continue;
Selection selection = getSelection(solution.getAssignment(), student);
Neighbour<Request, Enrollment> neighbour = selection.select();
if (neighbour != null) {
addStudent(student);
return neighbour;
} else
iProblemStudents.addAll(selection.getProblemStudents());
}
return null;
}
use of org.cpsolver.studentsct.model.Enrollment in project cpsolver by UniTime.
the class SuggestionsBranchAndBound method values.
/**
* Domain of a request
* @param request given request
* @return possible enrollments (meeting the filter etc)
*/
protected List<Enrollment> values(final Request request) {
if (!request.getStudent().canAssign(iAssignment, request))
return new ArrayList<Enrollment>();
List<Enrollment> values = iValues.get(request);
if (values != null)
return values;
if (request instanceof CourseRequest) {
CourseRequest cr = (CourseRequest) request;
values = (cr.equals(iSelectedRequest) ? cr.getAvaiableEnrollments(iAssignment) : cr.getAvaiableEnrollmentsSkipSameTime(iAssignment));
Collections.sort(values, new Comparator<Enrollment>() {
@Override
public int compare(Enrollment e1, Enrollment e2) {
return iComparator.compare(iAssignment, e1, e2);
}
});
} else {
values = new ArrayList<Enrollment>();
values.add(((FreeTimeRequest) request).createEnrollment());
}
if (canLeaveUnassigned(request)) {
Config config = null;
if (request instanceof CourseRequest)
config = ((CourseRequest) request).getCourses().get(0).getOffering().getConfigs().get(0);
values.add(new Enrollment(request, 0, config, new HashSet<Section>(), iAssignment));
}
iValues.put(request, values);
if (request.equals(iSelectedRequest) && iFilter != null && request instanceof CourseRequest) {
for (Iterator<Enrollment> i = values.iterator(); i.hasNext(); ) {
Enrollment enrollment = i.next();
if (enrollment.getAssignments() != null && !enrollment.getAssignments().isEmpty()) {
boolean match = false;
for (Iterator<Section> j = enrollment.getSections().iterator(); j.hasNext(); ) {
Section section = j.next();
if (iSelectedSection != null) {
if (section.getSubpart().getId() == iSelectedSection.getSubpart().getId()) {
if (iFilter.match(enrollment.getCourse(), section)) {
match = true;
break;
}
}
if (section.getSubpart().getConfig().getId() != iSelectedSection.getSubpart().getConfig().getId() && section.getSubpart().getInstructionalType().equals(iSelectedSection.getSubpart().getInstructionalType())) {
if (iFilter.match(enrollment.getCourse(), section)) {
match = true;
break;
}
}
} else {
if (iFilter.match(enrollment.getCourse(), section)) {
match = true;
break;
}
}
}
if (!match)
i.remove();
}
}
}
if (request.equals(iSelectedRequest))
iMatched = values.size();
return values;
}
use of org.cpsolver.studentsct.model.Enrollment 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.Enrollment in project cpsolver by UniTime.
the class Test method main.
public static void main(String[] args) {
try {
System.setProperty("jprof", "cpu");
BasicConfigurator.configure();
DataProperties cfg = new DataProperties();
cfg.setProperty("Neighbour.BranchAndBoundTimeout", "5000");
cfg.setProperty("Suggestions.Timeout", "1000");
cfg.setProperty("Extensions.Classes", DistanceConflict.class.getName() + ";" + TimeOverlapsCounter.class.getName());
cfg.setProperty("StudentWeights.Class", StudentSchedulingAssistantWeights.class.getName());
cfg.setProperty("StudentWeights.PriorityWeighting", "true");
cfg.setProperty("StudentWeights.LeftoverSpread", "true");
cfg.setProperty("StudentWeights.BalancingFactor", "0.0");
cfg.setProperty("Reservation.CanAssignOverTheLimit", "true");
cfg.setProperty("Distances.Ellipsoid", DistanceMetric.Ellipsoid.WGS84.name());
cfg.setProperty("StudentWeights.MultiCriteria", "true");
cfg.setProperty("CourseRequest.SameTimePrecise", "true");
cfg.setProperty("log4j.rootLogger", "INFO, A1");
cfg.setProperty("log4j.appender.A1", "org.apache.log4j.ConsoleAppender");
cfg.setProperty("log4j.appender.A1.layout", "org.apache.log4j.PatternLayout");
cfg.setProperty("log4j.appender.A1.layout.ConversionPattern", "%-5p %c{2}: %m%n");
cfg.setProperty("log4j.logger.org.hibernate", "INFO");
cfg.setProperty("log4j.logger.org.hibernate.cfg", "WARN");
cfg.setProperty("log4j.logger.org.hibernate.cache.EhCacheProvider", "ERROR");
cfg.setProperty("log4j.logger.org.unitime.commons.hibernate", "INFO");
cfg.setProperty("log4j.logger.net", "INFO");
cfg.setProperty("Xml.LoadBest", "false");
cfg.setProperty("Xml.LoadCurrent", "false");
cfg.putAll(System.getProperties());
PropertyConfigurator.configure(cfg);
final Test test = new Test(cfg);
final File input = new File(args[0]);
StudentSectioningXMLLoader loader = new StudentSectioningXMLLoader(test.model(), test.assignment());
loader.setInputFile(input);
loader.load();
test.run();
Solver<Request, Enrollment> s = new Solver<Request, Enrollment>(cfg);
s.setInitalSolution(test.model());
StudentSectioningXMLSaver saver = new StudentSectioningXMLSaver(s);
File output = new File(input.getParentFile(), input.getName().substring(0, input.getName().lastIndexOf('.')) + "-" + cfg.getProperty("run", "r0") + ".xml");
saver.save(output);
test.stats(input);
} catch (Exception e) {
sLog.error("Test failed: " + e.getMessage(), e);
}
}
use of org.cpsolver.studentsct.model.Enrollment in project cpsolver by UniTime.
the class StudentPreferencePenalties method getMinMaxEnrollmentPenalty.
/** Minimal and maximal available enrollment penalty of a request
* @param request student course request
* @return minimal and maximal penalty
**/
public double[] getMinMaxEnrollmentPenalty(CourseRequest request) {
List<Enrollment> enrollments = request.values(new EmptyAssignment<Request, Enrollment>());
if (enrollments.isEmpty())
return new double[] { 0, 0 };
double min = Double.MAX_VALUE, max = Double.MIN_VALUE;
for (Enrollment enrollment : enrollments) {
double penalty = getPenalty(enrollment);
min = Math.min(min, penalty);
max = Math.max(max, penalty);
}
return new double[] { min, max };
}
Aggregations