Search in sources :

Example 21 with TimetableModel

use of org.cpsolver.coursett.model.TimetableModel in project cpsolver by UniTime.

the class StudentSwapGenerator method selectNeighbour.

@Override
public Neighbour<Lecture, Placement> selectNeighbour(Solution<Lecture, Placement> solution) {
    TimetableModel model = (TimetableModel) solution.getModel();
    if (model.getAllStudents().isEmpty())
        return null;
    boolean next = false;
    while (iLecture == null || iStudents == null || !iStudents.hasNext()) {
        if (iLectures == null || !iLectures.hasNext()) {
            if (next)
                return null;
            iLectures = model.variables().iterator();
            next = true;
        }
        iLecture = iLectures.next();
        if (iLecture.getConfiguration() == null || (iLecture.getConfiguration().getAltConfigurations().isEmpty() && iLecture.isSingleSection()))
            continue;
        iStudents = iLecture.conflictStudents(solution.getAssignment()).iterator();
    }
    return generateSwap(model, solution.getAssignment(), iStudents.next(), iLecture.getConfiguration());
}
Also used : TimetableModel(org.cpsolver.coursett.model.TimetableModel)

Example 22 with TimetableModel

use of org.cpsolver.coursett.model.TimetableModel in project cpsolver by UniTime.

the class StudentSwapSectioning method objective.

/**
     * Student conflict weight of a solution 
     */
protected double objective(Solution<Lecture, Placement> solution) {
    List<StudentConflict> criteria = getStudentConflictCriteria();
    if (criteria == null) {
        double value = 0.0;
        for (JenrlConstraint constraint : ((TimetableModel) solution.getModel()).getJenrlConstraints()) {
            if (constraint.isInConflict(solution.getAssignment()))
                value += constraint.jenrl();
        }
        return value;
    }
    double value = 0.0;
    for (StudentConflict criterion : criteria) value += criterion.getWeightedValue(solution.getAssignment());
    return value;
}
Also used : JenrlConstraint(org.cpsolver.coursett.constraint.JenrlConstraint) StudentConflict(org.cpsolver.coursett.criteria.StudentConflict) TimetableModel(org.cpsolver.coursett.model.TimetableModel)

Example 23 with TimetableModel

use of org.cpsolver.coursett.model.TimetableModel in project cpsolver by UniTime.

the class TimeChange method selectNeighbour.

@Override
public Neighbour<Lecture, Placement> selectNeighbour(Solution<Lecture, Placement> solution) {
    TimetableModel model = (TimetableModel) solution.getModel();
    Assignment<Lecture, Placement> assignment = solution.getAssignment();
    int varIdx = ToolBox.random(model.variables().size());
    for (int i = 0; i < model.variables().size(); i++) {
        Lecture lecture = model.variables().get((i + varIdx) % model.variables().size());
        Placement old = assignment.getValue(lecture);
        if (old == null)
            continue;
        List<TimeLocation> values = lecture.timeLocations();
        if (values.isEmpty())
            continue;
        int valIdx = ToolBox.random(values.size());
        for (int j = 0; j < values.size(); j++) {
            TimeLocation time = values.get((j + valIdx) % values.size());
            if (time.getPreference() > 50)
                continue;
            Placement placement = null;
            if (lecture.getNrRooms() == 0)
                placement = new Placement(lecture, time, (RoomLocation) null);
            else if (lecture.getNrRooms() == 1)
                placement = new Placement(lecture, time, old.getRoomLocation());
            else
                placement = new Placement(lecture, time, old.getRoomLocations());
            if (placement.isValid() && !model.inConflict(assignment, placement)) {
                SimpleNeighbour<Lecture, Placement> n = new SimpleNeighbour<Lecture, Placement>(lecture, placement);
                if (!iHC || n.value(assignment) <= 0)
                    return n;
            }
        }
    }
    return null;
}
Also used : Lecture(org.cpsolver.coursett.model.Lecture) TimeLocation(org.cpsolver.coursett.model.TimeLocation) Placement(org.cpsolver.coursett.model.Placement) SimpleNeighbour(org.cpsolver.ifs.model.SimpleNeighbour) TimetableModel(org.cpsolver.coursett.model.TimetableModel)

Example 24 with TimetableModel

use of org.cpsolver.coursett.model.TimetableModel in project cpsolver by UniTime.

the class PlacementSelection method selectValue.

@Override
public Placement selectValue(Solution<Lecture, Placement> solution, Lecture var) {
    if (var == null)
        return null;
    Lecture selectedVariable = var;
    TimetableModel model = (TimetableModel) solution.getModel();
    Assignment<Lecture, Placement> assignment = solution.getAssignment();
    if (selectedVariable.getInitialAssignment() != null) {
        if (iMPPLimit >= 0 && model.perturbVariables(assignment).size() >= iMPPLimit) {
            if (!containsItselfSingletonOrCommited(model, model.conflictValues(assignment, selectedVariable.getInitialAssignment()), selectedVariable.getInitialAssignment()))
                return selectedVariable.getInitialAssignment();
        } else if (iMPPPenaltyLimit >= 0.0 && solution.getPerturbationsCounter() != null && solution.getPerturbationsCounter().getPerturbationPenalty(assignment, model) > iMPPPenaltyLimit) {
            if (!containsItselfSingletonOrCommited(model, model.conflictValues(assignment, selectedVariable.getInitialAssignment()), selectedVariable.getInitialAssignment()))
                return selectedVariable.getInitialAssignment();
        } else if (selectedVariable.getInitialAssignment() != null && ToolBox.random() <= iInitialSelectionProb) {
            if (!containsItselfSingletonOrCommited(model, model.conflictValues(assignment, selectedVariable.getInitialAssignment()), selectedVariable.getInitialAssignment()))
                return selectedVariable.getInitialAssignment();
        }
    }
    List<Placement> values = selectedVariable.values(solution.getAssignment());
    if (iRW && ToolBox.random() <= iRandomWalkProb) {
        for (int i = 0; i < 5; i++) {
            Placement ret = ToolBox.random(values);
            if (!containsItselfSingletonOrCommited(model, model.conflictValues(assignment, ret), ret))
                return ret;
        }
    }
    Placement current = assignment.getValue(selectedVariable);
    if (iProp != null && current == null && ToolBox.random() <= iGoodSelectionProb) {
        Collection<Placement> goodValues = iProp.goodValues(assignment, selectedVariable);
        if (!goodValues.isEmpty())
            values = new ArrayList<Placement>(goodValues);
    }
    if (values.size() == 1) {
        Placement ret = values.get(0);
        if (!containsItselfSingletonOrCommited(model, model.conflictValues(assignment, ret), ret))
            return ret;
    }
    long[] bestCost = new long[NR_LEVELS];
    List<Placement> selectionValues = null;
    HeuristicSelector<Placement> selector = (iUseThreshold ? new HeuristicSelector<Placement>(iThresholdKoef) : null);
    for (Placement value : values) {
        if (iTabu != null && iTabu.contains(value))
            continue;
        if (current != null && current.equals(value))
            continue;
        Set<Placement> conflicts = value.variable().getModel().conflictValues(assignment, value);
        if (containsItselfSingletonOrCommited(model, conflicts, value))
            continue;
        if (iUseThreshold) {
            Double flt = selector.firstLevelThreshold();
            double[] costs = new double[NR_LEVELS];
            for (int level = 0; level < NR_LEVELS; level++) {
                costs[level] = getCost(assignment, level, value, conflicts);
                if (level == 0 && flt != null && costs[0] > flt.doubleValue()) {
                    break;
                }
            }
            if (flt != null && costs[0] > flt.doubleValue())
                continue;
            selector.add(costs, value);
        } else {
            boolean fail = false;
            boolean best = false;
            for (int level = 0; !fail && level < 1; level++) {
                double val = getCost(assignment, level, value, conflicts);
                long cost = Math.round(PRECISION * val);
                if (selectionValues != null && !best) {
                    if (cost > bestCost[level]) {
                        fail = true;
                    }
                    if (cost < bestCost[level]) {
                        bestCost[level] = cost;
                        selectionValues.clear();
                        best = true;
                    }
                } else {
                    bestCost[level] = cost;
                }
            }
            if (selectionValues == null)
                selectionValues = new ArrayList<Placement>(values.size());
            if (!fail)
                selectionValues.add(value);
        }
    }
    // ToolBox.print("Best "+selectionValues.size()+" locations for variable "+selectedVariable.getId()+" have "+bestConflicts+" conflicts ("+bestRemovals+" weighted) and "+bestStudentConflicts+" ("+bestOriginalStudentConflicts+" * "+bestKoef+" + "+bestPenalty+") preference.");
    Placement selectedValue = null;
    if (iUseThreshold) {
        List<HeuristicSelector<Placement>.Element<Placement>> selectionElements = selector.selection();
        if (selectedVariable.getInitialAssignment() != null) {
            for (HeuristicSelector<Placement>.Element<Placement> element : selectionElements) {
                Placement value = element.getObject();
                if (value.equals(selectedVariable.getInitialAssignment())) {
                    selectedValue = value;
                    break;
                }
            }
        // &&
        // selectionValues.contains(selectedVariable.getInitialAssignment()))
        // return selectedVariable.getInitialAssignment();
        }
        if (selectedValue == null) {
            HeuristicSelector<Placement>.Element<Placement> selection = ToolBox.random(selectionElements);
            selectedValue = (selection == null ? null : selection.getObject());
        }
    } else {
        if (selectedVariable.getInitialAssignment() != null && selectionValues.contains(selectedVariable.getInitialAssignment()))
            return selectedVariable.getInitialAssignment();
        selectedValue = ToolBox.random(selectionValues);
    }
    if (selectedValue != null && iTabu != null) {
        if (iTabu.size() == iTabuPos)
            iTabu.add(selectedValue);
        else
            iTabu.set(iTabuPos, selectedValue);
        iTabuPos = (iTabuPos + 1) % iTabuSize;
    }
    return selectedValue;
}
Also used : Lecture(org.cpsolver.coursett.model.Lecture) ArrayList(java.util.ArrayList) Placement(org.cpsolver.coursett.model.Placement) TimetableModel(org.cpsolver.coursett.model.TimetableModel)

Example 25 with TimetableModel

use of org.cpsolver.coursett.model.TimetableModel in project cpsolver by UniTime.

the class InstructorLunchBreak method getInfo.

@Override
public void getInfo(Assignment<Lecture, Placement> assignment, Map<String, String> info) {
    Set<String> violatedLunchBreaks = new TreeSet<String>();
    int lunchViolations = 0;
    for (InstructorConstraint c : ((TimetableModel) getModel()).getInstructorConstraints()) {
        String days = "";
        CompactInfo compactInfo = ((InstructorLunchBreakContext) getContext(assignment)).getCompactInfo(c);
        for (int i = 0; i < Constants.NR_DAYS; i++) {
            if (compactInfo.getLunchDayViolations()[i] > 0) {
                if (iFullInfo)
                    days += (days.isEmpty() ? "" : ", ") + compactInfo.getLunchDayViolations()[i] + " &times; " + Constants.DAY_NAMES_SHORT[i];
                lunchViolations += compactInfo.getLunchDayViolations()[i];
            }
        }
        if (iFullInfo && !days.isEmpty())
            violatedLunchBreaks.add(c.getName() + ": " + days);
    }
    if (lunchViolations > 0) {
        info.put("Lunch breaks", getPerc(lunchViolations, 0, ((TimetableModel) getModel()).getInstructorConstraints().size() * Constants.NR_DAYS * getWeeks().size()) + "% (" + lunchViolations + ")");
        if (iFullInfo && !violatedLunchBreaks.isEmpty()) {
            String message = "";
            for (String s : violatedLunchBreaks) message += (message.isEmpty() ? "" : "<br>") + s;
            info.put("Lunch break violations", message);
        }
    }
}
Also used : TreeSet(java.util.TreeSet) TimetableModel(org.cpsolver.coursett.model.TimetableModel) InstructorConstraint(org.cpsolver.coursett.constraint.InstructorConstraint) InstructorConstraint(org.cpsolver.coursett.constraint.InstructorConstraint)

Aggregations

TimetableModel (org.cpsolver.coursett.model.TimetableModel)26 Placement (org.cpsolver.coursett.model.Placement)18 Lecture (org.cpsolver.coursett.model.Lecture)17 HashMap (java.util.HashMap)7 Constraint (org.cpsolver.ifs.model.Constraint)7 HashSet (java.util.HashSet)6 BitSet (java.util.BitSet)5 JenrlConstraint (org.cpsolver.coursett.constraint.JenrlConstraint)5 ArrayList (java.util.ArrayList)4 InstructorConstraint (org.cpsolver.coursett.constraint.InstructorConstraint)4 StudentConflict (org.cpsolver.coursett.criteria.StudentConflict)4 GlobalConstraint (org.cpsolver.ifs.model.GlobalConstraint)4 SimpleNeighbour (org.cpsolver.ifs.model.SimpleNeighbour)4 WeakeningConstraint (org.cpsolver.ifs.model.WeakeningConstraint)4 DataProperties (org.cpsolver.ifs.util.DataProperties)4 File (java.io.File)3 TreeSet (java.util.TreeSet)3 DepartmentSpreadConstraint (org.cpsolver.coursett.constraint.DepartmentSpreadConstraint)3 GroupConstraint (org.cpsolver.coursett.constraint.GroupConstraint)3 RoomConstraint (org.cpsolver.coursett.constraint.RoomConstraint)3