use of org.cpsolver.coursett.model.Placement in project cpsolver by UniTime.
the class SpreadConstraint method computeConflicts.
@Override
public void computeConflicts(Assignment<Lecture, Placement> assignment, Placement placement, Set<Placement> conflicts) {
SpreadConstraintContext context = getContext(assignment);
if (context.getUnassignmentsToWeaken() == 0)
return;
int penalty = context.getCurrentPenalty() + getPenalty(assignment, placement);
if (penalty <= context.getMaxAllowedPenalty())
return;
int firstSlot = placement.getTimeLocation().getStartSlot();
if (firstSlot > iLastDaySlot)
return;
int endSlot = firstSlot + placement.getTimeLocation().getNrSlotsPerMeeting() - 1;
if (endSlot < iFirstDaySlot)
return;
// sLogger.debug("-- computing conflict for value "+value+" ... (penalty="+iCurrentPenalty+", penalty with the value="+penalty+", max="+iMaxAllowedPenalty+")");
int[][] nrCourses = new int[iLastDaySlot - iFirstDaySlot + 1][iLastWorkDay - iFirstWorkDay + 1];
for (int i = 0; i < iLastDaySlot - iFirstDaySlot + 1; i++) for (int j = 0; j < iLastWorkDay - iFirstWorkDay + 1; j++) nrCourses[i][j] = context.getNrCourses(i + iFirstDaySlot, j + iFirstWorkDay, placement);
tryAssign(assignment, placement, nrCourses);
// sLogger.debug(" -- nrCurses="+fmt(nrCourses));
for (Lecture lect : variables()) {
if (lect.equals(placement.variable()))
continue;
if (conflicts.contains(lect)) {
penalty -= tryUnassign(assignment, assignment.getValue(lect), nrCourses);
}
if (penalty <= context.getMaxAllowedPenalty())
return;
}
if (USE_MOST_IMPROVEMENT_ADEPTS) {
while (penalty > context.getMaxAllowedPenalty()) {
Placement plac = getAdept(assignment, placement, nrCourses, conflicts);
if (plac == null)
break;
conflicts.add(plac);
penalty -= tryUnassign(assignment, plac, nrCourses);
}
} else {
if (penalty > context.getMaxAllowedPenalty()) {
Set<Placement>[] adepts = getAdepts(assignment, placement, nrCourses, conflicts);
for (int i = 0; penalty > context.getMaxAllowedPenalty() && i < adepts.length; i++) {
while (!adepts[i].isEmpty() && penalty > context.getMaxAllowedPenalty()) {
Placement plac = ToolBox.random(adepts[i]);
adepts[i].remove(plac);
conflicts.add(plac);
// sLogger.debug(" -- conflict "+lect.getAssignment()+" added");
penalty -= tryUnassign(assignment, plac, nrCourses);
}
}
}
}
}
use of org.cpsolver.coursett.model.Placement in project cpsolver by UniTime.
the class JenrlConstraint method jenrl.
/**
* Number of joined enrollments if the given value is assigned to the given
* variable
* @param assignment current assignment
* @param variable a class
* @param value class placement under consideration
* @return number of student conflicts caused by this constraint if assigned
*/
public long jenrl(Assignment<Lecture, Placement> assignment, Lecture variable, Placement value) {
Lecture other = (first().equals(variable) ? second() : first());
Placement otherPlacement = (other == null ? null : assignment.getValue(other));
return (otherPlacement != null && isInConflict(value, otherPlacement, getDistanceMetric()) ? Math.round(iJenrl) : 0);
}
use of org.cpsolver.coursett.model.Placement in project cpsolver by UniTime.
the class RoomConstraint method computeConflicts.
@Override
public void computeConflicts(Assignment<Lecture, Placement> assignment, Placement placement, Set<Placement> conflicts) {
if (!getConstraint())
return;
if (!placement.hasRoomLocation(getResourceId()))
return;
Lecture lecture = placement.variable();
Placement current = assignment.getValue(lecture);
boolean canShareRoom = lecture.canShareRoom();
int size = lecture.maxRoomUse();
HashSet<Placement> skipPlacements = null;
BitSet weekCode = placement.getTimeLocation().getWeekCode();
RoomConstraintContext context = getContext(assignment);
for (Enumeration<Integer> e = placement.getTimeLocation().getSlots(); e.hasMoreElements(); ) {
int slot = e.nextElement();
for (Placement confPlacement : context.getPlacements(slot)) {
if (!confPlacement.getTimeLocation().shareWeeks(weekCode))
continue;
if (confPlacement.equals(current))
continue;
Lecture confLecture = confPlacement.variable();
if (skipPlacements != null && skipPlacements.contains(confPlacement))
continue;
if (canShareRoom && confPlacement.canShareRooms(placement) && confLecture.maxRoomUse() + size <= getCapacity()) {
size += confLecture.maxRoomUse();
if (skipPlacements == null)
skipPlacements = new HashSet<Placement>();
skipPlacements.add(confPlacement);
continue;
}
conflicts.add(confPlacement);
}
}
}
use of org.cpsolver.coursett.model.Placement in project cpsolver by UniTime.
the class TimeSwap method resolve.
@Override
public Double resolve(Solution<Lecture, Placement> solution, double total, long startTime, Map<Lecture, Placement> assignments, List<Placement> conflicts, int index) {
Assignment<Lecture, Placement> assignment = solution.getAssignment();
if (index == conflicts.size())
return solution.getModel().getTotalValue(assignment) - total;
Placement conflict = conflicts.get(index);
Lecture variable = conflict.variable();
List<TimeLocation> values = variable.timeLocations();
if (values.isEmpty())
return null;
int valIdx = ToolBox.random(values.size());
int attempts = 0;
for (int i = 0; i < values.size(); i++) {
TimeLocation time = values.get((i + valIdx) % values.size());
if (time.getPreference() > 50)
continue;
if (time.equals(conflict.getTimeLocation()))
continue;
Placement value = null;
if (variable.getNrRooms() == 0)
value = new Placement(variable, time, (RoomLocation) null);
else if (variable.getNrRooms() == 1)
value = new Placement(variable, time, conflict.getRoomLocation());
else
value = new Placement(variable, time, conflict.getRoomLocations());
if (!value.isValid() || solution.getModel().inConflict(assignment, value))
continue;
assignment.assign(solution.getIteration(), value);
Double v = resolve(solution, total, startTime, assignments, conflicts, 1 + index);
assignment.unassign(solution.getIteration(), variable);
attempts++;
if (v != null && (!iHC || v <= 0)) {
assignments.put(variable, value);
return v;
}
if (attempts >= iMaxAttempts || isTimeLimitReached(startTime))
break;
}
return null;
}
use of org.cpsolver.coursett.model.Placement in project cpsolver by UniTime.
the class StudentSwap method value.
@Override
public double value(List<StudentConflict> criteria, Assignment<Lecture, Placement> assignment) {
double delta = 0;
for (Lecture l1 : iFirstLectures) {
Placement p1 = assignment.getValue(l1);
if (p1 == null)
continue;
for (Lecture l2 : iFirstStudent.getLectures()) {
Placement p2 = assignment.getValue(l2);
if (l1.equals(l2) || p2 == null)
continue;
if (iFirstLectures.contains(l2) && l1.getClassId().compareTo(l2.getClassId()) >= 0)
continue;
delta -= getJenrConflictWeight(criteria, iFirstStudent, p1, p2);
}
if (iSecondStudent != null) {
for (Lecture l2 : iSecondStudent.getLectures()) {
if (iSecondLectures.contains(l2))
continue;
Placement p2 = assignment.getValue(l2);
if (l1.equals(l2) || p2 == null)
continue;
delta += getJenrConflictWeight(criteria, iSecondStudent, p1, p2);
}
for (Lecture l2 : iFirstLectures) {
Placement p2 = assignment.getValue(l2);
if (l1.equals(l2) || p2 == null)
continue;
if (l1.getClassId().compareTo(l2.getClassId()) >= 0)
continue;
delta += getJenrConflictWeight(criteria, iSecondStudent, p1, p2);
}
}
}
for (Lecture l1 : iSecondLectures) {
Placement p1 = assignment.getValue(l1);
if (p1 == null)
continue;
if (iSecondStudent != null) {
for (Lecture l2 : iSecondStudent.getLectures()) {
Placement p2 = assignment.getValue(l2);
if (l1.equals(l2) || p2 == null)
continue;
if (iSecondLectures.contains(l2) && l1.getClassId().compareTo(l2.getClassId()) >= 0)
continue;
delta -= getJenrConflictWeight(criteria, iSecondStudent, p1, p2);
}
}
for (Lecture l2 : iFirstStudent.getLectures()) {
if (iFirstLectures.contains(l2))
continue;
Placement p2 = assignment.getValue(l2);
if (l1.equals(l2) || p2 == null)
continue;
delta += getJenrConflictWeight(criteria, iFirstStudent, p1, p2);
}
for (Lecture l2 : iSecondLectures) {
Placement p2 = assignment.getValue(l2);
if (l1.equals(l2) || p2 == null)
continue;
if (l1.getClassId().compareTo(l2.getClassId()) >= 0)
continue;
delta += getJenrConflictWeight(criteria, iFirstStudent, p1, p2);
}
}
return delta;
}
Aggregations