Search in sources :

Example 6 with Enrollment

use of org.cpsolver.studentsct.model.Enrollment in project cpsolver by UniTime.

the class EnrollmentSelection method selectValue.

/** Value selection */
@Override
public Enrollment selectValue(Solution<Request, Enrollment> solution, Request selectedVariable) {
    Assignment<Request, Enrollment> assignment = solution.getAssignment();
    if (iMPP) {
        if (selectedVariable.getInitialAssignment() != null) {
            if (solution.getModel().unassignedVariables(assignment).isEmpty()) {
                if (solution.getModel().perturbVariables(assignment).size() <= iMPPLimit)
                    iMPPLimit = solution.getModel().perturbVariables(assignment).size() - 1;
            }
            if (iMPPLimit >= 0 && solution.getModel().perturbVariables(assignment).size() > iMPPLimit) {
                if (isAllowed(assignment, selectedVariable.getInitialAssignment()))
                    return selectedVariable.getInitialAssignment();
            }
            if (selectedVariable.getInitialAssignment() != null && ToolBox.random() <= iInitialSelectionProb) {
                if (isAllowed(assignment, selectedVariable.getInitialAssignment()))
                    return selectedVariable.getInitialAssignment();
            }
        }
    }
    List<Enrollment> values = selectedVariable.values(assignment);
    if (ToolBox.random() <= iRandomWalkProb) {
        Enrollment value = ToolBox.random(values);
        if (isAllowed(assignment, value))
            return value;
    }
    if (iProp != null && assignment.getValue(selectedVariable) == null && ToolBox.random() <= iGoodSelectionProb) {
        Set<Enrollment> goodValues = iProp.goodValues(assignment, selectedVariable);
        if (!goodValues.isEmpty())
            values = new ArrayList<Enrollment>(goodValues);
    }
    if (values.size() == 1) {
        Enrollment value = values.get(0);
        if (isAllowed(assignment, value))
            return value;
        else
            return null;
    }
    List<Enrollment> bestValues = null;
    double bestWeightedSum = 0;
    for (Enrollment value : values) {
        if (iTabu != null && iTabu.contains(value))
            continue;
        if (assignment.getValue(selectedVariable) != null && assignment.getValue(selectedVariable).equals(value))
            continue;
        Set<Enrollment> conf = solution.getModel().conflictValues(assignment, value);
        if (conf.contains(value))
            continue;
        if (!isAllowed(assignment, value, conf))
            continue;
        double weightedConflicts = (iStat == null || iWeightWeightedCoflicts == 0.0 ? 0.0 : iStat.countRemovals(solution.getIteration(), conf, value));
        double potentialConflicts = (iStat == null || iWeightPotentialConflicts == 0.0 ? 0.0 : iStat.countPotentialConflicts(assignment, solution.getIteration(), value, 3));
        long deltaInitialAssignments = 0;
        if (iMPP && iWeightDeltaInitialAssignment != 0.0) {
            if (iViolatedInitials != null) {
                Set<Enrollment> violations = iViolatedInitials.getViolatedInitials(value);
                if (violations != null) {
                    for (Enrollment aValue : violations) {
                        if (assignment.getValue(aValue.variable()) == null || assignment.getValue(aValue.variable()).equals(aValue))
                            deltaInitialAssignments += 2;
                    }
                }
            }
            for (Enrollment aValue : conf) {
                if (aValue.variable().getInitialAssignment() != null)
                    deltaInitialAssignments--;
            }
            if (selectedVariable.getInitialAssignment() != null && !selectedVariable.getInitialAssignment().equals(value)) {
                deltaInitialAssignments++;
            }
            if (iMPPLimit >= 0 && (solution.getModel().perturbVariables(assignment).size() + deltaInitialAssignments) > iMPPLimit)
                continue;
        }
        double weightedSum = (iWeightDeltaInitialAssignment * deltaInitialAssignments) + (iWeightPotentialConflicts * potentialConflicts) + (iWeightWeightedCoflicts * weightedConflicts) + (iWeightCoflicts * conf.size()) + (iWeightValue * value.toDouble(assignment));
        if (bestValues == null || bestWeightedSum > weightedSum) {
            bestWeightedSum = weightedSum;
            if (bestValues == null)
                bestValues = new ArrayList<Enrollment>();
            else
                bestValues.clear();
            bestValues.add(value);
        } else {
            if (bestWeightedSum == weightedSum)
                bestValues.add(value);
        }
    }
    Enrollment selectedValue = (bestValues == null ? null : ToolBox.random(bestValues));
    if (selectedValue == null)
        selectedValue = ToolBox.random(values);
    if (iTabu != null) {
        if (iTabu.size() == iTabuPos)
            iTabu.add(selectedValue);
        else
            iTabu.set(iTabuPos, selectedValue);
        iTabuPos = (iTabuPos + 1) % iTabuSize;
    }
    return (bestValues == null ? null : selectedValue);
}
Also used : Request(org.cpsolver.studentsct.model.Request) ArrayList(java.util.ArrayList) Enrollment(org.cpsolver.studentsct.model.Enrollment)

Example 7 with Enrollment

use of org.cpsolver.studentsct.model.Enrollment in project cpsolver by UniTime.

the class RandomizedBacktrackNeighbourSelection method values.

/**
     * List of values of a variable.
     * {@link CourseRequest#computeRandomEnrollments(Assignment, int)} with the provided
     * limit is used for a {@link CourseRequest}.
     */
@Override
protected Iterator<Enrollment> values(BacktrackNeighbourSelection<Request, Enrollment>.BacktrackNeighbourSelectionContext<Request, Enrollment> context, Request variable) {
    if (variable instanceof CourseRequest) {
        final CourseRequest request = (CourseRequest) variable;
        final StudentSectioningModel model = (StudentSectioningModel) context.getModel();
        final Assignment<Request, Enrollment> assignment = context.getAssignment();
        List<Enrollment> values = (iMaxValues > 0 ? request.computeRandomEnrollments(assignment, iMaxValues) : request.computeEnrollments(assignment));
        Collections.sort(values, new Comparator<Enrollment>() {

            private HashMap<Enrollment, Double> iValues = new HashMap<Enrollment, Double>();

            private Double value(Enrollment e) {
                Double value = iValues.get(e);
                if (value == null) {
                    value = model.getStudentWeights().getWeight(assignment, e, (model.getDistanceConflict() == null ? null : model.getDistanceConflict().conflicts(e)), (model.getTimeOverlaps() == null ? null : model.getTimeOverlaps().conflicts(e)));
                    iValues.put(e, value);
                }
                return value;
            }

            @Override
            public int compare(Enrollment e1, Enrollment e2) {
                if (e1.equals(assignment.getValue(request)))
                    return -1;
                if (e2.equals(assignment.getValue(request)))
                    return 1;
                Double v1 = value(e1), v2 = value(e2);
                return v1.equals(v2) ? e1.compareTo(assignment, e2) : v2.compareTo(v1);
            }
        });
        return values.iterator();
    } else {
        return variable.computeEnrollments(context.getAssignment()).iterator();
    }
}
Also used : CourseRequest(org.cpsolver.studentsct.model.CourseRequest) HashMap(java.util.HashMap) CourseRequest(org.cpsolver.studentsct.model.CourseRequest) Request(org.cpsolver.studentsct.model.Request) Enrollment(org.cpsolver.studentsct.model.Enrollment) StudentSectioningModel(org.cpsolver.studentsct.StudentSectioningModel)

Example 8 with Enrollment

use of org.cpsolver.studentsct.model.Enrollment in project cpsolver by UniTime.

the class BranchBoundSelection method selectNeighbour.

/**
     * Select neighbour. All students are taken, one by one in a random order.
     * For each student a branch &amp; bound search is employed.
     */
@Override
public Neighbour<Request, Enrollment> selectNeighbour(Solution<Request, Enrollment> solution) {
    Student student = null;
    while ((student = nextStudent()) != null) {
        Progress.getInstance(solution.getModel()).incProgress();
        Neighbour<Request, Enrollment> neighbour = getSelection(solution.getAssignment(), student).select();
        if (neighbour != null)
            return neighbour;
    }
    return null;
}
Also used : FreeTimeRequest(org.cpsolver.studentsct.model.FreeTimeRequest) Request(org.cpsolver.studentsct.model.Request) CourseRequest(org.cpsolver.studentsct.model.CourseRequest) Enrollment(org.cpsolver.studentsct.model.Enrollment) Student(org.cpsolver.studentsct.model.Student)

Example 9 with Enrollment

use of org.cpsolver.studentsct.model.Enrollment in project cpsolver by UniTime.

the class ResectionIncompleteStudentsSelection method selectNeighbour.

/**
     * Select neighbour. All students with an incomplete and non-empty schedule
     * are taken, one by one in a random order. For each student a branch &amp; 
     * bound search is employed.
     */
@Override
public Neighbour<Request, Enrollment> selectNeighbour(Solution<Request, Enrollment> solution) {
    Student student = null;
    while ((student = nextStudent()) != null) {
        Progress.getInstance(solution.getModel()).incProgress();
        if (student.nrAssignedRequests(solution.getAssignment()) == 0 || student.isComplete(solution.getAssignment()))
            continue;
        Neighbour<Request, Enrollment> neighbour = getSelection(solution.getAssignment(), student).select();
        if (neighbour != null)
            return neighbour;
    }
    return null;
}
Also used : Request(org.cpsolver.studentsct.model.Request) Enrollment(org.cpsolver.studentsct.model.Enrollment) Student(org.cpsolver.studentsct.model.Student)

Example 10 with Enrollment

use of org.cpsolver.studentsct.model.Enrollment in project cpsolver by UniTime.

the class StudentConflict method computeConflicts.

/**
     * A given enrollment is conflicting when the student is enrolled into
     * another course / free time request that has an assignment that is
     * overlapping with one or more assignments of the given section. See
     * {@link Enrollment#isOverlapping(Enrollment)} for more details. All such
     * overlapping enrollments are added into the provided set of conflicts.
     * 
     * @param enrollment
     *            {@link Enrollment} that is being considered
     * @param conflicts
     *            resultant list of conflicting enrollments
     */
@Override
public void computeConflicts(Assignment<Request, Enrollment> assignment, Enrollment enrollment, Set<Enrollment> conflicts) {
    // enrollment -> conflict
    for (Request request : variables()) {
        if (request.equals(enrollment.getRequest()))
            continue;
        Enrollment e = assignment.getValue(request);
        if (e == null)
            continue;
        if (enrollment.isOverlapping(e))
            conflicts.add(e);
    }
    // schedule) -> unassignd a lowest priority request
    if (!enrollment.getAssignments().isEmpty() && !enrollment.getStudent().canAssign(assignment, enrollment.getRequest())) {
        Enrollment lowestPriorityEnrollment = null;
        int lowestPriority = -1;
        for (Request request : variables()) {
            if (request.equals(enrollment.getRequest()))
                continue;
            Enrollment e = assignment.getValue(request);
            if (e == null)
                continue;
            if (lowestPriority < request.getPriority()) {
                lowestPriority = request.getPriority();
                lowestPriorityEnrollment = e;
            }
        }
        if (lowestPriorityEnrollment != null)
            conflicts.add(lowestPriorityEnrollment);
    }
}
Also used : Request(org.cpsolver.studentsct.model.Request) Enrollment(org.cpsolver.studentsct.model.Enrollment) Constraint(org.cpsolver.ifs.model.Constraint)

Aggregations

Enrollment (org.cpsolver.studentsct.model.Enrollment)59 Request (org.cpsolver.studentsct.model.Request)47 CourseRequest (org.cpsolver.studentsct.model.CourseRequest)36 ArrayList (java.util.ArrayList)19 Section (org.cpsolver.studentsct.model.Section)19 HashSet (java.util.HashSet)17 Student (org.cpsolver.studentsct.model.Student)16 FreeTimeRequest (org.cpsolver.studentsct.model.FreeTimeRequest)15 Course (org.cpsolver.studentsct.model.Course)14 HashMap (java.util.HashMap)11 DataProperties (org.cpsolver.ifs.util.DataProperties)9 Config (org.cpsolver.studentsct.model.Config)9 Set (java.util.Set)8 TreeSet (java.util.TreeSet)8 Subpart (org.cpsolver.studentsct.model.Subpart)8 Map (java.util.Map)7 DefaultSingleAssignment (org.cpsolver.ifs.assignment.DefaultSingleAssignment)7 DistanceConflict (org.cpsolver.studentsct.extension.DistanceConflict)7 Offering (org.cpsolver.studentsct.model.Offering)7 File (java.io.File)6