use of org.cpsolver.coursett.model.Lecture in project cpsolver by UniTime.
the class InstructorLunchBreak method getValue.
@Override
public double getValue(Assignment<Lecture, Placement> assignment, Collection<Lecture> variables) {
double lunchValue = 0.0d;
Set<InstructorConstraint> constraints = new HashSet<InstructorConstraint>();
for (Lecture lecture : variables) {
constraints.addAll(lecture.getInstructorConstraints());
}
for (InstructorConstraint instructor : constraints) {
lunchValue += ((InstructorLunchBreakContext) getContext(assignment)).getLunchPreference(assignment, instructor);
}
return lunchValue;
}
use of org.cpsolver.coursett.model.Lecture in project cpsolver by UniTime.
the class InstructorLunchBreak method getInfo.
@Override
public void getInfo(Assignment<Lecture, Placement> assignment, Map<String, String> info, Collection<Lecture> variables) {
Set<InstructorConstraint> constraints = new HashSet<InstructorConstraint>();
for (Lecture lecture : variables) {
for (InstructorConstraint c : lecture.getInstructorConstraints()) {
constraints.add(c);
}
}
Set<String> violatedLunchBreaks = new TreeSet<String>();
int lunchViolations = 0;
for (InstructorConstraint c : constraints) {
String days = "";
CompactInfo compactInfo = ((InstructorLunchBreakContext) getContext(assignment)).getCompactInfo(c);
for (int i = 0; i < Constants.NR_DAYS; i++) {
if (compactInfo.getLunchDayViolations()[i] > 0) {
if (iFullInfo)
days += (days.isEmpty() ? "" : ", ") + compactInfo.getLunchDayViolations()[i] + " × " + Constants.DAY_NAMES_SHORT[i];
lunchViolations += compactInfo.getLunchDayViolations()[i];
}
}
if (iFullInfo && !days.isEmpty())
violatedLunchBreaks.add(c.getName() + ": " + days);
}
if (lunchViolations > 0) {
info.put("Lunch breaks", getPerc(lunchViolations, 0, constraints.size() * Constants.NR_DAYS * getWeeks().size()) + "% (" + lunchViolations + ")");
if (iFullInfo && !violatedLunchBreaks.isEmpty()) {
String message = "";
for (String s : violatedLunchBreaks) message += (message.isEmpty() ? "" : "; ") + s;
info.put("Lunch break violations", message);
}
}
}
use of org.cpsolver.coursett.model.Lecture in project cpsolver by UniTime.
the class NeighbourSelectionWithSuggestions method backtrack.
private void backtrack(NeighbourSelectionWithSuggestionsContext context, List<Lecture> initialLectures, Map<Lecture, Placement> resolvedLectures, HashMap<Lecture, Placement> conflictsToResolve, int depth) {
int nrUnassigned = conflictsToResolve.size();
if ((initialLectures == null || initialLectures.isEmpty()) && nrUnassigned == 0) {
context.setSuggestionNeighbourIfImproving(resolvedLectures);
return;
}
if (depth <= 0 || context.checkTimeoutReached())
return;
Assignment<Lecture, Placement> assignment = context.getAssignment();
for (Lecture lecture : initialLectures != null && !initialLectures.isEmpty() ? initialLectures : new ArrayList<Lecture>(conflictsToResolve.keySet())) {
if (context.isTimeoutReached())
break;
if (resolvedLectures.containsKey(lecture))
continue;
placements: for (Placement placement : lecture.values(assignment)) {
if (context.isTimeoutReached())
break;
Placement cur = assignment.getValue(lecture);
if (placement.equals(cur))
continue;
if (placement.isHard(assignment))
continue;
Set<Placement> conflicts = context.getModel().conflictValues(assignment, placement);
if (nrUnassigned + conflicts.size() > depth)
continue;
if (conflicts.contains(placement))
continue;
if (containsCommited(context, conflicts))
continue;
for (Iterator<Placement> i = conflicts.iterator(); i.hasNext(); ) {
Placement c = i.next();
if (resolvedLectures.containsKey(c.variable()))
continue placements;
}
for (Iterator<Placement> i = conflicts.iterator(); i.hasNext(); ) {
Placement c = i.next();
assignment.unassign(0, c.variable());
}
assignment.assign(0, placement);
for (Iterator<Placement> i = conflicts.iterator(); i.hasNext(); ) {
Placement c = i.next();
conflictsToResolve.put(c.variable(), c);
}
Placement resolvedConf = conflictsToResolve.remove(lecture);
resolvedLectures.put(lecture, placement);
backtrack(context, null, resolvedLectures, conflictsToResolve, depth - 1);
resolvedLectures.remove(lecture);
if (cur == null)
assignment.unassign(0, lecture);
else
assignment.assign(0, cur);
for (Iterator<Placement> i = conflicts.iterator(); i.hasNext(); ) {
Placement p = i.next();
assignment.assign(0, p);
conflictsToResolve.remove(p.variable());
}
if (resolvedConf != null)
conflictsToResolve.put(lecture, resolvedConf);
}
}
}
use of org.cpsolver.coursett.model.Lecture in project cpsolver by UniTime.
the class MinimizeNumberOfUsedRoomsConstraint method computeConflicts.
@Override
public void computeConflicts(Assignment<Lecture, Placement> assignment, Placement placement, Set<Placement> conflicts) {
MinimizeNumberOfUsedRoomsConstraintContext context = getContext(assignment);
int overLimit = context.getOverLimit(assignment, placement);
if (overLimit > 0) {
List<List<Placement>> adepts = new ArrayList<List<Placement>>();
for (Set<Lecture> lects : context.getUsedRooms().values()) {
List<Placement> placementsToUnassign = new ArrayList<Placement>();
boolean canUnassign = true;
for (Lecture l : lects) {
if (l.isCommitted()) {
canUnassign = false;
break;
}
Placement p = assignment.getValue(l);
if (!conflicts.contains(p))
placementsToUnassign.add(p);
}
if (!canUnassign)
continue;
adepts.add(placementsToUnassign);
}
if (adepts.size() < overLimit) {
conflicts.add(placement);
} else {
Collections.sort(adepts, new Comparator<List<Placement>>() {
@Override
public int compare(List<Placement> c1, List<Placement> c2) {
return Double.compare(c1.size(), c2.size());
}
});
for (int i = 0; i < overLimit; i++) {
conflicts.addAll(adepts.get(i));
}
}
}
}
use of org.cpsolver.coursett.model.Lecture in project cpsolver by UniTime.
the class MinimizeNumberOfUsedRoomsConstraint method estimateLimit.
public int estimateLimit() {
HashSet<RoomLocation> mandatoryRooms = new HashSet<RoomLocation>();
for (Lecture lecture : variables()) {
if (lecture.getNrRooms() == 0)
continue;
if (lecture.isCommitted() || lecture.roomLocations().size() == 1)
mandatoryRooms.addAll(lecture.roomLocations());
}
double[][] histogram = new double[iLastDaySlot - iFirstDaySlot + 1][iLastWorkDay - iFirstWorkDay + 1];
for (int i = 0; i < iLastDaySlot - iFirstDaySlot + 1; i++) for (int j = 0; j < iLastWorkDay - iFirstWorkDay + 1; j++) histogram[i][j] = 0.0;
for (Lecture lecture : variables()) {
if (lecture.getNrRooms() == 0)
continue;
List<Placement> values = lecture.values(null);
for (Placement p : lecture.values(null)) {
int firstSlot = p.getTimeLocation().getStartSlot();
if (firstSlot > iLastDaySlot)
continue;
int endSlot = firstSlot + p.getTimeLocation().getNrSlotsPerMeeting() - 1;
if (endSlot < iFirstDaySlot)
continue;
for (int i = Math.max(firstSlot, iFirstDaySlot); i <= Math.min(endSlot, iLastDaySlot); i++) {
int dayCode = p.getTimeLocation().getDayCode();
for (int j = iFirstWorkDay; j <= iLastWorkDay; j++) {
if ((dayCode & Constants.DAY_CODES[j]) != 0) {
histogram[i - iFirstDaySlot][j - iFirstWorkDay] += ((double) lecture.getNrRooms()) / values.size();
}
}
}
}
}
int maxAverageRooms = 0;
for (int i = 0; i < iLastDaySlot - iFirstDaySlot + 1; i++) for (int j = 0; j < iLastWorkDay - iFirstWorkDay + 1; j++) maxAverageRooms = Math.max(maxAverageRooms, (int) Math.ceil(histogram[i][j]));
return Math.max(1, Math.max(mandatoryRooms.size(), maxAverageRooms));
}
Aggregations