Search in sources :

Example 1 with NeighbourSelectionWithSuggestions

use of org.cpsolver.coursett.heuristics.NeighbourSelectionWithSuggestions 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)

Aggregations

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 Progress (org.cpsolver.ifs.util.Progress)1