Search in sources :

Example 11 with Lecture

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

the class RoomPreferences method getBounds.

@Override
public double[] getBounds(Assignment<Lecture, Placement> assignment, Collection<Lecture> variables) {
    double[] bounds = new double[] { 0.0, 0.0 };
    for (Lecture lect : variables) {
        if (lect.isCommitted())
            continue;
        int[] p = lect.getMinMaxRoomPreference();
        bounds[1] += lect.getWeight() * (p[1] - p[0]);
    }
    return bounds;
}
Also used : Lecture(org.cpsolver.coursett.model.Lecture)

Example 12 with Lecture

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

the class FlexibleConstraintCriterion method getInfo.

@Override
public void getInfo(Assignment<Lecture, Placement> assignment, Map<String, String> info, Collection<Lecture> variables) {
    for (FlexibleConstraintType type : FlexibleConstraintType.values()) {
        Set<FlexibleConstraint> constraints = new HashSet<FlexibleConstraint>();
        for (Lecture lecture : variables) {
            for (FlexibleConstraint c : lecture.getFlexibleGroupConstraints()) {
                if (type.equals(c.getType()))
                    constraints.add(c);
            }
        }
        if (!constraints.isEmpty()) {
            int violated = 0;
            StringBuilder debug = null;
            for (FlexibleConstraint c : constraints) {
                if (c.getContext(assignment).getPreference() > 0) {
                    violated++;
                    if (iDebug) {
                        if (debug == null)
                            debug = new StringBuilder(c.getOwner());
                        else
                            debug.append("; " + c.getOwner());
                    }
                }
            }
            info.put(type.getName() + " Constraints", getPerc(violated, 0, constraints.size()) + "% (" + violated + ")");
            if (iDebug && violated > 0)
                info.put(type.getName() + " Violations", debug.toString());
        }
    }
}
Also used : FlexibleConstraint(org.cpsolver.coursett.constraint.FlexibleConstraint) Lecture(org.cpsolver.coursett.model.Lecture) FlexibleConstraintType(org.cpsolver.coursett.constraint.FlexibleConstraint.FlexibleConstraintType) FlexibleConstraint(org.cpsolver.coursett.constraint.FlexibleConstraint) HashSet(java.util.HashSet)

Example 13 with Lecture

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

the class UniversalPerturbationsCounter method getCompactInfo.

public Map<String, Double> getCompactInfo(Assignment<Lecture, Placement> assignment, TimetableModel model, boolean includeZero, boolean weighted) {
    Map<String, Double> info = new HashMap<String, Double>();
    if (!iMPP)
        return info;
    int perts = 0;
    long affectedStudents = 0;
    int affectedInstructors = 0;
    long affectedStudentsByTime = 0;
    int affectedInstructorsByTime = 0;
    long affectedStudentsByRoom = 0;
    int affectedInstructorsByRoom = 0;
    long affectedStudentsByBldg = 0;
    int affectedInstructorsByBldg = 0;
    int differentRoom = 0;
    int differentBuilding = 0;
    int differentTime = 0;
    int differentDay = 0;
    int differentHour = 0;
    int tooFarForInstructors = 0;
    int tooFarForStudents = 0;
    int deltaStudentConflicts = 0;
    int newStudentConflicts = 0;
    double deltaTimePreferences = 0;
    int deltaRoomPreferences = 0;
    int deltaInstructorDistancePreferences = 0;
    for (Lecture lecture : model.perturbVariables(assignment)) {
        if (assignment.getValue(lecture) == null || lecture.getInitialAssignment() == null || assignment.getValue(lecture).equals(lecture.getInitialAssignment()))
            continue;
        perts++;
        Placement assignedPlacement = assignment.getValue(lecture);
        Placement initialPlacement = lecture.getInitialAssignment();
        affectedStudents += lecture.classLimit(assignment);
        affectedInstructors += lecture.getInstructorConstraints().size();
        int nrDiff = initialPlacement.nrDifferentRooms(assignedPlacement);
        differentRoom += nrDiff;
        affectedInstructorsByRoom += nrDiff * lecture.getInstructorConstraints().size();
        affectedStudentsByRoom += nrDiff * lecture.classLimit(assignment);
        nrDiff = initialPlacement.nrDifferentBuildings(initialPlacement);
        differentBuilding += nrDiff;
        affectedInstructorsByBldg += nrDiff * lecture.getInstructorConstraints().size();
        affectedStudentsByBldg += nrDiff * lecture.classLimit(assignment);
        deltaRoomPreferences += assignedPlacement.sumRoomPreference() - initialPlacement.sumRoomPreference();
        if (!initialPlacement.getTimeLocation().equals(assignedPlacement.getTimeLocation())) {
            differentTime++;
            affectedInstructorsByTime += lecture.getInstructorConstraints().size();
            affectedStudentsByTime += lecture.classLimit(assignment);
        }
        if (initialPlacement.getTimeLocation().getDayCode() != assignedPlacement.getTimeLocation().getDayCode())
            differentDay++;
        if (initialPlacement.getTimeLocation().getStartSlot() != assignedPlacement.getTimeLocation().getStartSlot())
            differentHour++;
        if (!initialPlacement.getTimeLocation().equals(assignedPlacement.getTimeLocation())) {
            double distance = Placement.getDistanceInMeters(iDistanceMetric, initialPlacement, assignedPlacement);
            if (!lecture.getInstructorConstraints().isEmpty()) {
                if (distance > iDistanceMetric.getInstructorNoPreferenceLimit() && distance <= iDistanceMetric.getInstructorDiscouragedLimit()) {
                    tooFarForInstructors += Constants.sPreferenceLevelDiscouraged;
                } else if (distance > iDistanceMetric.getInstructorDiscouragedLimit() && distance <= iDistanceMetric.getInstructorProhibitedLimit()) {
                    tooFarForInstructors += Constants.sPreferenceLevelStronglyDiscouraged;
                } else if (distance > iDistanceMetric.getInstructorProhibitedLimit()) {
                    tooFarForInstructors += Constants.sPreferenceLevelProhibited;
                }
            }
            if (distance > iDistanceMetric.minutes2meters(10))
                tooFarForStudents += lecture.classLimit(assignment);
        }
        deltaStudentConflicts += lecture.countStudentConflicts(assignment, assignedPlacement) - lecture.countInitialStudentConflicts();
        Set<Student> newStudentConflictsSet = lecture.conflictStudents(assignment, assignedPlacement);
        Set<Student> initialStudentConflicts = lecture.initialStudentConflicts();
        for (Iterator<Student> e1 = newStudentConflictsSet.iterator(); e1.hasNext(); ) if (!initialStudentConflicts.contains(e1.next()))
            newStudentConflicts++;
        deltaTimePreferences += assignedPlacement.getTimeLocation().getNormalizedPreference() - initialPlacement.getTimeLocation().getNormalizedPreference();
        for (InstructorConstraint ic : lecture.getInstructorConstraints()) {
            if (ic != null)
                for (Lecture lect : ic.variables()) {
                    if (lect.equals(lecture))
                        continue;
                    int initialPreference = (lect.getInitialAssignment() == null ? Constants.sPreferenceLevelNeutral : ic.getDistancePreference(initialPlacement, lect.getInitialAssignment()));
                    int assignedPreference = (assignedPlacement == null ? Constants.sPreferenceLevelNeutral : ic.getDistancePreference(assignedPlacement, assignedPlacement));
                    deltaInstructorDistancePreferences += assignedPreference - initialPreference;
                }
        }
    }
    if (includeZero || iDifferentPlacement != 0.0)
        info.put("Different placement", new Double(weighted ? iDifferentPlacement * perts : perts));
    if (includeZero || iAffectedStudentWeight != 0.0)
        info.put("Affected students", new Double(weighted ? iAffectedStudentWeight * affectedStudents : affectedStudents));
    if (includeZero || iAffectedInstructorWeight != 0.0)
        info.put("Affected instructors", new Double(weighted ? iAffectedInstructorWeight * affectedInstructors : affectedInstructors));
    if (includeZero || iAffectedStudentByTimeWeight != 0.0)
        info.put("Affected students [time]", new Double(weighted ? iAffectedStudentByTimeWeight * affectedStudentsByTime : affectedStudentsByTime));
    if (includeZero || iAffectedInstructorByTimeWeight != 0.0)
        info.put("Affected instructors [time]", new Double(weighted ? iAffectedInstructorByTimeWeight * affectedInstructorsByTime : affectedInstructorsByTime));
    if (includeZero || iAffectedStudentByRoomWeight != 0.0)
        info.put("Affected students [room]", new Double(weighted ? iAffectedStudentByRoomWeight * affectedStudentsByRoom : affectedStudentsByRoom));
    if (includeZero || iAffectedInstructorByRoomWeight != 0.0)
        info.put("Affected instructors [room]", new Double(weighted ? iAffectedInstructorByRoomWeight * affectedInstructorsByRoom : affectedInstructorsByRoom));
    if (includeZero || iAffectedStudentByBldgWeight != 0.0)
        info.put("Affected students [bldg]", new Double(weighted ? iAffectedStudentByBldgWeight * affectedStudentsByBldg : affectedStudentsByBldg));
    if (includeZero || iAffectedInstructorByBldgWeight != 0.0)
        info.put("Affected instructors [bldg]", new Double(weighted ? iAffectedInstructorByBldgWeight * affectedInstructorsByBldg : affectedInstructorsByBldg));
    if (includeZero || iDifferentRoomWeight != 0.0)
        info.put("Different room", new Double(weighted ? iDifferentRoomWeight * differentRoom : differentRoom));
    if (includeZero || iDifferentBuildingWeight != 0.0)
        info.put("Different building", new Double(weighted ? iDifferentBuildingWeight * differentBuilding : differentBuilding));
    if (includeZero || iDifferentTimeWeight != 0.0)
        info.put("Different time", new Double(weighted ? iDifferentTimeWeight * differentTime : differentTime));
    if (includeZero || iDifferentDayWeight != 0.0)
        info.put("Different day", new Double(weighted ? iDifferentDayWeight * differentDay : differentDay));
    if (includeZero || iDifferentHourWeight != 0.0)
        info.put("Different hour", new Double(weighted ? iDifferentHourWeight * differentHour : differentHour));
    if (includeZero || iTooFarForInstructorsWeight != 0.0)
        info.put("New placement too far for initial [instructors]", new Double(weighted ? iTooFarForInstructorsWeight * tooFarForInstructors : tooFarForInstructors));
    if (includeZero || iTooFarForStudentsWeight != 0.0)
        info.put("New placement too far for initial [students]", new Double(weighted ? iTooFarForStudentsWeight * tooFarForStudents : tooFarForStudents));
    if (includeZero || iDeltaStudentConflictsWeight != 0.0)
        info.put("Delta student conflicts", new Double(weighted ? iDeltaStudentConflictsWeight * deltaStudentConflicts : deltaStudentConflicts));
    if (includeZero || iNewStudentConflictsWeight != 0.0)
        info.put("New student conflicts", new Double(weighted ? iNewStudentConflictsWeight * newStudentConflicts : newStudentConflicts));
    if (includeZero || iDeltaTimePreferenceWeight != 0.0)
        info.put("Delta time preferences", new Double(weighted ? iDeltaTimePreferenceWeight * deltaTimePreferences : deltaTimePreferences));
    if (includeZero || iDeltaRoomPreferenceWeight != 0.0)
        info.put("Delta room preferences", new Double(weighted ? iDeltaRoomPreferenceWeight * deltaRoomPreferences : deltaRoomPreferences));
    if (includeZero || iDeltaInstructorDistancePreferenceWeight != 0.0)
        info.put("Delta instructor distance preferences", new Double(weighted ? iDeltaInstructorDistancePreferenceWeight * deltaInstructorDistancePreferences : deltaInstructorDistancePreferences));
    return info;
}
Also used : Lecture(org.cpsolver.coursett.model.Lecture) HashMap(java.util.HashMap) Student(org.cpsolver.coursett.model.Student) InstructorConstraint(org.cpsolver.coursett.constraint.InstructorConstraint) InstructorConstraint(org.cpsolver.coursett.constraint.InstructorConstraint) Placement(org.cpsolver.coursett.model.Placement)

Example 14 with Lecture

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

the class UniversalPerturbationsCounter method getInfo.

public void getInfo(Assignment<Lecture, Placement> assignment, TimetableModel model, Map<String, String> info, List<Lecture> variables) {
    if (variables == null)
        super.getInfo(assignment, model, info);
    else
        super.getInfo(assignment, model, info, variables);
    if (!iMPP)
        return;
    int perts = 0;
    long affectedStudents = 0;
    int affectedInstructors = 0;
    long affectedStudentsByTime = 0;
    int affectedInstructorsByTime = 0;
    long affectedStudentsByRoom = 0;
    int affectedInstructorsByRoom = 0;
    long affectedStudentsByBldg = 0;
    int affectedInstructorsByBldg = 0;
    int differentRoom = 0;
    int differentBuilding = 0;
    int differentTime = 0;
    int differentDay = 0;
    int differentHour = 0;
    int tooFarForInstructors = 0;
    int tooFarForStudents = 0;
    int deltaStudentConflicts = 0;
    int newStudentConflicts = 0;
    double deltaTimePreferences = 0;
    int deltaRoomPreferences = 0;
    int deltaInstructorDistancePreferences = 0;
    for (Lecture lecture : (variables == null ? model.perturbVariables(assignment) : model.perturbVariables(assignment, variables))) {
        if (assignment.getValue(lecture) == null || lecture.getInitialAssignment() == null || assignment.getValue(lecture).equals(lecture.getInitialAssignment()))
            continue;
        perts++;
        Placement assignedPlacement = assignment.getValue(lecture);
        Placement initialPlacement = lecture.getInitialAssignment();
        affectedStudents += lecture.classLimit(assignment);
        affectedInstructors += lecture.getInstructorConstraints().size();
        int nrDiff = initialPlacement.nrDifferentRooms(assignedPlacement);
        differentRoom += nrDiff;
        affectedInstructorsByRoom += nrDiff * lecture.getInstructorConstraints().size();
        affectedStudentsByRoom += nrDiff * lecture.classLimit(assignment);
        nrDiff = initialPlacement.nrDifferentBuildings(assignedPlacement);
        differentBuilding += nrDiff;
        affectedInstructorsByBldg += nrDiff * lecture.getInstructorConstraints().size();
        affectedStudentsByBldg += nrDiff * lecture.classLimit(assignment);
        deltaRoomPreferences += assignedPlacement.sumRoomPreference() - initialPlacement.sumRoomPreference();
        if (!initialPlacement.getTimeLocation().equals(assignedPlacement.getTimeLocation())) {
            differentTime++;
            affectedInstructorsByTime += lecture.getInstructorConstraints().size();
            affectedStudentsByTime += lecture.classLimit(assignment);
        }
        if (initialPlacement.getTimeLocation().getDayCode() != assignedPlacement.getTimeLocation().getDayCode())
            differentDay++;
        if (initialPlacement.getTimeLocation().getStartSlot() != assignedPlacement.getTimeLocation().getStartSlot())
            differentHour++;
        if (!initialPlacement.getTimeLocation().equals(assignedPlacement.getTimeLocation())) {
            double distance = Placement.getDistanceInMeters(iDistanceMetric, initialPlacement, assignedPlacement);
            if (!lecture.getInstructorConstraints().isEmpty()) {
                if (distance > iDistanceMetric.getInstructorNoPreferenceLimit() && distance <= iDistanceMetric.getInstructorDiscouragedLimit()) {
                    tooFarForInstructors += Constants.sPreferenceLevelDiscouraged * lecture.getInstructorConstraints().size();
                } else if (distance > iDistanceMetric.getInstructorDiscouragedLimit() && distance <= iDistanceMetric.getInstructorProhibitedLimit()) {
                    tooFarForInstructors += Constants.sPreferenceLevelStronglyDiscouraged * lecture.getInstructorConstraints().size();
                } else if (distance > iDistanceMetric.getInstructorProhibitedLimit()) {
                    tooFarForInstructors += Constants.sPreferenceLevelProhibited * lecture.getInstructorConstraints().size();
                }
            }
            if (distance > iDistanceMetric.minutes2meters(10))
                tooFarForStudents += lecture.classLimit(assignment);
        }
        deltaStudentConflicts += lecture.countStudentConflicts(assignment, assignedPlacement) - lecture.countInitialStudentConflicts();
        Set<Student> newStudentConflictsSet = lecture.conflictStudents(assignment, assignedPlacement);
        Set<Student> initialStudentConflicts = lecture.initialStudentConflicts();
        for (Iterator<Student> e1 = newStudentConflictsSet.iterator(); e1.hasNext(); ) if (!initialStudentConflicts.contains(e1.next()))
            newStudentConflicts++;
        deltaTimePreferences += assignedPlacement.getTimeLocation().getNormalizedPreference() - initialPlacement.getTimeLocation().getNormalizedPreference();
        for (InstructorConstraint ic : lecture.getInstructorConstraints()) {
            for (Lecture lect : ic.variables()) {
                if (lect.equals(lecture))
                    continue;
                int initialPreference = (lect.getInitialAssignment() == null ? Constants.sPreferenceLevelNeutral : ic.getDistancePreference(initialPlacement, lect.getInitialAssignment()));
                int assignedPreference = (assignedPlacement == null ? Constants.sPreferenceLevelNeutral : ic.getDistancePreference(assignedPlacement, assignedPlacement));
                deltaInstructorDistancePreferences += assignedPreference - initialPreference;
            }
        }
    }
    if (perts != 0)
        info.put("Perturbations: Different placement", String.valueOf(perts) + " (weighted " + sDoubleFormat.format(iDifferentPlacement * perts) + ")");
    if (affectedStudents != 0)
        info.put("Perturbations: Number of affected students", String.valueOf(affectedStudents) + " (weighted " + sDoubleFormat.format(iAffectedStudentWeight * affectedStudents) + ")");
    if (affectedInstructors != 0)
        info.put("Perturbations: Number of affected instructors", String.valueOf(affectedInstructors) + " (weighted " + sDoubleFormat.format(iAffectedInstructorWeight * affectedInstructors) + ")");
    if (affectedStudentsByTime != 0)
        info.put("Perturbations: Number of affected students [time]", String.valueOf(affectedStudentsByTime) + " (weighted " + sDoubleFormat.format(iAffectedStudentByTimeWeight * affectedStudentsByTime) + ")");
    if (affectedInstructorsByTime != 0)
        info.put("Perturbations: Number of affected instructors [time]", String.valueOf(affectedInstructorsByTime) + " (weighted " + sDoubleFormat.format(iAffectedInstructorByTimeWeight * affectedInstructorsByTime) + ")");
    if (affectedStudentsByRoom != 0)
        info.put("Perturbations: Number of affected students [room]", String.valueOf(affectedStudentsByRoom) + " (weighted " + sDoubleFormat.format(iAffectedStudentByRoomWeight * affectedStudentsByRoom) + ")");
    if (affectedInstructorsByRoom != 0)
        info.put("Perturbations: Number of affected instructors [room]", String.valueOf(affectedInstructorsByRoom) + " (weighted " + sDoubleFormat.format(iAffectedInstructorByRoomWeight * affectedInstructorsByRoom) + ")");
    if (affectedStudentsByBldg != 0)
        info.put("Perturbations: Number of affected students [bldg]", String.valueOf(affectedStudentsByBldg) + " (weighted " + sDoubleFormat.format(iAffectedStudentByBldgWeight * affectedStudentsByBldg) + ")");
    if (affectedInstructorsByBldg != 0)
        info.put("Perturbations: Number of affected instructors [bldg]", String.valueOf(affectedInstructorsByBldg) + " (weighted " + sDoubleFormat.format(iAffectedInstructorByBldgWeight * affectedInstructorsByBldg) + ")");
    if (differentRoom != 0)
        info.put("Perturbations: Different room", String.valueOf(differentRoom) + " (weighted " + sDoubleFormat.format(iDifferentRoomWeight * differentRoom) + ")");
    if (differentBuilding != 0)
        info.put("Perturbations: Different building", String.valueOf(differentBuilding) + " (weighted " + sDoubleFormat.format(iDifferentBuildingWeight * differentBuilding) + ")");
    if (differentTime != 0)
        info.put("Perturbations: Different time", String.valueOf(differentTime) + " (weighted " + sDoubleFormat.format(iDifferentTimeWeight * differentTime) + ")");
    if (differentDay != 0)
        info.put("Perturbations: Different day", String.valueOf(differentDay) + " (weighted " + sDoubleFormat.format(iDifferentDayWeight * differentDay) + ")");
    if (differentHour != 0)
        info.put("Perturbations: Different hour", String.valueOf(differentHour) + " (weighted " + sDoubleFormat.format(iDifferentHourWeight * differentHour) + ")");
    if (tooFarForInstructors != 0)
        info.put("Perturbations: New placement too far from initial [instructors]", String.valueOf(tooFarForInstructors) + " (weighted " + sDoubleFormat.format(iTooFarForInstructorsWeight * tooFarForInstructors) + ")");
    if (tooFarForStudents != 0)
        info.put("Perturbations: New placement too far from initial [students]", String.valueOf(tooFarForStudents) + " (weighted " + sDoubleFormat.format(iTooFarForStudentsWeight * tooFarForStudents) + ")");
    if (deltaStudentConflicts != 0)
        info.put("Perturbations: Delta student conflicts", String.valueOf(deltaStudentConflicts) + " (weighted " + sDoubleFormat.format(iDeltaStudentConflictsWeight * deltaStudentConflicts) + ")");
    if (newStudentConflicts != 0)
        info.put("Perturbations: New student conflicts", String.valueOf(newStudentConflicts) + " (weighted " + sDoubleFormat.format(iNewStudentConflictsWeight * newStudentConflicts) + ")");
    if (deltaTimePreferences != 0)
        info.put("Perturbations: Delta time preferences", String.valueOf(deltaTimePreferences) + " (weighted " + sDoubleFormat.format(iDeltaTimePreferenceWeight * deltaTimePreferences) + ")");
    if (deltaRoomPreferences != 0)
        info.put("Perturbations: Delta room preferences", String.valueOf(deltaRoomPreferences) + " (weighted " + sDoubleFormat.format(iDeltaRoomPreferenceWeight * deltaRoomPreferences) + ")");
    if (deltaInstructorDistancePreferences != 0)
        info.put("Perturbations: Delta instructor distance preferences", String.valueOf(deltaInstructorDistancePreferences) + " (weighted " + sDoubleFormat.format(iDeltaInstructorDistancePreferenceWeight * deltaInstructorDistancePreferences) + ")");
}
Also used : Lecture(org.cpsolver.coursett.model.Lecture) Placement(org.cpsolver.coursett.model.Placement) Student(org.cpsolver.coursett.model.Student) InstructorConstraint(org.cpsolver.coursett.constraint.InstructorConstraint) InstructorConstraint(org.cpsolver.coursett.constraint.InstructorConstraint)

Example 15 with Lecture

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

the class LectureSelection method selectVariable.

@Override
public Lecture selectVariable(Solution<Lecture, Placement> solution) {
    TimetableModel model = (TimetableModel) solution.getModel();
    Assignment<Lecture, Placement> assignment = solution.getAssignment();
    Collection<Lecture> unassignedVariables = model.unassignedVariables(assignment);
    if (iInteractiveMode) {
        // remove variables that have no values
        unassignedVariables = new ArrayList<Lecture>(unassignedVariables.size());
        for (Lecture variable : model.unassignedVariables(assignment)) {
            if (!variable.values(solution.getAssignment()).isEmpty())
                unassignedVariables.add(variable);
        }
    }
    if (unassignedVariables.isEmpty()) {
        Collection<Lecture> variables = model.perturbVariables(assignment);
        if (variables.isEmpty())
            variables = model.assignedVariables(assignment);
        if (iRW && ToolBox.random() <= iRandomWalkProb)
            return ToolBox.random(variables);
        List<Lecture> selectionVariables = new ArrayList<Lecture>();
        double worstValue = 0.0;
        for (Iterator<Lecture> i1 = (iSubSetSelection ? ToolBox.subSet(variables, iSelectionSubSetPart, iSelectionSubSetMinSize) : variables).iterator(); i1.hasNext(); ) {
            Lecture selectedVariable = i1.next();
            if (iTabu != null && iTabu.contains(selectedVariable))
                continue;
            double value = assignment.getValue(selectedVariable).toDouble(assignment);
            if (selectionVariables.isEmpty() || value > worstValue) {
                selectionVariables.clear();
                selectionVariables.add(selectedVariable);
                worstValue = value;
            } else if (worstValue == value) {
                selectionVariables.add(selectedVariable);
            }
        }
        Lecture selectedVariable = ToolBox.random(selectionVariables);
        if (selectedVariable == null)
            selectedVariable = ToolBox.random(variables);
        if (selectedVariable != null && iTabu != null) {
            if (iTabu.size() == iTabuPos)
                iTabu.add(selectedVariable);
            else
                iTabu.set(iTabuPos, selectedVariable);
            iTabuPos = (iTabuPos + 1) % iTabuSize;
        }
        return selectedVariable;
    } else {
        if (ToolBox.random() <= iRandomWalkProb)
            return ToolBox.random(unassignedVariables);
        if (iProp != null && iUnassignWhenNotGood) {
            List<Lecture> noGoodVariables = new ArrayList<Lecture>();
            for (Iterator<Lecture> i1 = ToolBox.subSet(unassignedVariables, iSelectionSubSetPart, iSelectionSubSetMinSize).iterator(); i1.hasNext(); ) {
                Lecture variable = i1.next();
                if (iProp.goodValues(assignment, variable).isEmpty())
                    noGoodVariables.add(variable);
            }
            if (!noGoodVariables.isEmpty()) {
                if (ToolBox.random() < 0.02)
                    return ToolBox.random(assignment.assignedVariables());
                for (int attempt = 0; attempt < 10; attempt++) {
                    Lecture noGoodVariable = ToolBox.random(noGoodVariables);
                    Placement noGoodValue = ToolBox.random(noGoodVariable.values(solution.getAssignment()));
                    if (!iProp.noGood(assignment, noGoodValue).isEmpty())
                        return ToolBox.random(iProp.noGood(assignment, noGoodValue)).variable();
                }
            }
        }
        if (iRouletteWheelSelection) {
            int iMaxDomainSize = 0;
            int iMaxGoodDomainSize = 0;
            int iMaxConstraints = 0;
            Collection<Lecture> variables = (iSubSetSelection ? ToolBox.subSet(unassignedVariables, iSelectionSubSetPart, iSelectionSubSetMinSize) : unassignedVariables);
            for (Lecture variable : variables) {
                if (iTabu != null && iTabu.contains(variable))
                    continue;
                iMaxDomainSize = Math.max(iMaxDomainSize, variable.values(solution.getAssignment()).size());
                iMaxGoodDomainSize = (iProp == null ? 0 : Math.max(iMaxGoodDomainSize, iProp.goodValues(assignment, variable).size()));
                iMaxConstraints = Math.max(iMaxConstraints, variable.constraints().size());
            }
            List<Integer> points = new ArrayList<Integer>();
            int totalPoints = 0;
            for (Lecture variable : variables) {
                long pointsThisVariable = Math.round(iDomainSizeWeight * (((double) (iMaxDomainSize - variable.values(solution.getAssignment()).size())) / ((double) iMaxDomainSize)) + (iProp == null ? 0.0 : iGoodValuesWeight * (((double) (iMaxGoodDomainSize - iProp.goodValues(assignment, variable).size())) / ((double) iMaxGoodDomainSize))) + iConstraintsWeight * (((double) (iMaxConstraints - variable.constraints().size())) / ((double) iMaxConstraints)) + iInitialAssignmentWeight * (variable.getInitialAssignment() != null ? model.conflictValues(assignment, variable.getInitialAssignment()).size() : 0.0));
                if (pointsThisVariable > 0) {
                    totalPoints += pointsThisVariable;
                    points.add(totalPoints);
                }
            }
            if (totalPoints > 0) {
                int rndPoints = ToolBox.random(totalPoints);
                Iterator<Lecture> x = variables.iterator();
                for (int i = 0; x.hasNext() && i < points.size(); i++) {
                    Lecture variable = x.next();
                    int tp = points.get(i);
                    if (tp > rndPoints) {
                        if (variable != null && iTabu != null) {
                            if (iTabu.size() == iTabuPos)
                                iTabu.add(variable);
                            else
                                iTabu.set(iTabuPos, variable);
                            iTabuPos = (iTabuPos + 1) % iTabuSize;
                        }
                        return variable;
                    }
                }
            }
        } else {
            List<Lecture> selectionVariables = null;
            long bestGood = 0;
            for (Iterator<Lecture> i = ToolBox.subSet(unassignedVariables, iSelectionSubSetPart, iSelectionSubSetMinSize).iterator(); i.hasNext(); ) {
                Lecture variable = i.next();
                if (iTabu != null && iTabu.contains(variable))
                    continue;
                long good = (long) (iDomainSizeWeight * variable.values(solution.getAssignment()).size() + iGoodValuesWeight * (iProp == null ? 0 : iProp.goodValues(assignment, variable).size()) + iConstraintsWeight * variable.constraints().size() + iInitialAssignmentWeight * (variable.getInitialAssignment() != null ? model.conflictValues(assignment, variable.getInitialAssignment()).size() : 0.0));
                if (selectionVariables == null || bestGood > good) {
                    if (selectionVariables == null)
                        selectionVariables = new ArrayList<Lecture>();
                    else
                        selectionVariables.clear();
                    bestGood = good;
                    selectionVariables.add(variable);
                } else if (good == bestGood) {
                    selectionVariables.add(variable);
                }
            }
            if (!selectionVariables.isEmpty()) {
                Lecture selectedVariable = ToolBox.random(selectionVariables);
                if (selectedVariable != null && iTabu != null) {
                    if (iTabu.size() == iTabuPos)
                        iTabu.add(selectedVariable);
                    else
                        iTabu.set(iTabuPos, selectedVariable);
                    iTabuPos = (iTabuPos + 1) % iTabuSize;
                }
                return selectedVariable;
            }
        }
        Lecture selectedVariable = ToolBox.random(unassignedVariables);
        if (selectedVariable != null && iTabu != null) {
            if (iTabu.size() == iTabuPos)
                iTabu.add(selectedVariable);
            else
                iTabu.set(iTabuPos, selectedVariable);
            iTabuPos = (iTabuPos + 1) % iTabuSize;
        }
        return selectedVariable;
    }
}
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