Search in sources :

Example 71 with Lecture

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

the class DepartmentBalancingPenalty method getValue.

@Override
public double getValue(Assignment<Lecture, Placement> assignment, Collection<Lecture> variables) {
    double ret = 0;
    Set<DepartmentSpreadConstraint> constraints = new HashSet<DepartmentSpreadConstraint>();
    for (Lecture lect : variables) if (lect.getDeptSpreadConstraint() != null && constraints.add(lect.getDeptSpreadConstraint()))
        ret += lect.getDeptSpreadConstraint().getPenalty(assignment);
    return ret / 12.0;
}
Also used : Lecture(org.cpsolver.coursett.model.Lecture) DepartmentSpreadConstraint(org.cpsolver.coursett.constraint.DepartmentSpreadConstraint) HashSet(java.util.HashSet)

Example 72 with Lecture

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

the class SpreadConstraint method getPenaltyEstimate.

public int getPenaltyEstimate(Assignment<Lecture, Placement> assignment) {
    double[][] histogramPerDay = new double[iLastDaySlot - iFirstDaySlot + 1][iLastWorkDay - iFirstWorkDay + 1];
    int[][] maxCourses = new int[iLastDaySlot - iFirstDaySlot + 1][iLastWorkDay - iFirstWorkDay + 1];
    int[][] nrCourses = new int[iLastDaySlot - iFirstDaySlot + 1][iLastWorkDay - iFirstWorkDay + 1];
    for (int i = 0; i < iLastDaySlot - iFirstDaySlot + 1; i++) for (int j = 0; j < iLastWorkDay - iFirstWorkDay + 1; j++) histogramPerDay[i][j] = 0.0;
    int totalUsedSlots = 0;
    for (Lecture lecture : variables()) {
        List<Placement> values = lecture.values(assignment);
        Placement firstPlacement = (values.isEmpty() ? null : values.get(0));
        if (firstPlacement != null) {
            totalUsedSlots += firstPlacement.getTimeLocation().getNrSlotsPerMeeting() * firstPlacement.getTimeLocation().getNrMeetings();
        }
        for (Placement p : values) {
            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) {
                        histogramPerDay[i - iFirstDaySlot][j - iFirstWorkDay] += 1.0 / values.size();
                    }
                }
            }
        }
    }
    double threshold = iSpreadFactor * ((double) totalUsedSlots / ((iLastWorkDay - iFirstWorkDay + 1) * (iLastDaySlot - iFirstDaySlot + 1)));
    for (int i = 0; i < iLastDaySlot - iFirstDaySlot + 1; i++) {
        for (int j = 0; j < iLastWorkDay - iFirstWorkDay + 1; j++) {
            nrCourses[i][j] = 0;
            maxCourses[i][j] = (int) (0.999 + (histogramPerDay[i][j] <= threshold ? iSpreadFactor * histogramPerDay[i][j] : histogramPerDay[i][j]));
        }
    }
    int currentPenalty = 0;
    for (Lecture lecture : variables()) {
        Placement placement = assignment.getValue(lecture);
        if (placement == null)
            continue;
        int firstSlot = placement.getTimeLocation().getStartSlot();
        if (firstSlot > iLastDaySlot)
            continue;
        int endSlot = firstSlot + placement.getTimeLocation().getNrSlotsPerMeeting() - 1;
        if (endSlot < iFirstDaySlot)
            continue;
        for (int i = Math.max(firstSlot, iFirstDaySlot); i <= Math.min(endSlot, iLastDaySlot); i++) {
            for (int j = iFirstWorkDay; j <= iLastWorkDay; j++) {
                int dayCode = Constants.DAY_CODES[j];
                if ((dayCode & placement.getTimeLocation().getDayCode()) != 0) {
                    nrCourses[i - iFirstDaySlot][j - iFirstWorkDay]++;
                }
            }
        }
    }
    for (int i = 0; i < iLastDaySlot - iFirstDaySlot + 1; i++) {
        for (int j = 0; j < iLastWorkDay - iFirstWorkDay + 1; j++) {
            currentPenalty += Math.max(0, nrCourses[i][j] - maxCourses[i][j]);
        }
    }
    return currentPenalty;
}
Also used : Lecture(org.cpsolver.coursett.model.Lecture) Placement(org.cpsolver.coursett.model.Placement) WeakeningConstraint(org.cpsolver.ifs.model.WeakeningConstraint)

Example 73 with Lecture

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

the class SpreadConstraint method computeConflicts.

@Override
public void computeConflicts(Assignment<Lecture, Placement> assignment, Placement placement, Set<Placement> conflicts) {
    SpreadConstraintContext context = getContext(assignment);
    if (context.getUnassignmentsToWeaken() == 0)
        return;
    int penalty = context.getCurrentPenalty() + getPenalty(assignment, placement);
    if (penalty <= context.getMaxAllowedPenalty())
        return;
    int firstSlot = placement.getTimeLocation().getStartSlot();
    if (firstSlot > iLastDaySlot)
        return;
    int endSlot = firstSlot + placement.getTimeLocation().getNrSlotsPerMeeting() - 1;
    if (endSlot < iFirstDaySlot)
        return;
    // sLogger.debug("-- computing conflict for value "+value+" ... (penalty="+iCurrentPenalty+", penalty with the value="+penalty+", max="+iMaxAllowedPenalty+")");
    int[][] nrCourses = new int[iLastDaySlot - iFirstDaySlot + 1][iLastWorkDay - iFirstWorkDay + 1];
    for (int i = 0; i < iLastDaySlot - iFirstDaySlot + 1; i++) for (int j = 0; j < iLastWorkDay - iFirstWorkDay + 1; j++) nrCourses[i][j] = context.getNrCourses(i + iFirstDaySlot, j + iFirstWorkDay, placement);
    tryAssign(assignment, placement, nrCourses);
    // sLogger.debug("  -- nrCurses="+fmt(nrCourses));
    for (Lecture lect : variables()) {
        if (lect.equals(placement.variable()))
            continue;
        if (conflicts.contains(lect)) {
            penalty -= tryUnassign(assignment, assignment.getValue(lect), nrCourses);
        }
        if (penalty <= context.getMaxAllowedPenalty())
            return;
    }
    if (USE_MOST_IMPROVEMENT_ADEPTS) {
        while (penalty > context.getMaxAllowedPenalty()) {
            Placement plac = getAdept(assignment, placement, nrCourses, conflicts);
            if (plac == null)
                break;
            conflicts.add(plac);
            penalty -= tryUnassign(assignment, plac, nrCourses);
        }
    } else {
        if (penalty > context.getMaxAllowedPenalty()) {
            Set<Placement>[] adepts = getAdepts(assignment, placement, nrCourses, conflicts);
            for (int i = 0; penalty > context.getMaxAllowedPenalty() && i < adepts.length; i++) {
                while (!adepts[i].isEmpty() && penalty > context.getMaxAllowedPenalty()) {
                    Placement plac = ToolBox.random(adepts[i]);
                    adepts[i].remove(plac);
                    conflicts.add(plac);
                    // sLogger.debug("  -- conflict "+lect.getAssignment()+" added");
                    penalty -= tryUnassign(assignment, plac, nrCourses);
                }
            }
        }
    }
}
Also used : Lecture(org.cpsolver.coursett.model.Lecture) Set(java.util.Set) HashSet(java.util.HashSet) Placement(org.cpsolver.coursett.model.Placement) WeakeningConstraint(org.cpsolver.ifs.model.WeakeningConstraint)

Example 74 with Lecture

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

the class RoomConstraint method setNotAvailable.

@SuppressWarnings("unchecked")
public void setNotAvailable(Placement placement) {
    if (iAvailable == null) {
        iAvailable = new List[Constants.SLOTS_PER_DAY * Constants.NR_DAYS];
        for (int i = 0; i < iAvailable.length; i++) iAvailable[i] = null;
    }
    for (Enumeration<Integer> e = placement.getTimeLocation().getSlots(); e.hasMoreElements(); ) {
        int slot = e.nextElement();
        if (iAvailable[slot] == null)
            iAvailable[slot] = new ArrayList<Placement>(1);
        iAvailable[slot].add(placement);
    }
    for (Lecture lecture : variables()) lecture.clearValueCache();
}
Also used : Lecture(org.cpsolver.coursett.model.Lecture) ArrayList(java.util.ArrayList)

Example 75 with Lecture

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

the class JenrlConstraint method jenrl.

/**
     * Number of joined enrollments if the given value is assigned to the given
     * variable
     * @param assignment current assignment
     * @param variable a class
     * @param value class placement under consideration
     * @return number of student conflicts caused by this constraint if assigned
     */
public long jenrl(Assignment<Lecture, Placement> assignment, Lecture variable, Placement value) {
    Lecture other = (first().equals(variable) ? second() : first());
    Placement otherPlacement = (other == null ? null : assignment.getValue(other));
    return (otherPlacement != null && isInConflict(value, otherPlacement, getDistanceMetric()) ? Math.round(iJenrl) : 0);
}
Also used : Lecture(org.cpsolver.coursett.model.Lecture) Placement(org.cpsolver.coursett.model.Placement)

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