Search in sources :

Example 1 with Progress

use of org.cpsolver.ifs.util.Progress in project cpsolver by UniTime.

the class TimetableSolver method fixCompleteSolution.

/**
 * Try to improve existing solution by backtracking search of very limited
 * depth. See {@link NeighbourSelectionWithSuggestions} for more details.
 * @param solution current solution
 * @param startTime start time
 */
protected void fixCompleteSolution(Solution<Lecture, Placement> solution, double startTime) {
    Progress progress = Progress.getInstance(solution.getModel());
    TimetableModel model = (TimetableModel) solution.getModel();
    Assignment<Lecture, Placement> assignment = solution.getAssignment();
    solution.saveBest();
    progress.save();
    double solutionValue = 0.0, newSolutionValue = model.getTotalValue(assignment);
    do {
        solutionValue = newSolutionValue;
        progress.setPhase("Fixing solution", model.variables().size());
        for (Lecture variable : model.variables()) {
            Placement bestValue = null;
            double bestVal = 0.0;
            Placement currentValue = assignment.getValue(variable);
            if (currentValue == null)
                continue;
            double currentVal = currentValue.toDouble(assignment);
            for (Placement value : variable.values()) {
                if (value.equals(currentValue))
                    continue;
                if (model.conflictValues(assignment, value).isEmpty()) {
                    double val = value.toDouble(assignment);
                    if (bestValue == null || val < bestVal) {
                        bestValue = value;
                        bestVal = val;
                    }
                }
            }
            if (bestValue != null && bestVal < currentVal)
                assignment.assign(0, bestValue);
            solution.update(JProf.currentTimeSec() - startTime);
            progress.incProgress();
            if (iStop)
                break;
        }
        newSolutionValue = model.getTotalValue(assignment);
        if (newSolutionValue < solutionValue) {
            progress.debug("New solution value is  " + newSolutionValue);
        }
    } while (!iStop && newSolutionValue < solutionValue && getTerminationCondition().canContinue(solution));
    progress.restore();
    if (!solution.getModel().unassignedVariables(assignment).isEmpty())
        return;
    progress.save();
    try {
        progress.setPhase("Fixing solution [2]", model.variables().size());
        NeighbourSelectionWithSuggestions ns = new NeighbourSelectionWithSuggestions(this);
        for (Lecture lecture : model.variables()) {
            Neighbour<Lecture, Placement> n = ns.selectNeighbourWithSuggestions(solution, lecture, 2);
            if (n != null && n.value(assignment) <= 0.0)
                n.assign(assignment, 0);
            solution.update(JProf.currentTimeSec() - startTime);
            progress.incProgress();
            if (iStop)
                break;
        }
    } catch (Exception e) {
        sLogger.debug(e.getMessage(), e);
    } finally {
        progress.restore();
    }
}
Also used : Progress(org.cpsolver.ifs.util.Progress) Lecture(org.cpsolver.coursett.model.Lecture) NeighbourSelectionWithSuggestions(org.cpsolver.coursett.heuristics.NeighbourSelectionWithSuggestions) Placement(org.cpsolver.coursett.model.Placement) TimetableModel(org.cpsolver.coursett.model.TimetableModel)

Example 2 with Progress

use of org.cpsolver.ifs.util.Progress 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 p = Progress.getInstance(solution.getModel());
        p.incProgress();
        if (p.getProgress() > 2.0 * p.getProgressMax())
            return null;
        if (student.isComplete(solution.getAssignment()) || student.nrAssignedRequests(solution.getAssignment()) == 0)
            continue;
        for (int i = 0; i < 5; i++) {
            try {
                Selection selection = getSelection(solution.getAssignment(), student);
                Neighbour<Request, Enrollment> neighbour = selection.select();
                if (neighbour != null) {
                    addStudent(student);
                    return neighbour;
                } else
                    iProblemStudents.addAll(selection.getProblemStudents());
                break;
            } catch (ConcurrentModificationException e) {
            }
        }
    }
    return null;
}
Also used : Progress(org.cpsolver.ifs.util.Progress) ConcurrentModificationException(java.util.ConcurrentModificationException) 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 3 with Progress

use of org.cpsolver.ifs.util.Progress in project cpsolver by UniTime.

the class RoundRobinNeighbourSelection method changeSelection.

/**
 * Change selection
 * @param selectionIndex current selection index
 */
@SuppressWarnings("unchecked")
public void changeSelection(int selectionIndex) {
    iSolver.currentSolution().getLock().writeLock().lock();
    try {
        Progress progress = Progress.getInstance(iSolver.currentSolution().getModel());
        int newSelectionIndex = 1 + selectionIndex;
        // already changed
        if (newSelectionIndex <= iSelectionIdx)
            return;
        // already changed
        if (selectionIndex == -1 && iSelectionIdx >= 0)
            return;
        iSelectionIdx = newSelectionIndex;
        if (selectionIndex >= 0) {
            try {
                NeighbourSelection<V, T> selection = iSelections.get(selectionIndex % iSelections.size());
                if (selection instanceof InfoProvider) {
                    Map<String, String> info = new HashMap<String, String>();
                    ((InfoProvider<V, T>) selection).getInfo(iSolver.currentSolution().getAssignment(), info);
                    if (!info.isEmpty())
                        for (Map.Entry<String, String> e : info.entrySet()) progress.debug(e.getKey() + ": " + e.getValue());
                }
            } catch (Exception e) {
            }
        }
        sLogger.info("Phase changed to " + ((newSelectionIndex % iSelections.size()) + 1));
        progress.debug(iSolver.currentSolution().toString());
        if (iSolver.currentSolution().getBestInfo() == null || iSolver.getSolutionComparator().isBetterThanBestSolution(iSolver.currentSolution()))
            iSolver.currentSolution().saveBest();
        iSelections.get(iSelectionIdx % iSelections.size()).init(iSolver);
    } finally {
        iSolver.currentSolution().getLock().writeLock().unlock();
    }
}
Also used : Progress(org.cpsolver.ifs.util.Progress) InfoProvider(org.cpsolver.ifs.model.InfoProvider) HashMap(java.util.HashMap)

Example 4 with Progress

use of org.cpsolver.ifs.util.Progress in project cpsolver by UniTime.

the class StudentEnrollmentSwapSelection method selectNeighbour.

@Override
public Neighbour<Request, Enrollment> selectNeighbour(Solution<Request, Enrollment> solution) {
    Request request = null;
    while ((request = nextRequest()) != null) {
        Progress p = Progress.getInstance(solution.getModel());
        p.incProgress();
        if (p.getProgress() > 2.0 * p.getProgressMax())
            return null;
        if (request instanceof CourseRequest) {
            try {
                Enrollment e = request.getAssignment(solution.getAssignment());
                if (e == null || e.getPriority() > 0 || !((CourseRequest) request).getSelectedChoices().isEmpty()) {
                    Neighbour<Request, Enrollment> n = iSelection.selectNeighbour(solution, request);
                    if (iSelection.getContext() != null) {
                        iNbrIterations++;
                        iTotalTime += iSelection.getContext().getTime();
                        if (iSelection.getContext().isTimeoutReached())
                            iNbrTimeoutReached++;
                        if (n == null)
                            iNbrNoSolution++;
                    }
                    if (n != null && n.value(solution.getAssignment()) <= 0.0)
                        return n;
                }
            } catch (ConcurrentModificationException e) {
                addRequest(request);
            }
        }
    }
    return null;
}
Also used : Progress(org.cpsolver.ifs.util.Progress) ConcurrentModificationException(java.util.ConcurrentModificationException) 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 5 with Progress

use of org.cpsolver.ifs.util.Progress in project cpsolver by UniTime.

the class FinalSectioning method execute.

public void execute(Solution<Lecture, Placement> solution, TerminationCondition<Lecture, Placement> termination) {
    Progress p = Progress.getInstance(iModel);
    p.setStatus("Student Sectioning...");
    Collection<Lecture> variables = new ArrayList<Lecture>(iModel.variables());
    // include committed classes that have structure
    if (iModel.hasConstantVariables())
        for (Lecture lecture : iModel.constantVariables()) {
            // if (lecture.getParent() != null || (lecture.sameStudentsLectures()!= null && !lecture.sameStudentsLectures().isEmpty()))
            variables.add(lecture);
        }
    while (!variables.isEmpty() && (termination == null || termination.canContinue(solution))) {
        // sLogger.debug("Shifting students ...");
        p.setPhase("moving students ...", variables.size());
        HashSet<Lecture> lecturesToRecompute = new HashSet<Lecture>(variables.size());
        for (Lecture lecture : variables) {
            if (lecture.getParent() == null) {
                Configuration cfg = lecture.getConfiguration();
                if (cfg != null && cfg.getAltConfigurations().size() > 1)
                    findAndPerformMoves(solution.getAssignment(), cfg, lecturesToRecompute);
            }
            // sLogger.debug("Shifting students for "+lecture);
            findAndPerformMoves(solution.getAssignment(), lecture, lecturesToRecompute);
            // sLogger.debug("Lectures to recompute: "+lects);
            p.incProgress();
        }
        // sLogger.debug("Shifting done, "+getViolatedStudentConflictsCounter().get()+" conflicts.");
        variables = lecturesToRecompute;
    }
}
Also used : Progress(org.cpsolver.ifs.util.Progress) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet)

Aggregations

Progress (org.cpsolver.ifs.util.Progress)5 ConcurrentModificationException (java.util.ConcurrentModificationException)2 CourseRequest (org.cpsolver.studentsct.model.CourseRequest)2 Enrollment (org.cpsolver.studentsct.model.Enrollment)2 Request (org.cpsolver.studentsct.model.Request)2 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 HashSet (java.util.HashSet)1 NeighbourSelectionWithSuggestions (org.cpsolver.coursett.heuristics.NeighbourSelectionWithSuggestions)1 Lecture (org.cpsolver.coursett.model.Lecture)1 Placement (org.cpsolver.coursett.model.Placement)1 TimetableModel (org.cpsolver.coursett.model.TimetableModel)1 NeighbourSelection (org.cpsolver.ifs.heuristics.NeighbourSelection)1 InfoProvider (org.cpsolver.ifs.model.InfoProvider)1 Student (org.cpsolver.studentsct.model.Student)1