    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.Student;
    013import org.cpsolver.coursett.model.TimeLocation;
    014import org.cpsolver.coursett.model.TimetableModel;
    015import org.cpsolver.ifs.assignment.Assignment;
    016import org.cpsolver.ifs.util.DataProperties;
    017
    018/**
    019 * Naive, yet effective approach for minimizing number of days in student schedule. This criterion
    020 * is based on {@link StudentConflict} and it penalizes all cases where a student has
    021 * two classes taught on different days.
    022 * These penalties are weighted by Comparator.MinimizeStudentScheduleDaysWeight,
    023 * which defaults to 1/20 of the 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 */
    047public class StudentMinimizeDaysOfWeek extends StudentConflict {
    048
    049    /**
    050     * Time distance between two placements in the number of days. The classes must be on the weeks.
    051     * If the two classes are taught on multiple days during the week, the
    052     * distance only count the different days.
    053     * @param p1 first placement
    054     * @param p2 second placement
    055     * @return number of different days
    056     */
    057    public static double dayDistance(Placement p1, Placement p2) {
    058        if (p1 == null || p2 == null) return 0;
    059        return dayDistance(p1.getTimeLocation(), p2.getTimeLocation());
    060    }
    061
    062    private static double dayDistance(TimeLocation t1, TimeLocation t2) {
    063        if (!t1.shareWeeks(t2)) return 0.0;
    064        return 1.0 - ((double)t1.nrSharedDays(t2)) / Math.min(t1.getNrMeetings(), t2.getNrMeetings());
    065    }
    066
    067    @Override
    068    public boolean inConflict(Placement p1, Placement p2) {
    069        return dayDistance(p1, p2) > 0.0;
    070    }
    071
    072    @Override
    073    protected double jointEnrollment(JenrlConstraint jenrl, Placement p1, Placement p2) {
    074        return dayDistance(p1, p2) * jenrl.jenrl();
    075    }
    076
    077    @Override
    078    protected double jointEnrollment(JenrlConstraint jenrl) {
    079        double max = 0;
    080        for (TimeLocation t1: jenrl.first().timeLocations())
    081            for (TimeLocation t2: jenrl.second().timeLocations()) {
    082                double distance = dayDistance(t1, t2);
    083                if (distance > max) max = distance;
    084            }
    085        return max;
    086    }
    087
    088    @Override
    089    public boolean isApplicable(Lecture l1, Lecture l2) {
    090        return l1 != null && l2 != null && !ignore(l1, l2) && applicable(l1, l2);
    091    }
    092
    093    @Override
    094    public void incJenrl(Assignment<Lecture, Placement> assignment, JenrlConstraint jenrl, double studentWeight, Double conflictPriority, Student student) {
    095        if (isApplicable(student, jenrl.first(), jenrl.second()))
    096            inc(assignment, studentWeight * dayDistance(assignment.getValue(jenrl.first()), assignment.getValue(jenrl.second())));
    097    }
    098
    099    @Override
    100    public double getWeightDefault(DataProperties config) {
    101        return config.getPropertyDouble("Comparator.MinimizeStudentScheduleDaysWeight", 0.05 * config.getPropertyDouble("Comparator.StudentConflictWeight", 1.0));
    102    }
    103
    104    @Override
    105    public String getPlacementSelectionWeightName() {
    106        return "Placement.MinimizeStudentScheduleDaysWeight";
    107    }
    108
    109    @Override
    110    public void getInfo(Assignment<Lecture, Placement> assignment, Map<String, String> info) {
    111        super.getInfo(assignment, info);
    112        double total = 0;
    113        for (JenrlConstraint jenrl: ((TimetableModel)getModel()).getJenrlConstraints()) {
    114            if (!jenrl.isToBeIgnored())
    115                total += jenrl.jenrl();
    116        }
    117        info.put("Student different days", sDoubleFormat.format(getValue(assignment) / total));
    118    }
    119
    120    @Override
    121    public void getInfo(Assignment<Lecture, Placement> assignment, Map<String, String> info, Collection<Lecture> variables) {
    122        super.getInfo(assignment, info, variables);
    123        Set<JenrlConstraint> jenrls = new HashSet<JenrlConstraint>();
    124        double distance = 0;
    125        double total = 0;
    126        for (Lecture lecture: variables) {
    127            for (JenrlConstraint jenrl: lecture.jenrlConstraints())
    128                if (jenrls.add(jenrl) && !jenrl.isToBeIgnored()) {
    129                    distance += jenrl.jenrl() * dayDistance(assignment.getValue(jenrl.first()), assignment.getValue(jenrl.second()));
    130                    total += jenrl.jenrl();
    131                }
    132        }
    133        info.put("Student different days", sDoubleFormat.format(distance / total));
    134    }
    135
    136}