use of org.cpsolver.exam.model.ExamRoomPlacement in project cpsolver by UniTime.
the class ExamTimeMove method selectNeighbour.
/**
* Select an exam randomly,
* select an available period randomly (if it is not assigned),
* use rooms if possible, select rooms using {@link Exam#findBestAvailableRooms(Assignment, ExamPeriodPlacement)} if not (exam is unassigned, a room is not available or used).
*/
@Override
public Neighbour<Exam, ExamPlacement> selectNeighbour(Solution<Exam, ExamPlacement> solution) {
ExamModel model = (ExamModel) solution.getModel();
Assignment<Exam, ExamPlacement> assignment = solution.getAssignment();
ExamRoomSharing sharing = model.getRoomSharing();
Exam exam = ToolBox.random(model.variables());
ExamPlacement placement = assignment.getValue(exam);
int px = ToolBox.random(exam.getPeriodPlacements().size());
for (int p = 0; p < exam.getPeriodPlacements().size(); p++) {
ExamPeriodPlacement period = exam.getPeriodPlacements().get((p + px) % exam.getPeriodPlacements().size());
if (placement != null && placement.getPeriod().equals(period))
continue;
if (iCheckStudentConflicts && exam.countStudentConflicts(assignment, period) > 0)
continue;
if (iCheckDistributionConstraints && !exam.checkDistributionConstraints(assignment, period))
continue;
if (placement != null) {
boolean ok = true;
if (sharing != null && placement.getRoomPlacements().size() == 1) {
ExamRoomPlacement room = placement.getRoomPlacements().iterator().next();
ok = room.isAvailable(period.getPeriod()) && !sharing.inConflict(exam, room.getRoom().getPlacements(assignment, period.getPeriod()), room.getRoom());
} else {
for (Iterator<ExamRoomPlacement> i = placement.getRoomPlacements().iterator(); i.hasNext(); ) {
ExamRoomPlacement room = i.next();
if (!room.isAvailable(period.getPeriod()) || !room.getRoom().getPlacements(assignment, period.getPeriod()).isEmpty()) {
ok = false;
break;
}
}
}
if (ok)
return new ExamSimpleNeighbour(assignment, new ExamPlacement(exam, period, placement.getRoomPlacements()));
}
Set<ExamRoomPlacement> rooms = exam.findBestAvailableRooms(assignment, period);
if (rooms == null)
continue;
return new ExamSimpleNeighbour(assignment, new ExamPlacement(exam, period, rooms));
}
return null;
}
use of org.cpsolver.exam.model.ExamRoomPlacement in project cpsolver by UniTime.
the class ExamRandomMove method selectNeighbour.
/**
* Select an exam randomly, select an available period randomly (from
* {@link Exam#getPeriodPlacements()}), select rooms using
* {@link Exam#findBestAvailableRooms(Assignment, ExamPeriodPlacement)}.
*/
@Override
public Neighbour<Exam, ExamPlacement> selectNeighbour(Solution<Exam, ExamPlacement> solution) {
ExamModel model = (ExamModel) solution.getModel();
Assignment<Exam, ExamPlacement> assignment = solution.getAssignment();
Exam exam = ToolBox.random(model.variables());
int px = ToolBox.random(exam.getPeriodPlacements().size());
for (int p = 0; p < exam.getPeriodPlacements().size(); p++) {
ExamPeriodPlacement period = exam.getPeriodPlacements().get((p + px) % exam.getPeriodPlacements().size());
if (iCheckStudentConflicts && exam.countStudentConflicts(assignment, period) > 0)
continue;
if (iCheckDistributionConstraints && !exam.checkDistributionConstraints(assignment, period))
continue;
Set<ExamRoomPlacement> rooms = exam.findBestAvailableRooms(assignment, period);
if (rooms == null)
continue;
return new ExamSimpleNeighbour(assignment, new ExamPlacement(exam, period, rooms));
}
return null;
}
use of org.cpsolver.exam.model.ExamRoomPlacement in project cpsolver by UniTime.
the class ExamCriterion method getBounds.
@Override
public double[] getBounds(Assignment<Exam, ExamPlacement> assignment, Collection<Exam> exams) {
double[] bounds = new double[] { 0.0, 0.0 };
for (Exam exam : exams) {
Double min = null, max = null;
for (ExamPeriodPlacement period : exam.getPeriodPlacements()) {
if (exam.getMaxRooms() == 0) {
double value = getValue(assignment, new ExamPlacement(exam, period, null), null);
if (min == null) {
min = value;
max = value;
continue;
}
min = Math.min(min, value);
max = Math.max(max, value);
} else {
for (ExamRoomPlacement room : exam.getRoomPlacements()) {
Set<ExamRoomPlacement> rooms = new HashSet<ExamRoomPlacement>();
rooms.add(room);
double value = getValue(assignment, new ExamPlacement(exam, period, rooms), null);
if (min == null) {
min = value;
max = value;
continue;
}
min = Math.min(min, value);
max = Math.max(max, value);
}
}
}
if (min != null) {
bounds[0] += min;
bounds[1] += max;
}
}
return bounds;
}
use of org.cpsolver.exam.model.ExamRoomPlacement in project cpsolver by UniTime.
the class RoomPerturbationPenalty method getValue.
@Override
public double getValue(Assignment<Exam, ExamPlacement> assignment, ExamPlacement value, Set<ExamPlacement> conflicts) {
if (!isMPP())
return 0;
Exam exam = value.variable();
ExamPlacement initial = exam.getInitialAssignment();
if (initial == null)
return 0;
int penalty = 0;
if (value.getRoomPlacements() != null)
for (ExamRoomPlacement rp : value.getRoomPlacements()) {
if (initial.getRoomPlacements() == null || !initial.getRoomPlacements().contains(rp))
penalty++;
}
return penalty;
}
use of org.cpsolver.exam.model.ExamRoomPlacement in project cpsolver by UniTime.
the class RoomSplitDistancePenalty method getValue.
@Override
public double getValue(Assignment<Exam, ExamPlacement> assignment, ExamPlacement value, Set<ExamPlacement> conflicts) {
if (value.getRoomPlacements() == null || value.getRoomPlacements().size() <= 1)
return 0.0;
double distance = 0.0;
for (ExamRoomPlacement r : value.getRoomPlacements()) {
for (ExamRoomPlacement w : value.getRoomPlacements()) {
if (r.getRoom().getId() < w.getRoom().getId())
distance += r.getRoom().getDistanceInMeters(w.getRoom());
}
}
int pairs = value.getRoomPlacements().size() * (value.getRoomPlacements().size() - 1) / 2;
return distance / pairs;
}
Aggregations