Search in sources :

Example 6 with ExamInstructor

use of org.cpsolver.exam.model.ExamInstructor in project cpsolver by UniTime.

the class ExamColoringConstruction method selectNeighbour.

@Override
public Neighbour<Exam, ExamPlacement> selectNeighbour(final Solution<Exam, ExamPlacement> solution) {
    ExamModel model = (ExamModel) solution.getModel();
    // if (!model.assignedVariables().isEmpty()) return null;
    final HashMap<Exam, Vertex> vertices = new HashMap<Exam, Vertex>();
    for (Exam x : model.variables()) {
        vertices.put(x, new Vertex(x));
    }
    for (ExamStudent s : model.getStudents()) {
        for (Exam x : s.variables()) {
            for (Exam y : s.variables()) {
                if (!x.equals(y)) {
                    vertices.get(x).neighbors().add(vertices.get(y));
                    vertices.get(y).neighbors().add(vertices.get(x));
                }
            }
        }
    }
    for (ExamInstructor i : model.getInstructors()) {
        for (Exam x : i.variables()) {
            for (Exam y : i.variables()) {
                if (!x.equals(y)) {
                    vertices.get(x).neighbors().add(vertices.get(y));
                    vertices.get(y).neighbors().add(vertices.get(x));
                }
            }
        }
    }
    iProgress.setPhase("Graph coloring-based construction", vertices.size());
    iProgress.info("Looking for a conflict-free assignment using " + model.getPeriods().size() + " periods.");
    iT0 = JProf.currentTimeSec();
    iTimeLimitReached = false;
    if (iMode.isGreedy()) {
        iProgress.info("Using greedy heuristics only (no backtracking)...");
    } else if (backTrack(solution, 0, (iMode.isRedundant() ? null : new HashSet<Integer>()), vertices.values())) {
        iProgress.info("Success!");
    } else if (iTimeLimitReached) {
        iProgress.info("There was no conflict-free schedule found during the given time.");
    } else if (iSolver.isStop()) {
        iProgress.info("Solver was stopped.");
    } else {
        if (iMode.isRedundant())
            iProgress.info("There is no conflict-free schedule!");
        else
            iProgress.info("Conflict-free schedule not found.");
    }
    if (iMode.isConstraintCheck())
        solution.restoreBest();
    HashSet<Vertex> remaning = new HashSet<Vertex>();
    for (Vertex v : vertices.values()) if (v.color() < 0)
        remaning.add(v);
    remaining: while (!remaning.isEmpty()) {
        Vertex vertex = null;
        for (Vertex v : remaning) if (vertex == null || v.compareTo(vertex) < 0)
            vertex = v;
        remaning.remove(vertex);
        for (int color : vertex.domain()) if (vertex.colorize(solution.getAssignment(), color))
            continue remaining;
    }
    if (!iMode.isConstraintCheck()) {
        return new Neighbour<Exam, ExamPlacement>() {

            @Override
            public void assign(Assignment<Exam, ExamPlacement> assignment, long iteration) {
                iProgress.info("Using graph coloring solution ...");
                for (Vertex vertex : new TreeSet<Vertex>(vertices.values())) {
                    ExamPeriodPlacement period = vertex.period();
                    if (period == null || !vertex.exam().checkDistributionConstraints(assignment, period))
                        continue;
                    Set<ExamRoomPlacement> rooms = findRooms(assignment, vertex.exam(), period);
                    if (rooms == null)
                        continue;
                    assignment.assign(iteration, new ExamPlacement(vertex.exam(), period, rooms));
                }
                HashSet<Vertex> unassigned = new HashSet<Vertex>();
                for (Vertex vertex : vertices.values()) {
                    if (assignment.getValue(vertex.exam()) == null) {
                        unassigned.add(vertex);
                        vertex.colorize(assignment, -1);
                    }
                }
                solution.saveBest();
                iProgress.info("Extending ...");
                unassigned: while (!unassigned.isEmpty()) {
                    Vertex vertex = null;
                    for (Vertex v : unassigned) if (vertex == null || v.compareTo(vertex) < 0)
                        vertex = v;
                    unassigned.remove(vertex);
                    for (int color : vertex.domain()) {
                        if (!vertex.colorize(assignment, color))
                            continue;
                        ExamPeriodPlacement period = vertex.period(color);
                        if (period == null || !vertex.exam().checkDistributionConstraints(assignment, period))
                            continue;
                        Set<ExamRoomPlacement> rooms = findRooms(assignment, vertex.exam(), period);
                        if (rooms == null)
                            continue;
                        assignment.assign(iteration, new ExamPlacement(vertex.exam(), period, rooms));
                        continue unassigned;
                    }
                    vertex.colorize(assignment, -1);
                }
                solution.saveBest();
                iProgress.info("Construction done.");
            }

            @Override
            public double value(Assignment<Exam, ExamPlacement> assignment) {
                return 0;
            }

            @Override
            public Map<Exam, ExamPlacement> assignments() {
                throw new UnsupportedOperationException();
            }
        };
    }
    return null;
}
Also used : ExamInstructor(org.cpsolver.exam.model.ExamInstructor) Set(java.util.Set) TreeSet(java.util.TreeSet) HashSet(java.util.HashSet) HashMap(java.util.HashMap) ExamModel(org.cpsolver.exam.model.ExamModel) ExamStudent(org.cpsolver.exam.model.ExamStudent) Assignment(org.cpsolver.ifs.assignment.Assignment) ExamRoomPlacement(org.cpsolver.exam.model.ExamRoomPlacement) Neighbour(org.cpsolver.ifs.model.Neighbour) ExamPlacement(org.cpsolver.exam.model.ExamPlacement) TreeSet(java.util.TreeSet) ExamPeriodPlacement(org.cpsolver.exam.model.ExamPeriodPlacement) Exam(org.cpsolver.exam.model.Exam) HashSet(java.util.HashSet)

Example 7 with ExamInstructor

use of org.cpsolver.exam.model.ExamInstructor in project cpsolver by UniTime.

the class ExamInstructorConflicts method report.

/**
     * generate report
     * @param assignment current assignment
     * @return resultant report
     */
public CSVFile report(Assignment<Exam, ExamPlacement> assignment) {
    CSVFile csv = new CSVFile();
    csv.setHeader(new CSVField[] { new CSVField("Instructor"), new CSVField("Type"), new CSVField("Section/Course"), new CSVField("Period"), new CSVField("Day"), new CSVField("Time"), new CSVField("Room"), new CSVField("Distance") });
    boolean isDayBreakBackToBack = ((InstructorBackToBackConflicts) iModel.getCriterion(InstructorBackToBackConflicts.class)).isDayBreakBackToBack();
    double backToBackDistance = ((InstructorDistanceBackToBackConflicts) iModel.getCriterion(InstructorDistanceBackToBackConflicts.class)).getBackToBackDistance();
    for (ExamInstructor instructor : iModel.getInstructors()) {
        for (ExamPeriod period : iModel.getPeriods()) {
            int nrExams = instructor.getExams(assignment, period).size();
            if (nrExams > 1) {
                String sections = "";
                String rooms = "";
                String periods = String.valueOf(period.getIndex() + 1);
                String periodDays = period.getDayStr();
                String periodTimes = period.getTimeStr();
                for (Exam exam : instructor.getExams(assignment, period)) {
                    ExamPlacement placement = assignment.getValue(exam);
                    String roomsThisExam = "";
                    for (ExamRoomPlacement room : placement.getRoomPlacements()) {
                        if (roomsThisExam.length() > 0)
                            roomsThisExam += ", ";
                        roomsThisExam += room.getName();
                    }
                    boolean first = true;
                    for (ExamOwner owner : exam.getOwners(instructor)) {
                        if (sections.length() > 0) {
                            sections += "\n";
                            rooms += "\n";
                            periods += "\n";
                            periodDays += "\n";
                            periodTimes += "\n";
                        }
                        sections += owner.getName();
                        if (first)
                            rooms += roomsThisExam;
                        first = false;
                    }
                    if (exam.getOwners(instructor).isEmpty()) {
                        sections += exam.getName();
                        rooms += roomsThisExam;
                    }
                }
                csv.addLine(new CSVField[] { new CSVField(instructor.getName()), new CSVField("direct"), new CSVField(sections), new CSVField(periods), new CSVField(periodDays), new CSVField(periodTimes), new CSVField(rooms) });
            }
            if (nrExams > 0) {
                if (period.next() != null && !instructor.getExams(assignment, period.next()).isEmpty() && (!isDayBreakBackToBack || period.next().getDay() == period.getDay())) {
                    for (Exam ex1 : instructor.getExams(assignment, period)) {
                        for (Exam ex2 : instructor.getExams(assignment, period.next())) {
                            ExamPlacement placement = assignment.getValue(ex1);
                            String sections = "";
                            String rooms = "";
                            String roomsThisExam = "";
                            String periods = String.valueOf(period.getIndex() + 1);
                            String periodDays = period.getDayStr();
                            String periodTimes = period.getTimeStr();
                            for (ExamRoomPlacement room : placement.getRoomPlacements()) {
                                if (roomsThisExam.length() > 0)
                                    roomsThisExam += ", ";
                                roomsThisExam += room.getName();
                            }
                            boolean first = true;
                            for (ExamOwner owner : ex1.getOwners(instructor)) {
                                if (sections.length() > 0) {
                                    sections += "\n";
                                    rooms += "\n";
                                    periods += "\n";
                                    periodDays += "\n";
                                    periodTimes += "\n";
                                }
                                sections += owner.getName();
                                if (first)
                                    rooms += roomsThisExam;
                                first = false;
                            }
                            if (ex1.getOwners(instructor).isEmpty()) {
                                sections += ex1.getName();
                                rooms += roomsThisExam;
                            }
                            placement = assignment.getValue(ex2);
                            roomsThisExam = "";
                            for (ExamRoomPlacement room : placement.getRoomPlacements()) {
                                if (roomsThisExam.length() > 0)
                                    roomsThisExam += ", ";
                                roomsThisExam += room.getName();
                            }
                            first = true;
                            for (ExamOwner owner : ex2.getOwners(instructor)) {
                                sections += "\n";
                                rooms += "\n";
                                periods += "\n";
                                periodDays += "\n";
                                periodTimes += "\n";
                                sections += owner.getName();
                                if (first) {
                                    rooms += roomsThisExam;
                                    periods += String.valueOf(period.next().getIndex() + 1);
                                    periodDays += period.next().getDayStr();
                                    periodTimes += period.next().getTimeStr();
                                }
                                first = false;
                            }
                            if (ex2.getOwners(instructor).isEmpty()) {
                                sections += "\n";
                                rooms += "\n";
                                periods += "\n";
                                periodDays += "\n";
                                periodTimes += "\n";
                                sections += ex2.getName();
                                rooms += roomsThisExam;
                                periods += String.valueOf(period.next().getIndex() + 1);
                                periodDays += period.next().getDayStr();
                                periodTimes += period.next().getTimeStr();
                            }
                            String distStr = "";
                            if (backToBackDistance >= 0) {
                                double dist = (assignment.getValue(ex1)).getDistanceInMeters(assignment.getValue(ex2));
                                if (dist > 0)
                                    distStr = String.valueOf(dist);
                            }
                            csv.addLine(new CSVField[] { new CSVField(instructor.getName()), new CSVField("back-to-back"), new CSVField(sections), new CSVField(periods), new CSVField(periodDays), new CSVField(periodTimes), new CSVField(rooms), new CSVField(distStr) });
                        }
                    }
                }
            }
            if (period.next() == null || period.next().getDay() != period.getDay()) {
                int nrExamsADay = instructor.getExamsADay(assignment, period.getDay()).size();
                if (nrExamsADay > 2) {
                    String sections = "";
                    String periods = "";
                    String periodDays = "";
                    String periodTimes = "";
                    String rooms = "";
                    for (Exam exam : instructor.getExamsADay(assignment, period.getDay())) {
                        ExamPlacement placement = assignment.getValue(exam);
                        String roomsThisExam = "";
                        for (ExamRoomPlacement room : placement.getRoomPlacements()) {
                            if (roomsThisExam.length() > 0)
                                roomsThisExam += ", ";
                            roomsThisExam += room.getName();
                        }
                        boolean first = true;
                        for (ExamOwner owner : exam.getOwners(instructor)) {
                            if (sections.length() > 0) {
                                sections += "\n";
                                rooms += "\n";
                                periods += "\n";
                                periodDays += "\n";
                                periodTimes += "\n";
                            }
                            sections += owner.getName();
                            if (first) {
                                periods += (placement.getPeriod().getIndex() + 1);
                                periodDays += placement.getPeriod().getDayStr();
                                periodTimes += placement.getPeriod().getTimeStr();
                                rooms += roomsThisExam;
                            }
                            first = false;
                        }
                        if (exam.getOwners(instructor).isEmpty()) {
                            if (sections.length() > 0) {
                                sections += "\n";
                                rooms += "\n";
                                periods += "\n";
                                periodDays += "\n";
                                periodTimes += "\n";
                            }
                            sections += exam.getName();
                            periods += (placement.getPeriod().getIndex() + 1);
                            periodDays += placement.getPeriod().getDayStr();
                            periodTimes += placement.getPeriod().getTimeStr();
                            rooms += roomsThisExam;
                        }
                    }
                    csv.addLine(new CSVField[] { new CSVField(instructor.getName()), new CSVField("more-2-day"), new CSVField(sections), new CSVField(periods), new CSVField(periodDays), new CSVField(periodTimes), new CSVField(rooms) });
                }
            }
        }
    }
    return csv;
}
Also used : ExamInstructor(org.cpsolver.exam.model.ExamInstructor) ExamPeriod(org.cpsolver.exam.model.ExamPeriod) CSVFile(org.cpsolver.ifs.util.CSVFile) InstructorDistanceBackToBackConflicts(org.cpsolver.exam.criteria.InstructorDistanceBackToBackConflicts) InstructorBackToBackConflicts(org.cpsolver.exam.criteria.InstructorBackToBackConflicts) ExamRoomPlacement(org.cpsolver.exam.model.ExamRoomPlacement) CSVField(org.cpsolver.ifs.util.CSVFile.CSVField) ExamPlacement(org.cpsolver.exam.model.ExamPlacement) ExamOwner(org.cpsolver.exam.model.ExamOwner) Exam(org.cpsolver.exam.model.Exam)

Example 8 with ExamInstructor

use of org.cpsolver.exam.model.ExamInstructor in project cpsolver by UniTime.

the class InstructorBackToBackConflicts method getValue.

@Override
public double getValue(Assignment<Exam, ExamPlacement> assignment, ExamPlacement value, Set<ExamPlacement> conflicts) {
    Exam exam = value.variable();
    int penalty = 0;
    ExamPeriod period = value.getPeriod();
    Map<ExamInstructor, Set<Exam>> prev = (period.prev() != null && (isDayBreakBackToBack() || period.prev().getDay() == period.getDay()) ? ((ExamModel) getModel()).getInstructorsOfPeriod(assignment, period.prev()) : null);
    Map<ExamInstructor, Set<Exam>> next = (period.next() != null && (isDayBreakBackToBack() || period.next().getDay() == period.getDay()) ? ((ExamModel) getModel()).getInstructorsOfPeriod(assignment, period.next()) : null);
    for (ExamInstructor s : exam.getInstructors()) {
        if (prev != null) {
            Set<Exam> exams = prev.get(s);
            if (exams != null) {
                int nrExams = exams.size() + (exams.contains(exam) ? -1 : 0);
                penalty += nrExams;
            }
        }
        if (next != null) {
            Set<Exam> exams = next.get(s);
            if (exams != null) {
                int nrExams = exams.size() + (exams.contains(exam) ? -1 : 0);
                penalty += nrExams;
            }
        }
    }
    /*
        for (ExamInstructor s : exam.getInstructors()) {
            if (period.prev() != null) {
                if (isDayBreakBackToBack() || period.prev().getDay() == period.getDay()) {
                    Set<Exam> exams = s.getExams(assignment, period.prev());
                    int nrExams = exams.size() + (exams.contains(exam) ? -1 : 0);
                    penalty += nrExams;
                }
            }
            if (period.next() != null) {
                if (isDayBreakBackToBack() || period.next().getDay() == period.getDay()) {
                    Set<Exam> exams = s.getExams(assignment, period.next());
                    int nrExams = exams.size() + (exams.contains(exam) ? -1 : 0);
                    penalty += nrExams;
                }
            }
        }
        */
    return penalty;
}
Also used : ExamPeriod(org.cpsolver.exam.model.ExamPeriod) ExamInstructor(org.cpsolver.exam.model.ExamInstructor) Set(java.util.Set) ExamModel(org.cpsolver.exam.model.ExamModel) Exam(org.cpsolver.exam.model.Exam)

Aggregations

Exam (org.cpsolver.exam.model.Exam)8 ExamInstructor (org.cpsolver.exam.model.ExamInstructor)8 ExamModel (org.cpsolver.exam.model.ExamModel)6 Set (java.util.Set)5 ExamPeriod (org.cpsolver.exam.model.ExamPeriod)3 ExamPlacement (org.cpsolver.exam.model.ExamPlacement)3 InstructorBackToBackConflicts (org.cpsolver.exam.criteria.InstructorBackToBackConflicts)2 InstructorDistanceBackToBackConflicts (org.cpsolver.exam.criteria.InstructorDistanceBackToBackConflicts)2 ExamRoomPlacement (org.cpsolver.exam.model.ExamRoomPlacement)2 ExamStudent (org.cpsolver.exam.model.ExamStudent)2 FileWriter (java.io.FileWriter)1 IOException (java.io.IOException)1 PrintWriter (java.io.PrintWriter)1 DecimalFormat (java.text.DecimalFormat)1 HashMap (java.util.HashMap)1 HashSet (java.util.HashSet)1 TreeSet (java.util.TreeSet)1 ExamRotationPenalty (org.cpsolver.exam.criteria.ExamRotationPenalty)1 InstructorDirectConflicts (org.cpsolver.exam.criteria.InstructorDirectConflicts)1 InstructorMoreThan2ADayConflicts (org.cpsolver.exam.criteria.InstructorMoreThan2ADayConflicts)1