Search in sources :

Example 6 with DefaultSingleAssignment

use of org.cpsolver.ifs.assignment.DefaultSingleAssignment in project cpsolver by UniTime.

the class StudentSctBBTest method getSolution.

/**
 * Compute and return the sectioning solution. It contains only the given
 * student with his/her schedule
 * @return current solution
 */
public Solution<Request, Enrollment> getSolution() {
    if (iSolution == null) {
        iSolution = new Solution<Request, Enrollment>(this, new DefaultSingleAssignment<Request, Enrollment>());
        BranchBoundSelection.Selection selection = new BranchBoundSelection(new DataProperties()).getSelection(iSolution.getAssignment(), getStudent());
        Neighbour<Request, Enrollment> neighbour = selection.select();
        if (neighbour != null)
            neighbour.assign(iSolution.getAssignment(), 0);
        iTime = selection.getTime();
        iTimeoutReached = selection.isTimeoutReached();
    }
    return iSolution;
}
Also used : BranchBoundSelection(org.cpsolver.studentsct.heuristics.selection.BranchBoundSelection) CourseRequest(org.cpsolver.studentsct.model.CourseRequest) Request(org.cpsolver.studentsct.model.Request) DataProperties(org.cpsolver.ifs.util.DataProperties) Enrollment(org.cpsolver.studentsct.model.Enrollment) DefaultSingleAssignment(org.cpsolver.ifs.assignment.DefaultSingleAssignment)

Example 7 with DefaultSingleAssignment

use of org.cpsolver.ifs.assignment.DefaultSingleAssignment in project cpsolver by UniTime.

the class EqualStudentWeights method main.

/**
 * Test case -- run to see the weights for a few courses
 * @param args program arguments
 */
public static void main(String[] args) {
    EqualStudentWeights pw = new EqualStudentWeights(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(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 sections:");
    pw.iMPP = true;
    for (Request r : s.getRequests()) {
        CourseRequest cr = (CourseRequest) r;
        double[] w = new double[] { 0.0, 0.0, 0.0 };
        double dif = 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);
            cr.setInitialAssignment(new Enrollment(cr, i, cfg, sections, assignment));
            w[i] = pw.getWeight(assignment, e, null, null);
            dif = pw.getDifference(e);
        }
        System.out.println(cr + ": " + df.format(w[0]) + "  " + df.format(w[1]) + "  " + df.format(w[2]) + " (" + df.format(dif) + ")");
    }
    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 };
        double dif = 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);
            dif = pw.getDifference(e);
        }
        System.out.println(cr + ": " + df.format(w[0]) + "  " + df.format(w[1]) + "  " + df.format(w[2]) + " (" + df.format(dif) + ")");
    }
    System.out.println("Same time sections:");
    for (Request r : s.getRequests()) {
        CourseRequest cr = (CourseRequest) r;
        double dif = 0;
        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(1, null, "Josef Novak", null)));
            cr.setInitialAssignment(new Enrollment(cr, i, cfg, other, assignment));
            w[i] = pw.getWeight(assignment, e, null, null);
            dif = pw.getDifference(e);
        }
        System.out.println(cr + ": " + df.format(w[0]) + "  " + df.format(w[1]) + "  " + df.format(w[2]) + " (" + df.format(dif) + ")");
    }
    System.out.println("Same configuration sections:");
    for (Request r : s.getRequests()) {
        CourseRequest cr = (CourseRequest) r;
        double[] w = new double[] { 0.0, 0.0, 0.0 };
        double dif = 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);
            cr.getSelectedChoices().add(new Choice(cfg));
            cr.setInitialAssignment(null);
            w[i] = pw.getWeight(assignment, e, null, null);
            dif = pw.getDifference(e);
        }
        System.out.println(cr + ": " + df.format(w[0]) + "  " + df.format(w[1]) + "  " + df.format(w[2]) + " (" + df.format(dif) + ")");
    }
    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 };
        double dif = 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);
            dif = pw.getDifference(e);
        }
        System.out.println(cr + ": " + df.format(w[0]) + "  " + df.format(w[1]) + "  " + df.format(w[2]) + " (" + df.format(dif) + ")");
    }
    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 };
        double dif = 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(1, null, "Josef Novak", null)));
            cr.setInitialAssignment(new Enrollment(cr, i, cfg, other, assignment));
            w[i] = pw.getWeight(assignment, e, null, null);
            dif = pw.getDifference(e);
        }
        System.out.println(cr + ": " + df.format(w[0]) + "  " + df.format(w[1]) + "  " + df.format(w[2]) + " (" + df.format(dif) + ")");
    }
}
Also used : Choice(org.cpsolver.studentsct.model.Choice) 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 8 with DefaultSingleAssignment

use of org.cpsolver.ifs.assignment.DefaultSingleAssignment in project cpsolver by UniTime.

the class OriginalStudentWeights method main.

/**
 * Test case -- run to see the weights for a few courses
 * @param args program arguments
 */
public static void main(String[] args) {
    OriginalStudentWeights pw = new OriginalStudentWeights(new DataProperties());
    DecimalFormat df = new DecimalFormat("0.000");
    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 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<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]));
    }
}
Also used : TimeLocation(org.cpsolver.coursett.model.TimeLocation) Config(org.cpsolver.studentsct.model.Config) DecimalFormat(java.text.DecimalFormat) DistanceConflict(org.cpsolver.studentsct.extension.DistanceConflict) 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 9 with DefaultSingleAssignment

use of org.cpsolver.ifs.assignment.DefaultSingleAssignment in project cpsolver by UniTime.

the class Solution2Expectations method main.

public static void main(String[] args) {
    try {
        ToolBox.configureLogging();
        DataProperties config = new DataProperties();
        String command = args[0];
        if ("real2exp".equals(command)) {
            StudentSectioningModel model = new StudentSectioningModel(config);
            Assignment<Request, Enrollment> assignment = new DefaultSingleAssignment<Request, Enrollment>();
            StudentSectioningXMLLoader loader = new StudentSectioningXMLLoader(model, assignment);
            loader.setInputFile(new File(args[1]));
            loader.load();
            sLog.info("Loaded: " + ToolBox.dict2string(model.getExtendedInfo(assignment), 2));
            for (Student s : model.getStudents()) s.setDummy(true);
            model.computeOnlineSectioningInfos(assignment);
            for (Student s : model.getStudents()) s.setDummy(false);
            for (Request request : model.variables()) assignment.unassign(0, request);
            Solution<Request, Enrollment> solution = new Solution<Request, Enrollment>(model, assignment, 0, 0);
            Solver<Request, Enrollment> solver = new Solver<Request, Enrollment>(config);
            solver.setInitalSolution(solution);
            new StudentSectioningXMLSaver(solver).save(new File(args[2]));
            sLog.info("Saved: " + ToolBox.dict2string(model.getExtendedInfo(assignment), 2));
        } else if ("ll2exp".equals(command)) {
            StudentSectioningModel model = new StudentSectioningModel(config);
            Assignment<Request, Enrollment> assignment = new DefaultSingleAssignment<Request, Enrollment>();
            StudentSectioningXMLLoader loader = new StudentSectioningXMLLoader(model, assignment);
            loader.setInputFile(new File(args[1]));
            loader.load();
            sLog.info("Loaded: " + ToolBox.dict2string(model.getExtendedInfo(assignment), 2));
            model.computeOnlineSectioningInfos(assignment);
            for (Request request : model.variables()) assignment.unassign(0, request);
            Solution<Request, Enrollment> solution = new Solution<Request, Enrollment>(model, assignment, 0, 0);
            Solver<Request, Enrollment> solver = new Solver<Request, Enrollment>(config);
            solver.setInitalSolution(solution);
            new StudentSectioningXMLSaver(solver).save(new File(args[2]));
            sLog.info("Saved: " + ToolBox.dict2string(model.getExtendedInfo(assignment), 2));
        } else if ("students".equals(command)) {
            StudentSectioningModel model = new StudentSectioningModel(config);
            Assignment<Request, Enrollment> assignment = new DefaultSingleAssignment<Request, Enrollment>();
            StudentSectioningXMLLoader loader = new StudentSectioningXMLLoader(model, assignment);
            loader.setInputFile(new File(args[1]));
            loader.setLoadStudents(false);
            loader.load();
            sLog.info("Loaded [1]: " + ToolBox.dict2string(model.getExtendedInfo(assignment), 2));
            StudentSectioningXMLLoader loder2 = new StudentSectioningXMLLoader(model, assignment);
            loder2.setInputFile(new File(args[2]));
            loder2.setLoadOfferings(false);
            loder2.setLoadStudents(true);
            loder2.load();
            sLog.info("Loaded [2]: " + ToolBox.dict2string(model.getExtendedInfo(assignment), 2));
            Solution<Request, Enrollment> solution = new Solution<Request, Enrollment>(model, assignment, 0, 0);
            Solver<Request, Enrollment> solver = new Solver<Request, Enrollment>(config);
            solver.setInitalSolution(solution);
            new StudentSectioningXMLSaver(solver).save(new File(args[3]));
            sLog.info("Saved [3]: " + ToolBox.dict2string(model.getExtendedInfo(assignment), 2));
        } else {
            sLog.error("Unknown command: " + command);
        }
    } catch (Exception e) {
        sLog.error(e.getMessage(), e);
    }
}
Also used : Solver(org.cpsolver.ifs.solver.Solver) DataProperties(org.cpsolver.ifs.util.DataProperties) Request(org.cpsolver.studentsct.model.Request) Student(org.cpsolver.studentsct.model.Student) DefaultSingleAssignment(org.cpsolver.ifs.assignment.DefaultSingleAssignment) Assignment(org.cpsolver.ifs.assignment.Assignment) Enrollment(org.cpsolver.studentsct.model.Enrollment) DefaultSingleAssignment(org.cpsolver.ifs.assignment.DefaultSingleAssignment) File(java.io.File) Solution(org.cpsolver.ifs.solution.Solution)

Example 10 with DefaultSingleAssignment

use of org.cpsolver.ifs.assignment.DefaultSingleAssignment in project cpsolver by UniTime.

the class TimetableModel method saveAsXML.

public void saveAsXML(DataProperties cfg, boolean gen, Solution<Activity, Location> solution, Assignment<Activity, Location> assignment, File outFile) throws IOException {
    outFile.getParentFile().mkdirs();
    sLogger.debug("Writting XML data to:" + outFile);
    Document document = DocumentHelper.createDocument();
    document.addComment("Interactive Timetabling - University Timetable Generator (version 2.0)");
    if (assignment == null && solution != null)
        assignment = solution.getAssignment();
    if (assignment == null)
        assignment = new DefaultSingleAssignment<Activity, Location>();
    if (!assignedVariables(assignment).isEmpty()) {
        StringBuffer comments = new StringBuffer("Solution Info:\n");
        Map<String, String> solutionInfo = (solution == null ? getInfo(assignment) : solution.getInfo());
        for (String key : new TreeSet<String>(solutionInfo.keySet())) {
            String value = solutionInfo.get(key);
            comments.append("    " + key + ": " + value + "\n");
        }
        document.addComment(comments.toString());
    }
    Element root = document.addElement("Timetable");
    if (gen) {
        Element generator = root.addElement("Generator");
        generator.addAttribute("version", "2.0");
        generator.addElement("DaysPerWeek").setText(String.valueOf(iNrDays));
        generator.addElement("SlotsPerDay").setText(String.valueOf(iNrHours));
        generator.addElement("NrRooms").setText(cfg.getProperty("Generator.NrRooms", "20"));
        generator.addElement("NrInstructors").setText(cfg.getProperty("Generator.NrInstructors", "20"));
        generator.addElement("NrClasses").setText(cfg.getProperty("Generator.NrClasses", "20"));
        generator.addElement("FillFactor").setText(cfg.getProperty("Generator.FillFactor", "0.8"));
        generator.addElement("ActivityLengthMax").setText(cfg.getProperty("Generator.ActivityLengthMax", "5"));
        generator.addElement("NrGroupsOfRooms").setText(cfg.getProperty("Generator.NrGroupsOfRooms", "20"));
        generator.addElement("NrRoomsInGroupMin").setText(cfg.getProperty("Generator.NrRoomsInGroupMin", "1"));
        generator.addElement("NrRoomsInGroupMax").setText(cfg.getProperty("Generator.NrRoomsInGroupMax", "10"));
        generator.addElement("NrRoomInGroupMin").setText(cfg.getProperty("Generator.NrRoomInGroupMin", "1"));
        generator.addElement("HardFreeResource").setText(cfg.getProperty("Generator.HardFreeResource", "0.05"));
        generator.addElement("SoftFreeResource").setText(cfg.getProperty("Generator.SoftFreeResource", "0.3"));
        generator.addElement("SoftUsedResource").setText(cfg.getProperty("Generator.SoftUsedResource", "0.05"));
        generator.addElement("SoftUsedActivity").setText(cfg.getProperty("Generator.SoftUsedActivity", "0.05"));
        generator.addElement("SoftFreeActivity").setText(cfg.getProperty("Generator.SoftFreeActivity", "0.3"));
        generator.addElement("HardFreeActivity").setText(cfg.getProperty("Generator.HardFreeActivity", "0.05"));
        generator.addElement("NrDependencies").setText(cfg.getProperty("Generator.NrDependencies", "50"));
    }
    ArrayList<Resource> rooms = new ArrayList<Resource>();
    ArrayList<Resource> classes = new ArrayList<Resource>();
    ArrayList<Resource> instructors = new ArrayList<Resource>();
    ArrayList<Resource> specials = new ArrayList<Resource>();
    ArrayList<Dependence> dependencies = new ArrayList<Dependence>();
    for (Constraint<Activity, Location> c : constraints()) {
        if (c instanceof Resource) {
            Resource r = (Resource) c;
            switch(r.getType()) {
                case Resource.TYPE_ROOM:
                    rooms.add(r);
                    break;
                case Resource.TYPE_CLASS:
                    classes.add(r);
                    break;
                case Resource.TYPE_INSTRUCTOR:
                    instructors.add(r);
                    break;
                default:
                    specials.add(r);
            }
        } else if (c instanceof Dependence) {
            dependencies.add((Dependence) c);
        }
    }
    Element problem = root.addElement("Problem");
    problem.addAttribute("version", "2.0");
    Element problemGen = problem.addElement("General");
    problemGen.addElement("DaysPerWeek").setText(String.valueOf(iNrDays));
    problemGen.addElement("SlotsPerDay").setText(String.valueOf(iNrHours));
    Element resourceGen = problemGen.addElement("Resources");
    resourceGen.addElement("Classrooms").setText(String.valueOf(rooms.size()));
    resourceGen.addElement("Teachers").setText(String.valueOf(instructors.size()));
    resourceGen.addElement("Classes").setText(String.valueOf(classes.size()));
    resourceGen.addElement("Special").setText(String.valueOf(specials.size()));
    problemGen.addElement("Activities").setText(String.valueOf(variables().size()));
    problemGen.addElement("Dependences").setText(String.valueOf(dependencies.size()));
    Element resources = problem.addElement("Resources");
    Element resEl = resources.addElement("Classrooms");
    for (Resource r : rooms) {
        Element el = resEl.addElement("Resource");
        el.addAttribute("id", r.getResourceId());
        el.addElement("Name").setText(r.getName());
        Element pref = el.addElement("TimePreferences");
        for (Integer slot : new TreeSet<Integer>(r.getDiscouragedSlots())) pref.addElement("Soft").setText(slot.toString());
        for (Integer slot : new TreeSet<Integer>(r.getProhibitedSlots())) pref.addElement("Hard").setText(slot.toString());
    }
    resEl = resources.addElement("Teachers");
    for (Resource r : instructors) {
        Element el = resEl.addElement("Resource");
        el.addAttribute("id", r.getResourceId());
        el.addElement("Name").setText(r.getName());
        Element pref = el.addElement("TimePreferences");
        for (Integer slot : new TreeSet<Integer>(r.getDiscouragedSlots())) pref.addElement("Soft").setText(slot.toString());
        for (Integer slot : new TreeSet<Integer>(r.getProhibitedSlots())) pref.addElement("Hard").setText(slot.toString());
    }
    resEl = resources.addElement("Classes");
    for (Resource r : classes) {
        Element el = resEl.addElement("Resource");
        el.addAttribute("id", r.getResourceId());
        el.addElement("Name").setText(r.getName());
        Element pref = el.addElement("TimePreferences");
        for (Integer slot : new TreeSet<Integer>(r.getDiscouragedSlots())) pref.addElement("Soft").setText(slot.toString());
        for (Integer slot : new TreeSet<Integer>(r.getProhibitedSlots())) pref.addElement("Hard").setText(slot.toString());
    }
    resEl = resources.addElement("Special");
    for (Resource r : specials) {
        Element el = resEl.addElement("Resource");
        el.addAttribute("id", r.getResourceId());
        el.addElement("Name").setText(r.getName());
        Element pref = el.addElement("TimePreferences");
        for (Integer slot : new TreeSet<Integer>(r.getDiscouragedSlots())) pref.addElement("Soft").setText(slot.toString());
        for (Integer slot : new TreeSet<Integer>(r.getProhibitedSlots())) pref.addElement("Hard").setText(slot.toString());
    }
    boolean hasSolution = false;
    Element actEl = problem.addElement("Activities");
    for (Activity a : variables()) {
        Element el = actEl.addElement("Activity");
        el.addAttribute("id", a.getActivityId());
        el.addElement("Name").setText(a.getName());
        el.addElement("Length").setText(String.valueOf(a.getLength()));
        if (assignment.getValue(a) != null)
            hasSolution = true;
        Element pref = el.addElement("TimePreferences");
        for (Integer slot : new TreeSet<Integer>(a.getDiscouragedSlots())) pref.addElement("Soft").setText(slot.toString());
        for (Integer slot : new TreeSet<Integer>(a.getProhibitedSlots())) pref.addElement("Hard").setText(slot.toString());
        Element reqRes = el.addElement("RequiredResources");
        for (List<Resource> gr : a.getResourceGroups()) {
            if (gr.size() == 1) {
                reqRes.addElement("Resource").setText(gr.get(0).getResourceId());
            } else {
                Element grEl = reqRes.addElement("Group").addAttribute("conjunctive", "no");
                for (Resource r : gr) grEl.addElement("Resource").setText(r.getResourceId());
            }
        }
    }
    Element depEl = problem.addElement("Dependences");
    for (Dependence d : dependencies) {
        Element el = depEl.addElement("Dependence");
        el.addAttribute("id", d.getResourceId());
        el.addElement("FirstActivity").setText((d.first()).getActivityId());
        el.addElement("SecondActivity").setText((d.second()).getActivityId());
        switch(d.getType()) {
            case Dependence.TYPE_AFTER:
                el.addElement("Operator").setText("After");
                break;
            case Dependence.TYPE_BEFORE:
                el.addElement("Operator").setText("Before");
                break;
            case Dependence.TYPE_CLOSELY_BEFORE:
                el.addElement("Operator").setText("Closely before");
                break;
            case Dependence.TYPE_CLOSELY_AFTER:
                el.addElement("Operator").setText("Closely after");
                break;
            case Dependence.TYPE_CONCURRENCY:
                el.addElement("Operator").setText("Concurrently");
                break;
            default:
                el.addElement("Operator").setText("Unknown");
        }
    }
    if (hasSolution) {
        Element solutionEl = root.addElement("Solution");
        solutionEl.addAttribute("version", "2.0");
        for (Activity a : variables()) {
            Element el = solutionEl.addElement("Activity");
            el.addAttribute("id", a.getActivityId());
            Location location = assignment.getValue(a);
            if (location != null) {
                el.addElement("StartTime").setText(String.valueOf(location.getSlot()));
                Element res = el.addElement("UsedResources");
                for (int i = 0; i < location.getResources().length; i++) res.addElement("Resource").setText(location.getResources()[i].getResourceId());
            }
        }
    }
    FileOutputStream fos = new FileOutputStream(outFile);
    (new XMLWriter(fos, OutputFormat.createPrettyPrint())).write(document);
    fos.flush();
    fos.close();
}
Also used : Element(org.dom4j.Element) ArrayList(java.util.ArrayList) Document(org.dom4j.Document) XMLWriter(org.dom4j.io.XMLWriter) Constraint(org.cpsolver.ifs.model.Constraint) TreeSet(java.util.TreeSet) FileOutputStream(java.io.FileOutputStream) DefaultSingleAssignment(org.cpsolver.ifs.assignment.DefaultSingleAssignment)

Aggregations

DefaultSingleAssignment (org.cpsolver.ifs.assignment.DefaultSingleAssignment)15 File (java.io.File)10 DataProperties (org.cpsolver.ifs.util.DataProperties)10 Enrollment (org.cpsolver.studentsct.model.Enrollment)7 Request (org.cpsolver.studentsct.model.Request)7 Solver (org.cpsolver.ifs.solver.Solver)6 CourseRequest (org.cpsolver.studentsct.model.CourseRequest)6 IOException (java.io.IOException)5 DecimalFormat (java.text.DecimalFormat)4 Placement (org.cpsolver.coursett.model.Placement)4 Student (org.cpsolver.studentsct.model.Student)4 FileWriter (java.io.FileWriter)3 PrintWriter (java.io.PrintWriter)3 BitSet (java.util.BitSet)3 HashSet (java.util.HashSet)3 RoomLocation (org.cpsolver.coursett.model.RoomLocation)3 TimeLocation (org.cpsolver.coursett.model.TimeLocation)3 Assignment (org.cpsolver.ifs.assignment.Assignment)3 ParallelSolver (org.cpsolver.ifs.solver.ParallelSolver)3 DistanceConflict (org.cpsolver.studentsct.extension.DistanceConflict)3