Search in sources :

Example 16 with CourseRequest

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

the class DistanceConflict method countTotalNrConflicts.

/**
 * Compute the actual number of all distance conflicts. Should be equal to
 * {@link DistanceConflict#getTotalNrConflicts(Assignment)}.
 * @param assignment current assignment
 * @return computed number of all distance conflicts
 */
public int countTotalNrConflicts(Assignment<Request, Enrollment> assignment) {
    int total = 0;
    for (Request r1 : getModel().variables()) {
        if (assignment.getValue(r1) == null || !(r1 instanceof CourseRequest))
            continue;
        Enrollment e1 = assignment.getValue(r1);
        total += nrConflicts(e1);
        for (Request r2 : r1.getStudent().getRequests()) {
            Enrollment e2 = assignment.getValue(r2);
            if (e2 == null || r1.getId() >= r2.getId() || !(r2 instanceof CourseRequest))
                continue;
            total += nrConflicts(e1, e2);
        }
    }
    return total;
}
Also used : CourseRequest(org.cpsolver.studentsct.model.CourseRequest) Request(org.cpsolver.studentsct.model.Request) CourseRequest(org.cpsolver.studentsct.model.CourseRequest) Enrollment(org.cpsolver.studentsct.model.Enrollment)

Example 17 with CourseRequest

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

the class DistanceConflict method computeAllConflicts.

/**
 * Compute a set of all distance conflicts ({@link Conflict} objects).
 * @param assignment current assignment
 * @return computed set of all distance conflicts
 */
public Set<Conflict> computeAllConflicts(Assignment<Request, Enrollment> assignment) {
    Set<Conflict> ret = new HashSet<Conflict>();
    for (Request r1 : getModel().variables()) {
        Enrollment e1 = assignment.getValue(r1);
        if (e1 == null || !(r1 instanceof CourseRequest))
            continue;
        ret.addAll(conflicts(e1));
        for (Request r2 : r1.getStudent().getRequests()) {
            Enrollment e2 = assignment.getValue(r2);
            if (e2 == null || r1.getId() >= r2.getId() || !(r2 instanceof CourseRequest))
                continue;
            ret.addAll(conflicts(e1, e2));
        }
    }
    return ret;
}
Also used : CourseRequest(org.cpsolver.studentsct.model.CourseRequest) Request(org.cpsolver.studentsct.model.Request) CourseRequest(org.cpsolver.studentsct.model.CourseRequest) Enrollment(org.cpsolver.studentsct.model.Enrollment) HashSet(java.util.HashSet)

Example 18 with CourseRequest

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

the class OnlineSelection method setPenalties.

/**
 * Set online sectioning penalties to all sections of all courses of the
 * given student
 */
private static void setPenalties(Assignment<Request, Enrollment> assignment, Student student) {
    for (Request request : student.getRequests()) {
        if (!(request instanceof CourseRequest))
            continue;
        CourseRequest courseRequest = (CourseRequest) request;
        for (Course course : courseRequest.getCourses()) {
            for (Config config : course.getOffering().getConfigs()) {
                for (Subpart subpart : config.getSubparts()) {
                    for (Section section : subpart.getSections()) {
                        section.setPenalty(section.getOnlineSectioningPenalty(assignment));
                    }
                }
            }
        }
        courseRequest.clearCache();
    }
}
Also used : CourseRequest(org.cpsolver.studentsct.model.CourseRequest) Config(org.cpsolver.studentsct.model.Config) Subpart(org.cpsolver.studentsct.model.Subpart) CourseRequest(org.cpsolver.studentsct.model.CourseRequest) Request(org.cpsolver.studentsct.model.Request) Course(org.cpsolver.studentsct.model.Course) Section(org.cpsolver.studentsct.model.Section)

Example 19 with CourseRequest

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

the class ShuffleStudentsSelection method init.

@Override
public void init(Solver<Request, Enrollment> solver) {
    StudentSectioningModel model = (StudentSectioningModel) solver.currentSolution().getModel();
    iQueue = new LinkedList<Shuffle>();
    Assignment<Request, Enrollment> assignment = solver.currentSolution().getAssignment();
    // Check all request groups that have a spread < 1.0
    RouletteWheelSelection<RequestGroup> groups = new RouletteWheelSelection<RequestGroup>();
    for (Offering offering : model.getOfferings()) {
        for (Course course : offering.getCourses()) {
            for (RequestGroup group : course.getRequestGroups()) {
                double spread = group.getAverageSpread(solver.currentSolution().getAssignment());
                if (spread >= 1.0)
                    continue;
                groups.add(group, 1.0 - spread);
            }
        }
    }
    // If there are some, pick one randomly (using roulette wheel selection)
    if (groups.hasMoreElements()) {
        RequestGroup group = groups.nextElement();
        RouletteWheelSelection<Subpart> subparts = new RouletteWheelSelection<Subpart>();
        for (CourseRequest cr : group.getRequests()) {
            Enrollment e = assignment.getValue(cr);
            if (e != null)
                for (Section section : e.getSections()) if (group.getSectionSpread(assignment, section) < 1.0)
                    subparts.addExisting(section.getSubpart(), 1.0);
        }
        if (subparts.hasMoreElements()) {
            // Pick a subpart that has sections with a section spread < 1.0
            Subpart subpart = subparts.nextElement();
            RouletteWheelSelection<Section> sections = new RouletteWheelSelection<Section>();
            section: for (Section section : subpart.getSections()) {
                // Only take sections that all requests can use
                for (CourseRequest cr : group.getRequests()) {
                    boolean match = false;
                    for (Enrollment e : cr.values(assignment)) if (e.getSections().contains(section)) {
                        match = true;
                        break;
                    }
                    if (!match)
                        continue section;
                }
                // Take sections with conflicts with lower probability
                int nrConflicts = 0;
                if (!section.isAllowOverlap())
                    requests: for (CourseRequest cr : group.getRequests()) {
                        for (Request r : cr.getStudent().getRequests()) {
                            if (r.equals(cr))
                                continue;
                            Enrollment e = assignment.getValue(r);
                            if (e != null && !e.isAllowOverlap() && section.isOverlapping(e.getSections())) {
                                nrConflicts++;
                                continue requests;
                            }
                        }
                    }
                sections.add(section, 1 + group.getRequests().size() - nrConflicts);
            }
            Set<Section> filter = new HashSet<Section>();
            double space = 0.0;
            // Pick enough sections
            while (sections.hasMoreElements()) {
                Section section = sections.nextElement();
                if (filter.add(section)) {
                    if (section.getLimit() < 0)
                        break;
                    space += section.getLimit();
                }
                if (space >= group.getTotalWeight())
                    break;
            }
            // Add all requests that should be moved into the queue
            for (CourseRequest cr : group.getRequests()) {
                Shuffle shuffle = new Shuffle(group, cr, filter);
                Enrollment e = assignment.getValue(cr);
                if (e != null && shuffle.matchFilter(e))
                    continue;
                iQueue.add(shuffle);
            }
        } else {
            // No subpart -> no section filter
            for (CourseRequest cr : group.getRequests()) iQueue.add(new Shuffle(group, cr, null));
        }
    }
    // Shuffle the queue
    Collections.shuffle((LinkedList<Shuffle>) iQueue);
    // Initialize the backtrack selection, if needed
    if (iBacktrack == null) {
        try {
            iBacktrack = new ShuffleBacktrackNeighbourSelection(solver.getProperties());
            iBacktrack.init(solver);
        } catch (Exception e) {
            throw new RuntimeException(e.getMessage(), e);
        }
    }
    // Change progress
    Progress.getInstance(solver.currentSolution().getModel()).setPhase("Shuffling students along request groups...", iQueue.size());
}
Also used : RouletteWheelSelection(org.cpsolver.ifs.heuristics.RouletteWheelSelection) Request(org.cpsolver.studentsct.model.Request) CourseRequest(org.cpsolver.studentsct.model.CourseRequest) Offering(org.cpsolver.studentsct.model.Offering) Section(org.cpsolver.studentsct.model.Section) CourseRequest(org.cpsolver.studentsct.model.CourseRequest) RequestGroup(org.cpsolver.studentsct.model.RequestGroup) Subpart(org.cpsolver.studentsct.model.Subpart) Enrollment(org.cpsolver.studentsct.model.Enrollment) Course(org.cpsolver.studentsct.model.Course) StudentSectioningModel(org.cpsolver.studentsct.StudentSectioningModel) HashSet(java.util.HashSet)

Example 20 with CourseRequest

use of org.cpsolver.studentsct.model.CourseRequest 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 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();
        final Enrollment current = assignment.getValue(request);
        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) {
                    if (model.getStudentQuality() != null)
                        value = model.getStudentWeights().getWeight(assignment, e, model.getStudentQuality().conflicts(e));
                    else
                        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(e2))
                    return 0;
                if (e1.equals(current))
                    return -1;
                if (e2.equals(current))
                    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)

Aggregations

CourseRequest (org.cpsolver.studentsct.model.CourseRequest)67 Request (org.cpsolver.studentsct.model.Request)51 Enrollment (org.cpsolver.studentsct.model.Enrollment)37 Section (org.cpsolver.studentsct.model.Section)36 Course (org.cpsolver.studentsct.model.Course)26 FreeTimeRequest (org.cpsolver.studentsct.model.FreeTimeRequest)21 Subpart (org.cpsolver.studentsct.model.Subpart)21 ArrayList (java.util.ArrayList)20 HashSet (java.util.HashSet)18 Student (org.cpsolver.studentsct.model.Student)17 HashMap (java.util.HashMap)15 Config (org.cpsolver.studentsct.model.Config)13 Map (java.util.Map)11 Offering (org.cpsolver.studentsct.model.Offering)10 CSVFile (org.cpsolver.ifs.util.CSVFile)9 Set (java.util.Set)8 DistanceConflict (org.cpsolver.studentsct.extension.DistanceConflict)7 TreeSet (java.util.TreeSet)6 Constraint (org.cpsolver.ifs.model.Constraint)6 StudentQuality (org.cpsolver.studentsct.extension.StudentQuality)6