Search in sources :

Example 26 with Placement

use of org.cpsolver.coursett.model.Placement 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 27 with Placement

use of org.cpsolver.coursett.model.Placement 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 28 with Placement

use of org.cpsolver.coursett.model.Placement 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 29 with Placement

use of org.cpsolver.coursett.model.Placement in project cpsolver by UniTime.

the class MaxBlockFlexibleConstraint method computeConflicts.

@Override
public void computeConflicts(Assignment<Lecture, Placement> assignment, Placement value, Set<Placement> conflicts) {
    if (!isHard())
        return;
    List<BitSet> weeks = getWeeks();
    // constraint is checked for every day in week
    for (int dayCode : Constants.DAY_CODES) {
        // constraint is checked for every week in semester (or for the whole semester)
        for (BitSet week : weeks) {
            boolean isProblem = false;
            do {
                isProblem = false;
                // each blocks contains placements which are BTB 
                List<Block> blocks = getBlocks(assignment, dayCode, conflicts, value, null, week);
                for (Block block : blocks) {
                    // if the block is not affected by the current placement, continue
                    if (!block.getPlacements().contains(value)) {
                        continue;
                    }
                    Set<Placement> adepts = new HashSet<Placement>();
                    // this solves problem when between 2 classes is required MEET_TOGETHER  
                    if (block.getNbrPlacements() == 1 || block.haveSameStartTime())
                        continue;
                    // if block is longer than maximum size, some of its placements are conflicts
                    if (block.getLengthInSlots() > iMaxBlockSlotsBTB) {
                        // classes from block are potential conflicts
                        adepts.addAll(block.getPlacements());
                        // do not set currently assigned value as conflict
                        adepts.remove(value);
                        isProblem = true;
                        // pick random placement
                        Placement conflict = ToolBox.random(adepts);
                        if (conflict != null) {
                            conflicts.add(conflict);
                        }
                    }
                }
            } while (isProblem);
        }
    }
}
Also used : Placement(org.cpsolver.coursett.model.Placement) BitSet(java.util.BitSet) HashSet(java.util.HashSet)

Example 30 with Placement

use of org.cpsolver.coursett.model.Placement 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

Placement (org.cpsolver.coursett.model.Placement)72 Lecture (org.cpsolver.coursett.model.Lecture)55 HashSet (java.util.HashSet)24 TimetableModel (org.cpsolver.coursett.model.TimetableModel)18 TimeLocation (org.cpsolver.coursett.model.TimeLocation)17 WeakeningConstraint (org.cpsolver.ifs.model.WeakeningConstraint)17 ArrayList (java.util.ArrayList)16 BitSet (java.util.BitSet)16 Constraint (org.cpsolver.ifs.model.Constraint)15 HashMap (java.util.HashMap)14 RoomLocation (org.cpsolver.coursett.model.RoomLocation)13 InstructorConstraint (org.cpsolver.coursett.constraint.InstructorConstraint)9 GlobalConstraint (org.cpsolver.ifs.model.GlobalConstraint)9 Student (org.cpsolver.coursett.model.Student)7 List (java.util.List)6 RoomConstraint (org.cpsolver.coursett.constraint.RoomConstraint)6 DataProperties (org.cpsolver.ifs.util.DataProperties)6 GroupConstraint (org.cpsolver.coursett.constraint.GroupConstraint)5 JenrlConstraint (org.cpsolver.coursett.constraint.JenrlConstraint)5 SpreadConstraint (org.cpsolver.coursett.constraint.SpreadConstraint)5