Search in sources :

Example 6 with ExamPeriodPlacement

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

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

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

Example 9 with ExamPeriodPlacement

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

the class ExamTimeMove 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();
    ExamRoomSharing sharing = model.getRoomSharing();
    Exam exam = ToolBox.random(model.variables());
    ExamPlacement placement = assignment.getValue(exam);
    int px = ToolBox.random(exam.getPeriodPlacements().size());
    for (int p = 0; p < exam.getPeriodPlacements().size(); p++) {
        ExamPeriodPlacement period = exam.getPeriodPlacements().get((p + px) % exam.getPeriodPlacements().size());
        if (placement != null && placement.getPeriod().equals(period))
            continue;
        if (iCheckStudentConflicts && exam.countStudentConflicts(assignment, period) > 0)
            continue;
        if (iCheckDistributionConstraints && !exam.checkDistributionConstraints(assignment, period))
            continue;
        if (placement != null) {
            boolean ok = true;
            if (sharing != null && placement.getRoomPlacements().size() == 1) {
                ExamRoomPlacement room = placement.getRoomPlacements().iterator().next();
                ok = room.isAvailable(period.getPeriod()) && !sharing.inConflict(exam, room.getRoom().getPlacements(assignment, period.getPeriod()), room.getRoom());
            } else {
                for (Iterator<ExamRoomPlacement> i = placement.getRoomPlacements().iterator(); i.hasNext(); ) {
                    ExamRoomPlacement room = i.next();
                    if (!room.isAvailable(period.getPeriod()) || !room.getRoom().getPlacements(assignment, period.getPeriod()).isEmpty()) {
                        ok = false;
                        break;
                    }
                }
            }
            if (ok)
                return new ExamSimpleNeighbour(assignment, new ExamPlacement(exam, period, placement.getRoomPlacements()));
        }
        Set<ExamRoomPlacement> rooms = exam.findBestAvailableRooms(assignment, period);
        if (rooms == null)
            continue;
        return new ExamSimpleNeighbour(assignment, new ExamPlacement(exam, period, rooms));
    }
    return null;
}
Also used : ExamRoomSharing(org.cpsolver.exam.model.ExamRoomSharing) ExamRoomPlacement(org.cpsolver.exam.model.ExamRoomPlacement) ExamPlacement(org.cpsolver.exam.model.ExamPlacement) ExamModel(org.cpsolver.exam.model.ExamModel) ExamPeriodPlacement(org.cpsolver.exam.model.ExamPeriodPlacement) Exam(org.cpsolver.exam.model.Exam)

Example 10 with ExamPeriodPlacement

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

the class ExamRandomMove method selectNeighbour.

/**
     * Select an exam randomly, select an available period randomly (from
     * {@link Exam#getPeriodPlacements()}), select rooms using
     * {@link Exam#findBestAvailableRooms(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());
    int px = ToolBox.random(exam.getPeriodPlacements().size());
    for (int p = 0; p < exam.getPeriodPlacements().size(); p++) {
        ExamPeriodPlacement period = exam.getPeriodPlacements().get((p + px) % exam.getPeriodPlacements().size());
        if (iCheckStudentConflicts && exam.countStudentConflicts(assignment, period) > 0)
            continue;
        if (iCheckDistributionConstraints && !exam.checkDistributionConstraints(assignment, period))
            continue;
        Set<ExamRoomPlacement> rooms = exam.findBestAvailableRooms(assignment, period);
        if (rooms == null)
            continue;
        return new ExamSimpleNeighbour(assignment, new ExamPlacement(exam, period, rooms));
    }
    return null;
}
Also used : ExamRoomPlacement(org.cpsolver.exam.model.ExamRoomPlacement) ExamPlacement(org.cpsolver.exam.model.ExamPlacement) ExamModel(org.cpsolver.exam.model.ExamModel) ExamPeriodPlacement(org.cpsolver.exam.model.ExamPeriodPlacement) Exam(org.cpsolver.exam.model.Exam)

Aggregations

ExamPeriodPlacement (org.cpsolver.exam.model.ExamPeriodPlacement)13 Exam (org.cpsolver.exam.model.Exam)12 ExamPlacement (org.cpsolver.exam.model.ExamPlacement)11 ExamRoomPlacement (org.cpsolver.exam.model.ExamRoomPlacement)11 ExamModel (org.cpsolver.exam.model.ExamModel)8 ArrayList (java.util.ArrayList)3 HashSet (java.util.HashSet)3 HashMap (java.util.HashMap)2 ExamSimpleNeighbour (org.cpsolver.exam.neighbours.ExamSimpleNeighbour)2 AssignmentContext (org.cpsolver.ifs.assignment.context.AssignmentContext)2 NeighbourSelectionWithContext (org.cpsolver.ifs.assignment.context.NeighbourSelectionWithContext)2 Set (java.util.Set)1 TreeSet (java.util.TreeSet)1 ExamDistributionConstraint (org.cpsolver.exam.model.ExamDistributionConstraint)1 ExamInstructor (org.cpsolver.exam.model.ExamInstructor)1 ExamRoomSharing (org.cpsolver.exam.model.ExamRoomSharing)1 ExamStudent (org.cpsolver.exam.model.ExamStudent)1 Assignment (org.cpsolver.ifs.assignment.Assignment)1 LazySwap (org.cpsolver.ifs.model.LazySwap)1 Neighbour (org.cpsolver.ifs.model.Neighbour)1