use of org.cpsolver.studentsct.model.CourseRequest in project cpsolver by UniTime.
the class DistanceConflict method countTotalNrConflicts.
/**
* Compute the actual number of all distance conflicts. Should be equal to
* {@link DistanceConflict#getTotalNrConflicts(Assignment)}.
* @param assignment current assignment
* @return computed number of all distance conflicts
*/
public int countTotalNrConflicts(Assignment<Request, Enrollment> assignment) {
int total = 0;
for (Request r1 : getModel().variables()) {
if (assignment.getValue(r1) == null || !(r1 instanceof CourseRequest))
continue;
Enrollment e1 = assignment.getValue(r1);
total += nrConflicts(e1);
for (Request r2 : r1.getStudent().getRequests()) {
Enrollment e2 = assignment.getValue(r2);
if (e2 == null || r1.getId() >= r2.getId() || !(r2 instanceof CourseRequest))
continue;
total += nrConflicts(e1, e2);
}
}
return total;
}
use of org.cpsolver.studentsct.model.CourseRequest in project cpsolver by UniTime.
the class DistanceConflict method computeAllConflicts.
/**
* Compute a set of all distance conflicts ({@link Conflict} objects).
* @param assignment current assignment
* @return computed set of all distance conflicts
*/
public Set<Conflict> computeAllConflicts(Assignment<Request, Enrollment> assignment) {
Set<Conflict> ret = new HashSet<Conflict>();
for (Request r1 : getModel().variables()) {
Enrollment e1 = assignment.getValue(r1);
if (e1 == null || !(r1 instanceof CourseRequest))
continue;
ret.addAll(conflicts(e1));
for (Request r2 : r1.getStudent().getRequests()) {
Enrollment e2 = assignment.getValue(r2);
if (e2 == null || r1.getId() >= r2.getId() || !(r2 instanceof CourseRequest))
continue;
ret.addAll(conflicts(e1, e2));
}
}
return ret;
}
use of org.cpsolver.studentsct.model.CourseRequest in project cpsolver by UniTime.
the class OnlineSelection method setPenalties.
/**
* Set online sectioning penalties to all sections of all courses of the
* given student
*/
private static void setPenalties(Assignment<Request, Enrollment> assignment, Student student) {
for (Request request : student.getRequests()) {
if (!(request instanceof CourseRequest))
continue;
CourseRequest courseRequest = (CourseRequest) request;
for (Course course : courseRequest.getCourses()) {
for (Config config : course.getOffering().getConfigs()) {
for (Subpart subpart : config.getSubparts()) {
for (Section section : subpart.getSections()) {
section.setPenalty(section.getOnlineSectioningPenalty(assignment));
}
}
}
}
courseRequest.clearCache();
}
}
use of org.cpsolver.studentsct.model.CourseRequest in project cpsolver by UniTime.
the class ShuffleStudentsSelection method init.
@Override
public void init(Solver<Request, Enrollment> solver) {
StudentSectioningModel model = (StudentSectioningModel) solver.currentSolution().getModel();
iQueue = new LinkedList<Shuffle>();
Assignment<Request, Enrollment> assignment = solver.currentSolution().getAssignment();
// Check all request groups that have a spread < 1.0
RouletteWheelSelection<RequestGroup> groups = new RouletteWheelSelection<RequestGroup>();
for (Offering offering : model.getOfferings()) {
for (Course course : offering.getCourses()) {
for (RequestGroup group : course.getRequestGroups()) {
double spread = group.getAverageSpread(solver.currentSolution().getAssignment());
if (spread >= 1.0)
continue;
groups.add(group, 1.0 - spread);
}
}
}
// If there are some, pick one randomly (using roulette wheel selection)
if (groups.hasMoreElements()) {
RequestGroup group = groups.nextElement();
RouletteWheelSelection<Subpart> subparts = new RouletteWheelSelection<Subpart>();
for (CourseRequest cr : group.getRequests()) {
Enrollment e = assignment.getValue(cr);
if (e != null)
for (Section section : e.getSections()) if (group.getSectionSpread(assignment, section) < 1.0)
subparts.addExisting(section.getSubpart(), 1.0);
}
if (subparts.hasMoreElements()) {
// Pick a subpart that has sections with a section spread < 1.0
Subpart subpart = subparts.nextElement();
RouletteWheelSelection<Section> sections = new RouletteWheelSelection<Section>();
section: for (Section section : subpart.getSections()) {
// Only take sections that all requests can use
for (CourseRequest cr : group.getRequests()) {
boolean match = false;
for (Enrollment e : cr.values(assignment)) if (e.getSections().contains(section)) {
match = true;
break;
}
if (!match)
continue section;
}
// Take sections with conflicts with lower probability
int nrConflicts = 0;
if (!section.isAllowOverlap())
requests: for (CourseRequest cr : group.getRequests()) {
for (Request r : cr.getStudent().getRequests()) {
if (r.equals(cr))
continue;
Enrollment e = assignment.getValue(r);
if (e != null && !e.isAllowOverlap() && section.isOverlapping(e.getSections())) {
nrConflicts++;
continue requests;
}
}
}
sections.add(section, 1 + group.getRequests().size() - nrConflicts);
}
Set<Section> filter = new HashSet<Section>();
double space = 0.0;
// Pick enough sections
while (sections.hasMoreElements()) {
Section section = sections.nextElement();
if (filter.add(section)) {
if (section.getLimit() < 0)
break;
space += section.getLimit();
}
if (space >= group.getTotalWeight())
break;
}
// Add all requests that should be moved into the queue
for (CourseRequest cr : group.getRequests()) {
Shuffle shuffle = new Shuffle(group, cr, filter);
Enrollment e = assignment.getValue(cr);
if (e != null && shuffle.matchFilter(e))
continue;
iQueue.add(shuffle);
}
} else {
// No subpart -> no section filter
for (CourseRequest cr : group.getRequests()) iQueue.add(new Shuffle(group, cr, null));
}
}
// Shuffle the queue
Collections.shuffle((LinkedList<Shuffle>) iQueue);
// Initialize the backtrack selection, if needed
if (iBacktrack == null) {
try {
iBacktrack = new ShuffleBacktrackNeighbourSelection(solver.getProperties());
iBacktrack.init(solver);
} catch (Exception e) {
throw new RuntimeException(e.getMessage(), e);
}
}
// Change progress
Progress.getInstance(solver.currentSolution().getModel()).setPhase("Shuffling students along request groups...", iQueue.size());
}
use of org.cpsolver.studentsct.model.CourseRequest in project cpsolver by UniTime.
the class RandomizedBacktrackNeighbourSelection method values.
/**
* List of values of a variable.
* {@link CourseRequest#computeRandomEnrollments(Assignment, int)} with the provided
* limit is used for a {@link CourseRequest}.
*/
@Override
protected Iterator<Enrollment> values(BacktrackNeighbourSelection<Request, Enrollment>.BacktrackNeighbourSelectionContext context, Request variable) {
if (variable instanceof CourseRequest) {
final CourseRequest request = (CourseRequest) variable;
final StudentSectioningModel model = (StudentSectioningModel) context.getModel();
final Assignment<Request, Enrollment> assignment = context.getAssignment();
final Enrollment current = assignment.getValue(request);
List<Enrollment> values = (iMaxValues > 0 ? request.computeRandomEnrollments(assignment, iMaxValues) : request.computeEnrollments(assignment));
Collections.sort(values, new Comparator<Enrollment>() {
private HashMap<Enrollment, Double> iValues = new HashMap<Enrollment, Double>();
private Double value(Enrollment e) {
Double value = iValues.get(e);
if (value == null) {
if (model.getStudentQuality() != null)
value = model.getStudentWeights().getWeight(assignment, e, model.getStudentQuality().conflicts(e));
else
value = model.getStudentWeights().getWeight(assignment, e, (model.getDistanceConflict() == null ? null : model.getDistanceConflict().conflicts(e)), (model.getTimeOverlaps() == null ? null : model.getTimeOverlaps().conflicts(e)));
iValues.put(e, value);
}
return value;
}
@Override
public int compare(Enrollment e1, Enrollment e2) {
if (e1.equals(e2))
return 0;
if (e1.equals(current))
return -1;
if (e2.equals(current))
return 1;
Double v1 = value(e1), v2 = value(e2);
return v1.equals(v2) ? e1.compareTo(assignment, e2) : v2.compareTo(v1);
}
});
return values.iterator();
} else {
return variable.computeEnrollments(context.getAssignment()).iterator();
}
}
Aggregations