Search in sources :

Example 6 with Student

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

the class InevitableStudentConflicts method check.

/** Check model for inevitable student conflicts 
     * @param assignment current assignment
     * @return true if there are no inevitable student conflicts
     **/
public boolean check(Assignment<Request, Enrollment> assignment) {
    sLog.info("Checking for inevitable student conflicts...");
    HashMap<TreeSet<Object>, Object[]> noGoods = new HashMap<TreeSet<Object>, Object[]>();
    long studentWithoutCompleteSchedule = 0;
    long inevitableRequests = 0;
    double inevitableRequestWeight = 0.0;
    long incompleteInevitableRequests = 0;
    double incompleteInevitableRequestWeight = 0.0;
    long total = 0;
    Comparator<Object> simpleCmp = new Comparator<Object>() {

        @Override
        public int compare(Object o1, Object o2) {
            return o1.toString().compareTo(o2.toString());
        }
    };
    HashSet<Request> requests2remove = new HashSet<Request>();
    for (Student student : getModel().getStudents()) {
        sLog.debug("  Checking " + (++total) + ". student " + student + "...");
        if (student.isComplete(assignment)) {
            for (Request request : student.getRequests()) {
                if (assignment.getValue(request) == null) {
                    inevitableRequests++;
                    inevitableRequestWeight += request.getWeight();
                }
            }
        } else {
            StudentCheck ch = new StudentCheck(student.getRequests());
            ch.check(assignment);
            if (!ch.isBestComplete()) {
                sLog.info("    Student " + student + " cannot have a complete schedule");
                studentWithoutCompleteSchedule++;
            }
            int idx = 0;
            for (Iterator<Request> f = student.getRequests().iterator(); f.hasNext(); idx++) {
                Request request = f.next();
                Enrollment enrollment = ch.getBestAssignment()[idx];
                if (enrollment == null) {
                    if (!ch.isBestComplete()) {
                        List<Request> noGood = noGood(assignment, student, ch, idx);
                        sLog.info("      Request " + request + " cannot be assigned");
                        for (Request r : noGood) {
                            sLog.debug("        " + r);
                            Collection<Enrollment> values = null;
                            if (r instanceof CourseRequest) {
                                values = ((CourseRequest) r).getEnrollmentsSkipSameTime(assignment);
                            } else {
                                values = request.computeEnrollments(assignment);
                            }
                            for (Enrollment en : values) {
                                sLog.debug("          " + enrollment2string(en));
                            }
                        }
                        if (iDeleteInevitable) {
                            // noGood.lastElement()
                            requests2remove.add(request);
                            sLog.info("        -- request " + request + " picked to be removed from the model");
                        }
                        TreeSet<Object> key = new TreeSet<Object>(simpleCmp);
                        for (Request r : noGood) {
                            if (r instanceof CourseRequest) {
                                key.add(((CourseRequest) r).getCourses().get(0));
                            } else {
                                key.add("Free " + ((FreeTimeRequest) r).getTime().getLongName(true));
                            }
                        }
                        Object[] counter = noGoods.get(key);
                        int ir = (counter == null ? 1 : ((Integer) counter[0]).intValue() + 1);
                        double irw = (counter == null ? 0.0 : ((Double) counter[1]).doubleValue()) + request.getWeight();
                        noGoods.put(key, new Object[] { new Integer(ir), new Double(irw) });
                        if (ch.canAssign(request, idx)) {
                            incompleteInevitableRequests++;
                            incompleteInevitableRequestWeight += request.getWeight();
                        }
                    }
                    inevitableRequests++;
                    inevitableRequestWeight += request.getWeight();
                }
            }
        }
    }
    for (Map.Entry<TreeSet<Object>, Object[]> entry : noGoods.entrySet()) {
        TreeSet<Object> noGood = entry.getKey();
        Object[] counter = entry.getValue();
        List<CSVFile.CSVField> fields = new ArrayList<CSVFile.CSVField>();
        String courseStr = "";
        for (Iterator<Object> j = noGood.iterator(); j.hasNext(); ) {
            Object x = j.next();
            if (x instanceof Course) {
                Course course = (Course) x;
                courseStr += course.getName();
            } else
                courseStr += x.toString();
            if (j.hasNext())
                courseStr += ", ";
        }
        fields.add(new CSVFile.CSVField(courseStr));
        fields.add(new CSVFile.CSVField(((Integer) counter[0]).intValue()));
        fields.add(new CSVFile.CSVField(((Double) counter[1]).doubleValue()));
        for (Iterator<Object> j = noGood.iterator(); j.hasNext(); ) {
            Object x = j.next();
            if (x instanceof Course) {
                Course course = (Course) x;
                List<Course> courses = new ArrayList<Course>(1);
                courses.add(course);
                CourseRequest cr = new CourseRequest(-1, 0, false, new Student(-1), courses, false, null);
                String field = course.getName();
                int idx = 0;
                for (Iterator<Enrollment> k = cr.getEnrollmentsSkipSameTime(assignment).iterator(); k.hasNext(); ) {
                    if (idx++ > 20) {
                        field += "\n ...";
                        break;
                    } else {
                        field += "\n  " + enrollment2string(k.next());
                    }
                }
                fields.add(new CSVFile.CSVField(field));
            } else
                fields.add(new CSVFile.CSVField(x.toString()));
        }
        iCSVFile.addLine(fields);
    }
    if (!requests2remove.isEmpty()) {
        for (Request request : requests2remove) {
            removeRequest(request);
        }
    }
    sLog.info("Students that can never obtain a complete schedule: " + studentWithoutCompleteSchedule);
    sLog.info("Inevitable student requests: " + inevitableRequests);
    sLog.info("Inevitable student request weight: " + inevitableRequestWeight);
    sLog.info("Inevitable student requests of students without a complete schedule: " + incompleteInevitableRequests);
    sLog.info("Inevitable student request weight of students without a complete schedule: " + incompleteInevitableRequestWeight);
    if (iCSVFile.getLines() != null)
        Collections.sort(iCSVFile.getLines(), new Comparator<CSVFile.CSVLine>() {

            @Override
            public int compare(CSVFile.CSVLine l1, CSVFile.CSVLine l2) {
                int cmp = Double.compare(l2.getField(1).toDouble(), l1.getField(1).toDouble());
                if (cmp != 0)
                    return cmp;
                return l1.getField(0).toString().compareTo(l2.getField(0).toString());
            }
        });
    return (inevitableRequests == 0);
}
Also used : CSVFile(org.cpsolver.ifs.util.CSVFile) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) Comparator(java.util.Comparator) TreeSet(java.util.TreeSet) Enrollment(org.cpsolver.studentsct.model.Enrollment) Course(org.cpsolver.studentsct.model.Course) HashSet(java.util.HashSet) CourseRequest(org.cpsolver.studentsct.model.CourseRequest) FreeTimeRequest(org.cpsolver.studentsct.model.FreeTimeRequest) Request(org.cpsolver.studentsct.model.Request) Student(org.cpsolver.studentsct.model.Student) CourseRequest(org.cpsolver.studentsct.model.CourseRequest) HashMap(java.util.HashMap) Map(java.util.Map)

Example 7 with Student

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

the class BranchBoundSelection method selectNeighbour.

/**
     * Select neighbour. All students are taken, one by one in a random order.
     * For each student a branch &amp; bound search is employed.
     */
@Override
public Neighbour<Request, Enrollment> selectNeighbour(Solution<Request, Enrollment> solution) {
    Student student = null;
    while ((student = nextStudent()) != null) {
        Progress.getInstance(solution.getModel()).incProgress();
        Neighbour<Request, Enrollment> neighbour = getSelection(solution.getAssignment(), student).select();
        if (neighbour != null)
            return neighbour;
    }
    return null;
}
Also used : FreeTimeRequest(org.cpsolver.studentsct.model.FreeTimeRequest) Request(org.cpsolver.studentsct.model.Request) CourseRequest(org.cpsolver.studentsct.model.CourseRequest) Enrollment(org.cpsolver.studentsct.model.Enrollment) Student(org.cpsolver.studentsct.model.Student)

Example 8 with Student

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

the class ResectionIncompleteStudentsSelection method selectNeighbour.

/**
     * Select neighbour. All students with an incomplete and non-empty schedule
     * are taken, one by one in a random order. For each student a branch &amp; 
     * bound search is employed.
     */
@Override
public Neighbour<Request, Enrollment> selectNeighbour(Solution<Request, Enrollment> solution) {
    Student student = null;
    while ((student = nextStudent()) != null) {
        Progress.getInstance(solution.getModel()).incProgress();
        if (student.nrAssignedRequests(solution.getAssignment()) == 0 || student.isComplete(solution.getAssignment()))
            continue;
        Neighbour<Request, Enrollment> neighbour = getSelection(solution.getAssignment(), student).select();
        if (neighbour != null)
            return neighbour;
    }
    return null;
}
Also used : Request(org.cpsolver.studentsct.model.Request) Enrollment(org.cpsolver.studentsct.model.Enrollment) Student(org.cpsolver.studentsct.model.Student)

Example 9 with Student

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

the class Test method loadStudentInfoXml.

/** Load student infos from a given XML file. 
     * @param model problem model
     * @param xml an XML file
     **/
public static void loadStudentInfoXml(StudentSectioningModel model, File xml) {
    try {
        sLog.info("Loading student infos from " + xml);
        Document document = (new SAXReader()).read(xml);
        Element root = document.getRootElement();
        HashMap<Long, Student> studentTable = new HashMap<Long, Student>();
        for (Student student : model.getStudents()) {
            studentTable.put(new Long(student.getId()), student);
        }
        for (Iterator<?> i = root.elementIterator("student"); i.hasNext(); ) {
            Element studentEl = (Element) i.next();
            Student student = studentTable.get(Long.valueOf(studentEl.attributeValue("externalId")));
            if (student == null) {
                sLog.debug(" -- student " + studentEl.attributeValue("externalId") + " not found");
                continue;
            }
            sLog.debug(" -- loading info for student " + student);
            student.getAcademicAreaClasiffications().clear();
            if (studentEl.element("studentAcadAreaClass") != null)
                for (Iterator<?> j = studentEl.element("studentAcadAreaClass").elementIterator("acadAreaClass"); j.hasNext(); ) {
                    Element studentAcadAreaClassElement = (Element) j.next();
                    student.getAcademicAreaClasiffications().add(new AcademicAreaCode(studentAcadAreaClassElement.attributeValue("academicArea"), studentAcadAreaClassElement.attributeValue("academicClass")));
                }
            sLog.debug("   -- acad areas classifs " + student.getAcademicAreaClasiffications());
            student.getMajors().clear();
            if (studentEl.element("studentMajors") != null)
                for (Iterator<?> j = studentEl.element("studentMajors").elementIterator("major"); j.hasNext(); ) {
                    Element studentMajorElement = (Element) j.next();
                    student.getMajors().add(new AcademicAreaCode(studentMajorElement.attributeValue("academicArea"), studentMajorElement.attributeValue("code")));
                }
            sLog.debug("   -- majors " + student.getMajors());
            student.getMinors().clear();
            if (studentEl.element("studentMinors") != null)
                for (Iterator<?> j = studentEl.element("studentMinors").elementIterator("minor"); j.hasNext(); ) {
                    Element studentMinorElement = (Element) j.next();
                    student.getMinors().add(new AcademicAreaCode(studentMinorElement.attributeValue("academicArea", ""), studentMinorElement.attributeValue("code", "")));
                }
            sLog.debug("   -- minors " + student.getMinors());
        }
    } catch (Exception e) {
        sLog.error(e.getMessage(), e);
    }
}
Also used : HashMap(java.util.HashMap) SAXReader(org.dom4j.io.SAXReader) Element(org.dom4j.Element) Iterator(java.util.Iterator) AcademicAreaCode(org.cpsolver.studentsct.model.AcademicAreaCode) Document(org.dom4j.Document) Student(org.cpsolver.studentsct.model.Student) IOException(java.io.IOException)

Example 10 with Student

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

the class AssignInitialSelection method selectNeighbour.

@Override
public Neighbour<Request, Enrollment> selectNeighbour(Solution<Request, Enrollment> solution) {
    Student student = null;
    while ((student = nextStudent()) != null) {
        Progress.getInstance(solution.getModel()).incProgress();
        if (student.nrAssignedRequests(solution.getAssignment()) > 0)
            continue;
        Neighbour<Request, Enrollment> neighbour = new InitialSelection(student, solution.getAssignment()).select();
        if (neighbour != null)
            return neighbour;
    }
    return null;
}
Also used : Request(org.cpsolver.studentsct.model.Request) Enrollment(org.cpsolver.studentsct.model.Enrollment) Student(org.cpsolver.studentsct.model.Student)

Aggregations

Student (org.cpsolver.studentsct.model.Student)29 Request (org.cpsolver.studentsct.model.Request)21 CourseRequest (org.cpsolver.studentsct.model.CourseRequest)16 Enrollment (org.cpsolver.studentsct.model.Enrollment)16 ArrayList (java.util.ArrayList)10 HashSet (java.util.HashSet)9 Course (org.cpsolver.studentsct.model.Course)9 HashMap (java.util.HashMap)8 Offering (org.cpsolver.studentsct.model.Offering)7 Section (org.cpsolver.studentsct.model.Section)7 Element (org.dom4j.Element)6 FreeTimeRequest (org.cpsolver.studentsct.model.FreeTimeRequest)5 IOException (java.io.IOException)4 BitSet (java.util.BitSet)4 TimeLocation (org.cpsolver.coursett.model.TimeLocation)4 DefaultSingleAssignment (org.cpsolver.ifs.assignment.DefaultSingleAssignment)4 DataProperties (org.cpsolver.ifs.util.DataProperties)4 DistanceConflict (org.cpsolver.studentsct.extension.DistanceConflict)4 TimeOverlapsCounter (org.cpsolver.studentsct.extension.TimeOverlapsCounter)4 SctAssignment (org.cpsolver.studentsct.model.SctAssignment)4