Search in sources :

Example 11 with TimetableModel

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

the class GroupConstraint method getCurrentPreference.

/** Current constraint preference change (if given placement is assigned) 
     * @param assignment current assignment
     * @param placement placement that is being considered
     * @return change in the current preference, if assigned 
     **/
public int getCurrentPreference(Assignment<Lecture, Placement> assignment, Placement placement) {
    // no preference
    if (isHard())
        return 0;
    // not enough variable
    if (countAssignedVariables(assignment) + (assignment.getValue(placement.variable()) == null ? 1 : 0) < 2)
        return 0;
    if (getType().is(Flag.MAX_HRS_DAY)) {
        HashMap<Lecture, Placement> assignments = new HashMap<Lecture, Placement>();
        assignments.put(placement.variable(), placement);
        HashMap<Lecture, Placement> unassignments = new HashMap<Lecture, Placement>();
        unassignments.put(placement.variable(), null);
        int after = 0;
        int before = 0;
        for (int dayCode : Constants.DAY_CODES) {
            if (iMaxNHoursADayConsiderDatePatterns) {
                for (BitSet week : ((TimetableModel) getModel()).getWeeks()) {
                    after += Math.max(0, nrSlotsADay(assignment, dayCode, week, assignments, null) - getType().getMax());
                    before += Math.max(0, nrSlotsADay(assignment, dayCode, week, unassignments, null) - getType().getMax());
                }
            } else {
                after += Math.max(0, nrSlotsADay(assignment, dayCode, null, assignments, null) - getType().getMax());
                before += Math.max(0, nrSlotsADay(assignment, dayCode, null, unassignments, null) - getType().getMax());
            }
        }
        return (after > 0 ? Math.abs(iPreference) * after / 12 : -Math.abs(iPreference)) - (before > 0 ? Math.abs(iPreference) * before / 12 : -Math.abs(iPreference));
    }
    int nrViolatedPairsAfter = 0;
    int nrViolatedPairsBefore = 0;
    for (Lecture v1 : variables()) {
        for (Lecture v2 : variables()) {
            if (v1.getId() >= v2.getId())
                continue;
            Placement p1 = (v1.equals(placement.variable()) ? null : assignment.getValue(v1));
            Placement p2 = (v2.equals(placement.variable()) ? null : assignment.getValue(v2));
            if (p1 != null && p2 != null && !isSatisfiedPair(assignment, p1, p2))
                nrViolatedPairsBefore++;
            if (v1.equals(placement.variable()))
                p1 = placement;
            if (v2.equals(placement.variable()))
                p2 = placement;
            if (p1 != null && p2 != null && !isSatisfiedPair(assignment, p1, p2))
                nrViolatedPairsAfter++;
        }
    }
    if (getType().is(Flag.BACK_TO_BACK)) {
        HashMap<Lecture, Placement> assignments = new HashMap<Lecture, Placement>();
        assignments.put(placement.variable(), placement);
        Set<Placement> conflicts = new HashSet<Placement>();
        if (isSatisfiedSeq(assignment, assignments, conflicts))
            nrViolatedPairsAfter += conflicts.size();
        else
            nrViolatedPairsAfter = variables().size();
        HashMap<Lecture, Placement> unassignments = new HashMap<Lecture, Placement>();
        unassignments.put(placement.variable(), null);
        Set<Placement> previous = new HashSet<Placement>();
        if (isSatisfiedSeq(assignment, unassignments, previous))
            nrViolatedPairsBefore += previous.size();
        else
            nrViolatedPairsBefore = variables().size();
    }
    return (nrViolatedPairsAfter > 0 ? Math.abs(iPreference) * nrViolatedPairsAfter : -Math.abs(iPreference)) - (nrViolatedPairsBefore > 0 ? Math.abs(iPreference) * nrViolatedPairsBefore : -Math.abs(iPreference));
}
Also used : Lecture(org.cpsolver.coursett.model.Lecture) HashMap(java.util.HashMap) Placement(org.cpsolver.coursett.model.Placement) BitSet(java.util.BitSet) TimetableModel(org.cpsolver.coursett.model.TimetableModel) WeakeningConstraint(org.cpsolver.ifs.model.WeakeningConstraint) Constraint(org.cpsolver.ifs.model.Constraint) GlobalConstraint(org.cpsolver.ifs.model.GlobalConstraint) HashSet(java.util.HashSet)

Example 12 with TimetableModel

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

the class GroupConstraint method isConsistent.

@Override
public boolean isConsistent(Placement value1, Placement value2) {
    if (!isHard())
        return true;
    if (!isSatisfiedPair(null, value1, value2))
        return false;
    if (getType().is(Flag.BACK_TO_BACK)) {
        HashMap<Lecture, Placement> assignments = new HashMap<Lecture, Placement>();
        assignments.put(value1.variable(), value1);
        assignments.put(value2.variable(), value2);
        if (!isSatisfiedSeq(null, assignments, null))
            return false;
    }
    if (getType().is(Flag.MAX_HRS_DAY)) {
        HashMap<Lecture, Placement> assignments = new HashMap<Lecture, Placement>();
        assignments.put(value1.variable(), value1);
        assignments.put(value2.variable(), value2);
        for (int dayCode : Constants.DAY_CODES) {
            if (iMaxNHoursADayConsiderDatePatterns) {
                for (BitSet week : ((TimetableModel) getModel()).getWeeks()) {
                    if (!value1.getTimeLocation().shareWeeks(week) && !value2.getTimeLocation().shareWeeks(week))
                        continue;
                    if (nrSlotsADay(null, dayCode, week, assignments, null) > getType().getMax())
                        return false;
                }
            } else {
                if (nrSlotsADay(null, dayCode, null, assignments, null) > getType().getMax())
                    return false;
            }
        }
    }
    return true;
}
Also used : Lecture(org.cpsolver.coursett.model.Lecture) HashMap(java.util.HashMap) Placement(org.cpsolver.coursett.model.Placement) BitSet(java.util.BitSet) TimetableModel(org.cpsolver.coursett.model.TimetableModel) WeakeningConstraint(org.cpsolver.ifs.model.WeakeningConstraint) Constraint(org.cpsolver.ifs.model.Constraint) GlobalConstraint(org.cpsolver.ifs.model.GlobalConstraint)

Example 13 with TimetableModel

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

the class GroupConstraint method inConflict.

@Override
public boolean inConflict(Assignment<Lecture, Placement> assignment, Placement value) {
    if (!isHard())
        return false;
    for (Lecture v : variables()) {
        if (v.equals(value.variable()))
            // ignore this variable
            continue;
        Placement p = assignment.getValue(v);
        if (p == null)
            // there is an unassigned variable -- great, still a chance to get violated
            continue;
        if (!isSatisfiedPair(assignment, p, value))
            return true;
    }
    if (getType().is(Flag.BACK_TO_BACK)) {
        HashMap<Lecture, Placement> assignments = new HashMap<Lecture, Placement>();
        assignments.put(value.variable(), value);
        if (!isSatisfiedSeq(assignment, assignments, null))
            return true;
    }
    if (getType().is(Flag.MAX_HRS_DAY)) {
        HashMap<Lecture, Placement> assignments = new HashMap<Lecture, Placement>();
        assignments.put(value.variable(), value);
        for (int dayCode : Constants.DAY_CODES) {
            if (iMaxNHoursADayConsiderDatePatterns) {
                for (BitSet week : ((TimetableModel) getModel()).getWeeks()) {
                    if (!value.getTimeLocation().shareWeeks(week))
                        continue;
                    if (nrSlotsADay(assignment, dayCode, week, assignments, null) > getType().getMax())
                        return true;
                }
            } else {
                if (nrSlotsADay(assignment, dayCode, null, assignments, null) > getType().getMax())
                    return true;
            }
        }
    }
    if (!forwardCheck(assignment, value, new HashSet<GroupConstraint>(), iForwardCheckMaxDepth - 1))
        return true;
    return false;
}
Also used : Lecture(org.cpsolver.coursett.model.Lecture) Placement(org.cpsolver.coursett.model.Placement) HashMap(java.util.HashMap) BitSet(java.util.BitSet) TimetableModel(org.cpsolver.coursett.model.TimetableModel) WeakeningConstraint(org.cpsolver.ifs.model.WeakeningConstraint) Constraint(org.cpsolver.ifs.model.Constraint) GlobalConstraint(org.cpsolver.ifs.model.GlobalConstraint) HashSet(java.util.HashSet)

Example 14 with TimetableModel

use of org.cpsolver.coursett.model.TimetableModel 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 15 with TimetableModel

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

the class FlexibleConstraint method getWeeks.

/**
     * Return weeks of the term.
     * @return a list of bitsets (one for each week of the term) representing datePatterns or null if semester is whole semester is considered
     */
public List<BitSet> getWeeks() {
    if (iWeeks == null) {
        TimetableModel model = (TimetableModel) getModel();
        iWeeks = new ArrayList<BitSet>();
        boolean checkWeeks = model.getProperties().getPropertyBoolean("FlexibleConstraint.CheckWeeks", false);
        if (checkWeeks) {
            // get weeks method returns bitsets representing weeks during semester
            iWeeks = model.getWeeks();
        } else {
            // weeks are not considered, all placements are taken into consideration
            iWeeks.add(null);
        }
    }
    return iWeeks;
}
Also used : BitSet(java.util.BitSet) TimetableModel(org.cpsolver.coursett.model.TimetableModel)

Aggregations

TimetableModel (org.cpsolver.coursett.model.TimetableModel)26 Placement (org.cpsolver.coursett.model.Placement)18 Lecture (org.cpsolver.coursett.model.Lecture)17 HashMap (java.util.HashMap)7 Constraint (org.cpsolver.ifs.model.Constraint)7 HashSet (java.util.HashSet)6 BitSet (java.util.BitSet)5 JenrlConstraint (org.cpsolver.coursett.constraint.JenrlConstraint)5 ArrayList (java.util.ArrayList)4 InstructorConstraint (org.cpsolver.coursett.constraint.InstructorConstraint)4 StudentConflict (org.cpsolver.coursett.criteria.StudentConflict)4 GlobalConstraint (org.cpsolver.ifs.model.GlobalConstraint)4 SimpleNeighbour (org.cpsolver.ifs.model.SimpleNeighbour)4 WeakeningConstraint (org.cpsolver.ifs.model.WeakeningConstraint)4 DataProperties (org.cpsolver.ifs.util.DataProperties)4 File (java.io.File)3 TreeSet (java.util.TreeSet)3 DepartmentSpreadConstraint (org.cpsolver.coursett.constraint.DepartmentSpreadConstraint)3 GroupConstraint (org.cpsolver.coursett.constraint.GroupConstraint)3 RoomConstraint (org.cpsolver.coursett.constraint.RoomConstraint)3