Search in sources :

Example 36 with TimeLocation

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

Aggregations

TimeLocation (org.cpsolver.coursett.model.TimeLocation)36 Placement (org.cpsolver.coursett.model.Placement)17 Lecture (org.cpsolver.coursett.model.Lecture)14 HashSet (java.util.HashSet)12 RoomLocation (org.cpsolver.coursett.model.RoomLocation)9 Constraint (org.cpsolver.ifs.model.Constraint)9 ArrayList (java.util.ArrayList)8 BitSet (java.util.BitSet)7 HashMap (java.util.HashMap)7 Element (org.dom4j.Element)7 WeakeningConstraint (org.cpsolver.ifs.model.WeakeningConstraint)6 DecimalFormat (java.text.DecimalFormat)5 List (java.util.List)4 Map (java.util.Map)4 TreeSet (java.util.TreeSet)4 RoomConstraint (org.cpsolver.coursett.constraint.RoomConstraint)4 GroupConstraint (org.cpsolver.coursett.constraint.GroupConstraint)3 InstructorConstraint (org.cpsolver.coursett.constraint.InstructorConstraint)3 SpreadConstraint (org.cpsolver.coursett.constraint.SpreadConstraint)3 Student (org.cpsolver.coursett.model.Student)3