Search in sources :

Example 21 with Lecture

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;
}
Also used : Lecture(org.cpsolver.coursett.model.Lecture) Placement(org.cpsolver.coursett.model.Placement) BitSet(java.util.BitSet) HashSet(java.util.HashSet)

Example 22 with Lecture

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);
            }
        }
    }
}
Also used : Lecture(org.cpsolver.coursett.model.Lecture) Placement(org.cpsolver.coursett.model.Placement) BitSet(java.util.BitSet) ArrayList(java.util.ArrayList) WeakeningConstraint(org.cpsolver.ifs.model.WeakeningConstraint)

Example 23 with Lecture

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));
            }
        }
    }
}
Also used : Lecture(org.cpsolver.coursett.model.Lecture) TimeLocation(org.cpsolver.coursett.model.TimeLocation) ArrayList(java.util.ArrayList) WeakeningConstraint(org.cpsolver.ifs.model.WeakeningConstraint) Placement(org.cpsolver.coursett.model.Placement) ArrayList(java.util.ArrayList) List(java.util.List)

Example 24 with Lecture

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());
}
Also used : Lecture(org.cpsolver.coursett.model.Lecture) Placement(org.cpsolver.coursett.model.Placement)

Example 25 with Lecture

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;
}
Also used : Lecture(org.cpsolver.coursett.model.Lecture) TimeLocation(org.cpsolver.coursett.model.TimeLocation) Placement(org.cpsolver.coursett.model.Placement) BitSet(java.util.BitSet)

Aggregations

Lecture (org.cpsolver.coursett.model.Lecture)96 Placement (org.cpsolver.coursett.model.Placement)55 HashSet (java.util.HashSet)35 TimetableModel (org.cpsolver.coursett.model.TimetableModel)17 WeakeningConstraint (org.cpsolver.ifs.model.WeakeningConstraint)17 HashMap (java.util.HashMap)16 ArrayList (java.util.ArrayList)14 TimeLocation (org.cpsolver.coursett.model.TimeLocation)14 Constraint (org.cpsolver.ifs.model.Constraint)14 InstructorConstraint (org.cpsolver.coursett.constraint.InstructorConstraint)13 Student (org.cpsolver.coursett.model.Student)13 BitSet (java.util.BitSet)11 JenrlConstraint (org.cpsolver.coursett.constraint.JenrlConstraint)10 GlobalConstraint (org.cpsolver.ifs.model.GlobalConstraint)9 RoomLocation (org.cpsolver.coursett.model.RoomLocation)8 Set (java.util.Set)7 TreeSet (java.util.TreeSet)7 GroupConstraint (org.cpsolver.coursett.constraint.GroupConstraint)7 StudentGroup (org.cpsolver.coursett.model.StudentGroup)7 RoomConstraint (org.cpsolver.coursett.constraint.RoomConstraint)6