Search in sources :

Example 61 with Placement

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

the class GroupConstraint method isChildrenNotOverlap.

public boolean isChildrenNotOverlap(Assignment<Lecture, Placement> assignment, Lecture lec1, Placement plc1, Lecture lec2, Placement plc2) {
    if (lec1.getSchedulingSubpartId().equals(lec2.getSchedulingSubpartId())) {
        // same subpart
        boolean overlap = plc1.getTimeLocation().hasIntersection(plc2.getTimeLocation());
        if (overlap && lec1.getParent() != null && variables().contains(lec1.getParent()) && lec2.getParent() != null && variables().contains(lec2.getParent())) {
            // children overlaps
            Placement p1 = assignment.getValue(lec1.getParent());
            Placement p2 = assignment.getValue(lec2.getParent());
            // parents not overlap, but children do
            if (p1 != null && p2 != null && !p1.getTimeLocation().hasIntersection(p2.getTimeLocation()))
                return false;
        }
        if (!overlap && lec1.getChildrenSubpartIds() != null && lec2.getChildrenSubpartIds() != null) {
            // parents not overlap
            for (Long subpartId : lec1.getChildrenSubpartIds()) {
                for (Lecture c1 : lec1.getChildren(subpartId)) {
                    Placement p1 = assignment.getValue(c1);
                    if (p1 == null)
                        continue;
                    for (Lecture c2 : lec2.getChildren(subpartId)) {
                        Placement p2 = assignment.getValue(c2);
                        if (p2 == null)
                            continue;
                        if (!c1.getSchedulingSubpartId().equals(c2.getSchedulingSubpartId()))
                            continue;
                        // parents not overlap, but children do
                        if (p1.getTimeLocation().hasIntersection(p2.getTimeLocation()))
                            return false;
                    }
                }
            }
        }
    } else {
    // different subpart
    }
    return true;
}
Also used : Lecture(org.cpsolver.coursett.model.Lecture) Placement(org.cpsolver.coursett.model.Placement)

Example 62 with Placement

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

the class ClassLimitConstraint method computeConflicts.

@Override
public void computeConflicts(Assignment<Lecture, Placement> assignment, Placement value, Set<Placement> conflicts) {
    if (!iEnabled)
        return;
    int currentLimit = currentClassLimit(assignment, value, conflicts);
    int classLimit = classLimit();
    if (currentLimit < classLimit) {
        // System.out.println(getName()+"> "+currentLimit+"<"+classLimit+" ("+value+")");
        TreeSet<Placement> adepts = new TreeSet<Placement>(new ClassLimitComparator());
        computeAdepts(assignment, adepts, variables(), value, conflicts);
        addParentAdepts(assignment, adepts, iParent, value, conflicts);
        // System.out.println(" -- found "+adepts.size()+" adepts");
        for (Placement adept : adepts) {
            // System.out.println("   -- selected "+adept);
            conflicts.add(adept);
            currentLimit = currentClassLimit(assignment, value, conflicts);
            // System.out.println("   -- new current limit "+currentLimit);
            if (currentLimit >= classLimit)
                break;
        }
    // System.out.println(" -- done (currentLimit="+currentLimit+", classLimit="+classLimit+")");
    }
    if (currentLimit < classLimit)
        conflicts.add(value);
    if (iParent != null && iParent.getClassLimitConstraint() != null)
        iParent.getClassLimitConstraint().computeConflicts(assignment, value, conflicts);
}
Also used : Placement(org.cpsolver.coursett.model.Placement) TreeSet(java.util.TreeSet) Constraint(org.cpsolver.ifs.model.Constraint)

Example 63 with Placement

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

the class ClassLimitConstraint method addParentAdepts.

public void addParentAdepts(Assignment<Lecture, Placement> assignment, Collection<Placement> adepts, Lecture parent, Placement value, Set<Placement> conflicts) {
    if (parent == null || parent.isCommitted() || parent.minClassLimit() == parent.maxClassLimit())
        return;
    Placement placement = assignment.getValue(parent);
    if (placement != null && !placement.equals(value) && !conflicts.contains(placement)) {
        adepts.add(placement);
    }
    addParentAdepts(assignment, adepts, parent.getParent(), value, conflicts);
}
Also used : Placement(org.cpsolver.coursett.model.Placement)

Example 64 with Placement

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

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

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