use of org.cpsolver.studentsct.model.Section 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.Section 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.Section in project cpsolver by UniTime.
the class StudentChoiceOrder method avgNrChoices.
/** Average number of choices for each student
* @param student given student
* @return average number of choices of the given student
**/
public double avgNrChoices(Student student) {
int nrRequests = 0;
int nrChoices = 0;
for (Request request : student.getRequests()) {
if (request instanceof CourseRequest) {
CourseRequest cr = (CourseRequest) request;
for (Course course : cr.getCourses()) {
for (Config config : course.getOffering().getConfigs()) {
Integer nrChoicesThisCfg = iCache.get(config);
if (nrChoicesThisCfg == null) {
nrChoicesThisCfg = new Integer(nrChoices(config, 0, new HashSet<Section>()));
iCache.put(config, nrChoicesThisCfg);
}
nrChoices += nrChoicesThisCfg.intValue();
}
}
nrRequests++;
}
}
return (nrRequests == 0 ? 0.0 : ((double) nrChoices) / nrRequests);
}
use of org.cpsolver.studentsct.model.Section in project cpsolver by UniTime.
the class StudentChoiceOrder method nrChoices.
private int nrChoices(Config config, int idx, HashSet<Section> sections) {
if (iFast) {
int nrChoices = 1;
for (Subpart subpart : config.getSubparts()) {
if (subpart.getChildren().isEmpty())
nrChoices *= subpart.getSections().size();
}
return nrChoices;
}
if (config.getSubparts().size() == idx) {
return 1;
} else {
Subpart subpart = config.getSubparts().get(idx);
int choicesThisSubpart = 0;
for (Section section : subpart.getSections()) {
if (section.getParent() != null && !sections.contains(section.getParent()))
continue;
if (section.isOverlapping(sections))
continue;
sections.add(section);
choicesThisSubpart += nrChoices(config, idx + 1, sections);
sections.remove(section);
}
return choicesThisSubpart;
}
}
use of org.cpsolver.studentsct.model.Section in project cpsolver by UniTime.
the class Test method getPercDisbalancedSections.
public double getPercDisbalancedSections(Assignment<Request, Enrollment> assignment, double perc) {
boolean balanceUnlimited = model().getProperties().getPropertyBoolean("General.BalanceUnlimited", false);
double disb10Sections = 0, nrSections = 0;
for (Offering offering : model().getOfferings()) {
for (Config config : offering.getConfigs()) {
double enrl = config.getEnrollmentTotalWeight(assignment, null);
for (Subpart subpart : config.getSubparts()) {
if (subpart.getSections().size() <= 1)
continue;
nrSections += subpart.getSections().size();
if (subpart.getLimit() > 0) {
// sections have limits -> desired size is section limit
// x (total enrollment / total limit)
double ratio = enrl / subpart.getLimit();
for (Section section : subpart.getSections()) {
double desired = ratio * section.getLimit();
if (Math.abs(desired - section.getEnrollmentTotalWeight(assignment, null)) >= Math.max(1.0, perc * section.getLimit()))
disb10Sections++;
}
} else if (balanceUnlimited) {
// enrollment / number of sections
for (Section section : subpart.getSections()) {
double desired = enrl / subpart.getSections().size();
if (Math.abs(desired - section.getEnrollmentTotalWeight(assignment, null)) >= Math.max(1.0, perc * desired))
disb10Sections++;
}
}
}
}
}
return 100.0 * disb10Sections / nrSections;
}
Aggregations