Search in sources :

Example 6 with Request

use of org.cpsolver.studentsct.model.Request in project cpsolver by UniTime.

the class StudentSectioningModel method computeOnlineSectioningInfos.

/**
     * Compute online student sectioning infos for all sections (see
     * {@link Section#getSpaceExpected()} and {@link Section#getSpaceHeld()}).
     * @param assignment current assignment
     */
public void computeOnlineSectioningInfos(Assignment<Request, Enrollment> assignment) {
    clearOnlineSectioningInfos();
    for (Student student : getStudents()) {
        if (!student.isDummy())
            continue;
        for (Request request : student.getRequests()) {
            if (!(request instanceof CourseRequest))
                continue;
            CourseRequest courseRequest = (CourseRequest) request;
            Enrollment enrollment = assignment.getValue(courseRequest);
            if (enrollment != null) {
                for (Section section : enrollment.getSections()) {
                    section.setSpaceHeld(courseRequest.getWeight() + section.getSpaceHeld());
                }
            }
            List<Enrollment> feasibleEnrollments = new ArrayList<Enrollment>();
            int totalLimit = 0;
            for (Enrollment enrl : courseRequest.values(assignment)) {
                boolean overlaps = false;
                for (Request otherRequest : student.getRequests()) {
                    if (otherRequest.equals(courseRequest) || !(otherRequest instanceof CourseRequest))
                        continue;
                    Enrollment otherErollment = assignment.getValue(otherRequest);
                    if (otherErollment == null)
                        continue;
                    if (enrl.isOverlapping(otherErollment)) {
                        overlaps = true;
                        break;
                    }
                }
                if (!overlaps) {
                    feasibleEnrollments.add(enrl);
                    if (totalLimit >= 0) {
                        int limit = enrl.getLimit();
                        if (limit < 0)
                            totalLimit = -1;
                        else
                            totalLimit += limit;
                    }
                }
            }
            double increment = courseRequest.getWeight() / (totalLimit > 0 ? totalLimit : feasibleEnrollments.size());
            for (Enrollment feasibleEnrollment : feasibleEnrollments) {
                for (Section section : feasibleEnrollment.getSections()) {
                    if (totalLimit > 0) {
                        section.setSpaceExpected(section.getSpaceExpected() + increment * feasibleEnrollment.getLimit());
                    } else {
                        section.setSpaceExpected(section.getSpaceExpected() + increment);
                    }
                }
            }
        }
    }
}
Also used : CourseRequest(org.cpsolver.studentsct.model.CourseRequest) Request(org.cpsolver.studentsct.model.Request) CourseRequest(org.cpsolver.studentsct.model.CourseRequest) ArrayList(java.util.ArrayList) Enrollment(org.cpsolver.studentsct.model.Enrollment) Student(org.cpsolver.studentsct.model.Student) Section(org.cpsolver.studentsct.model.Section) Constraint(org.cpsolver.ifs.model.Constraint)

Example 7 with Request

use of org.cpsolver.studentsct.model.Request 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 8 with Request

use of org.cpsolver.studentsct.model.Request in project cpsolver by UniTime.

the class Test method loadLastLikeCourseDemandsXml.

/**
     * Load last-like students from an XML file (the one that is used to load
     * last like course demands table in the timetabling application)
     * @param model problem model
     * @param xml an XML file
     */
public static void loadLastLikeCourseDemandsXml(StudentSectioningModel model, File xml) {
    try {
        Document document = (new SAXReader()).read(xml);
        Element root = document.getRootElement();
        HashMap<Course, List<Request>> requests = new HashMap<Course, List<Request>>();
        long reqId = 0;
        for (Iterator<?> i = root.elementIterator("student"); i.hasNext(); ) {
            Element studentEl = (Element) i.next();
            Student student = new Student(Long.parseLong(studentEl.attributeValue("externalId")));
            student.setDummy(true);
            int priority = 0;
            HashSet<Course> reqCourses = new HashSet<Course>();
            for (Iterator<?> j = studentEl.elementIterator("studentCourse"); j.hasNext(); ) {
                Element courseEl = (Element) j.next();
                String subjectArea = courseEl.attributeValue("subject");
                String courseNbr = courseEl.attributeValue("courseNumber");
                Course course = null;
                offerings: for (Offering offering : model.getOfferings()) {
                    for (Course c : offering.getCourses()) {
                        if (c.getSubjectArea().equals(subjectArea) && c.getCourseNumber().equals(courseNbr)) {
                            course = c;
                            break offerings;
                        }
                    }
                }
                if (course == null && courseNbr.charAt(courseNbr.length() - 1) >= 'A' && courseNbr.charAt(courseNbr.length() - 1) <= 'Z') {
                    String courseNbrNoSfx = courseNbr.substring(0, courseNbr.length() - 1);
                    offerings: for (Offering offering : model.getOfferings()) {
                        for (Course c : offering.getCourses()) {
                            if (c.getSubjectArea().equals(subjectArea) && c.getCourseNumber().equals(courseNbrNoSfx)) {
                                course = c;
                                break offerings;
                            }
                        }
                    }
                }
                if (course == null) {
                    sLog.warn("Course " + subjectArea + " " + courseNbr + " not found.");
                } else {
                    if (!reqCourses.add(course)) {
                        sLog.warn("Course " + subjectArea + " " + courseNbr + " already requested.");
                    } else {
                        List<Course> courses = new ArrayList<Course>(1);
                        courses.add(course);
                        CourseRequest request = new CourseRequest(reqId++, priority++, false, student, courses, false, null);
                        List<Request> requestsThisCourse = requests.get(course);
                        if (requestsThisCourse == null) {
                            requestsThisCourse = new ArrayList<Request>();
                            requests.put(course, requestsThisCourse);
                        }
                        requestsThisCourse.add(request);
                    }
                }
            }
            if (!student.getRequests().isEmpty())
                model.addStudent(student);
        }
        for (Map.Entry<Course, List<Request>> entry : requests.entrySet()) {
            Course course = entry.getKey();
            List<Request> requestsThisCourse = entry.getValue();
            double weight = getLastLikeStudentWeight(course, 0, requestsThisCourse.size());
            for (Request request : requestsThisCourse) {
                request.setWeight(weight);
            }
        }
    } catch (Exception e) {
        sLog.error(e.getMessage(), e);
    }
}
Also used : HashMap(java.util.HashMap) SAXReader(org.dom4j.io.SAXReader) Element(org.dom4j.Element) ArrayList(java.util.ArrayList) Document(org.dom4j.Document) List(java.util.List) ArrayList(java.util.ArrayList) Course(org.cpsolver.studentsct.model.Course) HashSet(java.util.HashSet) Request(org.cpsolver.studentsct.model.Request) CourseRequest(org.cpsolver.studentsct.model.CourseRequest) Student(org.cpsolver.studentsct.model.Student) Offering(org.cpsolver.studentsct.model.Offering) IOException(java.io.IOException) CourseRequest(org.cpsolver.studentsct.model.CourseRequest) Map(java.util.Map) HashMap(java.util.HashMap)

Example 9 with Request

use of org.cpsolver.studentsct.model.Request in project cpsolver by UniTime.

the class Test method combineStudents.

/** Combine students from the provided two files 
     * @param cfg solver configuration
     * @param lastLikeStudentData a file containing last-like student data
     * @param realStudentData a file containing real student data
     * @return combined solution
     **/
public static Solution<Request, Enrollment> combineStudents(DataProperties cfg, File lastLikeStudentData, File realStudentData) {
    try {
        RandomStudentFilter rnd = new RandomStudentFilter(1.0);
        StudentSectioningModel model = null;
        Assignment<Request, Enrollment> assignment = new DefaultSingleAssignment<Request, Enrollment>();
        for (StringTokenizer stk = new StringTokenizer(cfg.getProperty("Test.CombineAcceptProb", "1.0"), ","); stk.hasMoreTokens(); ) {
            double acceptProb = Double.parseDouble(stk.nextToken());
            sLog.info("Test.CombineAcceptProb=" + acceptProb);
            rnd.setProbability(acceptProb);
            StudentFilter batchFilter = new CombinedStudentFilter(new ReverseStudentFilter(new FreshmanStudentFilter()), rnd, CombinedStudentFilter.OP_AND);
            model = new StudentSectioningModel(cfg);
            StudentSectioningXMLLoader loader = new StudentSectioningXMLLoader(model, assignment);
            loader.setLoadStudents(false);
            loader.load();
            StudentSectioningXMLLoader lastLikeLoader = new StudentSectioningXMLLoader(model, assignment);
            lastLikeLoader.setInputFile(lastLikeStudentData);
            lastLikeLoader.setLoadOfferings(false);
            lastLikeLoader.setLoadStudents(true);
            lastLikeLoader.load();
            StudentSectioningXMLLoader realLoader = new StudentSectioningXMLLoader(model, assignment);
            realLoader.setInputFile(realStudentData);
            realLoader.setLoadOfferings(false);
            realLoader.setLoadStudents(true);
            realLoader.setStudentFilter(batchFilter);
            realLoader.load();
            fixWeights(model);
            fixPriorities(model);
            Solver<Request, Enrollment> solver = new Solver<Request, Enrollment>(model.getProperties());
            solver.setInitalSolution(model);
            new StudentSectioningXMLSaver(solver).save(new File(new File(model.getProperties().getProperty("General.Output", ".")), "solution-r" + ((int) (100.0 * acceptProb)) + ".xml"));
        }
        return model == null ? null : new Solution<Request, Enrollment>(model, assignment);
    } catch (Exception e) {
        sLog.error("Unable to combine students, reason: " + e.getMessage(), e);
        return null;
    }
}
Also used : FreshmanStudentFilter(org.cpsolver.studentsct.filter.FreshmanStudentFilter) RandomStudentFilter(org.cpsolver.studentsct.filter.RandomStudentFilter) ReverseStudentFilter(org.cpsolver.studentsct.filter.ReverseStudentFilter) StudentFilter(org.cpsolver.studentsct.filter.StudentFilter) CombinedStudentFilter(org.cpsolver.studentsct.filter.CombinedStudentFilter) Solver(org.cpsolver.ifs.solver.Solver) ParallelSolver(org.cpsolver.ifs.solver.ParallelSolver) FreshmanStudentFilter(org.cpsolver.studentsct.filter.FreshmanStudentFilter) RandomStudentFilter(org.cpsolver.studentsct.filter.RandomStudentFilter) Request(org.cpsolver.studentsct.model.Request) CourseRequest(org.cpsolver.studentsct.model.CourseRequest) IOException(java.io.IOException) StringTokenizer(java.util.StringTokenizer) ReverseStudentFilter(org.cpsolver.studentsct.filter.ReverseStudentFilter) Enrollment(org.cpsolver.studentsct.model.Enrollment) CombinedStudentFilter(org.cpsolver.studentsct.filter.CombinedStudentFilter) DefaultSingleAssignment(org.cpsolver.ifs.assignment.DefaultSingleAssignment) File(java.io.File)

Example 10 with Request

use of org.cpsolver.studentsct.model.Request in project cpsolver by UniTime.

the class InevitableStudentConflicts method noGood.

/**
     * No-good set of requests
     * 
     * @param student
     *            student
     * @param ch
     *            student checked that failed to find a complete schedule
     * @param idx
     *            index of unassigned course in the best found schedule
     * @return the smallest set of requests that cannot be assigned all
     *         together, containing the request with the given index
     */
private List<Request> noGood(Assignment<Request, Enrollment> assignment, Student student, StudentCheck ch, int idx) {
    List<Request> noGood = new ArrayList<Request>();
    Request rx = student.getRequests().get(idx);
    for (int i = 0; i < student.getRequests().size(); i++) {
        if (i == idx)
            noGood.add(rx);
        else if (ch.getBestAssignment()[i] != null)
            noGood.add(ch.getBestAssignment()[i].getRequest());
    }
    for (Request r : noGood) {
        if (r.equals(rx))
            continue;
        List<Request> newNoGood = new ArrayList<Request>(noGood);
        newNoGood.remove(r);
        StudentCheck chx = new StudentCheck(newNoGood);
        chx.check(assignment);
        if (!chx.isBestComplete())
            noGood = newNoGood;
    }
    return noGood;
}
Also used : ArrayList(java.util.ArrayList) CourseRequest(org.cpsolver.studentsct.model.CourseRequest) FreeTimeRequest(org.cpsolver.studentsct.model.FreeTimeRequest) Request(org.cpsolver.studentsct.model.Request)

Aggregations

Request (org.cpsolver.studentsct.model.Request)65 CourseRequest (org.cpsolver.studentsct.model.CourseRequest)51 Enrollment (org.cpsolver.studentsct.model.Enrollment)47 FreeTimeRequest (org.cpsolver.studentsct.model.FreeTimeRequest)23 Section (org.cpsolver.studentsct.model.Section)22 ArrayList (java.util.ArrayList)21 Student (org.cpsolver.studentsct.model.Student)21 Course (org.cpsolver.studentsct.model.Course)20 HashSet (java.util.HashSet)18 HashMap (java.util.HashMap)14 Subpart (org.cpsolver.studentsct.model.Subpart)10 Map (java.util.Map)9 DataProperties (org.cpsolver.ifs.util.DataProperties)9 Config (org.cpsolver.studentsct.model.Config)8 Offering (org.cpsolver.studentsct.model.Offering)8 Set (java.util.Set)7 DefaultSingleAssignment (org.cpsolver.ifs.assignment.DefaultSingleAssignment)7 File (java.io.File)6 IOException (java.io.IOException)6 TreeSet (java.util.TreeSet)6