Search in sources :

Example 36 with Request

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

the class StandardSelection method selectNeighbour.

/**
     * Employ the provided {@link VariableSelection} and {@link ValueSelection}
     * and return the selected value as {@link SimpleNeighbour}. The selection
     * is stopped (null is returned) after the number of iterations equal to the
     * number of variables in the problem or when a complete solution is found.
     */
@Override
public Neighbour<Request, Enrollment> selectNeighbour(Solution<Request, Enrollment> solution) {
    if (iNrIterations < 0) {
        iNrIterations = solution.getModel().nrUnassignedVariables(solution.getAssignment());
        Progress.getInstance(solution.getModel()).setPhase("Ifs...", iNrIterations);
    }
    if (solution.getModel().unassignedVariables(solution.getAssignment()).isEmpty() || ++iIteration >= iNrIterations)
        return null;
    Progress.getInstance(solution.getModel()).incProgress();
    for (int i = 0; i < 10; i++) {
        Request request = iVariableSelection.selectVariable(solution);
        if (request == null)
            continue;
        Enrollment enrollment = iValueSelection.selectValue(solution, request);
        if (enrollment == null)
            continue;
        Set<Enrollment> conflicts = enrollment.variable().getModel().conflictValues(solution.getAssignment(), enrollment);
        if (!conflicts.contains(enrollment))
            return new SimpleNeighbour<Request, Enrollment>(request, enrollment, conflicts);
    }
    return null;
}
Also used : Request(org.cpsolver.studentsct.model.Request) Enrollment(org.cpsolver.studentsct.model.Enrollment)

Example 37 with Request

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

the class SwapStudentSelection method selectNeighbour.

/**
     * For each student that does not have a complete schedule, try to find a
     * request and a student that can be moved out of an enrollment so that the
     * selected student can be assigned to the selected request.
     */
@Override
public Neighbour<Request, Enrollment> selectNeighbour(Solution<Request, Enrollment> solution) {
    Student student = null;
    while ((student = nextStudent()) != null) {
        Progress.getInstance(solution.getModel()).incProgress();
        if (student.isComplete(solution.getAssignment()) || student.nrAssignedRequests(solution.getAssignment()) == 0)
            continue;
        Selection selection = getSelection(solution.getAssignment(), student);
        Neighbour<Request, Enrollment> neighbour = selection.select();
        if (neighbour != null) {
            addStudent(student);
            return neighbour;
        } else
            iProblemStudents.addAll(selection.getProblemStudents());
    }
    return null;
}
Also used : NeighbourSelection(org.cpsolver.ifs.heuristics.NeighbourSelection) 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 38 with Request

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

the class SuggestionsBranchAndBound method backtrack.

/**
     * Main branch and bound rutine
     * @param requests2resolve remaining requests to assign
     * @param altRequests2resolve alternative requests to assign
     * @param idx current depth
     * @param depth remaining depth
     * @param alt can leave a request unassigned
     */
protected void backtrack(ArrayList<Request> requests2resolve, TreeSet<Request> altRequests2resolve, int idx, int depth, boolean alt) {
    if (!iTimeoutReached && iTimeout > 0 && System.currentTimeMillis() - iT0 > iTimeout)
        iTimeoutReached = true;
    int nrUnassigned = requests2resolve.size() - idx;
    if (nrUnassigned == 0) {
        List<FreeTimeRequest> okFreeTimes = new ArrayList<FreeTimeRequest>();
        double sectionsWithPenalty = 0;
        for (Request r : iStudent.getRequests()) {
            Enrollment e = iAssignment.getValue(r);
            if (iMaxSectionsWithPenalty >= 0 && e != null && r instanceof CourseRequest) {
                for (Section s : e.getSections()) sectionsWithPenalty += iModel.getOverExpected(iAssignment, s, r);
            }
            if (e == null && r instanceof FreeTimeRequest) {
                FreeTimeRequest ft = (FreeTimeRequest) r;
                Enrollment enrollment = ft.createEnrollment();
                if (iModel.conflictValues(iAssignment, enrollment).isEmpty()) {
                    iAssignment.assign(0, enrollment);
                    okFreeTimes.add(ft);
                }
            }
        }
        if (iMaxSectionsWithPenalty >= 0 && sectionsWithPenalty > iMaxSectionsWithPenalty)
            return;
        Suggestion s = new Suggestion(requests2resolve);
        if (iSuggestions.size() >= iMaxSuggestions && iSuggestions.last().compareTo(s) <= 0)
            return;
        if (iMatched != 1) {
            for (Iterator<Suggestion> i = iSuggestions.iterator(); i.hasNext(); ) {
                Suggestion x = i.next();
                if (x.sameSelectedSection()) {
                    if (x.compareTo(s) <= 0)
                        return;
                    i.remove();
                }
            }
        }
        s.init();
        iSuggestions.add(s);
        if (iSuggestions.size() > iMaxSuggestions)
            iSuggestions.remove(iSuggestions.last());
        for (FreeTimeRequest ft : okFreeTimes) iAssignment.unassign(0, ft);
        return;
    }
    if (!canContinue(requests2resolve, idx, depth))
        return;
    Request request = requests2resolve.get(idx);
    for (Enrollment enrollment : values(request)) {
        if (!canContinueEvaluation())
            break;
        if (!isAllowed(enrollment))
            continue;
        if (enrollment.equals(iAssignment.getValue(request)))
            continue;
        if (enrollment.getAssignments().isEmpty() && alt)
            continue;
        Set<Enrollment> conflicts = iModel.conflictValues(iAssignment, enrollment);
        if (!checkBound(requests2resolve, idx, depth, enrollment, conflicts))
            continue;
        Enrollment current = iAssignment.getValue(request);
        ArrayList<Request> newVariables2resolve = new ArrayList<Request>(requests2resolve);
        for (Iterator<Enrollment> i = conflicts.iterator(); i.hasNext(); ) {
            Enrollment conflict = i.next();
            iAssignment.unassign(0, conflict.variable());
            if (!newVariables2resolve.contains(conflict.variable()))
                newVariables2resolve.add(conflict.variable());
        }
        if (current != null)
            iAssignment.unassign(0, current.variable());
        iAssignment.assign(0, enrollment);
        if (enrollment.getAssignments().isEmpty()) {
            if (altRequests2resolve != null && !altRequests2resolve.isEmpty()) {
                Suggestion lastBefore = (iSuggestions.isEmpty() ? null : iSuggestions.last());
                int sizeBefore = iSuggestions.size();
                for (Request r : altRequests2resolve) {
                    newVariables2resolve.add(r);
                    backtrack(newVariables2resolve, null, idx + 1, depth, true);
                    newVariables2resolve.remove(r);
                }
                Suggestion lastAfter = (iSuggestions.isEmpty() ? null : iSuggestions.last());
                int sizeAfter = iSuggestions.size();
                // did not succeeded with an alternative -> try without it
                if (sizeBefore == sizeAfter && (sizeAfter < iMaxSuggestions || sizeAfter == 0 || lastAfter.compareTo(lastBefore) == 0))
                    backtrack(newVariables2resolve, altRequests2resolve, idx + 1, depth - 1, alt);
            } else {
                backtrack(newVariables2resolve, altRequests2resolve, idx + 1, depth - 1, alt);
            }
        } else {
            backtrack(newVariables2resolve, altRequests2resolve, idx + 1, depth - 1, alt);
        }
        if (current == null)
            iAssignment.unassign(0, request);
        else
            iAssignment.assign(0, current);
        for (Iterator<Enrollment> i = conflicts.iterator(); i.hasNext(); ) {
            Enrollment conflict = i.next();
            iAssignment.assign(0, conflict);
        }
    }
}
Also used : FreeTimeRequest(org.cpsolver.studentsct.model.FreeTimeRequest) ArrayList(java.util.ArrayList) FreeTimeRequest(org.cpsolver.studentsct.model.FreeTimeRequest) Request(org.cpsolver.studentsct.model.Request) CourseRequest(org.cpsolver.studentsct.model.CourseRequest) Section(org.cpsolver.studentsct.model.Section) CourseRequest(org.cpsolver.studentsct.model.CourseRequest) Enrollment(org.cpsolver.studentsct.model.Enrollment)

Example 39 with Request

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

the class StudentChoiceOrder method avgNrChoices.

/** Average number of choices for each student 
     * @param student given student
     * @return average number of choices of the given student
     **/
public double avgNrChoices(Student student) {
    int nrRequests = 0;
    int nrChoices = 0;
    for (Request request : student.getRequests()) {
        if (request instanceof CourseRequest) {
            CourseRequest cr = (CourseRequest) request;
            for (Course course : cr.getCourses()) {
                for (Config config : course.getOffering().getConfigs()) {
                    Integer nrChoicesThisCfg = iCache.get(config);
                    if (nrChoicesThisCfg == null) {
                        nrChoicesThisCfg = new Integer(nrChoices(config, 0, new HashSet<Section>()));
                        iCache.put(config, nrChoicesThisCfg);
                    }
                    nrChoices += nrChoicesThisCfg.intValue();
                }
            }
            nrRequests++;
        }
    }
    return (nrRequests == 0 ? 0.0 : ((double) nrChoices) / nrRequests);
}
Also used : CourseRequest(org.cpsolver.studentsct.model.CourseRequest) Config(org.cpsolver.studentsct.model.Config) 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 40 with Request

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

the class Test method main.

public static void main(String[] args) {
    try {
        System.setProperty("jprof", "cpu");
        BasicConfigurator.configure();
        DataProperties cfg = new DataProperties();
        cfg.setProperty("Neighbour.BranchAndBoundTimeout", "5000");
        cfg.setProperty("Suggestions.Timeout", "1000");
        cfg.setProperty("Extensions.Classes", DistanceConflict.class.getName() + ";" + TimeOverlapsCounter.class.getName());
        cfg.setProperty("StudentWeights.Class", StudentSchedulingAssistantWeights.class.getName());
        cfg.setProperty("StudentWeights.PriorityWeighting", "true");
        cfg.setProperty("StudentWeights.LeftoverSpread", "true");
        cfg.setProperty("StudentWeights.BalancingFactor", "0.0");
        cfg.setProperty("Reservation.CanAssignOverTheLimit", "true");
        cfg.setProperty("Distances.Ellipsoid", DistanceMetric.Ellipsoid.WGS84.name());
        cfg.setProperty("StudentWeights.MultiCriteria", "true");
        cfg.setProperty("CourseRequest.SameTimePrecise", "true");
        cfg.setProperty("log4j.rootLogger", "INFO, A1");
        cfg.setProperty("log4j.appender.A1", "org.apache.log4j.ConsoleAppender");
        cfg.setProperty("log4j.appender.A1.layout", "org.apache.log4j.PatternLayout");
        cfg.setProperty("log4j.appender.A1.layout.ConversionPattern", "%-5p %c{2}: %m%n");
        cfg.setProperty("log4j.logger.org.hibernate", "INFO");
        cfg.setProperty("log4j.logger.org.hibernate.cfg", "WARN");
        cfg.setProperty("log4j.logger.org.hibernate.cache.EhCacheProvider", "ERROR");
        cfg.setProperty("log4j.logger.org.unitime.commons.hibernate", "INFO");
        cfg.setProperty("log4j.logger.net", "INFO");
        cfg.setProperty("Xml.LoadBest", "false");
        cfg.setProperty("Xml.LoadCurrent", "false");
        cfg.putAll(System.getProperties());
        PropertyConfigurator.configure(cfg);
        final Test test = new Test(cfg);
        final File input = new File(args[0]);
        StudentSectioningXMLLoader loader = new StudentSectioningXMLLoader(test.model(), test.assignment());
        loader.setInputFile(input);
        loader.load();
        test.run();
        Solver<Request, Enrollment> s = new Solver<Request, Enrollment>(cfg);
        s.setInitalSolution(test.model());
        StudentSectioningXMLSaver saver = new StudentSectioningXMLSaver(s);
        File output = new File(input.getParentFile(), input.getName().substring(0, input.getName().lastIndexOf('.')) + "-" + cfg.getProperty("run", "r0") + ".xml");
        saver.save(output);
        test.stats(input);
    } catch (Exception e) {
        sLog.error("Test failed: " + e.getMessage(), e);
    }
}
Also used : StudentSchedulingAssistantWeights(org.cpsolver.studentsct.online.selection.StudentSchedulingAssistantWeights) Solver(org.cpsolver.ifs.solver.Solver) DistanceConflict(org.cpsolver.studentsct.extension.DistanceConflict) DataProperties(org.cpsolver.ifs.util.DataProperties) Request(org.cpsolver.studentsct.model.Request) CourseRequest(org.cpsolver.studentsct.model.CourseRequest) FreeTimeRequest(org.cpsolver.studentsct.model.FreeTimeRequest) Enrollment(org.cpsolver.studentsct.model.Enrollment) StudentSectioningXMLSaver(org.cpsolver.studentsct.StudentSectioningXMLSaver) File(java.io.File) NoSuchElementException(java.util.NoSuchElementException) IOException(java.io.IOException) StudentSectioningXMLLoader(org.cpsolver.studentsct.StudentSectioningXMLLoader)

Aggregations

Request (org.cpsolver.studentsct.model.Request)65 CourseRequest (org.cpsolver.studentsct.model.CourseRequest)51 Enrollment (org.cpsolver.studentsct.model.Enrollment)47 FreeTimeRequest (org.cpsolver.studentsct.model.FreeTimeRequest)23 Section (org.cpsolver.studentsct.model.Section)22 ArrayList (java.util.ArrayList)21 Student (org.cpsolver.studentsct.model.Student)21 Course (org.cpsolver.studentsct.model.Course)20 HashSet (java.util.HashSet)18 HashMap (java.util.HashMap)14 Subpart (org.cpsolver.studentsct.model.Subpart)10 Map (java.util.Map)9 DataProperties (org.cpsolver.ifs.util.DataProperties)9 Config (org.cpsolver.studentsct.model.Config)8 Offering (org.cpsolver.studentsct.model.Offering)8 Set (java.util.Set)7 DefaultSingleAssignment (org.cpsolver.ifs.assignment.DefaultSingleAssignment)7 File (java.io.File)6 IOException (java.io.IOException)6 TreeSet (java.util.TreeSet)6