Search in sources :

Example 26 with TimeLocation

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

the class InstructorConstraint method getPreferenceCombination.

public int getPreferenceCombination(Assignment<Lecture, Placement> assignment, Placement value) {
    Lecture lecture = value.variable();
    Placement placement = value;
    int pref = 0;
    HashSet<Placement> checked = new HashSet<Placement>();
    InstructorConstraintContext context = getContext(assignment);
    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()) || !checked.add(c))
                    continue;
                double dist = Placement.getDistanceInMeters(getDistanceMetric(), placement, c);
                if (dist > getDistanceMetric().getInstructorNoPreferenceLimit() && dist <= getDistanceMetric().getInstructorDiscouragedLimit())
                    pref = Math.max(pref, Constants.sPreferenceLevelDiscouraged);
                if (dist > getDistanceMetric().getInstructorDiscouragedLimit() && (dist <= getDistanceMetric().getInstructorProhibitedLimit() || iIgnoreDistances))
                    pref = Math.max(pref, Constants.sPreferenceLevelStronglyDiscouraged);
                if (!iIgnoreDistances && dist > getDistanceMetric().getInstructorProhibitedLimit())
                    pref = Math.max(pref, Constants.sPreferenceLevelProhibited);
            }
        }
        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()) || !checked.add(c))
                    continue;
                double dist = Placement.getDistanceInMeters(getDistanceMetric(), placement, c);
                if (dist > getDistanceMetric().getInstructorNoPreferenceLimit() && dist <= getDistanceMetric().getInstructorDiscouragedLimit())
                    pref = Math.max(pref, Constants.sPreferenceLevelDiscouraged);
                if (dist > getDistanceMetric().getInstructorDiscouragedLimit() && (dist <= getDistanceMetric().getInstructorProhibitedLimit() || iIgnoreDistances))
                    pref = Math.max(pref, Constants.sPreferenceLevelStronglyDiscouraged);
                if (!iIgnoreDistances && dist > getDistanceMetric().getInstructorProhibitedLimit())
                    pref = Constants.sPreferenceLevelProhibited;
            }
        }
        if (getDistanceMetric().doComputeDistanceConflictsBetweenNonBTBClasses()) {
            TimeLocation t1 = placement.getTimeLocation();
            Placement before = null, after = null;
            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()) {
                    int distanceInMinutes = Placement.getDistanceInMinutes(getDistanceMetric(), placement, otherPlacement);
                    if (distanceInMinutes > t1.getBreakTime() + Constants.SLOT_LENGTH_MIN * (t2.getStartSlot() - t1.getStartSlot() - t1.getLength()))
                        pref = Math.max(pref, (iIgnoreDistances ? Constants.sPreferenceLevelStronglyDiscouraged : Constants.sPreferenceLevelProhibited));
                    else if (distanceInMinutes > Constants.SLOT_LENGTH_MIN * (t2.getStartSlot() - t1.getStartSlot() - t1.getLength()))
                        pref = Math.max(pref, Constants.sPreferenceLevelDiscouraged);
                } else if (t2.getStartSlot() + t2.getLength() < t1.getStartSlot()) {
                    int distanceInMinutes = Placement.getDistanceInMinutes(getDistanceMetric(), placement, otherPlacement);
                    if (distanceInMinutes > t2.getBreakTime() + Constants.SLOT_LENGTH_MIN * (t1.getStartSlot() - t2.getStartSlot() - t2.getLength()))
                        pref = Math.max(pref, (iIgnoreDistances ? Constants.sPreferenceLevelStronglyDiscouraged : Constants.sPreferenceLevelProhibited));
                    else if (distanceInMinutes > Constants.SLOT_LENGTH_MIN * (t1.getStartSlot() - t2.getStartSlot() - t2.getLength()))
                        pref = Math.max(pref, Constants.sPreferenceLevelDiscouraged);
                }
                if (t1.getStartSlot() + t1.getLength() <= t2.getStartSlot()) {
                    if (after == null || t2.getStartSlot() < after.getTimeLocation().getStartSlot())
                        after = otherPlacement;
                } else if (t2.getStartSlot() + t2.getLength() <= t1.getStartSlot()) {
                    if (before == null || before.getTimeLocation().getStartSlot() < t2.getStartSlot())
                        before = otherPlacement;
                }
            }
            if (iUnavailabilities != null) {
                for (Placement c : iUnavailabilities) {
                    TimeLocation t2 = c.getTimeLocation();
                    if (t1 == null || t2 == null || !t1.shareDays(t2) || !t1.shareWeeks(t2))
                        continue;
                    if (t1.getStartSlot() + t1.getLength() <= t2.getStartSlot()) {
                        if (after == null || t2.getStartSlot() < after.getTimeLocation().getStartSlot())
                            after = c;
                    } else if (t2.getStartSlot() + t2.getLength() <= t1.getStartSlot()) {
                        if (before == null || before.getTimeLocation().getStartSlot() < t2.getStartSlot())
                            before = c;
                    }
                }
            }
            int tooLongTravel = 0;
            if (before != null && Placement.getDistanceInMinutes(getDistanceMetric(), before, placement) > getDistanceMetric().getInstructorLongTravelInMinutes())
                tooLongTravel++;
            if (after != null && Placement.getDistanceInMinutes(getDistanceMetric(), after, placement) > getDistanceMetric().getInstructorLongTravelInMinutes())
                tooLongTravel++;
            if (tooLongTravel > 0)
                pref += Math.max(pref, Constants.sPreferenceLevelStronglyDiscouraged);
        }
    }
    return pref;
}
Also used : Lecture(org.cpsolver.coursett.model.Lecture) TimeLocation(org.cpsolver.coursett.model.TimeLocation) Placement(org.cpsolver.coursett.model.Placement) HashSet(java.util.HashSet)

Example 27 with TimeLocation

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

the class GroupConstraint method isEveryOtherDay.

private boolean isEveryOtherDay(Placement p1, Placement p2, boolean firstGoesFirst) {
    int ord1 = variables().indexOf(p1.variable());
    int ord2 = variables().indexOf(p2.variable());
    TimeLocation t1 = null, t2 = null;
    if (ord1 < ord2) {
        if (firstGoesFirst) {
            t1 = p1.getTimeLocation();
            t2 = p2.getTimeLocation();
        } else {
            t2 = p1.getTimeLocation();
            t1 = p2.getTimeLocation();
        }
    } else {
        if (!firstGoesFirst) {
            t1 = p1.getTimeLocation();
            t2 = p2.getTimeLocation();
        } else {
            t2 = p1.getTimeLocation();
            t1 = p2.getTimeLocation();
        }
    }
    int f1 = -1, f2 = -1, e1 = -1;
    for (int i = 0; i < Constants.DAY_CODES.length; i++) {
        if ((t1.getDayCode() & Constants.DAY_CODES[i]) != 0) {
            if (f1 < 0)
                f1 = i;
            e1 = i;
        }
        if ((t2.getDayCode() & Constants.DAY_CODES[i]) != 0) {
            if (f2 < 0)
                f2 = i;
        }
    }
    return ((e1 + 2) % iNrWorkDays == f2);
}
Also used : TimeLocation(org.cpsolver.coursett.model.TimeLocation) WeakeningConstraint(org.cpsolver.ifs.model.WeakeningConstraint) Constraint(org.cpsolver.ifs.model.Constraint) GlobalConstraint(org.cpsolver.ifs.model.GlobalConstraint)

Example 28 with TimeLocation

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

the class GroupConstraint method isPrecedence.

private boolean isPrecedence(Placement p1, Placement p2, boolean firstGoesFirst, boolean considerDatePatterns) {
    int ord1 = variables().indexOf(p1.variable());
    int ord2 = variables().indexOf(p2.variable());
    TimeLocation t1 = null, t2 = null;
    if (ord1 < ord2) {
        if (firstGoesFirst) {
            t1 = p1.getTimeLocation();
            t2 = p2.getTimeLocation();
        } else {
            t2 = p1.getTimeLocation();
            t1 = p2.getTimeLocation();
        }
    } else {
        if (!firstGoesFirst) {
            t1 = p1.getTimeLocation();
            t2 = p2.getTimeLocation();
        } else {
            t2 = p1.getTimeLocation();
            t1 = p2.getTimeLocation();
        }
    }
    if (considerDatePatterns && iPrecedenceConsiderDatePatterns) {
        boolean sameDatePattern = (t1.getDatePatternId() != null ? t1.getDatePatternId().equals(t2.getDatePatternId()) : t1.getWeekCode().equals(t2.getWeekCode()));
        if (!sameDatePattern) {
            int m1 = t1.getFirstMeeting(iDayOfWeekOffset), m2 = t2.getFirstMeeting(iDayOfWeekOffset);
            if (m1 != m2)
                return m1 < m2;
        }
    }
    return t1.getStartSlots().nextElement() + t1.getLength() <= t2.getStartSlots().nextElement();
}
Also used : TimeLocation(org.cpsolver.coursett.model.TimeLocation) WeakeningConstraint(org.cpsolver.ifs.model.WeakeningConstraint) Constraint(org.cpsolver.ifs.model.Constraint) GlobalConstraint(org.cpsolver.ifs.model.GlobalConstraint)

Example 29 with TimeLocation

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

the class GroupConstraint method isFollowingDay.

private boolean isFollowingDay(Placement p1, Placement p2, boolean firstGoesFirst) {
    int ord1 = variables().indexOf(p1.variable());
    int ord2 = variables().indexOf(p2.variable());
    TimeLocation t1 = null, t2 = null;
    if (ord1 < ord2) {
        if (firstGoesFirst) {
            t1 = p1.getTimeLocation();
            t2 = p2.getTimeLocation();
        } else {
            t2 = p1.getTimeLocation();
            t1 = p2.getTimeLocation();
        }
    } else {
        if (!firstGoesFirst) {
            t1 = p1.getTimeLocation();
            t2 = p2.getTimeLocation();
        } else {
            t2 = p1.getTimeLocation();
            t1 = p2.getTimeLocation();
        }
    }
    int f1 = -1, f2 = -1, e1 = -1;
    for (int i = 0; i < Constants.DAY_CODES.length; i++) {
        if ((t1.getDayCode() & Constants.DAY_CODES[i]) != 0) {
            if (f1 < 0)
                f1 = i;
            e1 = i;
        }
        if ((t2.getDayCode() & Constants.DAY_CODES[i]) != 0) {
            if (f2 < 0)
                f2 = i;
        }
    }
    return ((e1 + 1) % iNrWorkDays == f2);
}
Also used : TimeLocation(org.cpsolver.coursett.model.TimeLocation) WeakeningConstraint(org.cpsolver.ifs.model.WeakeningConstraint) Constraint(org.cpsolver.ifs.model.Constraint) GlobalConstraint(org.cpsolver.ifs.model.GlobalConstraint)

Example 30 with TimeLocation

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

the class InstructorConstraint method getDistancePreference.

/** Back-to-back preference of two placements (3 means prohibited) 
     * @param p1 first placement
     * @param p2 second placement
     * @return distance preference between the two placements
     **/
public int getDistancePreference(Placement p1, Placement p2) {
    TimeLocation t1 = p1.getTimeLocation();
    TimeLocation t2 = p2.getTimeLocation();
    if (t1 == null || t2 == null || !t1.shareDays(t2) || !t1.shareWeeks(t2))
        return Constants.sPreferenceLevelNeutral;
    if (t1.getStartSlot() + t1.getLength() == t2.getStartSlot() || t2.getStartSlot() + t2.getLength() == t1.getStartSlot()) {
        double distance = Placement.getDistanceInMeters(getDistanceMetric(), p1, p2);
        if (distance <= getDistanceMetric().getInstructorNoPreferenceLimit())
            return Constants.sPreferenceLevelNeutral;
        if (distance <= getDistanceMetric().getInstructorDiscouragedLimit())
            return Constants.sPreferenceLevelDiscouraged;
        if (iIgnoreDistances || distance <= getDistanceMetric().getInstructorProhibitedLimit())
            return Constants.sPreferenceLevelStronglyDiscouraged;
        return Constants.sPreferenceLevelProhibited;
    } else if (getDistanceMetric().doComputeDistanceConflictsBetweenNonBTBClasses()) {
        if (t1.getStartSlot() + t1.getLength() < t2.getStartSlot()) {
            int distanceInMinutes = Placement.getDistanceInMinutes(getDistanceMetric(), p1, p2);
            if (// too far apart
            distanceInMinutes > t1.getBreakTime() + Constants.SLOT_LENGTH_MIN * (t2.getStartSlot() - t1.getStartSlot() - t1.getLength()))
                return (iIgnoreDistances ? Constants.sPreferenceLevelStronglyDiscouraged : Constants.sPreferenceLevelProhibited);
            if (// long travel
            distanceInMinutes >= getDistanceMetric().getInstructorLongTravelInMinutes())
                return Constants.sPreferenceLevelStronglyDiscouraged;
            if (// too far if no break time
            distanceInMinutes > Constants.SLOT_LENGTH_MIN * (t2.getStartSlot() - t1.getStartSlot() - t1.getLength()))
                return Constants.sPreferenceLevelDiscouraged;
        } else if (t2.getStartSlot() + t2.getLength() < t1.getStartSlot()) {
            int distanceInMinutes = Placement.getDistanceInMinutes(getDistanceMetric(), p1, p2);
            if (// too far apart
            distanceInMinutes > t2.getBreakTime() + Constants.SLOT_LENGTH_MIN * (t1.getStartSlot() - t2.getStartSlot() - t2.getLength()))
                return (iIgnoreDistances ? Constants.sPreferenceLevelStronglyDiscouraged : Constants.sPreferenceLevelProhibited);
            if (// long travel
            distanceInMinutes >= getDistanceMetric().getInstructorLongTravelInMinutes())
                return Constants.sPreferenceLevelStronglyDiscouraged;
            if (// too far if no break time
            distanceInMinutes > Constants.SLOT_LENGTH_MIN * (t1.getStartSlot() - t2.getStartSlot() - t2.getLength()))
                return Constants.sPreferenceLevelDiscouraged;
        }
    }
    return Constants.sPreferenceLevelNeutral;
}
Also used : TimeLocation(org.cpsolver.coursett.model.TimeLocation)

Aggregations

TimeLocation (org.cpsolver.coursett.model.TimeLocation)36 Placement (org.cpsolver.coursett.model.Placement)17 Lecture (org.cpsolver.coursett.model.Lecture)14 HashSet (java.util.HashSet)12 RoomLocation (org.cpsolver.coursett.model.RoomLocation)9 Constraint (org.cpsolver.ifs.model.Constraint)9 ArrayList (java.util.ArrayList)8 BitSet (java.util.BitSet)7 HashMap (java.util.HashMap)7 Element (org.dom4j.Element)7 WeakeningConstraint (org.cpsolver.ifs.model.WeakeningConstraint)6 DecimalFormat (java.text.DecimalFormat)5 List (java.util.List)4 Map (java.util.Map)4 TreeSet (java.util.TreeSet)4 RoomConstraint (org.cpsolver.coursett.constraint.RoomConstraint)4 GroupConstraint (org.cpsolver.coursett.constraint.GroupConstraint)3 InstructorConstraint (org.cpsolver.coursett.constraint.InstructorConstraint)3 SpreadConstraint (org.cpsolver.coursett.constraint.SpreadConstraint)3 Student (org.cpsolver.coursett.model.Student)3