Search in sources :

Example 31 with TimeLocation

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

the class PriorityStudentWeights method main.

/**
     * Test case -- run to see the weights for a few courses
     * @param args program arguments
     */
public static void main(String[] args) {
    PriorityStudentWeights pw = new PriorityStudentWeights(new DataProperties());
    DecimalFormat df = new DecimalFormat("0.0000");
    Student s = new Student(0l);
    new CourseRequest(1l, 0, false, s, ToolBox.toList(new Course(1, "A", "1", new Offering(0, "A")), new Course(1, "A", "2", new Offering(0, "A")), new Course(1, "A", "3", new Offering(0, "A"))), false, null);
    new CourseRequest(2l, 1, false, s, ToolBox.toList(new Course(1, "B", "1", new Offering(0, "B")), new Course(1, "B", "2", new Offering(0, "B")), new Course(1, "B", "3", new Offering(0, "B"))), false, null);
    new CourseRequest(3l, 2, false, s, ToolBox.toList(new Course(1, "C", "1", new Offering(0, "C")), new Course(1, "C", "2", new Offering(0, "C")), new Course(1, "C", "3", new Offering(0, "C"))), false, null);
    new CourseRequest(4l, 3, false, s, ToolBox.toList(new Course(1, "D", "1", new Offering(0, "D")), new Course(1, "D", "2", new Offering(0, "D")), new Course(1, "D", "3", new Offering(0, "D"))), false, null);
    new CourseRequest(5l, 4, false, s, ToolBox.toList(new Course(1, "E", "1", new Offering(0, "E")), new Course(1, "E", "2", new Offering(0, "E")), new Course(1, "E", "3", new Offering(0, "E"))), false, null);
    new CourseRequest(6l, 5, true, s, ToolBox.toList(new Course(1, "F", "1", new Offering(0, "F")), new Course(1, "F", "2", new Offering(0, "F")), new Course(1, "F", "3", new Offering(0, "F"))), false, null);
    new CourseRequest(7l, 6, true, s, ToolBox.toList(new Course(1, "G", "1", new Offering(0, "G")), new Course(1, "G", "2", new Offering(0, "G")), new Course(1, "G", "3", new Offering(0, "G"))), false, null);
    Assignment<Request, Enrollment> assignment = new DefaultSingleAssignment<Request, Enrollment>();
    Placement p = new Placement(null, new TimeLocation(1, 90, 12, 0, 0, null, null, new BitSet(), 10), new ArrayList<RoomLocation>());
    for (Request r : s.getRequests()) {
        CourseRequest cr = (CourseRequest) r;
        double[] w = new double[] { 0.0, 0.0, 0.0 };
        for (int i = 0; i < cr.getCourses().size(); i++) {
            Config cfg = new Config(0l, -1, "", cr.getCourses().get(i).getOffering());
            Set<SctAssignment> sections = new HashSet<SctAssignment>();
            sections.add(new Section(0, 1, "x", new Subpart(0, "Lec", "Lec", cfg, null), p, null));
            Enrollment e = new Enrollment(cr, i, cfg, sections, assignment);
            w[i] = pw.getWeight(assignment, e, null, null);
        }
        System.out.println(cr + ": " + df.format(w[0]) + "  " + df.format(w[1]) + "  " + df.format(w[2]));
    }
    System.out.println("With one distance conflict:");
    for (Request r : s.getRequests()) {
        CourseRequest cr = (CourseRequest) r;
        double[] w = new double[] { 0.0, 0.0, 0.0 };
        for (int i = 0; i < cr.getCourses().size(); i++) {
            Config cfg = new Config(0l, -1, "", cr.getCourses().get(i).getOffering());
            Set<SctAssignment> sections = new HashSet<SctAssignment>();
            sections.add(new Section(0, 1, "x", new Subpart(0, "Lec", "Lec", cfg, null), p, null));
            Enrollment e = new Enrollment(cr, i, cfg, sections, assignment);
            Set<DistanceConflict.Conflict> dc = new HashSet<DistanceConflict.Conflict>();
            dc.add(new DistanceConflict.Conflict(s, e, (Section) sections.iterator().next(), e, (Section) sections.iterator().next()));
            w[i] = pw.getWeight(assignment, e, dc, null);
        }
        System.out.println(cr + ": " + df.format(w[0]) + "  " + df.format(w[1]) + "  " + df.format(w[2]));
    }
    System.out.println("With two distance conflicts:");
    for (Request r : s.getRequests()) {
        CourseRequest cr = (CourseRequest) r;
        double[] w = new double[] { 0.0, 0.0, 0.0 };
        for (int i = 0; i < cr.getCourses().size(); i++) {
            Config cfg = new Config(0l, -1, "", cr.getCourses().get(i).getOffering());
            Set<SctAssignment> sections = new HashSet<SctAssignment>();
            sections.add(new Section(0, 1, "x", new Subpart(0, "Lec", "Lec", cfg, null), p, null));
            Enrollment e = new Enrollment(cr, i, cfg, sections, assignment);
            Set<DistanceConflict.Conflict> dc = new HashSet<DistanceConflict.Conflict>();
            dc.add(new DistanceConflict.Conflict(s, e, (Section) sections.iterator().next(), e, (Section) sections.iterator().next()));
            dc.add(new DistanceConflict.Conflict(s, e, (Section) sections.iterator().next(), e, new Section(1, 1, "x", new Subpart(0, "Lec", "Lec", cfg, null), p, null)));
            w[i] = pw.getWeight(assignment, e, dc, null);
        }
        System.out.println(cr + ": " + df.format(w[0]) + "  " + df.format(w[1]) + "  " + df.format(w[2]));
    }
    System.out.println("With 25% time overlapping conflict:");
    for (Request r : s.getRequests()) {
        CourseRequest cr = (CourseRequest) r;
        double[] w = new double[] { 0.0, 0.0, 0.0 };
        for (int i = 0; i < cr.getCourses().size(); i++) {
            Config cfg = new Config(0l, -1, "", cr.getCourses().get(i).getOffering());
            Set<SctAssignment> sections = new HashSet<SctAssignment>();
            sections.add(new Section(0, 1, "x", new Subpart(0, "Lec", "Lec", cfg, null), p, null));
            Enrollment e = new Enrollment(cr, i, cfg, sections, assignment);
            Set<TimeOverlapsCounter.Conflict> toc = new HashSet<TimeOverlapsCounter.Conflict>();
            toc.add(new TimeOverlapsCounter.Conflict(s, 3, e, sections.iterator().next(), e, sections.iterator().next()));
            w[i] = pw.getWeight(assignment, e, null, toc);
        }
        System.out.println(cr + ": " + df.format(w[0]) + "  " + df.format(w[1]) + "  " + df.format(w[2]));
    }
    System.out.println("Disbalanced sections (by 2 / 10 students):");
    for (Request r : s.getRequests()) {
        CourseRequest cr = (CourseRequest) r;
        double[] w = new double[] { 0.0, 0.0, 0.0 };
        for (int i = 0; i < cr.getCourses().size(); i++) {
            Config cfg = new Config(0l, -1, "", cr.getCourses().get(i).getOffering());
            Set<SctAssignment> sections = new HashSet<SctAssignment>();
            Subpart x = new Subpart(0, "Lec", "Lec", cfg, null);
            Section a = new Section(0, 10, "x", x, p, null);
            new Section(1, 10, "y", x, p, null);
            sections.add(a);
            a.assigned(assignment, new Enrollment(s.getRequests().get(0), i, cfg, sections, assignment));
            a.assigned(assignment, new Enrollment(s.getRequests().get(0), i, cfg, sections, assignment));
            cfg.getContext(assignment).assigned(assignment, new Enrollment(s.getRequests().get(0), i, cfg, sections, assignment));
            cfg.getContext(assignment).assigned(assignment, new Enrollment(s.getRequests().get(0), i, cfg, sections, assignment));
            Enrollment e = new Enrollment(cr, i, cfg, sections, assignment);
            w[i] = pw.getWeight(assignment, e, null, null);
        }
        System.out.println(cr + ": " + df.format(w[0]) + "  " + df.format(w[1]) + "  " + df.format(w[2]));
    }
    System.out.println("Same choice sections:");
    pw.iMPP = true;
    for (Request r : s.getRequests()) {
        CourseRequest cr = (CourseRequest) r;
        double[] w = new double[] { 0.0, 0.0, 0.0 };
        for (int i = 0; i < cr.getCourses().size(); i++) {
            Config cfg = new Config(0l, -1, "", cr.getCourses().get(i).getOffering());
            Set<SctAssignment> sections = new HashSet<SctAssignment>();
            sections.add(new Section(0, 1, "x", new Subpart(0, "Lec", "Lec", cfg, null), p, null));
            Enrollment e = new Enrollment(cr, i, cfg, sections, assignment);
            Set<SctAssignment> other = new HashSet<SctAssignment>();
            other.add(new Section(1, 1, "x", new Subpart(0, "Lec", "Lec", cfg, null), p, null));
            cr.setInitialAssignment(new Enrollment(cr, i, cfg, other, assignment));
            w[i] = pw.getWeight(assignment, e, null, null);
        }
        System.out.println(cr + ": " + df.format(w[0]) + "  " + df.format(w[1]) + "  " + df.format(w[2]));
    }
    System.out.println("Same time sections:");
    for (Request r : s.getRequests()) {
        CourseRequest cr = (CourseRequest) r;
        double[] w = new double[] { 0.0, 0.0, 0.0 };
        for (int i = 0; i < cr.getCourses().size(); i++) {
            Config cfg = new Config(0l, -1, "", cr.getCourses().get(i).getOffering());
            Set<SctAssignment> sections = new HashSet<SctAssignment>();
            sections.add(new Section(0, 1, "x", new Subpart(0, "Lec", "Lec", cfg, null), p, null));
            Enrollment e = new Enrollment(cr, i, cfg, sections, assignment);
            Set<SctAssignment> other = new HashSet<SctAssignment>();
            other.add(new Section(1, 1, "x", new Subpart(0, "Lec", "Lec", cfg, null), p, null, new Instructor(1l, null, "Josef Novak", null)));
            cr.setInitialAssignment(new Enrollment(cr, i, cfg, other, assignment));
            w[i] = pw.getWeight(assignment, e, null, null);
        }
        System.out.println(cr + ": " + df.format(w[0]) + "  " + df.format(w[1]) + "  " + df.format(w[2]));
    }
    System.out.println("Different time sections:");
    Placement q = new Placement(null, new TimeLocation(1, 102, 12, 0, 0, null, null, new BitSet(), 10), new ArrayList<RoomLocation>());
    for (Request r : s.getRequests()) {
        CourseRequest cr = (CourseRequest) r;
        double[] w = new double[] { 0.0, 0.0, 0.0 };
        for (int i = 0; i < cr.getCourses().size(); i++) {
            Config cfg = new Config(0l, -1, "", cr.getCourses().get(i).getOffering());
            Set<SctAssignment> sections = new HashSet<SctAssignment>();
            sections.add(new Section(0, 1, "x", new Subpart(0, "Lec", "Lec", cfg, null), p, null));
            Enrollment e = new Enrollment(cr, i, cfg, sections, assignment);
            Set<SctAssignment> other = new HashSet<SctAssignment>();
            other.add(new Section(1, 1, "x", new Subpart(0, "Lec", "Lec", cfg, null), q, null));
            cr.setInitialAssignment(new Enrollment(cr, i, cfg, other, assignment));
            w[i] = pw.getWeight(assignment, e, null, null);
        }
        System.out.println(cr + ": " + df.format(w[0]) + "  " + df.format(w[1]) + "  " + df.format(w[2]));
    }
    System.out.println("Two sections, one same choice, one same time:");
    for (Request r : s.getRequests()) {
        CourseRequest cr = (CourseRequest) r;
        double[] w = new double[] { 0.0, 0.0, 0.0 };
        for (int i = 0; i < cr.getCourses().size(); i++) {
            Config cfg = new Config(0l, -1, "", cr.getCourses().get(i).getOffering());
            Set<SctAssignment> sections = new HashSet<SctAssignment>();
            sections.add(new Section(0, 1, "x", new Subpart(0, "Lec", "Lec", cfg, null), p, null));
            sections.add(new Section(1, 1, "y", new Subpart(1, "Rec", "Rec", cfg, null), p, null));
            Enrollment e = new Enrollment(cr, i, cfg, sections, assignment);
            Set<SctAssignment> other = new HashSet<SctAssignment>();
            other.add(new Section(2, 1, "x", new Subpart(0, "Lec", "Lec", cfg, null), p, null));
            other.add(new Section(3, 1, "y", new Subpart(1, "Rec", "Rec", cfg, null), p, null, new Instructor(1l, null, "Josef Novak", null)));
            cr.setInitialAssignment(new Enrollment(cr, i, cfg, other, assignment));
            w[i] = pw.getWeight(assignment, e, null, null);
        }
        System.out.println(cr + ": " + df.format(w[0]) + "  " + df.format(w[1]) + "  " + df.format(w[2]));
    }
}
Also used : TimeLocation(org.cpsolver.coursett.model.TimeLocation) Config(org.cpsolver.studentsct.model.Config) DecimalFormat(java.text.DecimalFormat) DistanceConflict(org.cpsolver.studentsct.extension.DistanceConflict) Instructor(org.cpsolver.studentsct.model.Instructor) DataProperties(org.cpsolver.ifs.util.DataProperties) Placement(org.cpsolver.coursett.model.Placement) Enrollment(org.cpsolver.studentsct.model.Enrollment) Course(org.cpsolver.studentsct.model.Course) HashSet(java.util.HashSet) RoomLocation(org.cpsolver.coursett.model.RoomLocation) Request(org.cpsolver.studentsct.model.Request) CourseRequest(org.cpsolver.studentsct.model.CourseRequest) BitSet(java.util.BitSet) Student(org.cpsolver.studentsct.model.Student) Offering(org.cpsolver.studentsct.model.Offering) Section(org.cpsolver.studentsct.model.Section) TimeOverlapsCounter(org.cpsolver.studentsct.extension.TimeOverlapsCounter) CourseRequest(org.cpsolver.studentsct.model.CourseRequest) DistanceConflict(org.cpsolver.studentsct.extension.DistanceConflict) Subpart(org.cpsolver.studentsct.model.Subpart) DefaultSingleAssignment(org.cpsolver.ifs.assignment.DefaultSingleAssignment) SctAssignment(org.cpsolver.studentsct.model.SctAssignment)

Example 32 with TimeLocation

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

the class BrokenTimePatterns method countUselessSlotsBrokenTimePatterns.

/** Number of broken time patterns for this room 
     * @param rc room constraint
     * @param placement placement that is being considered
     * @return number of broken time patterns caused by the given placement
     **/
protected static int countUselessSlotsBrokenTimePatterns(RoomConstraintContext rc, Placement placement) {
    int ret = 0;
    TimeLocation time = placement.getTimeLocation();
    int slot = time.getStartSlot() % Constants.SLOTS_PER_DAY;
    int days = time.getDayCode();
    if ((days & sDaysMWF) != 0 && (days & sDaysMWF) != sDaysMWF) {
        for (int s = slot; s < slot + time.getLength(); s++) {
            int d = (days & sDaysMWF);
            if (d == Constants.DAY_CODES[0] && isEmpty(rc, s, 0, placement)) {
                if (isEmpty(rc, s, 2, placement) != isEmpty(rc, s, 4, placement))
                    ret++;
                if (!isEmpty(rc, s, 2, placement) && !isEmpty(rc, s, 4, placement))
                    ret--;
            } else if (d == Constants.DAY_CODES[2] && isEmpty(rc, s, 2, placement)) {
                if (isEmpty(rc, s, 0, placement) != isEmpty(rc, s, 4, placement))
                    ret++;
                if (!isEmpty(rc, s, 0, placement) && !isEmpty(rc, s, 4, placement))
                    ret--;
            } else if (d == Constants.DAY_CODES[4] && isEmpty(rc, s, 4, placement)) {
                if (isEmpty(rc, s, 0, placement) != isEmpty(rc, s, 2, placement))
                    ret++;
                if (!isEmpty(rc, s, 0, placement) && !isEmpty(rc, s, 2, placement))
                    ret--;
            } else if (d == (Constants.DAY_CODES[0] | Constants.DAY_CODES[2]) && isEmpty(rc, s, 0, placement) && isEmpty(rc, s, 2, placement)) {
                if (isEmpty(rc, s, 4, placement))
                    ret++;
            } else if (d == (Constants.DAY_CODES[2] | Constants.DAY_CODES[4]) && isEmpty(rc, s, 2, placement) && isEmpty(rc, s, 4, placement)) {
                if (isEmpty(rc, s, 0, placement))
                    ret++;
            } else if (d == (Constants.DAY_CODES[0] | Constants.DAY_CODES[4]) && isEmpty(rc, s, 0, placement) && isEmpty(rc, s, 4, placement)) {
                if (isEmpty(rc, s, 2, placement))
                    ret++;
            }
        }
    }
    if ((days & sDaysTTh) != 0 && (days & sDaysTTh) != sDaysTTh) {
        for (int s = slot; s < slot + time.getLength(); s++) {
            if (isEmpty(rc, s, 1, placement) && isEmpty(rc, s, 3, placement))
                ret++;
            int d = (days & sDaysTTh);
            if (d == Constants.DAY_CODES[1] && isEmpty(rc, s, 1, placement) && !isEmpty(rc, s, 3, placement))
                ret--;
            if (d == Constants.DAY_CODES[3] && isEmpty(rc, s, 3, placement) && !isEmpty(rc, s, 1, placement))
                ret--;
        }
    }
    return ret;
}
Also used : TimeLocation(org.cpsolver.coursett.model.TimeLocation) RoomConstraint(org.cpsolver.coursett.constraint.RoomConstraint)

Example 33 with TimeLocation

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

the class MinimizeNumberOfUsedGroupsOfTime method estimateLimit.

public int estimateLimit() {
    int nrSlotsUsed = 0;
    int minSlotsUsed = 0;
    boolean firstLecture = true;
    for (Lecture lecture : variables()) {
        boolean first = true;
        int minSlotsUsedThisLecture = 0;
        for (TimeLocation time : lecture.timeLocations()) {
            int min = 0;
            for (int i = 0; i < iGroupsOfTime.length; i++) {
                if (iGroupsOfTime[i].overlap(time))
                    min++;
            }
            if (first) {
                nrSlotsUsed += time.getLength() * time.getNrMeetings();
                minSlotsUsedThisLecture = min;
                first = false;
            } else {
                minSlotsUsedThisLecture = Math.min(minSlotsUsedThisLecture, min);
            }
        }
        if (firstLecture) {
            minSlotsUsed = minSlotsUsedThisLecture;
            firstLecture = false;
        } else {
            minSlotsUsed = Math.min(minSlotsUsed, minSlotsUsedThisLecture);
        }
    }
    return Math.max(Math.max(1, (int) Math.ceil(((double) nrSlotsUsed) / iGroupsOfTime[0].size())), minSlotsUsed);
}
Also used : Lecture(org.cpsolver.coursett.model.Lecture) TimeLocation(org.cpsolver.coursett.model.TimeLocation) WeakeningConstraint(org.cpsolver.ifs.model.WeakeningConstraint)

Example 34 with TimeLocation

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

the class TimeSwap method resolve.

@Override
public Double resolve(Solution<Lecture, Placement> solution, double total, long startTime, Map<Lecture, Placement> assignments, List<Placement> conflicts, int index) {
    Assignment<Lecture, Placement> assignment = solution.getAssignment();
    if (index == conflicts.size())
        return solution.getModel().getTotalValue(assignment) - total;
    Placement conflict = conflicts.get(index);
    Lecture variable = conflict.variable();
    List<TimeLocation> values = variable.timeLocations();
    if (values.isEmpty())
        return null;
    int valIdx = ToolBox.random(values.size());
    int attempts = 0;
    for (int i = 0; i < values.size(); i++) {
        TimeLocation time = values.get((i + valIdx) % values.size());
        if (time.getPreference() > 50)
            continue;
        if (time.equals(conflict.getTimeLocation()))
            continue;
        Placement value = null;
        if (variable.getNrRooms() == 0)
            value = new Placement(variable, time, (RoomLocation) null);
        else if (variable.getNrRooms() == 1)
            value = new Placement(variable, time, conflict.getRoomLocation());
        else
            value = new Placement(variable, time, conflict.getRoomLocations());
        if (!value.isValid() || solution.getModel().inConflict(assignment, value))
            continue;
        assignment.assign(solution.getIteration(), value);
        Double v = resolve(solution, total, startTime, assignments, conflicts, 1 + index);
        assignment.unassign(solution.getIteration(), variable);
        attempts++;
        if (v != null && (!iHC || v <= 0)) {
            assignments.put(variable, value);
            return v;
        }
        if (attempts >= iMaxAttempts || isTimeLimitReached(startTime))
            break;
    }
    return null;
}
Also used : Lecture(org.cpsolver.coursett.model.Lecture) TimeLocation(org.cpsolver.coursett.model.TimeLocation) Placement(org.cpsolver.coursett.model.Placement)

Example 35 with TimeLocation

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

the class TimeChange method selectNeighbour.

@Override
public Neighbour<Lecture, Placement> selectNeighbour(Solution<Lecture, Placement> solution) {
    TimetableModel model = (TimetableModel) solution.getModel();
    Assignment<Lecture, Placement> assignment = solution.getAssignment();
    int varIdx = ToolBox.random(model.variables().size());
    for (int i = 0; i < model.variables().size(); i++) {
        Lecture lecture = model.variables().get((i + varIdx) % model.variables().size());
        Placement old = assignment.getValue(lecture);
        if (old == null)
            continue;
        List<TimeLocation> values = lecture.timeLocations();
        if (values.isEmpty())
            continue;
        int valIdx = ToolBox.random(values.size());
        for (int j = 0; j < values.size(); j++) {
            TimeLocation time = values.get((j + valIdx) % values.size());
            if (time.getPreference() > 50)
                continue;
            Placement placement = null;
            if (lecture.getNrRooms() == 0)
                placement = new Placement(lecture, time, (RoomLocation) null);
            else if (lecture.getNrRooms() == 1)
                placement = new Placement(lecture, time, old.getRoomLocation());
            else
                placement = new Placement(lecture, time, old.getRoomLocations());
            if (placement.isValid() && !model.inConflict(assignment, placement)) {
                SimpleNeighbour<Lecture, Placement> n = new SimpleNeighbour<Lecture, Placement>(lecture, placement);
                if (!iHC || n.value(assignment) <= 0)
                    return n;
            }
        }
    }
    return null;
}
Also used : Lecture(org.cpsolver.coursett.model.Lecture) TimeLocation(org.cpsolver.coursett.model.TimeLocation) Placement(org.cpsolver.coursett.model.Placement) SimpleNeighbour(org.cpsolver.ifs.model.SimpleNeighbour) TimetableModel(org.cpsolver.coursett.model.TimetableModel)

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