use of org.cpsolver.studentsct.model.Enrollment in project cpsolver by UniTime.
the class EnrollmentSelection method selectValue.
/** Value selection */
@Override
public Enrollment selectValue(Solution<Request, Enrollment> solution, Request selectedVariable) {
Assignment<Request, Enrollment> assignment = solution.getAssignment();
if (iMPP) {
if (selectedVariable.getInitialAssignment() != null) {
if (solution.getModel().unassignedVariables(assignment).isEmpty()) {
if (solution.getModel().perturbVariables(assignment).size() <= iMPPLimit)
iMPPLimit = solution.getModel().perturbVariables(assignment).size() - 1;
}
if (iMPPLimit >= 0 && solution.getModel().perturbVariables(assignment).size() > iMPPLimit) {
if (isAllowed(assignment, selectedVariable.getInitialAssignment()))
return selectedVariable.getInitialAssignment();
}
if (selectedVariable.getInitialAssignment() != null && ToolBox.random() <= iInitialSelectionProb) {
if (isAllowed(assignment, selectedVariable.getInitialAssignment()))
return selectedVariable.getInitialAssignment();
}
}
}
List<Enrollment> values = selectedVariable.values(assignment);
if (ToolBox.random() <= iRandomWalkProb) {
Enrollment value = ToolBox.random(values);
if (isAllowed(assignment, value))
return value;
}
if (iProp != null && assignment.getValue(selectedVariable) == null && ToolBox.random() <= iGoodSelectionProb) {
Set<Enrollment> goodValues = iProp.goodValues(assignment, selectedVariable);
if (!goodValues.isEmpty())
values = new ArrayList<Enrollment>(goodValues);
}
if (values.size() == 1) {
Enrollment value = values.get(0);
if (isAllowed(assignment, value))
return value;
else
return null;
}
List<Enrollment> bestValues = null;
double bestWeightedSum = 0;
for (Enrollment value : values) {
if (iTabu != null && iTabu.contains(value))
continue;
if (assignment.getValue(selectedVariable) != null && assignment.getValue(selectedVariable).equals(value))
continue;
Set<Enrollment> conf = solution.getModel().conflictValues(assignment, value);
if (conf.contains(value))
continue;
if (!isAllowed(assignment, value, conf))
continue;
double weightedConflicts = (iStat == null || iWeightWeightedCoflicts == 0.0 ? 0.0 : iStat.countRemovals(solution.getIteration(), conf, value));
double potentialConflicts = (iStat == null || iWeightPotentialConflicts == 0.0 ? 0.0 : iStat.countPotentialConflicts(assignment, solution.getIteration(), value, 3));
long deltaInitialAssignments = 0;
if (iMPP && iWeightDeltaInitialAssignment != 0.0) {
if (iViolatedInitials != null) {
Set<Enrollment> violations = iViolatedInitials.getViolatedInitials(value);
if (violations != null) {
for (Enrollment aValue : violations) {
if (assignment.getValue(aValue.variable()) == null || assignment.getValue(aValue.variable()).equals(aValue))
deltaInitialAssignments += 2;
}
}
}
for (Enrollment aValue : conf) {
if (aValue.variable().getInitialAssignment() != null)
deltaInitialAssignments--;
}
if (selectedVariable.getInitialAssignment() != null && !selectedVariable.getInitialAssignment().equals(value)) {
deltaInitialAssignments++;
}
if (iMPPLimit >= 0 && (solution.getModel().perturbVariables(assignment).size() + deltaInitialAssignments) > iMPPLimit)
continue;
}
double weightedSum = (iWeightDeltaInitialAssignment * deltaInitialAssignments) + (iWeightPotentialConflicts * potentialConflicts) + (iWeightWeightedCoflicts * weightedConflicts) + (iWeightCoflicts * conf.size()) + (iWeightValue * value.toDouble(assignment));
if (bestValues == null || bestWeightedSum > weightedSum) {
bestWeightedSum = weightedSum;
if (bestValues == null)
bestValues = new ArrayList<Enrollment>();
else
bestValues.clear();
bestValues.add(value);
} else {
if (bestWeightedSum == weightedSum)
bestValues.add(value);
}
}
Enrollment selectedValue = (bestValues == null ? null : ToolBox.random(bestValues));
if (selectedValue == null)
selectedValue = ToolBox.random(values);
if (iTabu != null) {
if (iTabu.size() == iTabuPos)
iTabu.add(selectedValue);
else
iTabu.set(iTabuPos, selectedValue);
iTabuPos = (iTabuPos + 1) % iTabuSize;
}
return (bestValues == null ? null : selectedValue);
}
use of org.cpsolver.studentsct.model.Enrollment 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<Request, Enrollment> 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();
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) {
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(assignment.getValue(request)))
return -1;
if (e2.equals(assignment.getValue(request)))
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();
}
}
use of org.cpsolver.studentsct.model.Enrollment in project cpsolver by UniTime.
the class BranchBoundSelection method selectNeighbour.
/**
* Select neighbour. All students are taken, one by one in a random order.
* For each student a branch & bound search is employed.
*/
@Override
public Neighbour<Request, Enrollment> selectNeighbour(Solution<Request, Enrollment> solution) {
Student student = null;
while ((student = nextStudent()) != null) {
Progress.getInstance(solution.getModel()).incProgress();
Neighbour<Request, Enrollment> neighbour = getSelection(solution.getAssignment(), student).select();
if (neighbour != null)
return neighbour;
}
return null;
}
use of org.cpsolver.studentsct.model.Enrollment in project cpsolver by UniTime.
the class ResectionIncompleteStudentsSelection method selectNeighbour.
/**
* Select neighbour. All students with an incomplete and non-empty schedule
* are taken, one by one in a random order. For each student a branch &
* bound search is employed.
*/
@Override
public Neighbour<Request, Enrollment> selectNeighbour(Solution<Request, Enrollment> solution) {
Student student = null;
while ((student = nextStudent()) != null) {
Progress.getInstance(solution.getModel()).incProgress();
if (student.nrAssignedRequests(solution.getAssignment()) == 0 || student.isComplete(solution.getAssignment()))
continue;
Neighbour<Request, Enrollment> neighbour = getSelection(solution.getAssignment(), student).select();
if (neighbour != null)
return neighbour;
}
return null;
}
use of org.cpsolver.studentsct.model.Enrollment in project cpsolver by UniTime.
the class StudentConflict method computeConflicts.
/**
* A given enrollment is conflicting when the student is enrolled into
* another course / free time request that has an assignment that is
* overlapping with one or more assignments of the given section. See
* {@link Enrollment#isOverlapping(Enrollment)} for more details. All such
* overlapping enrollments are added into the provided set of conflicts.
*
* @param enrollment
* {@link Enrollment} that is being considered
* @param conflicts
* resultant list of conflicting enrollments
*/
@Override
public void computeConflicts(Assignment<Request, Enrollment> assignment, Enrollment enrollment, Set<Enrollment> conflicts) {
// enrollment -> conflict
for (Request request : variables()) {
if (request.equals(enrollment.getRequest()))
continue;
Enrollment e = assignment.getValue(request);
if (e == null)
continue;
if (enrollment.isOverlapping(e))
conflicts.add(e);
}
// schedule) -> unassignd a lowest priority request
if (!enrollment.getAssignments().isEmpty() && !enrollment.getStudent().canAssign(assignment, enrollment.getRequest())) {
Enrollment lowestPriorityEnrollment = null;
int lowestPriority = -1;
for (Request request : variables()) {
if (request.equals(enrollment.getRequest()))
continue;
Enrollment e = assignment.getValue(request);
if (e == null)
continue;
if (lowestPriority < request.getPriority()) {
lowestPriority = request.getPriority();
lowestPriorityEnrollment = e;
}
}
if (lowestPriorityEnrollment != null)
conflicts.add(lowestPriorityEnrollment);
}
}
Aggregations