Search in sources :

Example 16 with Lecture

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

the class InstructorLunchBreak method getValue.

@Override
public double getValue(Assignment<Lecture, Placement> assignment, Collection<Lecture> variables) {
    double lunchValue = 0.0d;
    Set<InstructorConstraint> constraints = new HashSet<InstructorConstraint>();
    for (Lecture lecture : variables) {
        constraints.addAll(lecture.getInstructorConstraints());
    }
    for (InstructorConstraint instructor : constraints) {
        lunchValue += ((InstructorLunchBreakContext) getContext(assignment)).getLunchPreference(assignment, instructor);
    }
    return lunchValue;
}
Also used : Lecture(org.cpsolver.coursett.model.Lecture) InstructorConstraint(org.cpsolver.coursett.constraint.InstructorConstraint) HashSet(java.util.HashSet)

Example 17 with Lecture

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

the class InstructorLunchBreak method getInfo.

@Override
public void getInfo(Assignment<Lecture, Placement> assignment, Map<String, String> info, Collection<Lecture> variables) {
    Set<InstructorConstraint> constraints = new HashSet<InstructorConstraint>();
    for (Lecture lecture : variables) {
        for (InstructorConstraint c : lecture.getInstructorConstraints()) {
            constraints.add(c);
        }
    }
    Set<String> violatedLunchBreaks = new TreeSet<String>();
    int lunchViolations = 0;
    for (InstructorConstraint c : constraints) {
        String days = "";
        CompactInfo compactInfo = ((InstructorLunchBreakContext) getContext(assignment)).getCompactInfo(c);
        for (int i = 0; i < Constants.NR_DAYS; i++) {
            if (compactInfo.getLunchDayViolations()[i] > 0) {
                if (iFullInfo)
                    days += (days.isEmpty() ? "" : ", ") + compactInfo.getLunchDayViolations()[i] + " &times; " + Constants.DAY_NAMES_SHORT[i];
                lunchViolations += compactInfo.getLunchDayViolations()[i];
            }
        }
        if (iFullInfo && !days.isEmpty())
            violatedLunchBreaks.add(c.getName() + ": " + days);
    }
    if (lunchViolations > 0) {
        info.put("Lunch breaks", getPerc(lunchViolations, 0, constraints.size() * Constants.NR_DAYS * getWeeks().size()) + "% (" + lunchViolations + ")");
        if (iFullInfo && !violatedLunchBreaks.isEmpty()) {
            String message = "";
            for (String s : violatedLunchBreaks) message += (message.isEmpty() ? "" : "; ") + s;
            info.put("Lunch break violations", message);
        }
    }
}
Also used : Lecture(org.cpsolver.coursett.model.Lecture) TreeSet(java.util.TreeSet) InstructorConstraint(org.cpsolver.coursett.constraint.InstructorConstraint) InstructorConstraint(org.cpsolver.coursett.constraint.InstructorConstraint) HashSet(java.util.HashSet)

Example 18 with Lecture

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

the class NeighbourSelectionWithSuggestions method backtrack.

private void backtrack(NeighbourSelectionWithSuggestionsContext context, List<Lecture> initialLectures, Map<Lecture, Placement> resolvedLectures, HashMap<Lecture, Placement> conflictsToResolve, int depth) {
    int nrUnassigned = conflictsToResolve.size();
    if ((initialLectures == null || initialLectures.isEmpty()) && nrUnassigned == 0) {
        context.setSuggestionNeighbourIfImproving(resolvedLectures);
        return;
    }
    if (depth <= 0 || context.checkTimeoutReached())
        return;
    Assignment<Lecture, Placement> assignment = context.getAssignment();
    for (Lecture lecture : initialLectures != null && !initialLectures.isEmpty() ? initialLectures : new ArrayList<Lecture>(conflictsToResolve.keySet())) {
        if (context.isTimeoutReached())
            break;
        if (resolvedLectures.containsKey(lecture))
            continue;
        placements: for (Placement placement : lecture.values(assignment)) {
            if (context.isTimeoutReached())
                break;
            Placement cur = assignment.getValue(lecture);
            if (placement.equals(cur))
                continue;
            if (placement.isHard(assignment))
                continue;
            Set<Placement> conflicts = context.getModel().conflictValues(assignment, placement);
            if (nrUnassigned + conflicts.size() > depth)
                continue;
            if (conflicts.contains(placement))
                continue;
            if (containsCommited(context, conflicts))
                continue;
            for (Iterator<Placement> i = conflicts.iterator(); i.hasNext(); ) {
                Placement c = i.next();
                if (resolvedLectures.containsKey(c.variable()))
                    continue placements;
            }
            for (Iterator<Placement> i = conflicts.iterator(); i.hasNext(); ) {
                Placement c = i.next();
                assignment.unassign(0, c.variable());
            }
            assignment.assign(0, placement);
            for (Iterator<Placement> i = conflicts.iterator(); i.hasNext(); ) {
                Placement c = i.next();
                conflictsToResolve.put(c.variable(), c);
            }
            Placement resolvedConf = conflictsToResolve.remove(lecture);
            resolvedLectures.put(lecture, placement);
            backtrack(context, null, resolvedLectures, conflictsToResolve, depth - 1);
            resolvedLectures.remove(lecture);
            if (cur == null)
                assignment.unassign(0, lecture);
            else
                assignment.assign(0, cur);
            for (Iterator<Placement> i = conflicts.iterator(); i.hasNext(); ) {
                Placement p = i.next();
                assignment.assign(0, p);
                conflictsToResolve.remove(p.variable());
            }
            if (resolvedConf != null)
                conflictsToResolve.put(lecture, resolvedConf);
        }
    }
}
Also used : Lecture(org.cpsolver.coursett.model.Lecture) Set(java.util.Set) Placement(org.cpsolver.coursett.model.Placement) Iterator(java.util.Iterator)

Example 19 with Lecture

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

the class MinimizeNumberOfUsedRoomsConstraint method computeConflicts.

@Override
public void computeConflicts(Assignment<Lecture, Placement> assignment, Placement placement, Set<Placement> conflicts) {
    MinimizeNumberOfUsedRoomsConstraintContext context = getContext(assignment);
    int overLimit = context.getOverLimit(assignment, placement);
    if (overLimit > 0) {
        List<List<Placement>> adepts = new ArrayList<List<Placement>>();
        for (Set<Lecture> lects : context.getUsedRooms().values()) {
            List<Placement> placementsToUnassign = new ArrayList<Placement>();
            boolean canUnassign = true;
            for (Lecture l : lects) {
                if (l.isCommitted()) {
                    canUnassign = false;
                    break;
                }
                Placement p = assignment.getValue(l);
                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) Placement(org.cpsolver.coursett.model.Placement) ArrayList(java.util.ArrayList) ArrayList(java.util.ArrayList) List(java.util.List) WeakeningConstraint(org.cpsolver.ifs.model.WeakeningConstraint)

Example 20 with Lecture

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

the class MinimizeNumberOfUsedRoomsConstraint method estimateLimit.

public int estimateLimit() {
    HashSet<RoomLocation> mandatoryRooms = new HashSet<RoomLocation>();
    for (Lecture lecture : variables()) {
        if (lecture.getNrRooms() == 0)
            continue;
        if (lecture.isCommitted() || lecture.roomLocations().size() == 1)
            mandatoryRooms.addAll(lecture.roomLocations());
    }
    double[][] histogram = new double[iLastDaySlot - iFirstDaySlot + 1][iLastWorkDay - iFirstWorkDay + 1];
    for (int i = 0; i < iLastDaySlot - iFirstDaySlot + 1; i++) for (int j = 0; j < iLastWorkDay - iFirstWorkDay + 1; j++) histogram[i][j] = 0.0;
    for (Lecture lecture : variables()) {
        if (lecture.getNrRooms() == 0)
            continue;
        List<Placement> values = lecture.values(null);
        for (Placement p : lecture.values(null)) {
            int firstSlot = p.getTimeLocation().getStartSlot();
            if (firstSlot > iLastDaySlot)
                continue;
            int endSlot = firstSlot + p.getTimeLocation().getNrSlotsPerMeeting() - 1;
            if (endSlot < iFirstDaySlot)
                continue;
            for (int i = Math.max(firstSlot, iFirstDaySlot); i <= Math.min(endSlot, iLastDaySlot); i++) {
                int dayCode = p.getTimeLocation().getDayCode();
                for (int j = iFirstWorkDay; j <= iLastWorkDay; j++) {
                    if ((dayCode & Constants.DAY_CODES[j]) != 0) {
                        histogram[i - iFirstDaySlot][j - iFirstWorkDay] += ((double) lecture.getNrRooms()) / values.size();
                    }
                }
            }
        }
    }
    int maxAverageRooms = 0;
    for (int i = 0; i < iLastDaySlot - iFirstDaySlot + 1; i++) for (int j = 0; j < iLastWorkDay - iFirstWorkDay + 1; j++) maxAverageRooms = Math.max(maxAverageRooms, (int) Math.ceil(histogram[i][j]));
    return Math.max(1, Math.max(mandatoryRooms.size(), maxAverageRooms));
}
Also used : Lecture(org.cpsolver.coursett.model.Lecture) RoomLocation(org.cpsolver.coursett.model.RoomLocation) Placement(org.cpsolver.coursett.model.Placement) WeakeningConstraint(org.cpsolver.ifs.model.WeakeningConstraint) HashSet(java.util.HashSet)

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