Search in sources :

Example 1 with UniversalPerturbationsCounter

use of org.cpsolver.coursett.heuristics.UniversalPerturbationsCounter in project cpsolver by UniTime.

the class Test method saveOutputCSV.

public static void saveOutputCSV(Solution<Lecture, Placement> s, File file) {
    try {
        DecimalFormat dx = new DecimalFormat("000");
        PrintWriter w = new PrintWriter(new FileWriter(file));
        TimetableModel m = (TimetableModel) s.getModel();
        int firstDaySlot = m.getProperties().getPropertyInt("General.FirstDaySlot", Constants.DAY_SLOTS_FIRST);
        int lastDaySlot = m.getProperties().getPropertyInt("General.LastDaySlot", Constants.DAY_SLOTS_LAST);
        int firstWorkDay = m.getProperties().getPropertyInt("General.FirstWorkDay", 0);
        int lastWorkDay = m.getProperties().getPropertyInt("General.LastWorkDay", Constants.NR_DAYS_WEEK - 1);
        if (lastWorkDay < firstWorkDay)
            lastWorkDay += 7;
        Assignment<Lecture, Placement> a = s.getAssignment();
        int idx = 1;
        w.println("000." + dx.format(idx++) + " Assigned variables," + a.nrAssignedVariables());
        w.println("000." + dx.format(idx++) + " Time [sec]," + sDoubleFormat.format(s.getBestTime()));
        w.println("000." + dx.format(idx++) + " Hard student conflicts," + Math.round(m.getCriterion(StudentHardConflict.class).getValue(a)));
        if (m.getProperties().getPropertyBoolean("General.UseDistanceConstraints", true))
            w.println("000." + dx.format(idx++) + " Distance student conf.," + Math.round(m.getCriterion(StudentDistanceConflict.class).getValue(a)));
        w.println("000." + dx.format(idx++) + " Student conflicts," + Math.round(m.getCriterion(StudentConflict.class).getValue(a)));
        w.println("000." + dx.format(idx++) + " Committed student conflicts," + Math.round(m.getCriterion(StudentCommittedConflict.class).getValue(a)));
        w.println("000." + dx.format(idx++) + " All Student conflicts," + Math.round(m.getCriterion(StudentConflict.class).getValue(a) + m.getCriterion(StudentCommittedConflict.class).getValue(a)));
        w.println("000." + dx.format(idx++) + " Time preferences," + sDoubleFormat.format(m.getCriterion(TimePreferences.class).getValue(a)));
        w.println("000." + dx.format(idx++) + " Room preferences," + Math.round(m.getCriterion(RoomPreferences.class).getValue(a)));
        w.println("000." + dx.format(idx++) + " Useless half-hours," + Math.round(m.getCriterion(UselessHalfHours.class).getValue(a)));
        w.println("000." + dx.format(idx++) + " Broken time patterns," + Math.round(m.getCriterion(BrokenTimePatterns.class).getValue(a)));
        w.println("000." + dx.format(idx++) + " Too big room," + Math.round(m.getCriterion(TooBigRooms.class).getValue(a)));
        w.println("000." + dx.format(idx++) + " Distribution preferences," + sDoubleFormat.format(m.getCriterion(DistributionPreferences.class).getValue(a)));
        if (m.getProperties().getPropertyBoolean("General.UseDistanceConstraints", true))
            w.println("000." + dx.format(idx++) + " Back-to-back instructor pref.," + Math.round(m.getCriterion(BackToBackInstructorPreferences.class).getValue(a)));
        if (m.getProperties().getPropertyBoolean("General.DeptBalancing", true)) {
            w.println("000." + dx.format(idx++) + " Dept. balancing penalty," + sDoubleFormat.format(m.getCriterion(DepartmentBalancingPenalty.class).getValue(a)));
        }
        w.println("000." + dx.format(idx++) + " Same subpart balancing penalty," + sDoubleFormat.format(m.getCriterion(SameSubpartBalancingPenalty.class).getValue(a)));
        if (m.getProperties().getPropertyBoolean("General.MPP", false)) {
            Map<String, Double> mppInfo = ((UniversalPerturbationsCounter) ((Perturbations) m.getCriterion(Perturbations.class)).getPerturbationsCounter()).getCompactInfo(a, m, false, false);
            int pidx = 51;
            w.println("000." + dx.format(pidx++) + " Perturbation penalty," + sDoubleFormat.format(m.getCriterion(Perturbations.class).getValue(a)));
            w.println("000." + dx.format(pidx++) + " Additional perturbations," + m.perturbVariables(a).size());
            int nrPert = 0, nrStudentPert = 0;
            for (Lecture lecture : m.variables()) {
                if (lecture.getInitialAssignment() != null)
                    continue;
                nrPert++;
                nrStudentPert += lecture.classLimit(a);
            }
            w.println("000." + dx.format(pidx++) + " Given perturbations," + nrPert);
            w.println("000." + dx.format(pidx++) + " Given student perturbations," + nrStudentPert);
            for (String key : new TreeSet<String>(mppInfo.keySet())) {
                Double value = mppInfo.get(key);
                w.println("000." + dx.format(pidx++) + " " + key + "," + sDoubleFormat.format(value));
            }
        }
        HashSet<Student> students = new HashSet<Student>();
        int enrls = 0;
        int minRoomPref = 0, maxRoomPref = 0;
        int minGrPref = 0, maxGrPref = 0;
        int minTimePref = 0, maxTimePref = 0;
        int worstInstrPref = 0;
        HashSet<Constraint<Lecture, Placement>> used = new HashSet<Constraint<Lecture, Placement>>();
        for (Lecture lecture : m.variables()) {
            enrls += (lecture.students() == null ? 0 : lecture.students().size());
            students.addAll(lecture.students());
            int[] minMaxRoomPref = lecture.getMinMaxRoomPreference();
            maxRoomPref += minMaxRoomPref[1] - minMaxRoomPref[0];
            double[] minMaxTimePref = lecture.getMinMaxTimePreference();
            maxTimePref += minMaxTimePref[1] - minMaxTimePref[0];
            for (Constraint<Lecture, Placement> c : lecture.constraints()) {
                if (!used.add(c))
                    continue;
                if (c instanceof InstructorConstraint) {
                    InstructorConstraint ic = (InstructorConstraint) c;
                    worstInstrPref += ic.getWorstPreference();
                }
                if (c instanceof GroupConstraint) {
                    GroupConstraint gc = (GroupConstraint) c;
                    if (gc.isHard())
                        continue;
                    maxGrPref += Math.abs(gc.getPreference()) * (1 + (gc.variables().size() * (gc.variables().size() - 1)) / 2);
                }
            }
        }
        int totalCommitedPlacements = 0;
        for (Student student : students) {
            if (student.getCommitedPlacements() != null)
                totalCommitedPlacements += student.getCommitedPlacements().size();
        }
        HashMap<Long, List<Lecture>> subs = new HashMap<Long, List<Lecture>>();
        for (Lecture lecture : m.variables()) {
            if (lecture.isCommitted() || lecture.getScheduler() == null)
                continue;
            List<Lecture> vars = subs.get(lecture.getScheduler());
            if (vars == null) {
                vars = new ArrayList<Lecture>();
                subs.put(lecture.getScheduler(), vars);
            }
            vars.add(lecture);
        }
        int bidx = 101;
        w.println("000." + dx.format(bidx++) + " Assigned variables max," + m.variables().size());
        w.println("000." + dx.format(bidx++) + " Student enrollments," + enrls);
        w.println("000." + dx.format(bidx++) + " Student commited enrollments," + totalCommitedPlacements);
        w.println("000." + dx.format(bidx++) + " All student enrollments," + (enrls + totalCommitedPlacements));
        w.println("000." + dx.format(bidx++) + " Time preferences min," + minTimePref);
        w.println("000." + dx.format(bidx++) + " Time preferences max," + maxTimePref);
        w.println("000." + dx.format(bidx++) + " Room preferences min," + minRoomPref);
        w.println("000." + dx.format(bidx++) + " Room preferences max," + maxRoomPref);
        w.println("000." + dx.format(bidx++) + " Useless half-hours max," + (Constants.sPreferenceLevelStronglyDiscouraged * m.getRoomConstraints().size() * (lastDaySlot - firstDaySlot + 1) * (lastWorkDay - firstWorkDay + 1)));
        w.println("000." + dx.format(bidx++) + " Too big room max," + (Constants.sPreferenceLevelStronglyDiscouraged * m.variables().size()));
        w.println("000." + dx.format(bidx++) + " Distribution preferences min," + minGrPref);
        w.println("000." + dx.format(bidx++) + " Distribution preferences max," + maxGrPref);
        w.println("000." + dx.format(bidx++) + " Back-to-back instructor pref max," + worstInstrPref);
        TooBigRooms tbr = (TooBigRooms) m.getCriterion(TooBigRooms.class);
        for (Long scheduler : new TreeSet<Long>(subs.keySet())) {
            List<Lecture> vars = subs.get(scheduler);
            idx = 001;
            bidx = 101;
            int nrAssg = 0;
            enrls = 0;
            int roomPref = 0;
            minRoomPref = 0;
            maxRoomPref = 0;
            double timePref = 0;
            minTimePref = 0;
            maxTimePref = 0;
            double grPref = 0;
            minGrPref = 0;
            maxGrPref = 0;
            long allSC = 0, hardSC = 0, distSC = 0;
            int instPref = 0;
            worstInstrPref = 0;
            int spreadPen = 0, deptSpreadPen = 0;
            int tooBigRooms = 0;
            int rcs = 0, uselessSlots = 0;
            used = new HashSet<Constraint<Lecture, Placement>>();
            for (Lecture lecture : vars) {
                if (lecture.isCommitted())
                    continue;
                enrls += lecture.students().size();
                Placement placement = a.getValue(lecture);
                if (placement != null) {
                    nrAssg++;
                }
                int[] minMaxRoomPref = lecture.getMinMaxRoomPreference();
                minRoomPref += minMaxRoomPref[0];
                maxRoomPref += minMaxRoomPref[1];
                double[] minMaxTimePref = lecture.getMinMaxTimePreference();
                minTimePref += minMaxTimePref[0];
                maxTimePref += minMaxTimePref[1];
                if (placement != null) {
                    roomPref += placement.getRoomPreference();
                    timePref += placement.getTimeLocation().getNormalizedPreference();
                    if (tbr != null)
                        tooBigRooms += tbr.getPreference(placement);
                }
                for (Constraint<Lecture, Placement> c : lecture.constraints()) {
                    if (!used.add(c))
                        continue;
                    if (c instanceof InstructorConstraint) {
                        InstructorConstraint ic = (InstructorConstraint) c;
                        instPref += ic.getPreference(a);
                        worstInstrPref += ic.getWorstPreference();
                    }
                    if (c instanceof DepartmentSpreadConstraint) {
                        DepartmentSpreadConstraint dsc = (DepartmentSpreadConstraint) c;
                        deptSpreadPen += dsc.getPenalty(a);
                    } else if (c instanceof SpreadConstraint) {
                        SpreadConstraint sc = (SpreadConstraint) c;
                        spreadPen += sc.getPenalty(a);
                    }
                    if (c instanceof GroupConstraint) {
                        GroupConstraint gc = (GroupConstraint) c;
                        if (gc.isHard())
                            continue;
                        minGrPref -= Math.abs(gc.getPreference());
                        maxGrPref += 0;
                        grPref += Math.min(0, gc.getCurrentPreference(a));
                    // minGrPref += Math.min(gc.getPreference(), 0);
                    // maxGrPref += Math.max(gc.getPreference(), 0);
                    // grPref += gc.getCurrentPreference();
                    }
                    if (c instanceof JenrlConstraint) {
                        JenrlConstraint jc = (JenrlConstraint) c;
                        if (!jc.isInConflict(a) || !jc.isOfTheSameProblem())
                            continue;
                        Lecture l1 = jc.first();
                        Lecture l2 = jc.second();
                        allSC += jc.getJenrl();
                        if (l1.areStudentConflictsHard(l2))
                            hardSC += jc.getJenrl();
                        Placement p1 = a.getValue(l1);
                        Placement p2 = a.getValue(l2);
                        if (!p1.getTimeLocation().hasIntersection(p2.getTimeLocation()))
                            distSC += jc.getJenrl();
                    }
                    if (c instanceof RoomConstraint) {
                        RoomConstraint rc = (RoomConstraint) c;
                        uselessSlots += UselessHalfHours.countUselessSlotsHalfHours(rc.getContext(a)) + BrokenTimePatterns.countUselessSlotsBrokenTimePatterns(rc.getContext(a));
                        rcs++;
                    }
                }
            }
            w.println(dx.format(scheduler) + "." + dx.format(idx++) + " Assigned variables," + nrAssg);
            w.println(dx.format(scheduler) + "." + dx.format(bidx++) + " Assigned variables max," + vars.size());
            w.println(dx.format(scheduler) + "." + dx.format(idx++) + " Hard student conflicts," + hardSC);
            w.println(dx.format(scheduler) + "." + dx.format(bidx++) + " Student enrollments," + enrls);
            if (m.getProperties().getPropertyBoolean("General.UseDistanceConstraints", true))
                w.println(dx.format(scheduler) + "." + dx.format(idx++) + " Distance student conf.," + distSC);
            w.println(dx.format(scheduler) + "." + dx.format(idx++) + " Student conflicts," + allSC);
            w.println(dx.format(scheduler) + "." + dx.format(idx++) + " Time preferences," + timePref);
            w.println(dx.format(scheduler) + "." + dx.format(bidx++) + " Time preferences min," + minTimePref);
            w.println(dx.format(scheduler) + "." + dx.format(bidx++) + " Time preferences max," + maxTimePref);
            w.println(dx.format(scheduler) + "." + dx.format(idx++) + " Room preferences," + roomPref);
            w.println(dx.format(scheduler) + "." + dx.format(bidx++) + " Room preferences min," + minRoomPref);
            w.println(dx.format(scheduler) + "." + dx.format(bidx++) + " Room preferences max," + maxRoomPref);
            w.println(dx.format(scheduler) + "." + dx.format(idx++) + " Useless half-hours," + uselessSlots);
            w.println(dx.format(scheduler) + "." + dx.format(bidx++) + " Useless half-hours max," + (Constants.sPreferenceLevelStronglyDiscouraged * rcs * (lastDaySlot - firstDaySlot + 1) * (lastWorkDay - firstWorkDay + 1)));
            w.println(dx.format(scheduler) + "." + dx.format(idx++) + " Too big room," + tooBigRooms);
            w.println(dx.format(scheduler) + "." + dx.format(bidx++) + " Too big room max," + (Constants.sPreferenceLevelStronglyDiscouraged * vars.size()));
            w.println(dx.format(scheduler) + "." + dx.format(idx++) + " Distribution preferences," + grPref);
            w.println(dx.format(scheduler) + "." + dx.format(bidx++) + " Distribution preferences min," + minGrPref);
            w.println(dx.format(scheduler) + "." + dx.format(bidx++) + " Distribution preferences max," + maxGrPref);
            if (m.getProperties().getPropertyBoolean("General.UseDistanceConstraints", true))
                w.println(dx.format(scheduler) + "." + dx.format(idx++) + " Back-to-back instructor pref," + instPref);
            w.println(dx.format(scheduler) + "." + dx.format(bidx++) + " Back-to-back instructor pref max," + worstInstrPref);
            if (m.getProperties().getPropertyBoolean("General.DeptBalancing", true)) {
                w.println(dx.format(scheduler) + "." + dx.format(idx++) + " Department balancing penalty," + sDoubleFormat.format((deptSpreadPen) / 12.0));
            }
            w.println(dx.format(scheduler) + "." + dx.format(idx++) + " Same subpart balancing penalty," + sDoubleFormat.format((spreadPen) / 12.0));
        }
        w.flush();
        w.close();
    } catch (java.io.IOException io) {
        sLogger.error(io.getMessage(), io);
    }
}
Also used : Lecture(org.cpsolver.coursett.model.Lecture) RoomConstraint(org.cpsolver.coursett.constraint.RoomConstraint) DepartmentSpreadConstraint(org.cpsolver.coursett.constraint.DepartmentSpreadConstraint) InstructorConstraint(org.cpsolver.coursett.constraint.InstructorConstraint) SpreadConstraint(org.cpsolver.coursett.constraint.SpreadConstraint) GroupConstraint(org.cpsolver.coursett.constraint.GroupConstraint) Constraint(org.cpsolver.ifs.model.Constraint) RoomConstraint(org.cpsolver.coursett.constraint.RoomConstraint) JenrlConstraint(org.cpsolver.coursett.constraint.JenrlConstraint) HashMap(java.util.HashMap) DepartmentSpreadConstraint(org.cpsolver.coursett.constraint.DepartmentSpreadConstraint) DecimalFormat(java.text.DecimalFormat) FileWriter(java.io.FileWriter) StudentDistanceConflict(org.cpsolver.coursett.criteria.StudentDistanceConflict) DepartmentSpreadConstraint(org.cpsolver.coursett.constraint.DepartmentSpreadConstraint) SpreadConstraint(org.cpsolver.coursett.constraint.SpreadConstraint) UniversalPerturbationsCounter(org.cpsolver.coursett.heuristics.UniversalPerturbationsCounter) Placement(org.cpsolver.coursett.model.Placement) TreeSet(java.util.TreeSet) List(java.util.List) ArrayList(java.util.ArrayList) TimetableModel(org.cpsolver.coursett.model.TimetableModel) PrintWriter(java.io.PrintWriter) HashSet(java.util.HashSet) JenrlConstraint(org.cpsolver.coursett.constraint.JenrlConstraint) TooBigRooms(org.cpsolver.coursett.criteria.TooBigRooms) IOException(java.io.IOException) Student(org.cpsolver.coursett.model.Student) DepartmentSpreadConstraint(org.cpsolver.coursett.constraint.DepartmentSpreadConstraint) InstructorConstraint(org.cpsolver.coursett.constraint.InstructorConstraint) SpreadConstraint(org.cpsolver.coursett.constraint.SpreadConstraint) GroupConstraint(org.cpsolver.coursett.constraint.GroupConstraint) Constraint(org.cpsolver.ifs.model.Constraint) RoomConstraint(org.cpsolver.coursett.constraint.RoomConstraint) JenrlConstraint(org.cpsolver.coursett.constraint.JenrlConstraint) InstructorConstraint(org.cpsolver.coursett.constraint.InstructorConstraint) StudentConflict(org.cpsolver.coursett.criteria.StudentConflict) GroupConstraint(org.cpsolver.coursett.constraint.GroupConstraint) BackToBackInstructorPreferences(org.cpsolver.coursett.criteria.BackToBackInstructorPreferences) StudentCommittedConflict(org.cpsolver.coursett.criteria.StudentCommittedConflict)

Aggregations

FileWriter (java.io.FileWriter)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 TreeSet (java.util.TreeSet)1 DepartmentSpreadConstraint (org.cpsolver.coursett.constraint.DepartmentSpreadConstraint)1 GroupConstraint (org.cpsolver.coursett.constraint.GroupConstraint)1 InstructorConstraint (org.cpsolver.coursett.constraint.InstructorConstraint)1 JenrlConstraint (org.cpsolver.coursett.constraint.JenrlConstraint)1 RoomConstraint (org.cpsolver.coursett.constraint.RoomConstraint)1 SpreadConstraint (org.cpsolver.coursett.constraint.SpreadConstraint)1 BackToBackInstructorPreferences (org.cpsolver.coursett.criteria.BackToBackInstructorPreferences)1 StudentCommittedConflict (org.cpsolver.coursett.criteria.StudentCommittedConflict)1 StudentConflict (org.cpsolver.coursett.criteria.StudentConflict)1 StudentDistanceConflict (org.cpsolver.coursett.criteria.StudentDistanceConflict)1 TooBigRooms (org.cpsolver.coursett.criteria.TooBigRooms)1