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