    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 holes 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 the same day that are not back-to-back. The penalisation
    022 * is based on the time distance between the two classes, computed in hours.
    023 * These penalties are weighted by Comparator.MinimizeStudentScheduleHolesWeight,
    024 * which defaults to 1/20 of the Comparator.StudentConflictWeight.
    025 *
    026 * <br>
    027 *
    028 * @author  Tomáš Müller
    029 * @version CourseTT 1.3 (University Course Timetabling)<br>
    030 *          Copyright (C) 2006 - 2014 Tomáš Müller<br>
    031 *          <a href="mailto:muller@unitime.org">muller@unitime.org</a><br>
    032 *          <a href="http://muller.unitime.org">http://muller.unitime.org</a><br>
    033 * <br>
    034 *          This library is free software; you can redistribute it and/or modify
    035 *          it under the terms of the GNU Lesser General Public License as
    036 *          published by the Free Software Foundation; either version 3 of the
    037 *          License, or (at your option) any later version. <br>
    038 * <br>
    039 *          This library is distributed in the hope that it will be useful, but
    040 *          WITHOUT ANY WARRANTY; without even the implied warranty of
    041 *          MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
    042 *          Lesser General Public License for more details. <br>
    043 * <br>
    044 *          You should have received a copy of the GNU Lesser General Public
    045 *          License along with this library; if not see
    046 *          <a href='http://www.gnu.org/licenses/'>http://www.gnu.org/licenses/</a>.
    047 */
    048public class StudentMinimizeScheduleHoles extends StudentConflict {
    049
    050    /**
    051     * Time distance between two placements in hours. The classes must be on the same days and weeks,
    052     * but not overlapping in time. If the two classes are taught on multiple days during the week, the
    053     * distance is also multiplied by the number of shared days of week.
    054     * @param p1 first placement
    055     * @param p2 second placement
    056     * @return distance between the two classes in hours
    057     */
    058    public static double btbDistance(Placement p1, Placement p2) {
    059        if (p1 == null || p2 == null) return 0.0;
    060        return btbDistance(p1.getTimeLocation(), p2.getTimeLocation());
    061    }
    062
    063    private static double btbDistance(TimeLocation t1, TimeLocation t2) {
    064        if (!t1.shareDays(t2) || !t1.shareWeeks(t2) || t1.shareHours(t2)) return 0.0;
    065        int s1 = t1.getStartSlot(), e1 = s1 + t1.getLength();
    066        int s2 = t2.getStartSlot(), e2 = s2 + t2.getLength();
    067        if (e1 < s2) {
    068            return t1.nrSharedDays(t2) * (s2 - e1) / 12.0;
    069        } else if (e2 < s1) {
    070            return t1.nrSharedDays(t2) * (s1 - e2) / 12.0;
    071        }
    072        return 0.0;
    073    }
    074
    075    @Override
    076    public boolean inConflict(Placement p1, Placement p2) {
    077        return btbDistance(p1, p2) > 0.0;
    078    }
    079
    080    @Override
    081    protected double jointEnrollment(JenrlConstraint jenrl, Placement p1, Placement p2) {
    082        return btbDistance(p1, p2) * jenrl.jenrl();
    083    }
    084
    085    @Override
    086    protected double jointEnrollment(JenrlConstraint jenrl) {
    087        double max = 0;
    088        for (TimeLocation t1: jenrl.first().timeLocations())
    089            for (TimeLocation t2: jenrl.second().timeLocations()) {
    090                double distance = btbDistance(t1, t2);
    091                if (distance > max) max = distance;
    092            }
    093        return max;
    094    }
    095
    096    @Override
    097    public boolean isApplicable(Lecture l1, Lecture l2) {
    098        return l1 != null && l2 != null && !ignore(l1, l2) && applicable(l1, l2);
    099    }
    100
    101    @Override
    102    public void incJenrl(Assignment<Lecture, Placement> assignment, JenrlConstraint jenrl, double studentWeight, Double conflictPriority, Student student) {
    103        if (isApplicable(student, jenrl.first(), jenrl.second()))
    104            inc(assignment, studentWeight * btbDistance(assignment.getValue(jenrl.first()), assignment.getValue(jenrl.second())));
    105    }
    106
    107    @Override
    108    public double getWeightDefault(DataProperties config) {
    109        return config.getPropertyDouble("Comparator.MinimizeStudentScheduleHolesWeight", 0.05 * config.getPropertyDouble("Comparator.StudentConflictWeight", 1.0));
    110    }
    111
    112    @Override
    113    public String getPlacementSelectionWeightName() {
    114        return "Placement.MinimizeStudentScheduleHolesWeight";
    115    }
    116
    117    @Override
    118    public void getInfo(Assignment<Lecture, Placement> assignment, Map<String, String> info) {
    119        super.getInfo(assignment, info);
    120        double total = 0;
    121        for (JenrlConstraint jenrl: ((TimetableModel)getModel()).getJenrlConstraints()) {
    122            if (!jenrl.isToBeIgnored())
    123                total += jenrl.jenrl();
    124        }
    125        info.put("Student class distance", sDoubleFormat.format(60.0 * getValue(assignment) / total) + " minutes");
    126    }
    127
    128    @Override
    129    public void getInfo(Assignment<Lecture, Placement> assignment, Map<String, String> info, Collection<Lecture> variables) {
    130        super.getInfo(assignment, info, variables);
    131        Set<JenrlConstraint> jenrls = new HashSet<JenrlConstraint>();
    132        double distance = 0;
    133        double total = 0;
    134        for (Lecture lecture: variables) {
    135            for (JenrlConstraint jenrl: lecture.jenrlConstraints())
    136                if (jenrls.add(jenrl) && !jenrl.isToBeIgnored()) {
    137                    distance += jenrl.jenrl() * btbDistance(assignment.getValue(jenrl.first()), assignment.getValue(jenrl.second()));
    138                    total += jenrl.jenrl();
    139                }
    140        }
    141        info.put("Student class distance", sDoubleFormat.format(60.0 * distance / total) + " minutes");
    142    }
    143
    144}