Search in sources :

Example 41 with Placement

use of org.cpsolver.coursett.model.Placement in project cpsolver by UniTime.

the class FixCompleteSolutionNeighbourSelection method selectNeighbour.

/**
     * Try to improve existing solution by backtracking search of very limited
     * depth. See {@link NeighbourSelectionWithSuggestions} for more details.
     */
@Override
public Neighbour<Lecture, Placement> selectNeighbour(Solution<Lecture, Placement> solution) {
    FixCompleteSolutionNeighbourContext context = getContext(solution.getAssignment());
    if (context.getPhase() == 0) {
        if (solution.getModel().nrUnassignedVariables(solution.getAssignment()) == 0) {
            // complete solution was found
            if (iCompleteSolutionFixInterval < 0) {
                // feature disabled
                return iParent.selectNeighbour(solution);
            } else if (iCompleteSolutionFixInterval == 0) {
                // only run first time a complete solution is found
                if (iLastCompleteSolutionFixIteration >= 0)
                    return iParent.selectNeighbour(solution);
            } else {
                // run first time and if not run for a given number of iterations
                if (iLastCompleteSolutionFixIteration >= 0 && solution.getIteration() - iLastCompleteSolutionFixIteration < iCompleteSolutionFixInterval)
                    return iParent.selectNeighbour(solution);
            }
            if (solution.getBestIteration() == solution.getIteration())
                context.incPhase(solution);
        } else if (!solution.isBestComplete()) {
            // complete solution has not been found yet
            if (iIncompleteSolutionFixInterval < 0) {
                // feature disabled
                return iParent.selectNeighbour(solution);
            } else if (iIncompleteSolutionFixInterval == 0) {
                // only run first time a complete solution is found
                if (iLastIncompleteSolutionFixIteration >= 0)
                    return iParent.selectNeighbour(solution);
            } else {
                // run first time and if not run for a given number of iterations
                if (solution.getIteration() - iLastIncompleteSolutionFixIteration < iIncompleteSolutionFixInterval)
                    return iParent.selectNeighbour(solution);
            }
            if (solution.getBestIteration() == solution.getIteration())
                context.incPhase(solution);
        }
    }
    while (context.getPhase() > 0 && !iSolver.isStop()) {
        if (context.hasMoreElements()) {
            Lecture variable = context.nextElement();
            // iProgress.incProgress();
            if (context.getPhase() == 1) {
                Placement bestValue = null;
                double bestVal = 0.0;
                Placement currentValue = solution.getAssignment().getValue(variable);
                if (currentValue == null)
                    continue;
                double currentVal = currentValue.toDouble(solution.getAssignment());
                for (Placement value : variable.values(solution.getAssignment())) {
                    if (value.equals(currentValue))
                        continue;
                    if (solution.getModel().conflictValues(solution.getAssignment(), value).isEmpty()) {
                        double val = value.toDouble(solution.getAssignment());
                        if (bestValue == null || val < bestVal) {
                            bestValue = value;
                            bestVal = val;
                        }
                    }
                }
                if (bestValue != null && bestVal < currentVal)
                    return new SimpleNeighbour<Lecture, Placement>(variable, bestValue);
            } else {
                Neighbour<Lecture, Placement> n = iSuggestions.selectNeighbourWithSuggestions(solution, variable, 2);
                if (n != null && n.value(solution.getAssignment()) <= solution.getModel().getTotalValue(solution.getAssignment()))
                    return n;
            }
        } else {
            context.incPhase(solution);
        }
    }
    return iParent.selectNeighbour(solution);
}
Also used : Lecture(org.cpsolver.coursett.model.Lecture) Placement(org.cpsolver.coursett.model.Placement)

Example 42 with Placement

use of org.cpsolver.coursett.model.Placement in project cpsolver by UniTime.

the class FlexibleConstraintCriterion method getInfo.

@Override
public void getInfo(Assignment<Lecture, Placement> assignment, Map<String, String> info) {
    TimetableModel m = (TimetableModel) getModel();
    if (m.getFlexibleConstraints().isEmpty())
        return;
    for (FlexibleConstraintType type : FlexibleConstraintType.values()) {
        StringBuilder debug = null;
        int violated = 0, constraints = 0;
        for (FlexibleConstraint c : m.getFlexibleConstraints()) {
            if (type.equals(c.getType())) {
                constraints++;
                if (c.getContext(assignment).getPreference() > 0) {
                    violated++;
                    if (iDebug) {
                        if (debug == null)
                            debug = new StringBuilder(c.getOwner() + " (" + sDoubleFormat.format(c.getNrViolations(assignment, new HashSet<Placement>(), null)) + ")");
                        else
                            debug.append("; " + c.getOwner() + " (" + sDoubleFormat.format(c.getNrViolations(assignment, new HashSet<Placement>(), null)) + ")");
                    }
                }
            }
        }
        if (constraints > 0) {
            info.put(type.getName() + " Constraints", getPerc(violated, 0, constraints) + "% (" + violated + ")");
            if (iDebug && violated > 0)
                info.put(type.getName() + " Violations", debug.toString());
        }
    }
}
Also used : FlexibleConstraint(org.cpsolver.coursett.constraint.FlexibleConstraint) Placement(org.cpsolver.coursett.model.Placement) FlexibleConstraintType(org.cpsolver.coursett.constraint.FlexibleConstraint.FlexibleConstraintType) TimetableModel(org.cpsolver.coursett.model.TimetableModel) FlexibleConstraint(org.cpsolver.coursett.constraint.FlexibleConstraint) HashSet(java.util.HashSet)

Example 43 with Placement

use of org.cpsolver.coursett.model.Placement in project cpsolver by UniTime.

the class StudentConflict method getValue.

@Override
public double getValue(Assignment<Lecture, Placement> assignment, Collection<Lecture> variables) {
    double ret = 0.0;
    Set<JenrlConstraint> constraints = new HashSet<JenrlConstraint>();
    for (Lecture lect : variables) {
        Placement plac = assignment.getValue(lect);
        if (plac == null)
            continue;
        for (JenrlConstraint jenrl : lect.jenrlConstraints()) {
            if (!constraints.add(jenrl))
                continue;
            Lecture other = jenrl.another(lect);
            if (!other.isCommitted() && !variables.contains(other))
                continue;
            if (!isApplicable(lect, other))
                continue;
            if (inConflict(plac, assignment.getValue(other)))
                ret += jointEnrollment(jenrl);
        }
    }
    return ret;
}
Also used : JenrlConstraint(org.cpsolver.coursett.constraint.JenrlConstraint) Lecture(org.cpsolver.coursett.model.Lecture) Placement(org.cpsolver.coursett.model.Placement) HashSet(java.util.HashSet)

Example 44 with Placement

use of org.cpsolver.coursett.model.Placement in project cpsolver by UniTime.

the class StudentConflict method getValue.

@Override
public double getValue(Assignment<Lecture, Placement> assignment, Placement value, Set<Placement> conflicts) {
    double ret = 0.0;
    for (JenrlConstraint jenrl : value.variable().jenrlConstraints()) {
        Lecture other = jenrl.another(value.variable());
        if (!isApplicable(value.variable(), other))
            continue;
        Placement another = assignment.getValue(other);
        if (another == null)
            continue;
        if (conflicts != null && conflicts.contains(another))
            continue;
        if (inConflict(value, another))
            ret += jointEnrollment(jenrl);
    }
    if (iIncludeConflicts && conflicts != null)
        for (Placement conflict : conflicts) {
            for (JenrlConstraint jenrl : conflict.variable().jenrlConstraints()) {
                Lecture other = jenrl.another(conflict.variable());
                if (!isApplicable(conflict.variable(), other))
                    continue;
                Placement another = assignment.getValue(other);
                if (another == null || another.variable().equals(value.variable()))
                    continue;
                if (conflicts != null && conflicts.contains(another))
                    continue;
                if (inConflict(conflict, another))
                    ret -= jointEnrollment(jenrl);
            }
        }
    return ret;
}
Also used : JenrlConstraint(org.cpsolver.coursett.constraint.JenrlConstraint) Lecture(org.cpsolver.coursett.model.Lecture) Placement(org.cpsolver.coursett.model.Placement)

Example 45 with Placement

use of org.cpsolver.coursett.model.Placement in project cpsolver by UniTime.

the class IgnoredCommittedStudentConflict method countCommittedConflicts.

public int countCommittedConflicts(Student student, Placement placement) {
    if (student.getCommitedPlacements() == null)
        return 0;
    int conflicts = 0;
    Lecture lecture = placement.variable();
    for (Placement commitedPlacement : student.getCommitedPlacements()) {
        Lecture commitedLecture = commitedPlacement.variable();
        if (lecture.getSchedulingSubpartId() != null && lecture.getSchedulingSubpartId().equals(commitedLecture.getSchedulingSubpartId()))
            continue;
        if (ignore(lecture, commitedLecture) && (overlaps(placement, commitedPlacement) || distance(getMetrics(), placement, commitedPlacement)))
            conflicts++;
    }
    if (conflicts == 0)
        return 0;
    double w = student.getOfferingWeight((placement.variable()).getConfiguration());
    return (int) Math.round(student.avg(w, 1.0) * conflicts);
}
Also used : Lecture(org.cpsolver.coursett.model.Lecture) Placement(org.cpsolver.coursett.model.Placement)

Aggregations

Placement (org.cpsolver.coursett.model.Placement)72 Lecture (org.cpsolver.coursett.model.Lecture)55 HashSet (java.util.HashSet)24 TimetableModel (org.cpsolver.coursett.model.TimetableModel)18 TimeLocation (org.cpsolver.coursett.model.TimeLocation)17 WeakeningConstraint (org.cpsolver.ifs.model.WeakeningConstraint)17 ArrayList (java.util.ArrayList)16 BitSet (java.util.BitSet)16 Constraint (org.cpsolver.ifs.model.Constraint)15 HashMap (java.util.HashMap)14 RoomLocation (org.cpsolver.coursett.model.RoomLocation)13 InstructorConstraint (org.cpsolver.coursett.constraint.InstructorConstraint)9 GlobalConstraint (org.cpsolver.ifs.model.GlobalConstraint)9 Student (org.cpsolver.coursett.model.Student)7 List (java.util.List)6 RoomConstraint (org.cpsolver.coursett.constraint.RoomConstraint)6 DataProperties (org.cpsolver.ifs.util.DataProperties)6 GroupConstraint (org.cpsolver.coursett.constraint.GroupConstraint)5 JenrlConstraint (org.cpsolver.coursett.constraint.JenrlConstraint)5 SpreadConstraint (org.cpsolver.coursett.constraint.SpreadConstraint)5