use of org.cpsolver.ifs.model.WeakeningConstraint in project cpsolver by UniTime.
the class GroupConstraint method forwardCheck.
public boolean forwardCheck(Assignment<Lecture, Placement> assignment, Placement value, Set<GroupConstraint> ignore, int depth) {
try {
if (depth < 0)
return true;
ignore.add(this);
int neededSize = value.variable().maxRoomUse();
for (Lecture lecture : variables()) {
// Skip this lecture
if (lecture.equals(value.variable()))
continue;
Placement current = assignment.getValue(lecture);
if (current != null) {
// Has assignment, check whether it is conflicting
if (isSatisfiedPair(assignment, value, current)) {
// Increase needed size if the assignment is of the same room and overlapping in time
if (canShareRoom() && sameRoomAndOverlaps(value, current)) {
neededSize += lecture.maxRoomUse();
}
continue;
}
return false;
}
// Look for supporting assignments assignment
boolean shareRoomAndOverlaps = canShareRoom();
Placement support = null;
int nrSupports = 0;
if (lecture.nrValues() >= iForwardCheckMaxDomainSize) {
// ignore variables with large domains
return true;
}
List<Placement> values = lecture.values(assignment);
if (values.isEmpty()) {
// ignore variables with empty domain
return true;
}
for (Placement other : lecture.values(assignment)) {
if (nrSupports < 2) {
if (isSatisfiedPair(assignment, value, other)) {
if (support == null)
support = other;
nrSupports++;
if (shareRoomAndOverlaps && !sameRoomAndOverlaps(value, other))
shareRoomAndOverlaps = false;
}
} else if (shareRoomAndOverlaps && !sameRoomAndOverlaps(value, other) && isSatisfiedPair(assignment, value, other)) {
shareRoomAndOverlaps = false;
}
if (nrSupports > 1 && !shareRoomAndOverlaps)
break;
}
// No supporting assignment -> fail
if (nrSupports == 0) {
// other class cannot be assigned with this value
return false;
}
// Increase needed size if all supporters are of the same room and in overlapping times
if (shareRoomAndOverlaps) {
neededSize += lecture.maxRoomUse();
}
// Only one supporter -> propagate the new assignment over other hard constraints of the lecture
if (nrSupports == 1) {
for (Constraint<Lecture, Placement> other : lecture.hardConstraints()) {
if (other instanceof WeakeningConstraint)
continue;
if (other instanceof GroupConstraint) {
GroupConstraint gc = (GroupConstraint) other;
if (depth > 0 && !ignore.contains(gc) && !gc.forwardCheck(assignment, support, ignore, depth - 1))
return false;
} else {
if (other.inConflict(assignment, support))
return false;
}
}
for (GlobalConstraint<Lecture, Placement> other : getModel().globalConstraints()) {
if (other instanceof WeakeningConstraint)
continue;
if (other.inConflict(assignment, support))
return false;
}
}
}
if (canShareRoom() && neededSize > value.getRoomSize()) {
// room is too small to fit all meet with classes
return false;
}
return true;
} finally {
ignore.remove(this);
}
}
use of org.cpsolver.ifs.model.WeakeningConstraint in project cpsolver by UniTime.
the class GroupConstraint method forwardCheck.
public void forwardCheck(Assignment<Lecture, Placement> assignment, Placement value, Set<Placement> conflicts, Set<GroupConstraint> ignore, int depth) {
try {
if (depth < 0)
return;
ignore.add(this);
List<Placement> neededSize = null;
for (Lecture lecture : variables()) {
// already conflicting
if (conflicts.contains(value))
break;
// Skip this lecture
if (lecture.equals(value.variable()))
continue;
Placement current = assignment.getValue(lecture);
if (current != null) {
// Has assignment, check whether it is conflicting
if (isSatisfiedPair(assignment, value, current)) {
// Increase needed size if the assignment is of the same room and overlapping in time
if (canShareRoom() && sameRoomAndOverlaps(value, current)) {
if (neededSize == null)
neededSize = new ArrayList<Placement>();
neededSize.add(current);
}
continue;
}
conflicts.add(current);
}
// Look for supporting assignments assignment
boolean shareRoomAndOverlaps = canShareRoom();
Placement support = null;
int nrSupports = 0;
if (lecture.nrValues() >= iForwardCheckMaxDomainSize) {
// ignore variables with large domains
return;
}
List<Placement> values = lecture.values(assignment);
if (values.isEmpty()) {
// ignore variables with empty domain
return;
}
for (Placement other : values) {
if (nrSupports < 2) {
if (isSatisfiedPair(assignment, value, other)) {
if (support == null)
support = other;
nrSupports++;
if (shareRoomAndOverlaps && !sameRoomAndOverlaps(value, other))
shareRoomAndOverlaps = false;
}
} else if (shareRoomAndOverlaps && !sameRoomAndOverlaps(value, other) && isSatisfiedPair(assignment, value, other)) {
shareRoomAndOverlaps = false;
}
if (nrSupports > 1 && !shareRoomAndOverlaps)
break;
}
// No supporting assignment -> fail
if (nrSupports == 0) {
// other class cannot be assigned with this value
conflicts.add(value);
return;
}
// Increase needed size if all supporters are of the same room and in overlapping times
if (shareRoomAndOverlaps && support != null) {
if (neededSize == null)
neededSize = new ArrayList<Placement>();
neededSize.add(support);
}
// Only one supporter -> propagate the new assignment over other hard constraints of the lecture
if (nrSupports == 1) {
for (Constraint<Lecture, Placement> other : lecture.hardConstraints()) {
if (other instanceof WeakeningConstraint)
continue;
if (other instanceof GroupConstraint) {
GroupConstraint gc = (GroupConstraint) other;
if (depth > 0 && !ignore.contains(gc))
gc.forwardCheck(assignment, support, conflicts, ignore, depth - 1);
} else {
other.computeConflicts(assignment, support, conflicts);
}
}
for (GlobalConstraint<Lecture, Placement> other : getModel().globalConstraints()) {
if (other instanceof WeakeningConstraint)
continue;
other.computeConflicts(assignment, support, conflicts);
}
if (conflicts.contains(support))
conflicts.add(value);
}
}
if (canShareRoom() && neededSize != null) {
if (value.getRoomLocations() != null) {
for (RoomLocation room : value.getRoomLocations()) if (room.getRoomConstraint() != null && !room.getRoomConstraint().checkRoomSize(value, neededSize)) {
// room is too small to fit all meet with classes
conflicts.add(value);
}
} else if (value.getRoomLocation() != null) {
RoomLocation room = value.getRoomLocation();
if (room.getRoomConstraint() != null && !room.getRoomConstraint().checkRoomSize(value, neededSize)) {
// room is too small to fit all meet with classes
conflicts.add(value);
}
}
}
} finally {
ignore.remove(this);
}
}
use of org.cpsolver.ifs.model.WeakeningConstraint in project cpsolver by UniTime.
the class Lecture method removeContstraint.
@Override
public void removeContstraint(Constraint<Lecture, Placement> constraint) {
super.removeContstraint(constraint);
if (constraint instanceof WeakeningConstraint)
iWeakeningConstraints.remove(constraint);
if (constraint instanceof FlexibleConstraint)
iFlexibleGroupConstraints.remove(constraint);
if (constraint instanceof JenrlConstraint) {
JenrlConstraint jenrl = (JenrlConstraint) constraint;
Lecture another = jenrl.another(this);
if (another != null) {
iJenrlConstraints.remove(jenrl);
another.iJenrlConstraints.remove(jenrl);
iJenrlConstraintsHash.remove(another);
another.iJenrlConstraintsHash.remove(this);
}
} else if (constraint instanceof GroupConstraint) {
iCanShareRoomGroupConstraints.remove(constraint);
iHardGroupSoftConstraints.remove(constraint);
iGroupConstraints.remove(constraint);
} else if (constraint instanceof DepartmentSpreadConstraint)
iDeptSpreadConstraint = null;
else if (constraint instanceof SpreadConstraint)
iSpreadConstraints.remove(constraint);
else if (constraint instanceof InstructorConstraint)
iInstructorConstraints.remove(constraint);
else if (constraint instanceof ClassLimitConstraint)
iClassLimitConstraint = null;
}
use of org.cpsolver.ifs.model.WeakeningConstraint in project cpsolver by UniTime.
the class Lecture method addContstraint.
@Override
public void addContstraint(Constraint<Lecture, Placement> constraint) {
super.addContstraint(constraint);
if (constraint instanceof WeakeningConstraint)
iWeakeningConstraints.add(constraint);
if (constraint instanceof FlexibleConstraint)
iFlexibleGroupConstraints.add((FlexibleConstraint) constraint);
if (constraint instanceof JenrlConstraint) {
JenrlConstraint jenrl = (JenrlConstraint) constraint;
Lecture another = jenrl.another(this);
if (another != null) {
iJenrlConstraints.add(jenrl);
another.iJenrlConstraints.add(jenrl);
iJenrlConstraintsHash.put(another, (JenrlConstraint) constraint);
another.iJenrlConstraintsHash.put(this, (JenrlConstraint) constraint);
}
} else if (constraint instanceof DepartmentSpreadConstraint)
iDeptSpreadConstraint = (DepartmentSpreadConstraint) constraint;
else if (constraint instanceof SpreadConstraint)
iSpreadConstraints.add((SpreadConstraint) constraint);
else if (constraint instanceof InstructorConstraint) {
InstructorConstraint ic = (InstructorConstraint) constraint;
if (ic.getResourceId() != null && ic.getResourceId().longValue() > 0)
iInstructorConstraints.add(ic);
} else if (constraint instanceof ClassLimitConstraint)
iClassLimitConstraint = (ClassLimitConstraint) constraint;
else if (constraint instanceof GroupConstraint) {
GroupConstraint gc = (GroupConstraint) constraint;
if (gc.canShareRoom()) {
iCanShareRoomGroupConstraints.add((GroupConstraint) constraint);
} else {
iGroupConstraints.add((GroupConstraint) constraint);
if (Constants.sPreferenceProhibited.equals(Constants.preferenceLevel2preference(gc.getPreference())) || Constants.sPreferenceRequired.equals(Constants.preferenceLevel2preference(gc.getPreference())))
iHardGroupSoftConstraints.add((GroupConstraint) constraint);
}
}
}
Aggregations