use of org.cpsolver.exam.model.ExamPlacement in project cpsolver by UniTime.
the class ExamHillClimbing method selectNeighbour.
/**
* Select one of the given neighbourhoods randomly, select neighbour, return
* it if its value is below or equal to zero (continue with the next
* selection otherwise). Return null when the given number of idle
* iterations is reached.
*/
@Override
public Neighbour<Exam, ExamPlacement> selectNeighbour(Solution<Exam, ExamPlacement> solution) {
Context context = getContext(solution.getAssignment());
context.activateIfNeeded();
while (true) {
if (context.incIter(solution))
break;
NeighbourSelection<Exam, ExamPlacement> ns = iNeighbours.get(ToolBox.random(iNeighbours.size()));
Neighbour<Exam, ExamPlacement> n = ns.selectNeighbour(solution);
if (n != null) {
if (n instanceof LazyNeighbour) {
((LazyNeighbour<Exam, ExamPlacement>) n).setAcceptanceCriterion(this);
return n;
} else if (n.value(solution.getAssignment()) <= 0.0)
return n;
}
}
context.reset();
return null;
}
use of org.cpsolver.exam.model.ExamPlacement 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.ExamPlacement 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.ExamPlacement in project cpsolver by UniTime.
the class ExamStudentConflicts method report.
/**
* generate report
* @param assignment current assignment
* @return resultant report
*/
public CSVFile report(Assignment<Exam, ExamPlacement> assignment) {
CSVFile csv = new CSVFile();
csv.setHeader(new CSVField[] { new CSVField("Student"), new CSVField("Type"), new CSVField("Section/Course"), new CSVField("Period"), new CSVField("Day"), new CSVField("Time"), new CSVField("Room"), new CSVField("Distance") });
boolean isDayBreakBackToBack = ((StudentBackToBackConflicts) iModel.getCriterion(StudentBackToBackConflicts.class)).isDayBreakBackToBack();
double backToBackDistance = ((StudentDistanceBackToBackConflicts) iModel.getCriterion(StudentDistanceBackToBackConflicts.class)).getBackToBackDistance();
for (ExamStudent student : iModel.getStudents()) {
for (ExamPeriod period : iModel.getPeriods()) {
int nrExams = student.getExams(assignment, period).size();
if (nrExams > 1) {
String sections = "";
String rooms = "";
String periods = String.valueOf(period.getIndex() + 1);
String periodDays = period.getDayStr();
String periodTimes = period.getTimeStr();
for (Exam exam : student.getExams(assignment, period)) {
ExamPlacement placement = assignment.getValue(exam);
String roomsThisExam = "";
for (ExamRoomPlacement room : placement.getRoomPlacements()) {
if (roomsThisExam.length() > 0)
roomsThisExam += ", ";
roomsThisExam += room.getName();
}
boolean first = true;
for (ExamOwner cs : exam.getOwners(student)) {
if (sections.length() > 0) {
sections += "\n";
rooms += "\n";
periods += "\n";
periodDays += "\n";
periodTimes += "\n";
}
sections += cs.getName();
if (first)
rooms += roomsThisExam;
first = false;
}
if (exam.getOwners(student).isEmpty()) {
sections += exam.getName();
rooms += roomsThisExam;
}
}
csv.addLine(new CSVField[] { new CSVField(student.getName()), new CSVField("direct"), new CSVField(sections), new CSVField(periods), new CSVField(periodDays), new CSVField(periodTimes), new CSVField(rooms) });
}
if (nrExams > 0) {
if (period.next() != null && !student.getExams(assignment, period.next()).isEmpty() && (!isDayBreakBackToBack || period.next().getDay() == period.getDay())) {
for (Exam ex1 : student.getExams(assignment, period)) {
for (Exam ex2 : student.getExams(assignment, period.next())) {
ExamPlacement placement = assignment.getValue(ex1);
String sections = "";
String rooms = "";
String roomsThisExam = "";
String periods = String.valueOf(period.getIndex() + 1);
String periodDays = period.getDayStr();
String periodTimes = period.getTimeStr();
for (ExamRoomPlacement room : placement.getRoomPlacements()) {
if (roomsThisExam.length() > 0)
roomsThisExam += ", ";
roomsThisExam += room.getName();
}
boolean first = true;
for (ExamOwner cs : ex1.getOwners(student)) {
if (sections.length() > 0) {
sections += "\n";
rooms += "\n";
periods += "\n";
periodDays += "\n";
periodTimes += "\n";
}
sections += cs.getName();
if (first)
rooms += roomsThisExam;
first = false;
}
if (ex1.getOwners(student).isEmpty()) {
sections += ex1.getName();
rooms += roomsThisExam;
}
placement = assignment.getValue(ex2);
roomsThisExam = "";
for (ExamRoomPlacement room : placement.getRoomPlacements()) {
if (roomsThisExam.length() > 0)
roomsThisExam += ", ";
roomsThisExam += room.getName();
}
first = true;
for (ExamOwner cs : ex2.getOwners(student)) {
sections += "\n";
rooms += "\n";
periods += "\n";
periodDays += "\n";
periodTimes += "\n";
sections += cs.getName();
if (first) {
rooms += roomsThisExam;
periods += String.valueOf(period.next().getIndex() + 1);
periodDays += period.next().getDayStr();
periodTimes += period.next().getTimeStr();
}
first = false;
}
if (ex2.getOwners(student).isEmpty()) {
sections += "\n";
rooms += "\n";
periods += "\n";
periodDays += "\n";
periodTimes += "\n";
sections += ex2.getName();
rooms += roomsThisExam;
periods += String.valueOf(period.next().getIndex() + 1);
periodDays += period.next().getDayStr();
periodTimes += period.next().getTimeStr();
rooms += roomsThisExam;
}
String distStr = "";
if (backToBackDistance >= 0) {
double dist = (assignment.getValue(ex1)).getDistanceInMeters(assignment.getValue(ex2));
if (dist > 0)
distStr = String.valueOf(dist);
}
csv.addLine(new CSVField[] { new CSVField(student.getName()), new CSVField("back-to-back"), new CSVField(sections), new CSVField(periods), new CSVField(periodDays), new CSVField(periodTimes), new CSVField(rooms), new CSVField(distStr) });
}
}
}
}
if (period.next() == null || period.next().getDay() != period.getDay()) {
int nrExamsADay = student.getExamsADay(assignment, period.getDay()).size();
if (nrExamsADay > 2) {
String sections = "";
String periods = "";
String periodDays = "";
String periodTimes = "";
String rooms = "";
for (Exam exam : student.getExamsADay(assignment, period.getDay())) {
ExamPlacement placement = assignment.getValue(exam);
String roomsThisExam = "";
for (ExamRoomPlacement room : placement.getRoomPlacements()) {
if (roomsThisExam.length() > 0)
roomsThisExam += ", ";
roomsThisExam += room.getName();
}
boolean first = true;
for (ExamOwner cs : exam.getOwners(student)) {
if (sections.length() > 0) {
sections += "\n";
rooms += "\n";
periods += "\n";
periodDays += "\n";
periodTimes += "\n";
}
sections += cs.getName();
if (first) {
periods += (placement.getPeriod().getIndex() + 1);
periodDays += placement.getPeriod().getDayStr();
periodTimes += placement.getPeriod().getTimeStr();
rooms += roomsThisExam;
}
first = false;
}
if (exam.getOwners(student).isEmpty()) {
if (sections.length() > 0) {
sections += "\n";
rooms += "\n";
periods += "\n";
periodDays += "\n";
periodTimes += "\n";
}
sections += exam.getName();
periods += (placement.getPeriod().getIndex() + 1);
periodDays += placement.getPeriod().getDayStr();
periodTimes += placement.getPeriod().getTimeStr();
rooms += roomsThisExam;
}
}
csv.addLine(new CSVField[] { new CSVField(student.getName()), new CSVField("more-2-day"), new CSVField(sections), new CSVField(periods), new CSVField(periodDays), new CSVField(periodTimes), new CSVField(rooms) });
}
}
}
}
return csv;
}
use of org.cpsolver.exam.model.ExamPlacement in project cpsolver by UniTime.
the class ExamStudentConflictsBySectionCourse method report.
/**
* generate report
* @param assignment current assignment
* @return resultant report
*/
public CSVFile report(Assignment<Exam, ExamPlacement> assignment) {
CSVFile csv = new CSVFile();
csv.setHeader(new CSVField[] { new CSVField("Section/Course"), new CSVField("Period"), new CSVField("Day"), new CSVField("Time"), new CSVField("Room"), new CSVField("Student"), new CSVField("Type"), new CSVField("Section/Course"), new CSVField("Period"), new CSVField("Time"), new CSVField("Room"), new CSVField("Distance") });
boolean isDayBreakBackToBack = ((StudentBackToBackConflicts) iModel.getCriterion(StudentBackToBackConflicts.class)).isDayBreakBackToBack();
double backToBackDistance = ((StudentDistanceBackToBackConflicts) iModel.getCriterion(StudentDistanceBackToBackConflicts.class)).getBackToBackDistance();
TreeSet<ExamOwner> courseSections = new TreeSet<ExamOwner>();
for (Exam exam : iModel.variables()) {
courseSections.addAll(getOwners(exam));
}
for (ExamOwner cs : courseSections) {
Exam exam = cs.getExam();
ExamPlacement placement = assignment.getValue(exam);
if (placement == null)
continue;
String roomsThisExam = "";
for (ExamRoomPlacement room : placement.getRoomPlacements()) {
if (roomsThisExam.length() > 0)
roomsThisExam += ", ";
roomsThisExam += room.getName();
}
ExamPeriod period = placement.getPeriod();
boolean csPrinted = false;
List<ExamStudent> students = new ArrayList<ExamStudent>(cs.getStudents());
Collections.sort(students, new Comparator<ExamStudent>() {
@Override
public int compare(ExamStudent s1, ExamStudent s2) {
int cmp = s1.getName().compareTo(s2.getName());
if (cmp != 0)
return cmp;
return Double.compare(s1.getId(), s2.getId());
}
});
for (ExamStudent student : students) {
boolean stdPrinted = false;
int nrExams = student.getExams(assignment, period).size();
if (nrExams > 1) {
boolean typePrinted = false;
for (Exam otherExam : student.getExams(assignment, period)) {
if (otherExam.equals(exam))
continue;
ExamPlacement otherPlacement = assignment.getValue(otherExam);
ExamPeriod otherPeriod = otherPlacement.getPeriod();
String roomsOtherExam = "";
for (ExamRoomPlacement room : otherPlacement.getRoomPlacements()) {
if (roomsOtherExam.length() > 0)
roomsOtherExam += ", ";
roomsOtherExam += room.getName();
}
boolean otherPrinted = false;
for (ExamOwner ocs : getOwners(otherExam, student)) {
csv.addLine(new CSVField[] { new CSVField(csPrinted ? "" : cs.getName()), new CSVField(csPrinted ? "" : String.valueOf(1 + period.getIndex())), new CSVField(csPrinted ? "" : period.getDayStr()), new CSVField(csPrinted ? "" : period.getTimeStr()), new CSVField(csPrinted ? "" : roomsThisExam), new CSVField(stdPrinted ? "" : student.getName()), new CSVField(typePrinted ? "" : "direct"), new CSVField(ocs.getName()), new CSVField(otherPrinted ? "" : String.valueOf(1 + otherPeriod.getIndex())), new CSVField(otherPrinted ? "" : otherPeriod.getTimeStr()), new CSVField(otherPrinted ? "" : roomsOtherExam) });
csPrinted = true;
stdPrinted = true;
typePrinted = true;
otherPrinted = true;
}
}
}
if (nrExams > 0) {
boolean typePrinted = false;
List<ExamPeriod> periods = new ArrayList<ExamPeriod>(2);
if (period.prev() != null && !student.getExams(assignment, period.prev()).isEmpty() && (!isDayBreakBackToBack || period.prev().getDay() == period.getDay()))
periods.add(period.prev());
if (period.next() != null && !student.getExams(assignment, period.next()).isEmpty() && (!isDayBreakBackToBack || period.next().getDay() == period.getDay()))
periods.add(period.next());
for (ExamPeriod otherPeriod : periods) {
for (Exam otherExam : student.getExams(assignment, otherPeriod)) {
ExamPlacement otherPlacement = assignment.getValue(otherExam);
String roomsOtherExam = "";
for (ExamRoomPlacement room : otherPlacement.getRoomPlacements()) {
if (roomsOtherExam.length() > 0)
roomsOtherExam += ", ";
roomsOtherExam += room.getName();
}
String distStr = "";
if (backToBackDistance >= 0) {
double dist = placement.getDistanceInMeters(otherPlacement);
if (dist > 0)
distStr = String.valueOf(dist);
}
boolean otherPrinted = false;
for (ExamOwner ocs : getOwners(otherExam, student)) {
csv.addLine(new CSVField[] { new CSVField(csPrinted ? "" : cs.getName()), new CSVField(csPrinted ? "" : String.valueOf(1 + period.getIndex())), new CSVField(csPrinted ? "" : period.getDayStr()), new CSVField(csPrinted ? "" : period.getTimeStr()), new CSVField(csPrinted ? "" : roomsThisExam), new CSVField(stdPrinted ? "" : student.getName()), new CSVField(typePrinted ? "" : "back-to-back"), new CSVField(ocs.getName()), new CSVField(otherPrinted ? "" : String.valueOf(1 + otherPeriod.getIndex())), new CSVField(otherPrinted ? "" : otherPeriod.getTimeStr()), new CSVField(otherPrinted ? "" : roomsOtherExam), new CSVField(otherPrinted ? "" : distStr) });
csPrinted = true;
stdPrinted = true;
typePrinted = true;
otherPrinted = true;
}
}
}
}
int nrExamsADay = student.getExamsADay(assignment, period.getDay()).size();
if (nrExamsADay > 2) {
boolean typePrinted = false;
for (Exam otherExam : student.getExamsADay(assignment, period.getDay())) {
if (otherExam.equals(exam))
continue;
ExamPlacement otherPlacement = assignment.getValue(otherExam);
ExamPeriod otherPeriod = otherPlacement.getPeriod();
String roomsOtherExam = "";
for (ExamRoomPlacement room : otherPlacement.getRoomPlacements()) {
if (roomsOtherExam.length() > 0)
roomsOtherExam += ", ";
roomsOtherExam += room.getName();
}
boolean otherPrinted = false;
for (ExamOwner ocs : getOwners(otherExam, student)) {
csv.addLine(new CSVField[] { new CSVField(csPrinted ? "" : cs.getName()), new CSVField(csPrinted ? "" : String.valueOf(1 + period.getIndex())), new CSVField(csPrinted ? "" : period.getDayStr()), new CSVField(csPrinted ? "" : period.getTimeStr()), new CSVField(csPrinted ? "" : roomsThisExam), new CSVField(stdPrinted ? "" : student.getName()), new CSVField(typePrinted ? "" : "more-2-day"), new CSVField(ocs.getName()), new CSVField(otherPrinted ? "" : String.valueOf(1 + otherPeriod.getIndex())), new CSVField(otherPrinted ? "" : otherPeriod.getTimeStr()), new CSVField(otherPrinted ? "" : roomsOtherExam) });
csPrinted = true;
stdPrinted = true;
typePrinted = true;
otherPrinted = true;
}
}
}
}
}
return csv;
}
Aggregations