use of org.cpsolver.coursett.model.Lecture in project cpsolver by UniTime.
the class RoomConstraint method inConflict.
@Override
public boolean inConflict(Assignment<Lecture, Placement> assignment, Placement placement) {
if (!getConstraint())
return false;
if (!placement.hasRoomLocation(getResourceId()))
return false;
Lecture lecture = placement.variable();
Placement current = assignment.getValue(lecture);
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 (confPlacement.canShareRooms(placement) && confLecture.maxRoomUse() + size <= getCapacity()) {
size += confLecture.maxRoomUse();
if (skipPlacements == null)
skipPlacements = new HashSet<Placement>();
skipPlacements.add(confPlacement);
continue;
}
return true;
}
}
return false;
}
use of org.cpsolver.coursett.model.Lecture in project cpsolver by UniTime.
the class MaxBreaksFlexibleConstraint method computeConflicts.
@Override
public void computeConflicts(Assignment<Lecture, Placement> assignment, Placement value, Set<Placement> conflicts) {
if (!isHard())
return;
if (((MaxBreaksFlexibleConstraintContext) getContext(assignment)).isWeak(value)) {
for (Lecture v : variables()) if (assignment.getValue(v) == null && !v.equals(value.variable())) {
// incomplete and week -- do not check for conflicts just yet
return;
}
}
// constraint is checked for every day in week
for (int dayCode : Constants.DAY_CODES) {
// ignore other days
if ((value.getTimeLocation().getDayCode() & dayCode) == 0)
continue;
// constraint is checked for every week in semester (or for the whole semester)
for (BitSet week : getWeeks()) {
// ignore other weeks
if (week != null && !week.intersects(value.getTimeLocation().getWeekCode()))
continue;
// each blocks contains placements which are BTB
List<Block> blocks = getBlocks(assignment, dayCode, conflicts, value, null, week);
while (blocks.size() > iMaxBlocksOnADay) {
// too many blocks -> identify adepts for unassignment
List<Block> adepts = new ArrayList<Block>();
int size = 0;
for (Block block : blocks) {
// skip block containing given value
if (block.getPlacements().contains(value))
continue;
// select adepts of the smallest size
if (adepts.isEmpty() || size > block.getPlacements().size()) {
adepts.clear();
adepts.add(block);
size = block.getPlacements().size();
} else if (size == block.getPlacements().size()) {
adepts.add(block);
}
}
// pick one randomly
Block block = ToolBox.random(adepts);
blocks.remove(block);
for (Placement conflict : block.getPlacements()) if (conflict.equals(assignment.getValue(conflict.variable())))
conflicts.add(conflict);
}
}
}
}
use of org.cpsolver.coursett.model.Lecture in project cpsolver by UniTime.
the class MinimizeNumberOfUsedGroupsOfTime method computeConflicts.
@Override
public void computeConflicts(Assignment<Lecture, Placement> assignment, Placement placement, Set<Placement> conflicts) {
MinimizeNumberOfUsedGroupsOfTimeContext context = getContext(assignment);
int overLimit = context.getOverLimit(placement);
if (overLimit > 0) {
TimeLocation time = placement.getTimeLocation();
List<List<Placement>> adepts = new ArrayList<List<Placement>>();
for (int i = 0; i < iGroupsOfTime.length; i++) {
GroupOfTime groupOfTime = iGroupsOfTime[i];
Set<Placement> usage = context.getUsage(i);
if (groupOfTime.overlap(time) || usage.isEmpty())
continue;
boolean canUnassign = true;
List<Placement> placementsToUnassign = new ArrayList<Placement>(usage.size());
for (Placement p : usage) {
Lecture l = p.variable();
if (l.isCommitted()) {
canUnassign = false;
break;
}
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 JenrlConstraint method inConflict.
@Override
public boolean inConflict(Assignment<Lecture, Placement> assignment, Placement value) {
if (!getContext(assignment).isOverLimit() || value.variable().isCommitted())
return false;
Lecture other = another(value.variable());
if (other == null)
return false;
Placement otherPlacement = assignment.getValue(other);
return otherPlacement != null && !other.isCommitted() && isInConflict(value, otherPlacement, getDistanceMetric());
}
use of org.cpsolver.coursett.model.Lecture in project cpsolver by UniTime.
the class InstructorConstraint method inConflict.
@Override
public boolean inConflict(Assignment<Lecture, Placement> assignment, Placement placement) {
Lecture lecture = placement.variable();
Placement current = assignment.getValue(lecture);
BitSet weekCode = placement.getTimeLocation().getWeekCode();
InstructorConstraintContext context = getContext(assignment);
for (Enumeration<Integer> e = placement.getTimeLocation().getSlots(); e.hasMoreElements(); ) {
int slot = e.nextElement();
for (Placement p : context.getPlacements(slot)) {
if (!p.equals(current) && p.getTimeLocation().shareWeeks(weekCode)) {
if (p.canShareRooms(placement) && p.sameRooms(placement))
continue;
return true;
}
}
}
if (!iIgnoreDistances) {
for (Enumeration<Integer> e = placement.getTimeLocation().getStartSlots(); e.hasMoreElements(); ) {
int startSlot = e.nextElement();
int prevSlot = startSlot - 1;
if (prevSlot >= 0 && (prevSlot / Constants.SLOTS_PER_DAY) == (startSlot / Constants.SLOTS_PER_DAY)) {
for (Placement c : context.getPlacements(prevSlot, placement)) {
if (lecture.equals(c.variable()))
continue;
if (c.canShareRooms(placement) && c.sameRooms(placement))
continue;
if (Placement.getDistanceInMeters(getDistanceMetric(), placement, c) > getDistanceMetric().getInstructorProhibitedLimit())
return true;
}
}
int nextSlot = startSlot + placement.getTimeLocation().getLength();
if ((nextSlot / Constants.SLOTS_PER_DAY) == (startSlot / Constants.SLOTS_PER_DAY)) {
for (Placement c : context.getPlacements(nextSlot, placement)) {
if (lecture.equals(c.variable()))
continue;
if (c.canShareRooms(placement) && c.sameRooms(placement))
continue;
if (Placement.getDistanceInMeters(getDistanceMetric(), placement, c) > getDistanceMetric().getInstructorProhibitedLimit())
return true;
}
}
if (getDistanceMetric().doComputeDistanceConflictsBetweenNonBTBClasses()) {
TimeLocation t1 = placement.getTimeLocation();
for (Lecture other : variables()) {
Placement otherPlacement = assignment.getValue(other);
if (otherPlacement == null || other.equals(placement.variable()))
continue;
TimeLocation t2 = otherPlacement.getTimeLocation();
if (t1 == null || t2 == null || !t1.shareDays(t2) || !t1.shareWeeks(t2))
continue;
if (t1.getStartSlot() + t1.getLength() < t2.getStartSlot()) {
if (Placement.getDistanceInMinutes(getDistanceMetric(), placement, otherPlacement) > t1.getBreakTime() + Constants.SLOT_LENGTH_MIN * (t2.getStartSlot() - t1.getStartSlot() - t1.getLength()))
return true;
} else if (t2.getStartSlot() + t2.getLength() < t1.getStartSlot()) {
if (Placement.getDistanceInMinutes(getDistanceMetric(), placement, otherPlacement) > t2.getBreakTime() + Constants.SLOT_LENGTH_MIN * (t1.getStartSlot() - t2.getStartSlot() - t2.getLength()))
return true;
}
}
}
}
}
return false;
}
Aggregations