    001package org.cpsolver.coursett.criteria.additional;
    002
    003import java.util.Collection;
    004import java.util.Map;
    005
    006import org.cpsolver.coursett.criteria.StudentConflict;
    007import org.cpsolver.coursett.model.Lecture;
    008import org.cpsolver.coursett.model.Placement;
    009import org.cpsolver.coursett.model.TimeLocation;
    010import org.cpsolver.ifs.assignment.Assignment;
    011import org.cpsolver.ifs.solver.Solver;
    012import org.cpsolver.ifs.util.DataProperties;
    013
    014
    015/**
    016 * Naive, yet effective approach for modeling student lunch breaks. This criterion
    017 * is based on {@link StudentConflict} and it creates a conflict whenever there are
    018 * two classes (that share students) overlapping with the lunch time which are one
    019 * after the other with a break in between smaller than the requested lunch break.
    020 * Lunch time is defined by StudentLunch.StartSlot and StudentLunch.EndStart
    021 * properties (default is 11:00 am - 1:30 pm), with lunch break of at least
    022 * StudentLunch.Length slots (default is 30 minutes). Such a conflict is weighted
    023 * by Comparator.StudentLunchWeight, which defaults to Comparator.StudentConflictWeight.
    024 *
    025 * <br>
    026 *
    027 * @author  Tomáš Müller
    028 * @version CourseTT 1.3 (University Course Timetabling)<br>
    029 *          Copyright (C) 2006 - 2014 Tomáš Müller<br>
    030 *          <a href="mailto:muller@unitime.org">muller@unitime.org</a><br>
    031 *          <a href="http://muller.unitime.org">http://muller.unitime.org</a><br>
    032 * <br>
    033 *          This library is free software; you can redistribute it and/or modify
    034 *          it under the terms of the GNU Lesser General Public License as
    035 *          published by the Free Software Foundation; either version 3 of the
    036 *          License, or (at your option) any later version. <br>
    037 * <br>
    038 *          This library is distributed in the hope that it will be useful, but
    039 *          WITHOUT ANY WARRANTY; without even the implied warranty of
    040 *          MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
    041 *          Lesser General Public License for more details. <br>
    042 * <br>
    043 *          You should have received a copy of the GNU Lesser General Public
    044 *          License along with this library; if not see
    045 *          <a href='http://www.gnu.org/licenses/'>http://www.gnu.org/licenses/</a>.
    046 */
    047
    048public class StudentLuchBreak extends StudentConflict {
    049    private int iLunchStart, iLunchEnd, iLunchLength;
    050
    051    @Override
    052    public boolean init(Solver<Lecture, Placement> solver) {
    053        iLunchStart = solver.getProperties().getPropertyInt("StudentLunch.StartSlot", (11 * 60) / 5);
    054        iLunchEnd = solver.getProperties().getPropertyInt("StudentLunch.EndStart", (13 * 60 + 30) / 5);
    055        iLunchLength = solver.getProperties().getPropertyInt("StudentLunch.Length", 30 / 5);
    056        return super.init(solver);
    057    }
    058
    059
    060    @Override
    061    public boolean isApplicable(Lecture l1, Lecture l2) {
    062        return l1 != null && l2 != null && !ignore(l1, l2) && applicable(l1, l2);
    063    }
    064
    065    @Override
    066    public double getWeightDefault(DataProperties config) {
    067        return config.getPropertyDouble("Comparator.StudentLunchWeight", config.getPropertyDouble("Comparator.StudentConflictWeight", 1.0));
    068    }
    069
    070    @Override
    071    public String getPlacementSelectionWeightName() {
    072        return "Placement.StudentLunchWeight";
    073    }
    074
    075    public boolean nolunch(Placement p1, Placement p2) {
    076        if (p1 == null || p2 == null || overlaps(p1, p2)) return false;
    077        if (p1.variable().isCommitted() && p2.variable().isCommitted()) return false;
    078        TimeLocation t1 = p1.getTimeLocation(), t2 = p2.getTimeLocation();
    079        if (!t1.shareDays(t2) || !t1.shareWeeks(t2)) return false;
    080        int s1 = t1.getStartSlot(), s2 = t2.getStartSlot();
    081        int e1 = t1.getStartSlot() + t1.getNrSlotsPerMeeting(), e2 = t2.getStartSlot() + t2.getNrSlotsPerMeeting();
    082        if (e1 + iLunchLength > s2 && e2 + iLunchLength > s1 && e1 > iLunchStart && iLunchEnd > s1 && e2 > iLunchStart && iLunchEnd > s2)
    083            return true;
    084        return false;
    085    }
    086
    087    @Override
    088    public boolean inConflict(Placement p1, Placement p2) {
    089        return nolunch(p1, p2);
    090    }
    091
    092    @Override
    093    public void getInfo(Assignment<Lecture, Placement> assignment, Map<String, String> info) {
    094        super.getInfo(assignment, info);
    095        double conf = getValue(assignment);
    096        if (conf > 0.0)
    097            info.put("Student lunch conflicts", String.valueOf(Math.round(conf)));
    098    }
    099
    100    @Override
    101    public void getInfo(Assignment<Lecture, Placement> assignment, Map<String, String> info, Collection<Lecture> variables) {
    102        super.getInfo(assignment, info, variables);
    103        double conf = getValue(assignment, variables);
    104        if (conf > 0.0)
    105            info.put("Student lunch conflicts", String.valueOf(Math.round(conf)));
    106    }
    107
    108}