use of org.cpsolver.exam.model.ExamPlacement in project cpsolver by UniTime.
the class ExamUnassignedVariableSelection method selectVariable.
/** Variable selection */
@Override
public Exam selectVariable(Solution<Exam, ExamPlacement> solution) {
ExamModel model = (ExamModel) solution.getModel();
Assignment<Exam, ExamPlacement> assignment = solution.getAssignment();
if (model.variables().size() == assignment.nrAssignedVariables())
return null;
if (iRandomSelection) {
int idx = ToolBox.random(model.variables().size() - assignment.nrAssignedVariables());
for (Exam v : model.variables()) {
if (assignment.getValue(v) != null)
continue;
if (idx == 0)
return v;
idx--;
}
}
Exam variable = null;
for (Exam v : model.variables()) {
if (assignment.getValue(v) != null)
continue;
if (variable == null || v.compareTo(variable) < 0)
variable = v;
}
return variable;
}
use of org.cpsolver.exam.model.ExamPlacement in project cpsolver by UniTime.
the class ExamPeriodSwapMove 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();
Exam x1 = ToolBox.random(model.variables());
ExamPlacement v1 = assignment.getValue(x1);
if (v1 == null)
return null;
int x = ToolBox.random(model.variables().size());
for (int v = 0; v < model.variables().size(); v++) {
Exam x2 = model.variables().get((v + x) % (model.variables().size()));
ExamPlacement v2 = assignment.getValue(x2);
if (x1.equals(x2) || v2 == null)
continue;
ExamPeriodPlacement p1 = x1.getPeriodPlacement(v2.getPeriod());
ExamPeriodPlacement p2 = x2.getPeriodPlacement(v1.getPeriod());
if (p1 == null || p2 == null)
continue;
if (iCheckStudentConflicts && (x1.countStudentConflicts(assignment, p1) > 0 || x2.countStudentConflicts(assignment, p2) > 0))
continue;
if (iCheckDistributionConstraints) {
Map<Exam, ExamPlacement> placements = new HashMap<Exam, ExamPlacement>();
placements.put(x1, new ExamPlacement(x1, p1, new HashSet<ExamRoomPlacement>()));
placements.put(x2, new ExamPlacement(x2, p2, new HashSet<ExamRoomPlacement>()));
if (!checkDistributionConstraints(assignment, x1, p1, placements) || !checkDistributionConstraints(assignment, x2, p2, placements))
continue;
}
Set<ExamPlacement> conflicts = new HashSet<ExamPlacement>();
conflicts.add(v1);
conflicts.add(v2);
Map<Exam, ExamPlacement> placements = new HashMap<Exam, ExamPlacement>();
Set<ExamRoomPlacement> r1 = findBestAvailableRooms(assignment, x1, p1, conflicts, placements);
if (r1 == null)
continue;
placements.put(x1, new ExamPlacement(x1, p1, r1));
Set<ExamRoomPlacement> r2 = findBestAvailableRooms(assignment, x2, p2, conflicts, placements);
if (r2 == null)
continue;
return new LazySwap<Exam, ExamPlacement>(new ExamPlacement(x1, p1, r1), new ExamPlacement(x2, p2, r2));
}
return null;
}
use of org.cpsolver.exam.model.ExamPlacement 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.ExamPlacement 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.ExamPlacement in project cpsolver by UniTime.
the class ExamPeriodUsage method report.
/**
* generate report
* @param assignment current assignment
* @return resultant report
*/
public CSVFile report(Assignment<Exam, ExamPlacement> assignment) {
CSVFile csv = new CSVFile();
List<CSVField> header = new ArrayList<CSVField>();
header.add(new CSVField("Period"));
header.add(new CSVField("Date"));
header.add(new CSVField("Time"));
header.add(new CSVField("Weight"));
header.add(new CSVField("NrExams"));
header.add(new CSVField("Students"));
for (int i = 0; i < sLimits.length; i++) {
header.add(new CSVField("NrExams>=" + sLimits[i]));
}
header.add(new CSVField("AvgPeriod"));
header.add(new CSVField("WgAvgPeriod"));
csv.setHeader(header);
for (ExamPeriod period : iModel.getPeriods()) {
int nrExams = 0;
int nrStudents = 0;
int[] nrExamsLim = new int[sLimits.length];
int totAvgPer = 0, nrAvgPer = 0, totWgAvgPer = 0;
for (int i = 0; i < sLimits.length; i++) nrExamsLim[i] = 0;
for (Exam exam : iModel.variables()) {
ExamPlacement placement = assignment.getValue(exam);
if (placement == null || !(placement.getPeriod().equals(period)))
continue;
nrExams++;
nrStudents += exam.getStudents().size();
if (exam.getAveragePeriod() >= 0) {
totAvgPer += exam.getAveragePeriod();
nrAvgPer++;
totWgAvgPer += exam.getAveragePeriod() * exam.getStudents().size();
}
for (int i = 0; i < sLimits.length; i++) if (exam.getStudents().size() >= sLimits[i])
nrExamsLim[i]++;
}
List<CSVField> line = new ArrayList<CSVField>();
line.add(new CSVField(period.getIndex() + 1));
line.add(new CSVField(period.getDayStr()));
line.add(new CSVField(period.getTimeStr()));
line.add(new CSVField(period.getPenalty()));
line.add(new CSVField(nrExams));
line.add(new CSVField(nrStudents));
for (int i = 0; i < sLimits.length; i++) line.add(new CSVField(nrExamsLim[i]));
if (nrAvgPer > 0) {
line.add(new CSVField(sDF.format(((double) totAvgPer) / nrAvgPer)));
line.add(new CSVField(sDF.format(((double) totWgAvgPer) / nrAvgPer)));
}
csv.addLine(line);
}
return csv;
}
Aggregations