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