Search in sources :

Example 11 with ExamRoomPlacement

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

the class ExamAssignments 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"), 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);
        ArrayList<CSVField> fields = new ArrayList<CSVField>();
        fields.add(new CSVField(exam.getName()));
        fields.add(new CSVField(exam.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.getRoom().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) ArrayList(java.util.ArrayList) Exam(org.cpsolver.exam.model.Exam)

Example 12 with ExamRoomPlacement

use of org.cpsolver.exam.model.ExamRoomPlacement 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 13 with ExamRoomPlacement

use of org.cpsolver.exam.model.ExamRoomPlacement 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 14 with ExamRoomPlacement

use of org.cpsolver.exam.model.ExamRoomPlacement 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)

Example 15 with ExamRoomPlacement

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

the class ExamPeriodSwapMove method selectNeighbour.

/**
     * Select an exam randomly,
     * select an available period randomly (if it is not assigned), 
     * use rooms if possible, select rooms using {@link Exam#findBestAvailableRooms(Assignment, ExamPeriodPlacement)} if not (exam is unassigned, a room is not available or used).
     */
@Override
public Neighbour<Exam, ExamPlacement> selectNeighbour(Solution<Exam, ExamPlacement> solution) {
    ExamModel model = (ExamModel) solution.getModel();
    Assignment<Exam, ExamPlacement> assignment = solution.getAssignment();
    Exam x1 = ToolBox.random(model.variables());
    ExamPlacement v1 = assignment.getValue(x1);
    if (v1 == null)
        return null;
    int x = ToolBox.random(model.variables().size());
    for (int v = 0; v < model.variables().size(); v++) {
        Exam x2 = model.variables().get((v + x) % (model.variables().size()));
        ExamPlacement v2 = assignment.getValue(x2);
        if (x1.equals(x2) || v2 == null)
            continue;
        ExamPeriodPlacement p1 = x1.getPeriodPlacement(v2.getPeriod());
        ExamPeriodPlacement p2 = x2.getPeriodPlacement(v1.getPeriod());
        if (p1 == null || p2 == null)
            continue;
        if (iCheckStudentConflicts && (x1.countStudentConflicts(assignment, p1) > 0 || x2.countStudentConflicts(assignment, p2) > 0))
            continue;
        if (iCheckDistributionConstraints) {
            Map<Exam, ExamPlacement> placements = new HashMap<Exam, ExamPlacement>();
            placements.put(x1, new ExamPlacement(x1, p1, new HashSet<ExamRoomPlacement>()));
            placements.put(x2, new ExamPlacement(x2, p2, new HashSet<ExamRoomPlacement>()));
            if (!checkDistributionConstraints(assignment, x1, p1, placements) || !checkDistributionConstraints(assignment, x2, p2, placements))
                continue;
        }
        Set<ExamPlacement> conflicts = new HashSet<ExamPlacement>();
        conflicts.add(v1);
        conflicts.add(v2);
        Map<Exam, ExamPlacement> placements = new HashMap<Exam, ExamPlacement>();
        Set<ExamRoomPlacement> r1 = findBestAvailableRooms(assignment, x1, p1, conflicts, placements);
        if (r1 == null)
            continue;
        placements.put(x1, new ExamPlacement(x1, p1, r1));
        Set<ExamRoomPlacement> r2 = findBestAvailableRooms(assignment, x2, p2, conflicts, placements);
        if (r2 == null)
            continue;
        return new LazySwap<Exam, ExamPlacement>(new ExamPlacement(x1, p1, r1), new ExamPlacement(x2, p2, r2));
    }
    return null;
}
Also used : HashMap(java.util.HashMap) ExamModel(org.cpsolver.exam.model.ExamModel) ExamDistributionConstraint(org.cpsolver.exam.model.ExamDistributionConstraint) LazySwap(org.cpsolver.ifs.model.LazySwap) ExamRoomPlacement(org.cpsolver.exam.model.ExamRoomPlacement) ExamPlacement(org.cpsolver.exam.model.ExamPlacement) ExamPeriodPlacement(org.cpsolver.exam.model.ExamPeriodPlacement) Exam(org.cpsolver.exam.model.Exam) HashSet(java.util.HashSet)

Aggregations

ExamRoomPlacement (org.cpsolver.exam.model.ExamRoomPlacement)25 Exam (org.cpsolver.exam.model.Exam)21 ExamPlacement (org.cpsolver.exam.model.ExamPlacement)21 ExamPeriodPlacement (org.cpsolver.exam.model.ExamPeriodPlacement)11 ArrayList (java.util.ArrayList)9 ExamModel (org.cpsolver.exam.model.ExamModel)9 CSVFile (org.cpsolver.ifs.util.CSVFile)6 CSVField (org.cpsolver.ifs.util.CSVFile.CSVField)6 HashSet (java.util.HashSet)4 ExamOwner (org.cpsolver.exam.model.ExamOwner)4 ExamStudent (org.cpsolver.exam.model.ExamStudent)4 TreeSet (java.util.TreeSet)3 ExamPeriod (org.cpsolver.exam.model.ExamPeriod)3 HashMap (java.util.HashMap)2 StudentBackToBackConflicts (org.cpsolver.exam.criteria.StudentBackToBackConflicts)2 StudentDistanceBackToBackConflicts (org.cpsolver.exam.criteria.StudentDistanceBackToBackConflicts)2 ExamInstructor (org.cpsolver.exam.model.ExamInstructor)2 ExamRoomSharing (org.cpsolver.exam.model.ExamRoomSharing)2 ExamSimpleNeighbour (org.cpsolver.exam.neighbours.ExamSimpleNeighbour)2 AssignmentContext (org.cpsolver.ifs.assignment.context.AssignmentContext)2