Search in sources :

Example 86 with Lecture

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

the class StudentSwap method assign.

@Override
public void assign(Assignment<Lecture, Placement> assignment, long iteration) {
    for (Lecture l1 : iFirstLectures) {
        for (Lecture l2 : iFirstStudent.getLectures()) {
            if (l1.equals(l2))
                continue;
            if (iFirstLectures.contains(l2) && l1.getClassId().compareTo(l2.getClassId()) >= 0)
                continue;
            decJenrl(assignment, iFirstStudent, l1, l2);
        }
    }
    if (iSecondStudent != null) {
        for (Lecture l1 : iSecondLectures) {
            for (Lecture l2 : iSecondStudent.getLectures()) {
                if (l1.equals(l2))
                    continue;
                if (iSecondLectures.contains(l2) && l1.getClassId().compareTo(l2.getClassId()) >= 0)
                    continue;
                decJenrl(assignment, iSecondStudent, l1, l2);
            }
        }
    }
    for (Lecture l1 : iFirstLectures) {
        l1.removeStudent(assignment, iFirstStudent);
        iFirstStudent.removeLecture(l1);
        if (iSecondStudent != null) {
            l1.addStudent(assignment, iSecondStudent);
            iSecondStudent.addLecture(l1);
        }
    }
    for (Lecture l1 : iSecondLectures) {
        if (iSecondStudent != null) {
            l1.removeStudent(assignment, iSecondStudent);
            iSecondStudent.removeLecture(l1);
        }
        l1.addStudent(assignment, iFirstStudent);
        iFirstStudent.addLecture(l1);
    }
    if (iSecondStudent != null) {
        for (Lecture l1 : iFirstLectures) {
            for (Lecture l2 : iSecondStudent.getLectures()) {
                if (l1.equals(l2))
                    continue;
                if (iFirstLectures.contains(l2) && l1.getClassId().compareTo(l2.getClassId()) >= 0)
                    continue;
                incJenrl(assignment, iSecondStudent, l1, l2);
            }
        }
    }
    for (Lecture l1 : iSecondLectures) {
        for (Lecture l2 : iFirstStudent.getLectures()) {
            if (l1.equals(l2))
                continue;
            if (iSecondLectures.contains(l2) && l1.getClassId().compareTo(l2.getClassId()) >= 0)
                continue;
            incJenrl(assignment, iFirstStudent, l1, l2);
        }
    }
    if (!iFirstConfig.equals(iSecondConfig)) {
        iFirstStudent.removeConfiguration(iFirstConfig);
        iFirstStudent.addConfiguration(iSecondConfig);
        if (iSecondStudent != null) {
            iSecondStudent.removeConfiguration(iSecondConfig);
            iSecondStudent.addConfiguration(iFirstConfig);
        }
    }
}
Also used : Lecture(org.cpsolver.coursett.model.Lecture)

Example 87 with Lecture

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

the class StudentSwap method value.

@Override
public double value(List<StudentConflict> criteria, Assignment<Lecture, Placement> assignment) {
    double delta = 0;
    for (Lecture l1 : iFirstLectures) {
        Placement p1 = assignment.getValue(l1);
        if (p1 == null)
            continue;
        for (Lecture l2 : iFirstStudent.getLectures()) {
            Placement p2 = assignment.getValue(l2);
            if (l1.equals(l2) || p2 == null)
                continue;
            if (iFirstLectures.contains(l2) && l1.getClassId().compareTo(l2.getClassId()) >= 0)
                continue;
            delta -= getJenrConflictWeight(criteria, iFirstStudent, p1, p2);
        }
        if (iSecondStudent != null) {
            for (Lecture l2 : iSecondStudent.getLectures()) {
                if (iSecondLectures.contains(l2))
                    continue;
                Placement p2 = assignment.getValue(l2);
                if (l1.equals(l2) || p2 == null)
                    continue;
                delta += getJenrConflictWeight(criteria, iSecondStudent, p1, p2);
            }
            for (Lecture l2 : iFirstLectures) {
                Placement p2 = assignment.getValue(l2);
                if (l1.equals(l2) || p2 == null)
                    continue;
                if (l1.getClassId().compareTo(l2.getClassId()) >= 0)
                    continue;
                delta += getJenrConflictWeight(criteria, iSecondStudent, p1, p2);
            }
        }
    }
    for (Lecture l1 : iSecondLectures) {
        Placement p1 = assignment.getValue(l1);
        if (p1 == null)
            continue;
        if (iSecondStudent != null) {
            for (Lecture l2 : iSecondStudent.getLectures()) {
                Placement p2 = assignment.getValue(l2);
                if (l1.equals(l2) || p2 == null)
                    continue;
                if (iSecondLectures.contains(l2) && l1.getClassId().compareTo(l2.getClassId()) >= 0)
                    continue;
                delta -= getJenrConflictWeight(criteria, iSecondStudent, p1, p2);
            }
        }
        for (Lecture l2 : iFirstStudent.getLectures()) {
            if (iFirstLectures.contains(l2))
                continue;
            Placement p2 = assignment.getValue(l2);
            if (l1.equals(l2) || p2 == null)
                continue;
            delta += getJenrConflictWeight(criteria, iFirstStudent, p1, p2);
        }
        for (Lecture l2 : iSecondLectures) {
            Placement p2 = assignment.getValue(l2);
            if (l1.equals(l2) || p2 == null)
                continue;
            if (l1.getClassId().compareTo(l2.getClassId()) >= 0)
                continue;
            delta += getJenrConflictWeight(criteria, iFirstStudent, p1, p2);
        }
    }
    return delta;
}
Also used : Lecture(org.cpsolver.coursett.model.Lecture) Placement(org.cpsolver.coursett.model.Placement)

Example 88 with Lecture

use of org.cpsolver.coursett.model.Lecture 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 89 with Lecture

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

the class NeighbourSelectionWithSuggestions method selectNeighbourWithSuggestions.

public Neighbour<Lecture, Placement> selectNeighbourWithSuggestions(Solution<Lecture, Placement> solution, Lecture lecture, int depth) {
    if (lecture == null)
        return null;
    NeighbourSelectionWithSuggestionsContext context = new NeighbourSelectionWithSuggestionsContext(solution);
    Lock lock = solution.getLock().writeLock();
    lock.lock();
    try {
        // System.out.println("BEFORE BT ("+lecture.getName()+"): nrAssigned="+iSolution.getModel().assignedVariables().size()+",  value="+iCmp.currentValue(iSolution));
        List<Lecture> initialLectures = new ArrayList<Lecture>(1);
        initialLectures.add(lecture);
        backtrack(context, initialLectures, new HashMap<Lecture, Placement>(), new HashMap<Lecture, Placement>(), depth);
    // System.out.println("AFTER  BT ("+lecture.getName()+"): nrAssigned="+iSolution.getModel().assignedVariables().size()+",  value="+iCmp.currentValue(iSolution));
    } finally {
        lock.unlock();
    }
    return context.getSuggestionNeighbour();
}
Also used : Lecture(org.cpsolver.coursett.model.Lecture) Placement(org.cpsolver.coursett.model.Placement) ArrayList(java.util.ArrayList) Lock(java.util.concurrent.locks.Lock)

Example 90 with Lecture

use of org.cpsolver.coursett.model.Lecture 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)

Aggregations

Lecture (org.cpsolver.coursett.model.Lecture)96 Placement (org.cpsolver.coursett.model.Placement)55 HashSet (java.util.HashSet)35 TimetableModel (org.cpsolver.coursett.model.TimetableModel)17 WeakeningConstraint (org.cpsolver.ifs.model.WeakeningConstraint)17 HashMap (java.util.HashMap)16 ArrayList (java.util.ArrayList)14 TimeLocation (org.cpsolver.coursett.model.TimeLocation)14 Constraint (org.cpsolver.ifs.model.Constraint)14 InstructorConstraint (org.cpsolver.coursett.constraint.InstructorConstraint)13 Student (org.cpsolver.coursett.model.Student)13 BitSet (java.util.BitSet)11 JenrlConstraint (org.cpsolver.coursett.constraint.JenrlConstraint)10 GlobalConstraint (org.cpsolver.ifs.model.GlobalConstraint)9 RoomLocation (org.cpsolver.coursett.model.RoomLocation)8 Set (java.util.Set)7 TreeSet (java.util.TreeSet)7 GroupConstraint (org.cpsolver.coursett.constraint.GroupConstraint)7 StudentGroup (org.cpsolver.coursett.model.StudentGroup)7 RoomConstraint (org.cpsolver.coursett.constraint.RoomConstraint)6