Search in sources :

Example 1 with TeachingRequest

use of org.cpsolver.instructor.model.TeachingRequest in project cpsolver by UniTime.

the class MathTest method load.

@Override
protected boolean load(File dir, Assignment<TeachingRequest.Variable, TeachingAssignment> assignment) {
    if (!dir.isDirectory())
        return super.load(dir, assignment);
    try {
        String line = null;
        BufferedReader r = new BufferedReader(new FileReader(new File(dir, "courses.csv")));
        Map<String, Course> courses = new HashMap<String, Course>();
        Map<Long, List<TeachingRequest>> id2classes = new HashMap<Long, List<TeachingRequest>>();
        Map<String, List<TeachingRequest>> link2classes = new HashMap<String, List<TeachingRequest>>();
        long assId = 0, reqId = 0;
        while ((line = r.readLine()) != null) {
            if (line.trim().isEmpty())
                continue;
            String[] fields = line.split(",");
            Long id = Long.valueOf(fields[0]);
            String course = fields[1];
            String section = fields[2];
            int idx = 3;
            int dayCode = 0;
            idx: while (idx < fields.length && (idx == 3 || fields[idx].length() == 1)) {
                for (int i = 0; i < fields[idx].length(); i++) {
                    switch(fields[idx].charAt(i)) {
                        case 'M':
                            dayCode += Constants.DAY_CODES[0];
                            break;
                        case 'T':
                            dayCode += Constants.DAY_CODES[1];
                            break;
                        case 'W':
                            dayCode += Constants.DAY_CODES[2];
                            break;
                        case 'R':
                            dayCode += Constants.DAY_CODES[3];
                            break;
                        case 'F':
                            dayCode += Constants.DAY_CODES[4];
                            break;
                        default:
                            break idx;
                    }
                }
                idx++;
            }
            int startSlot = 0;
            if (dayCode > 0) {
                int time = Integer.parseInt(fields[idx++]);
                startSlot = 12 * (time / 100) + (time % 100) / 5;
            }
            String room = null;
            if (idx < fields.length)
                room = fields[idx++];
            String link = null;
            if (idx < fields.length)
                link = fields[idx++];
            int length = 12;
            if (idx < fields.length) {
                int time = Integer.parseInt(fields[idx++]);
                int endSlot = 12 * (time / 100) + (time % 100) / 5;
                length = endSlot - startSlot;
                if (length == 10)
                    length = 12;
                else if (length == 15)
                    length = 18;
            }
            List<Section> sections = new ArrayList<Section>();
            TimeLocation time = new TimeLocation(dayCode, startSlot, length, 0, 0.0, 0, null, "", null, (length == 18 ? 15 : 10));
            sections.add(new Section(assId++, id.toString(), section, course + " " + section + " " + time.getName(true) + (room == null ? "" : " " + room), time, room, false, false));
            Course c = courses.get(course);
            if (c == null) {
                c = new Course(courses.size(), course);
                courses.put(course, c);
            }
            TeachingRequest clazz = new TeachingRequest(reqId++, 1, c, 0f, sections, Constants.sPreferenceLevelRequired, Constants.sPreferenceLevelNeutral);
            addRequest(clazz);
            List<TeachingRequest> classes = id2classes.get(id);
            if (classes == null) {
                classes = new ArrayList<TeachingRequest>();
                id2classes.put(id, classes);
            }
            classes.add(clazz);
            if (link != null && !link.isEmpty()) {
                List<TeachingRequest> linked = link2classes.get(course + "-" + link);
                if (linked == null) {
                    linked = new ArrayList<TeachingRequest>();
                    link2classes.put(course + "-" + link, linked);
                }
                linked.add(clazz);
            }
        }
        for (Map.Entry<Long, List<TeachingRequest>> e : id2classes.entrySet()) {
            Long id = e.getKey();
            List<TeachingRequest> classes = e.getValue();
            if (classes.size() > 1) {
                SameInstructorConstraint sa = new SameInstructorConstraint(id, "A" + id.toString(), Constants.sPreferenceRequired);
                for (TeachingRequest c : classes) sa.addVariable(c.getVariables()[0]);
                addConstraint(sa);
            }
        }
        for (Map.Entry<String, List<TeachingRequest>> e : link2classes.entrySet()) {
            String link = e.getKey();
            List<TeachingRequest> classes = e.getValue();
            if (classes.size() > 1) {
                SameLinkConstraint sa = new SameLinkConstraint(null, link, Constants.sPreferencePreferred);
                for (TeachingRequest c : classes) sa.addVariable(c.getVariables()[0]);
                addConstraint(sa);
            }
        }
        Attribute.Type level = new Attribute.Type(0l, "Level", false, true);
        addAttributeType(level);
        Map<String, Attribute> code2attribute = new HashMap<String, Attribute>();
        r = new BufferedReader(new FileReader(new File(dir, "level_codes.csv")));
        String[] codes = r.readLine().split(",");
        while ((line = r.readLine()) != null) {
            if (line.trim().isEmpty())
                continue;
            String[] fields = line.split(",");
            String code = fields[0];
            if (code.startsWith("\"") && code.endsWith("\""))
                code = code.substring(1, code.length() - 1);
            Attribute attribute = code2attribute.get(code);
            if (attribute == null) {
                attribute = new Attribute(code2attribute.size(), code, level);
                code2attribute.put(code, attribute);
            }
            for (int i = 1; i < codes.length; i++) {
                int pref = Integer.parseInt(fields[i]);
                if (pref > 0)
                    for (TeachingRequest clazz : getRequests()) {
                        if (clazz.getCourse().getCourseName().contains(codes[i]))
                            clazz.addAttributePreference(new Preference<Attribute>(attribute, -pref));
                    }
            }
        }
        r = new BufferedReader(new FileReader(new File(dir, "hours_per_course.csv")));
        while ((line = r.readLine()) != null) {
            if (line.trim().isEmpty())
                continue;
            String[] fields = line.split(",");
            for (TeachingRequest clazz : getRequests()) {
                if (clazz.getCourse().getCourseName().contains(fields[0]))
                    clazz.setLoad(Float.parseFloat(fields[1]));
            }
        }
        String defaultCode = getProperties().getProperty("TA.DefaultLevelCode", "XXX");
        Attribute defaultAttribute = code2attribute.get(defaultCode);
        if (defaultAttribute == null) {
            defaultAttribute = new Attribute(code2attribute.size(), defaultCode, level);
            code2attribute.put(defaultCode, defaultAttribute);
        }
        for (TeachingRequest.Variable clazz : variables()) {
            sLog.info("Added class " + toString(clazz));
            if (clazz.getRequest().getAttributePreferences().isEmpty()) {
                sLog.error("No level: " + toString(clazz));
                clazz.getRequest().addAttributePreference(new Preference<Attribute>(defaultAttribute, -1));
            }
            if (clazz.getRequest().getLoad() == 0.0) {
                sLog.error("No load: " + toString(clazz));
                clazz.getRequest().setLoad(getProperties().getPropertyFloat("TA.DefaultLoad", 10f));
            }
        }
        r = new BufferedReader(new FileReader(new File(dir, "students.csv")));
        Set<String> studentIds = new HashSet<String>();
        double studentMaxLoad = 0.0;
        while ((line = r.readLine()) != null) {
            if (line.trim().isEmpty())
                continue;
            String[] fields = line.split(",");
            if ("puid".equals(fields[0]))
                continue;
            int idx = 0;
            String id = fields[idx++];
            if (!studentIds.add(id)) {
                sLog.error("Student " + id + " is two or more times in the file.");
            }
            boolean[] av = new boolean[50];
            for (int i = 0; i < 50; i++) av[i] = "1".equals(fields[idx++]);
            List<String> prefs = new ArrayList<String>();
            for (int i = 0; i < 3; i++) {
                String p = fields[idx++].replace("Large lecture", "LEC").replace("Lecture", "LEC").replace("Recitation", "REC");
                if (p.startsWith("MA "))
                    p = p.substring(3);
                if ("I have no preference".equals(p))
                    continue;
                prefs.add(p);
            }
            boolean grad = "Yes".equals(fields[idx++]);
            int b2b = Integer.parseInt(fields[idx++]);
            float maxLoad = Float.parseFloat(fields[idx++]);
            if (maxLoad == 0)
                maxLoad = getProperties().getPropertyFloat("TA.DefaultMaxLoad", 20f);
            String code = (idx < fields.length ? fields[idx++] : null);
            Instructor instructor = new Instructor(Long.valueOf(id.replace("-", "")), id, null, grad ? Constants.sPreferenceLevelNeutral : Constants.sPreferenceLevelDiscouraged, maxLoad);
            for (int i = 0; i < prefs.size(); i++) {
                String pref = prefs.get(i);
                if (pref.indexOf(' ') > 0)
                    pref = pref.substring(0, pref.indexOf(' '));
                Course c = courses.get(pref);
                if (c == null) {
                    c = new Course(courses.size(), pref);
                    courses.put(pref, c);
                }
                instructor.addCoursePreference(new Preference<Course>(c, i == 0 ? -10 : i == 1 ? -8 : -5));
            }
            if (code != null) {
                Attribute attribute = code2attribute.get(code);
                if (attribute == null) {
                    attribute = new Attribute(code2attribute.size(), code, level);
                    code2attribute.put(code, attribute);
                }
                instructor.addAttribute(attribute);
            }
            if (b2b == 1)
                instructor.setBackToBackPreference(Constants.sPreferenceLevelPreferred);
            else if (b2b == -1)
                instructor.setBackToBackPreference(Constants.sPreferenceLevelDiscouraged);
            for (int d = 0; d < 5; d++) {
                int f = -1;
                for (int t = 0; t < 10; t++) {
                    if (!av[10 * d + t]) {
                        if (f < 0)
                            f = t;
                    } else {
                        if (f >= 0) {
                            instructor.addTimePreference(new Preference<TimeLocation>(new TimeLocation(Constants.DAY_CODES[d], 90 + 12 * f, (t - f) * 12, 0, 0.0, null, "", null, 0), Constants.sPreferenceLevelProhibited));
                            f = -1;
                        }
                    }
                }
                if (f >= 0) {
                    instructor.addTimePreference(new Preference<TimeLocation>(new TimeLocation(Constants.DAY_CODES[d], 90 + 12 * f, (10 - f) * 12, 0, 0.0, null, "", null, 0), Constants.sPreferenceLevelProhibited));
                    f = -1;
                }
            }
            if (instructor.getMaxLoad() > 0) {
                addInstructor(instructor);
                sLog.info("Added student " + toString(instructor));
                int nrClasses = 0;
                for (TeachingRequest.Variable req : variables()) {
                    if (instructor.canTeach(req.getRequest()) && !req.getRequest().getAttributePreference(instructor).isProhibited()) {
                        sLog.info("  -- " + toString(req) + "," + (-req.getRequest().getAttributePreference(instructor).getPreferenceInt()) + "," + (-instructor.getCoursePreference(req.getCourse()).getPreference()));
                        nrClasses++;
                    }
                }
                if (nrClasses == 0) {
                    sLog.info("  -- no courses available");
                }
                studentMaxLoad += instructor.getMaxLoad();
            } else {
                sLog.info("Ignoring student " + instructor);
                if (instructor.getMaxLoad() == 0)
                    sLog.info("  -- zero max load");
                else
                    sLog.info("  -- no courses available");
            }
        }
        double totalLoad = 0.0;
        for (TeachingRequest.Variable clazz : variables()) {
            if (clazz.values(getEmptyAssignment()).isEmpty())
                sLog.error("No values: " + toString(clazz));
            totalLoad += clazz.getRequest().getLoad();
        }
        Map<String, Double> studentLevel2load = new HashMap<String, Double>();
        for (Instructor instructor : getInstructors()) {
            Set<Attribute> levels = instructor.getAttributes(level);
            String studentLevel = (levels.isEmpty() ? "null" : levels.iterator().next().getAttributeName());
            Double load = studentLevel2load.get(studentLevel);
            studentLevel2load.put(studentLevel, instructor.getMaxLoad() + (load == null ? 0.0 : load));
        }
        sLog.info("Student max loads: (total: " + sDoubleFormat.format(studentMaxLoad) + ")");
        for (String studentLevel : new TreeSet<String>(studentLevel2load.keySet())) {
            Double load = studentLevel2load.get(studentLevel);
            sLog.info("  " + studentLevel + ": " + sDoubleFormat.format(load));
        }
        Map<String, Double> clazzLevel2load = new HashMap<String, Double>();
        for (TeachingRequest.Variable clazz : variables()) {
            String classLevel = null;
            TreeSet<String> levels = new TreeSet<String>();
            for (Preference<Attribute> ap : clazz.getRequest().getAttributePreferences()) levels.add(ap.getTarget().getAttributeName());
            for (String l : levels) {
                classLevel = (classLevel == null ? "" : classLevel + ",") + l;
            }
            if (classLevel == null)
                classLevel = "null";
            if (clazz.getId() < 0)
                classLevel = clazz.getName();
            Double load = clazzLevel2load.get(level);
            clazzLevel2load.put(classLevel, clazz.getRequest().getLoad() + (load == null ? 0.0 : load));
        }
        sLog.info("Class loads: (total: " + sDoubleFormat.format(totalLoad) + ")");
        for (String classLevel : new TreeSet<String>(clazzLevel2load.keySet())) {
            Double load = clazzLevel2load.get(classLevel);
            sLog.info("  " + level + ": " + sDoubleFormat.format(load));
        }
        return true;
    } catch (IOException e) {
        sLog.error("Failed to load the problem: " + e.getMessage(), e);
        return false;
    }
}
Also used : TimeLocation(org.cpsolver.coursett.model.TimeLocation) HashMap(java.util.HashMap) Attribute(org.cpsolver.instructor.model.Attribute) Instructor(org.cpsolver.instructor.model.Instructor) ArrayList(java.util.ArrayList) TeachingRequest(org.cpsolver.instructor.model.TeachingRequest) SameLinkConstraint(org.cpsolver.instructor.constraints.SameLinkConstraint) TreeSet(java.util.TreeSet) FileReader(java.io.FileReader) ArrayList(java.util.ArrayList) List(java.util.List) Course(org.cpsolver.instructor.model.Course) HashSet(java.util.HashSet) SameInstructorConstraint(org.cpsolver.instructor.constraints.SameInstructorConstraint) IOException(java.io.IOException) Section(org.cpsolver.instructor.model.Section) SameLinkConstraint(org.cpsolver.instructor.constraints.SameLinkConstraint) Constraint(org.cpsolver.ifs.model.Constraint) SameInstructorConstraint(org.cpsolver.instructor.constraints.SameInstructorConstraint) BufferedReader(java.io.BufferedReader) File(java.io.File) HashMap(java.util.HashMap) Map(java.util.Map)

Example 2 with TeachingRequest

use of org.cpsolver.instructor.model.TeachingRequest in project cpsolver by UniTime.

the class ChmTest method generateReports.

@Override
protected void generateReports(File dir, Assignment<TeachingRequest.Variable, TeachingAssignment> assignment) throws IOException {
    PrintWriter out = new PrintWriter(new File(dir, "solution-assignments.csv"));
    out.println("Course,Sections,Time,Room,Skill,Qualification,Performance,Load,Student,Name,Not Available,Max Load,Skill,Qualification,Performance,Requested");
    for (TeachingRequest.Variable request : variables()) {
        out.print(request.getCourse().getCourseName());
        String sect = "", time = "", room = "";
        if (request.getId() < 0) {
            out.print(",\"SUPER\",,");
        } else {
            for (Iterator<Section> i = request.getSections().iterator(); i.hasNext(); ) {
                Section section = i.next();
                // if (section.isCommon() && section.isAllowOverlap()) continue;
                if (!sect.isEmpty()) {
                    sect += ", ";
                    time += ", ";
                    room += ", ";
                }
                sect += (section.isCommon() ? "(" : "") + section.getSectionType() + " " + section.getExternalId() + (section.isCommon() ? ")" : "");
                time += (section.getTime() == null ? "-" : section.getTime().getDayHeader() + " " + section.getTime().getStartTimeHeader(true) + "-" + section.getTime().getEndTimeHeader(true));
                room += (section.getRoom() == null ? "-" : section.getRoom());
            }
            out.print(",\"" + sect + "\",\"" + time + "\",\"" + room + "\"");
        }
        out.print(",\"" + getAttributes(request.getRequest(), "Skill") + "\"");
        out.print(",\"" + getAttributes(request.getRequest(), "Qualification") + "\"");
        out.print(",\"" + getAttributes(request.getRequest(), "Performance Level") + "\"");
        out.print("," + new DecimalFormat("0.0").format(request.getRequest().getLoad()));
        TeachingAssignment ta = assignment.getValue(request);
        if (ta != null) {
            Instructor instructor = ta.getInstructor();
            out.print("," + instructor.getExternalId());
            out.print(",\"" + instructor.getName() + "\"");
            out.print(",\"" + instructor.getAvailable() + "\"");
            out.print("," + new DecimalFormat("0.0").format(instructor.getMaxLoad()));
            out.print(",\"" + getAttributes(request.getRequest(), instructor, "Skill") + "\"");
            out.print(",\"" + getAttributes(request.getRequest(), instructor, "Qualification") + "\"");
            out.print(",\"" + getAttributes(request.getRequest(), instructor, "Performance Level") + "\"");
            out.print(",\"" + getCoursePreference(request.getRequest(), instructor) + "\"");
        }
        out.println();
    }
    out.flush();
    out.close();
    out = new PrintWriter(new File(dir, "solution-students.csv"));
    out.println("Student,Name,Not Available,Skill,Qualification,Performance,Requests,Max Load,Assigned Load,1st Assignment,2nd Assignment,Skill,Qualification,Performance,Requested");
    for (Instructor instructor : getInstructors()) {
        out.print(instructor.getExternalId());
        out.print(",\"" + instructor.getName() + "\"");
        out.print(",\"" + instructor.getAvailable() + "\"");
        out.print(",\"" + getAttributes(instructor, "Skill") + "\"");
        out.print(",\"" + getAttributes(instructor, "Qualification") + "\"");
        out.print(",\"" + getAttributes(instructor, "Performance Level") + "\"");
        out.print(",\"" + getCoursePrefs(instructor) + "\"");
        out.print("," + new DecimalFormat("0.0").format(instructor.getMaxLoad()));
        Instructor.Context context = instructor.getContext(assignment);
        out.print("," + new DecimalFormat("0.0").format(context.getLoad()));
        /*
            out.print("," + (context.countBackToBackPercentage() == 0.0 ? "" : new DecimalFormat("0.0").format(100.0 * context.countBackToBackPercentage())));
            out.print("," + (context.countDifferentLectures() == 0.0 ? "" : new DecimalFormat("0.0").format(100.0 * context.countDifferentLectures())));
            int share = 0;
            for (TeachingAssignment ta : context.getAssignments()) {
                for (Preference<TimeLocation> p: instructor.getTimePreferences()) {
                    if (!p.isProhibited())
                        share += ta.variable().getRequest().share(p.getTarget());
                }
            }
            out.print("," + (share == 0 ? "" : new DecimalFormat("0.#").format(share / 12.0)));
            */
        TeachingRequest req = null;
        for (TeachingAssignment ta : context.getAssignments()) {
            String sect = "";
            if (req == null || req.getRequestId() < 0)
                req = ta.variable().getRequest();
            if (ta.variable().getId() < 0) {
                sect = "SUPER";
            } else {
                for (Iterator<Section> i = ta.variable().getSections().iterator(); i.hasNext(); ) {
                    Section section = i.next();
                    if (section.isCommon() && section.isAllowOverlap())
                        continue;
                    sect += (sect.isEmpty() ? "" : ", ") + (section.isCommon() ? "(" : "") + section.getSectionType() + " " + section.getExternalId() + (section.getTime() == null ? "" : " " + section.getTime().getDayHeader() + " " + section.getTime().getStartTimeHeader(true) + "-" + section.getTime().getEndTimeHeader(true)) + (section.isCommon() ? ")" : "");
                }
            }
            out.print(",\"" + ta.variable().getCourse() + " " + sect + "\"");
        }
        if (req != null) {
            for (int i = context.getAssignments().size(); i < 2; i++) out.print(",");
            out.print(",\"" + getAttributes(req, instructor, "Skill") + "\"");
            out.print(",\"" + getAttributes(req, instructor, "Qualification") + "\"");
            out.print(",\"" + getAttributes(req, instructor, "Performance Level") + "\"");
            out.print(",\"" + getCoursePreference(req, instructor) + "\"");
        }
        out.println();
    }
    out.flush();
    out.close();
}
Also used : TeachingAssignment(org.cpsolver.instructor.model.TeachingAssignment) DecimalFormat(java.text.DecimalFormat) Instructor(org.cpsolver.instructor.model.Instructor) TeachingRequest(org.cpsolver.instructor.model.TeachingRequest) File(java.io.File) Section(org.cpsolver.instructor.model.Section) PrintWriter(java.io.PrintWriter)

Aggregations

File (java.io.File)2 Instructor (org.cpsolver.instructor.model.Instructor)2 Section (org.cpsolver.instructor.model.Section)2 TeachingRequest (org.cpsolver.instructor.model.TeachingRequest)2 BufferedReader (java.io.BufferedReader)1 FileReader (java.io.FileReader)1 IOException (java.io.IOException)1 PrintWriter (java.io.PrintWriter)1 DecimalFormat (java.text.DecimalFormat)1 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 HashSet (java.util.HashSet)1 List (java.util.List)1 Map (java.util.Map)1 TreeSet (java.util.TreeSet)1 TimeLocation (org.cpsolver.coursett.model.TimeLocation)1 Constraint (org.cpsolver.ifs.model.Constraint)1 SameInstructorConstraint (org.cpsolver.instructor.constraints.SameInstructorConstraint)1 SameLinkConstraint (org.cpsolver.instructor.constraints.SameLinkConstraint)1 Attribute (org.cpsolver.instructor.model.Attribute)1