Search in sources :

Example 21 with ExamPlacement

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

the class ExamCourseSectionAssignments 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("Section/Course"), new CSVField("Enrl"), new CSVField("Alt"), new CSVField("Period"), new CSVField("Date"), new CSVField("Time"), new CSVField("Room"), new CSVField("Cap") });
    for (Exam exam : iModel.variables()) {
        ExamPlacement placement = assignment.getValue(exam);
        for (ExamOwner owner : exam.getOwners()) {
            List<CSVField> fields = new ArrayList<CSVField>();
            fields.add(new CSVField(owner.getName()));
            fields.add(new CSVField(owner.getStudents().size()));
            fields.add(new CSVField(exam.hasAltSeating() ? "Yes" : "No"));
            if (placement == null) {
                fields.add(new CSVField(""));
                fields.add(new CSVField(""));
                fields.add(new CSVField(""));
                fields.add(new CSVField(""));
                fields.add(new CSVField(""));
            } else {
                fields.add(new CSVField(placement.getPeriod().getIndex() + 1));
                fields.add(new CSVField(placement.getPeriod().getDayStr()));
                fields.add(new CSVField(placement.getPeriod().getTimeStr()));
                String rooms = "";
                String roomSizes = "";
                for (Iterator<ExamRoomPlacement> i = placement.getRoomPlacements().iterator(); i.hasNext(); ) {
                    ExamRoomPlacement room = i.next();
                    rooms += room.getName();
                    roomSizes += room.getSize(exam.hasAltSeating());
                    if (i.hasNext()) {
                        rooms += ", ";
                        roomSizes += ", ";
                    }
                }
                fields.add(new CSVField(rooms));
                fields.add(new CSVField(roomSizes));
            }
            csv.addLine(fields);
        }
    }
    return csv;
}
Also used : CSVFile(org.cpsolver.ifs.util.CSVFile) 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) ArrayList(java.util.ArrayList) Exam(org.cpsolver.exam.model.Exam)

Example 22 with ExamPlacement

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

the class ExamStudentBackToBackConflicts 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("Exam 1"), new CSVField("Enrl 1"), new CSVField("Period 1"), new CSVField("Date 1"), new CSVField("Time 1"), new CSVField("Exam 2"), new CSVField("Enrl 2"), new CSVField("Back-To-Back"), new CSVField("Back-To-Back [%]"), new CSVField("Distance") });
    DecimalFormat df = new DecimalFormat("0.0");
    boolean isDayBreakBackToBack = ((StudentBackToBackConflicts) iModel.getCriterion(StudentBackToBackConflicts.class)).isDayBreakBackToBack();
    double backToBackDistance = ((StudentDistanceBackToBackConflicts) iModel.getCriterion(StudentDistanceBackToBackConflicts.class)).getBackToBackDistance();
    for (Exam ex1 : iModel.variables()) {
        ExamPlacement p1 = assignment.getValue(ex1);
        if (p1 == null || p1.getPeriod().next() == null)
            continue;
        if (!isDayBreakBackToBack && p1.getPeriod().getDay() != p1.getPeriod().next().getDay())
            continue;
        for (Exam ex2 : iModel.variables()) {
            ExamPlacement p2 = assignment.getValue(ex2);
            if (p2 == null || !p2.getPeriod().equals(p1.getPeriod().next()))
                continue;
            List<ExamStudent> students = ex1.getJointEnrollments().get(ex2);
            if (students == null || students.isEmpty())
                continue;
            String distStr = "";
            if (backToBackDistance >= 0) {
                double dist = p1.getDistanceInMeters(p2);
                if (dist > 0)
                    distStr = String.valueOf(dist);
            }
            csv.addLine(new CSVField[] { new CSVField(ex1.getName()), new CSVField(ex1.getStudents().size()), new CSVField(p1.getPeriod().getIndex() + 1), new CSVField(p1.getPeriod().getDayStr()), new CSVField(p1.getPeriod().getTimeStr()), new CSVField(ex2.getName()), new CSVField(ex2.getStudents().size()), new CSVField(students.size()), new CSVField(df.format(100.0 * students.size() / Math.min(ex1.getStudents().size(), ex2.getStudents().size()))), new CSVField(distStr) });
        }
    }
    return csv;
}
Also used : CSVFile(org.cpsolver.ifs.util.CSVFile) CSVField(org.cpsolver.ifs.util.CSVFile.CSVField) ExamPlacement(org.cpsolver.exam.model.ExamPlacement) StudentBackToBackConflicts(org.cpsolver.exam.criteria.StudentBackToBackConflicts) DecimalFormat(java.text.DecimalFormat) ExamStudent(org.cpsolver.exam.model.ExamStudent) StudentDistanceBackToBackConflicts(org.cpsolver.exam.criteria.StudentDistanceBackToBackConflicts) Exam(org.cpsolver.exam.model.Exam)

Example 23 with ExamPlacement

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

the class ExamRoomSchedule method report.

public CSVFile report(Assignment<Exam, ExamPlacement> assignment) {
    CSVFile csv = new CSVFile();
    csv.setHeader(new CSVField[] { new CSVField("Room"), new CSVField("Cap"), new CSVField("AltCap"), new CSVField("Period"), new CSVField("Date"), new CSVField("Time"), new CSVField("Exam"), new CSVField("Enrl") });
    List<ExamRoom> rooms = new ArrayList<ExamRoom>(iModel.getRooms());
    Collections.sort(rooms, new Comparator<ExamRoom>() {

        @Override
        public int compare(ExamRoom r1, ExamRoom r2) {
            int cmp = -Double.compare(r1.getSize(), r2.getSize());
            if (cmp != 0)
                return cmp;
            cmp = -Double.compare(r1.getAltSize(), r2.getAltSize());
            if (cmp != 0)
                return cmp;
            return r1.compareTo(r2);
        }
    });
    for (ExamRoom room : rooms) {
        boolean first = true;
        int day = -1;
        for (ExamPeriod period : iModel.getPeriods()) {
            for (ExamPlacement placement : room.getPlacements(assignment, period)) {
                Exam exam = placement.variable();
                csv.addLine(new CSVField[] { new CSVField(first ? room.getName() : ""), new CSVField(first ? "" + room.getSize() : ""), new CSVField(first ? "" + room.getAltSize() : ""), new CSVField(period.getIndex() + 1), new CSVField(day == period.getDay() ? "" : period.getDayStr()), new CSVField(period.getTimeStr()), new CSVField(exam.getName()), new CSVField(exam.getStudents().size()) });
                first = false;
                day = period.getDay();
            }
        }
    }
    return csv;
}
Also used : ExamPeriod(org.cpsolver.exam.model.ExamPeriod) CSVFile(org.cpsolver.ifs.util.CSVFile) ExamRoom(org.cpsolver.exam.model.ExamRoom) CSVField(org.cpsolver.ifs.util.CSVFile.CSVField) ExamPlacement(org.cpsolver.exam.model.ExamPlacement) ArrayList(java.util.ArrayList) Exam(org.cpsolver.exam.model.Exam)

Example 24 with ExamPlacement

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

the class ExamRoomMove method selectNeighbour.

/**
     * Select an exam randomly, select an available period randomly (if it is
     * not assigned, from {@link Exam#getPeriodPlacements()}), select rooms
     * using {@link Exam#findRoomsRandom(Assignment, ExamPeriodPlacement)}
     */
@Override
public Neighbour<Exam, ExamPlacement> selectNeighbour(Solution<Exam, ExamPlacement> solution) {
    ExamModel model = (ExamModel) solution.getModel();
    Assignment<Exam, ExamPlacement> assignment = solution.getAssignment();
    Exam exam = ToolBox.random(model.variables());
    if (exam.getMaxRooms() <= 0)
        return null;
    ExamPlacement placement = assignment.getValue(exam);
    ExamPeriodPlacement period = (placement != null ? placement.getPeriodPlacement() : (ExamPeriodPlacement) ToolBox.random(exam.getPeriodPlacements()));
    if (iCheckStudentConflicts && placement == null && exam.countStudentConflicts(assignment, period) > 0)
        return null;
    if (iCheckDistributionConstraints && placement == null && !exam.checkDistributionConstraints(assignment, period))
        return null;
    Set<ExamRoomPlacement> rooms = (placement != null ? placement.getRoomPlacements() : exam.findBestAvailableRooms(assignment, period));
    if (rooms == null || rooms.isEmpty())
        return null;
    if (placement == null)
        placement = new ExamPlacement(exam, period, rooms);
    List<ExamRoomPlacement> roomVect = new ArrayList<ExamRoomPlacement>(rooms);
    int rx = ToolBox.random(roomVect.size());
    for (int r = 0; r < roomVect.size(); r++) {
        ExamRoomPlacement current = roomVect.get((r + rx) % roomVect.size());
        int mx = ToolBox.random(exam.getRoomPlacements().size());
        for (int m = 0; m < exam.getRoomPlacements().size(); m++) {
            ExamRoomPlacement swap = exam.getRoomPlacements().get((m + mx) % exam.getRoomPlacements().size());
            ExamRoomSwapNeighbour n = new ExamRoomSwapNeighbour(assignment, placement, current, swap);
            if (n.canDo())
                return n;
        }
    }
    rooms = exam.findRoomsRandom(assignment, period);
    if (rooms == null)
        return null;
    return new ExamSimpleNeighbour(assignment, new ExamPlacement(exam, period, rooms));
}
Also used : ExamRoomPlacement(org.cpsolver.exam.model.ExamRoomPlacement) ExamPlacement(org.cpsolver.exam.model.ExamPlacement) ExamModel(org.cpsolver.exam.model.ExamModel) ArrayList(java.util.ArrayList) ExamPeriodPlacement(org.cpsolver.exam.model.ExamPeriodPlacement) Exam(org.cpsolver.exam.model.Exam)

Example 25 with ExamPlacement

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

the class ExamTabuSearch method selectValue.

/**
     * Value selection
     */
@Override
public ExamPlacement selectValue(Solution<Exam, ExamPlacement> solution, Exam exam) {
    if (iFirstIteration < 0)
        iFirstIteration = solution.getIteration();
    TabuList tabu = getContext(solution.getAssignment());
    long idle = solution.getIteration() - Math.max(iFirstIteration, solution.getBestIteration());
    if (idle > iMaxIdleIterations) {
        sLog.debug("  [tabu]    max idle iterations reached");
        iFirstIteration = -1;
        if (tabu.size() > 0)
            tabu.clear();
        return null;
    }
    if (tabu.size() > 0 && iTabuMaxSize > iTabuMinSize) {
        if (idle == 0) {
            tabu.resize(iTabuMinSize);
        } else if (idle % (iMaxIdleIterations / (iTabuMaxSize - iTabuMinSize)) == 0) {
            tabu.resize(Math.min(iTabuMaxSize, tabu.size() + 1));
        }
    }
    ExamModel model = (ExamModel) solution.getModel();
    Assignment<Exam, ExamPlacement> assignment = solution.getAssignment();
    double bestEval = 0.0;
    List<ExamPlacement> best = null;
    ExamPlacement assigned = assignment.getValue(exam);
    // double assignedVal =
    // (assigned==null?-iConflictWeight:iValueWeight*assigned.toDouble());
    double assignedVal = (assigned == null ? iConflictWeight : iValueWeight * assigned.toDouble(assignment));
    for (ExamPeriodPlacement period : exam.getPeriodPlacements()) {
        Set<ExamRoomPlacement> rooms = exam.findBestAvailableRooms(assignment, period);
        if (rooms == null)
            rooms = exam.findRoomsRandom(assignment, period, false);
        if (rooms == null) {
            sLog.info("Exam " + exam.getName() + " has no rooms for period " + period);
            continue;
        }
        ExamPlacement value = new ExamPlacement(exam, period, rooms);
        if (value.equals(assigned))
            continue;
        Set<ExamPlacement> conflicts = model.conflictValues(assignment, value);
        double eval = iValueWeight * value.toDouble(assignment) - assignedVal;
        for (ExamPlacement conflict : conflicts) {
            eval -= iValueWeight * conflict.toDouble(assignment);
            eval += iConflictWeight * (1.0 + (iStat == null ? 0.0 : iStat.countRemovals(solution.getIteration(), conflict, value)));
        }
        if (tabu.size() > 0 && tabu.contains(exam.getId() + ":" + value.getPeriod().getIndex())) {
            int un = model.variables().size() - assignment.nrAssignedVariables() - (assigned == null ? 0 : 1);
            if (un > model.getBestUnassignedVariables())
                continue;
            if (un == model.getBestUnassignedVariables() && model.getTotalValue(assignment) + eval >= solution.getBestValue())
                continue;
        }
        if (best == null || bestEval > eval) {
            if (best == null)
                best = new ArrayList<ExamPlacement>();
            else
                best.clear();
            best.add(value);
            bestEval = eval;
        } else if (bestEval == eval) {
            best.add(value);
        }
    }
    if (best == null)
        return null;
    ExamPlacement bestVal = ToolBox.random(best);
    if (sLog.isDebugEnabled()) {
        Set<ExamPlacement> conflicts = model.conflictValues(assignment, bestVal);
        double wconf = (iStat == null ? 0.0 : iStat.countRemovals(solution.getIteration(), conflicts, bestVal));
        sLog.debug("  [tabu] " + bestVal + " (" + (assignment.getValue(bestVal.variable()) == null ? "" : "was=" + assignment.getValue(bestVal.variable()) + ", ") + "val=" + bestEval + (conflicts.isEmpty() ? "" : ", conf=" + (wconf + conflicts.size()) + "/" + conflicts) + ")");
    }
    if (tabu.size() > 0)
        tabu.add(exam.getId() + ":" + bestVal.getPeriod().getIndex());
    return bestVal;
}
Also used : ExamModel(org.cpsolver.exam.model.ExamModel) ArrayList(java.util.ArrayList) ExamRoomPlacement(org.cpsolver.exam.model.ExamRoomPlacement) ExamPlacement(org.cpsolver.exam.model.ExamPlacement) ExamPeriodPlacement(org.cpsolver.exam.model.ExamPeriodPlacement) Exam(org.cpsolver.exam.model.Exam)

Aggregations

ExamPlacement (org.cpsolver.exam.model.ExamPlacement)40 Exam (org.cpsolver.exam.model.Exam)38 ExamRoomPlacement (org.cpsolver.exam.model.ExamRoomPlacement)21 ArrayList (java.util.ArrayList)14 ExamModel (org.cpsolver.exam.model.ExamModel)13 CSVFile (org.cpsolver.ifs.util.CSVFile)12 CSVField (org.cpsolver.ifs.util.CSVFile.CSVField)12 ExamPeriodPlacement (org.cpsolver.exam.model.ExamPeriodPlacement)11 ExamStudent (org.cpsolver.exam.model.ExamStudent)9 DecimalFormat (java.text.DecimalFormat)6 AssignmentContext (org.cpsolver.ifs.assignment.context.AssignmentContext)6 NeighbourSelectionWithContext (org.cpsolver.ifs.assignment.context.NeighbourSelectionWithContext)6 ExamPeriod (org.cpsolver.exam.model.ExamPeriod)5 HashMap (java.util.HashMap)4 HashSet (java.util.HashSet)4 ExamOwner (org.cpsolver.exam.model.ExamOwner)4 IOException (java.io.IOException)3 TreeSet (java.util.TreeSet)3 ExamInstructor (org.cpsolver.exam.model.ExamInstructor)3 File (java.io.File)2