001package org.cpsolver.coursett.criteria.additional;
002
003import java.util.Collection;
004import java.util.HashSet;
005import java.util.Map;
006import java.util.Set;
007
008import org.cpsolver.coursett.constraint.JenrlConstraint;
009import org.cpsolver.coursett.criteria.StudentConflict;
010import org.cpsolver.coursett.model.Lecture;
011import org.cpsolver.coursett.model.Placement;
012import org.cpsolver.coursett.model.TimetableModel;
013import org.cpsolver.ifs.assignment.Assignment;
014import org.cpsolver.ifs.util.DataProperties;
015
016/**
017 * An experimental criterion that tries to keep student all classes before or after the lunch period.
018 * There is a conflict (penalized by Comparator.StudentOverLunchConflictWeight parameter) every time when
019 * a student has two classes, one in the morning (starting before or at the lunch period) 
020 * and one in the afternoon (starting after lunch period). When StudentConflict.OverLunchSameDayOnly is true,
021 * only conflicts between classes that are on the same day are counted. The lunch period is defined by
022 * StudentConflict.NoonSlot parameter (defaults to 144).
023 * <br>
024 * 
025 * @author  Tomáš Müller
026 * @version CourseTT 1.3 (University Course Timetabling)<br>
027 *          Copyright (C) 2013 - 2014 Tomáš Müller<br>
028 *          <a href="mailto:muller@unitime.org">muller@unitime.org</a><br>
029 *          <a href="http://muller.unitime.org">http://muller.unitime.org</a><br>
030 * <br>
031 *          This library is free software; you can redistribute it and/or modify
032 *          it under the terms of the GNU Lesser General Public License as
033 *          published by the Free Software Foundation; either version 3 of the
034 *          License, or (at your option) any later version. <br>
035 * <br>
036 *          This library is distributed in the hope that it will be useful, but
037 *          WITHOUT ANY WARRANTY; without even the implied warranty of
038 *          MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
039 *          Lesser General Public License for more details. <br>
040 * <br>
041 *          You should have received a copy of the GNU Lesser General Public
042 *          License along with this library; if not see
043 *          <a href='http://www.gnu.org/licenses/'>http://www.gnu.org/licenses/</a>.
044 */
045public class StudentOverLunchConflict extends StudentConflict {
046    private int iNoonSlot = 144;
047    private boolean iSameDay = true;
048    
049    @Override
050    public void configure(DataProperties properties) {   
051        super.configure(properties);
052        iNoonSlot = properties.getPropertyInt("StudentConflict.NoonSlot", 144);
053        iSameDay = properties.getPropertyBoolean("StudentConflict.OverLunchSameDayOnly", true);
054    }
055    
056    /**
057     * Are the two placements at the same day? True when the two placements share days and weeks.
058     */
059    public boolean shareDays(Placement p1, Placement p2) {
060        return p1 != null && p2 != null && p1.getTimeLocation().shareDays(p2.getTimeLocation()) && p1.getTimeLocation().shareWeeks(p2.getTimeLocation());
061    }
062    
063    /**
064     * Is the given placement in the morning or in the afternoon?
065     */
066    public boolean isMorning(Placement placement) {
067        return placement != null && placement.getTimeLocation().getStartSlot() <= iNoonSlot;
068    }
069    
070    /**
071     * There is a conflict when {@link StudentOverLunchConflict#isMorning(Placement)} differs for the two placements.
072     * When parameter StudentConflict.OverLunchSameDayOnly is true, only conflicts that are on the same day
073     * ({@link StudentOverLunchConflict#shareDays(Placement, Placement)} returns true) are counted.
074     */
075    @Override
076    public boolean inConflict(Placement p1, Placement p2) {
077        return p1 != null && p2 != null && isMorning(p1) != isMorning(p2) && (!iSameDay || shareDays(p1, p2));
078    }
079    
080    @Override
081    public boolean isApplicable(Lecture l1, Lecture l2) {
082        return l1 != null && l2 != null && !ignore(l1, l2) && applicable(l1, l2);
083    }
084    
085    @Override
086    public double getWeightDefault(DataProperties config) {
087        return config.getPropertyDouble("Comparator.StudentOverLunchConflictWeight", 0.1 * config.getPropertyDouble("Comparator.StudentConflictWeight", 1.0));
088    }
089    
090    @Override
091    public String getPlacementSelectionWeightName() {
092        return "Placement.StudentOverLunchConflictWeight";
093    }
094    
095    @Override
096    public void getInfo(Assignment<Lecture, Placement> assignment, Map<String, String> info) {
097        super.getInfo(assignment, info);
098        double conf = getValue(assignment);
099        if (conf > 0.0) {
100            double total = 0;
101            for (JenrlConstraint jenrl: ((TimetableModel)getModel()).getJenrlConstraints()) {
102                if (!jenrl.isToBeIgnored()) {
103                    total += jenrl.jenrl();
104                }
105            }
106            info.put("Student over-lunch conflicts", getPerc(conf, 0.0, total) + "% (" + sDoubleFormat.format(conf) + " / " + sDoubleFormat.format(total) + ", weighted: " + sDoubleFormat.format(getWeightedValue(assignment)) + ")");
107        }
108    }
109    
110    @Override
111    public void getInfo(Assignment<Lecture, Placement> assignment, Map<String, String> info, Collection<Lecture> variables) {
112        super.getInfo(assignment, info, variables);
113        double conf = getValue(assignment, variables);
114        if (conf > 0.0) {
115            Set<JenrlConstraint> jenrls = new HashSet<JenrlConstraint>();
116            double total = 0;
117            for (Lecture lecture: variables) {
118                for (JenrlConstraint jenrl: lecture.jenrlConstraints())
119                    if (jenrls.add(jenrl) && !jenrl.isToBeIgnored())
120                        total += jenrl.jenrl();
121            }
122            info.put("Student over-lunch conflicts", getPerc(conf, 0.0, total) + "% (" + sDoubleFormat.format(conf) + ")");
123        }
124    }
125}