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 * @version CourseTT 1.3 (University Course Timetabling)<br>
028 *          Copyright (C) 2006 - 2014 Tomáš Müller<br>
029 *          <a href="mailto:muller@unitime.org">muller@unitime.org</a><br>
030 *          <a href="http://muller.unitime.org">http://muller.unitime.org</a><br>
031 * <br>
032 *          This library is free software; you can redistribute it and/or modify
033 *          it under the terms of the GNU Lesser General Public License as
034 *          published by the Free Software Foundation; either version 3 of the
035 *          License, or (at your option) any later version. <br>
036 * <br>
037 *          This library is distributed in the hope that it will be useful, but
038 *          WITHOUT ANY WARRANTY; without even the implied warranty of
039 *          MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
040 *          Lesser General Public License for more details. <br>
041 * <br>
042 *          You should have received a copy of the GNU Lesser General Public
043 *          License along with this library; if not see
044 *          <a href='http://www.gnu.org/licenses/'>http://www.gnu.org/licenses/</a>.
045 */
046
047public class StudentLuchBreak extends StudentConflict {
048    private int iLunchStart, iLunchEnd, iLunchLength; 
049    
050    @Override
051    public boolean init(Solver<Lecture, Placement> solver) {
052        iLunchStart = solver.getProperties().getPropertyInt("StudentLunch.StartSlot", (11 * 60) / 5);
053        iLunchEnd = solver.getProperties().getPropertyInt("StudentLunch.EndStart", (13 * 60 + 30) / 5);
054        iLunchLength = solver.getProperties().getPropertyInt("StudentLunch.Length", 30 / 5);
055        return super.init(solver);
056    }
057
058
059    @Override
060    public boolean isApplicable(Lecture l1, Lecture l2) {
061        return l1 != null && l2 != null && !ignore(l1, l2) && applicable(l1, l2);
062    }
063    
064    @Override
065    public double getWeightDefault(DataProperties config) {
066        return config.getPropertyDouble("Comparator.StudentLunchWeight", config.getPropertyDouble("Comparator.StudentConflictWeight", 1.0));
067    }
068    
069    @Override
070    public String getPlacementSelectionWeightName() {
071        return "Placement.StudentLunchWeight";
072    }
073
074    public boolean nolunch(Placement p1, Placement p2) {
075        if (p1 == null || p2 == null || overlaps(p1, p2)) return false;
076        if (p1.variable().isCommitted() && p2.variable().isCommitted()) return false;
077        TimeLocation t1 = p1.getTimeLocation(), t2 = p2.getTimeLocation();
078        if (!t1.shareDays(t2) || !t1.shareWeeks(t2)) return false;
079        int s1 = t1.getStartSlot(), s2 = t2.getStartSlot();
080        int e1 = t1.getStartSlot() + t1.getNrSlotsPerMeeting(), e2 = t2.getStartSlot() + t2.getNrSlotsPerMeeting();
081        if (e1 + iLunchLength > s2 && e2 + iLunchLength > s1 && e1 > iLunchStart && iLunchEnd > s1 && e2 > iLunchStart && iLunchEnd > s2)
082            return true;
083        return false;
084    }
085    
086    @Override
087    public boolean inConflict(Placement p1, Placement p2) {
088        return nolunch(p1, p2);
089    }
090    
091    @Override
092    public void getInfo(Assignment<Lecture, Placement> assignment, Map<String, String> info) {
093        super.getInfo(assignment, info);
094        double conf = getValue(assignment);
095        if (conf > 0.0)
096            info.put("Student lunch conflicts", String.valueOf(Math.round(conf)));
097    }
098    
099    @Override
100    public void getInfo(Assignment<Lecture, Placement> assignment, Map<String, String> info, Collection<Lecture> variables) {
101        super.getInfo(assignment, info, variables);
102        double conf = getValue(assignment, variables);
103        if (conf > 0.0)
104            info.put("Student lunch conflicts", String.valueOf(Math.round(conf)));
105    }
106
107}