Skip to content

Commit

Permalink
Student Scheduling: Backtracking
Browse files Browse the repository at this point in the history
- take the unassigned requests in the order by their priority, only shuffle requests with the same priority
  - critical requests or priority student first (based on solver config)
  - request priority (higher priority first, substitute request last)
- when failed, put the request at back to the queue after the requests with the same priority
  • Loading branch information
tomas-muller committed Nov 25, 2020
1 parent e97bdd5 commit 57d1903
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,11 @@
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.Comparator;
import java.util.ConcurrentModificationException;
import java.util.LinkedList;
import java.util.List;
import java.util.ListIterator;
import java.util.Map;

import org.cpsolver.ifs.assignment.Assignment;
Expand All @@ -25,6 +27,8 @@
import org.cpsolver.studentsct.model.Enrollment;
import org.cpsolver.studentsct.model.FreeTimeRequest;
import org.cpsolver.studentsct.model.Request;
import org.cpsolver.studentsct.model.Request.RequestPriority;
import org.cpsolver.studentsct.model.Student.StudentPriority;


/**
Expand Down Expand Up @@ -65,14 +69,17 @@ public class BacktrackSelection implements NeighbourSelection<Request, Enrollmen
protected long iNbrTimeoutReached = 0;
protected long iNbrNoSolution = 0;
protected StudentFilter iFilter = null;
protected RequestComparator iRequestComparator = null;

public BacktrackSelection(DataProperties properties) {
iIncludeAssignedRequests = properties.getPropertyBoolean("Neighbour.IncludeAssignedRequests", iIncludeAssignedRequests);
iRequestComparator = new RequestComparator(properties);
}

public void init(Solver<Request, Enrollment> solver, String name) {
List<Request> variables = new ArrayList<Request>(iIncludeAssignedRequests ? solver.currentSolution().getModel().variables() : solver.currentSolution().getModel().unassignedVariables(solver.currentSolution().getAssignment()));
Collections.shuffle(variables);
Collections.sort(variables, iRequestComparator);
iRequests = new LinkedList<Request>(variables);
if (iRBtNSel == null) {
try {
Expand Down Expand Up @@ -104,7 +111,19 @@ protected synchronized Request nextRequest() {
}

public synchronized void addRequest(Request request) {
if (iRequests != null && request != null && !request.getStudent().isDummy()) iRequests.addFirst(request);
if (iRequests != null && request != null && !request.getStudent().isDummy()) {
if (request.getStudent().getPriority().ordinal() < StudentPriority.Normal.ordinal() || request.getRequestPriority().ordinal() < RequestPriority.Normal.ordinal()) {
for (ListIterator<Request> i = iRequests.listIterator(); i.hasNext();) {
Request r = i.next();
if (iRequestComparator.compare(r, request) > 0) {
i.previous(); // go one back
i.add(request);
return;
}
}
}
iRequests.add(request);
}
}

@Override
Expand Down Expand Up @@ -173,4 +192,31 @@ public void neighbourFailed(Assignment<Request, Enrollment> assignment, long ite
if (neighbour instanceof BacktrackNeighbourSelection.BackTrackNeighbour)
addRequest(((BacktrackNeighbourSelection<Request, Enrollment>.BackTrackNeighbour)neighbour).getAssignments().get(0).getRequest());
}

public static class RequestComparator implements Comparator<Request> {
protected boolean iPreferPriorityStudents = true;
protected RequestComparator(DataProperties properties) {
iPreferPriorityStudents = properties.getPropertyBoolean("Sectioning.PriorityStudentsFirstSelection.AllIn", true);
}

@Override
public int compare(Request r1, Request r2) {
if (iPreferPriorityStudents) {
if (r1.getStudent().getPriority() != r2.getStudent().getPriority())
return r1.getStudent().getPriority().compareTo(r2.getStudent().getPriority());
if (r1.getRequestPriority() != r2.getRequestPriority())
return r1.getRequestPriority().compareTo(r2.getRequestPriority());
} else {
if (r1.getRequestPriority() != r2.getRequestPriority())
return r1.getRequestPriority().compareTo(r2.getRequestPriority());
if (r1.getRequestPriority() != r2.getRequestPriority())
return r1.getStudent().getPriority().compareTo(r2.getStudent().getPriority());
}
if (r1.isAlternative() != r2.isAlternative())
return r2.isAlternative() ? -1 : 1;
if (r1.getPriority() != r2.getPriority())
return r1.getPriority() < r2.getPriority() ? -1 : 1;
return 0;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ public void init(Solver<Request, Enrollment> solver, String name) {
for (Request r: (iIncludeAssignedRequests ? solver.currentSolution().getModel().variables() : solver.currentSolution().getModel().unassignedVariables(solver.currentSolution().getAssignment())))
if (iPriority.isCritical(r)) variables.add(r);
Collections.shuffle(variables);
Collections.sort(variables, iRequestComparator);
iRequests = new LinkedList<Request>(variables);
if (iRBtNSel == null) {
try {
Expand Down

0 comments on commit 57d1903

Please sign in to comment.