001package org.cpsolver.coursett.criteria; 002 003import java.util.Collection; 004import java.util.HashSet; 005import java.util.Set; 006 007import org.cpsolver.coursett.Constants; 008import org.cpsolver.coursett.constraint.JenrlConstraint; 009import org.cpsolver.coursett.model.Lecture; 010import org.cpsolver.coursett.model.Placement; 011import org.cpsolver.coursett.model.Student; 012import org.cpsolver.coursett.model.TimeLocation; 013import org.cpsolver.coursett.model.TimetableModel; 014import org.cpsolver.ifs.assignment.Assignment; 015import org.cpsolver.ifs.solver.Solver; 016import org.cpsolver.ifs.util.DistanceMetric; 017 018 019/** 020 * Student conflicts. This criterion counts student conflicts between classes. A conflict 021 * occurs when two classes that are attended by the same student (or students) are overlapping 022 * in time or place back-to-back in rooms that are too far a part. The combinations of classes 023 * that share students are maintained by {@link JenrlConstraint}. 024 * <br> 025 * 026 * @version CourseTT 1.3 (University Course Timetabling)<br> 027 * Copyright (C) 2006 - 2014 Tomas Muller<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 StudentConflict extends TimetablingCriterion { 046 protected boolean iIncludeConflicts = false; 047 048 public StudentConflict() { 049 setValueUpdateType(ValueUpdateType.BeforeUnassignedBeforeAssigned); 050 } 051 052 @Override 053 public boolean init(Solver<Lecture, Placement> solver) { 054 iIncludeConflicts = solver.getProperties().getPropertyBoolean("StudentConflict.IncludeConflicts", false); 055 return super.init(solver); 056 } 057 058 @Override 059 public String getPlacementSelectionWeightName() { 060 return null; 061 } 062 063 public DistanceMetric getMetrics() { 064 return (getModel() == null ? null : ((TimetableModel)getModel()).getDistanceMetric()); 065 } 066 067 public static boolean overlaps(Placement p1, Placement p2) { 068 return p1 != null && p2 != null && p1.getTimeLocation().hasIntersection(p2.getTimeLocation()) && (!p1.variable().isCommitted() || !p2.variable().isCommitted()); 069 } 070 071 protected double jointEnrollment(JenrlConstraint jenrl) { 072 return jenrl.jenrl(); 073 } 074 075 public static boolean distance(DistanceMetric m, Placement p1, Placement p2) { 076 if (m == null && p1 != null) m = ((TimetableModel)p1.variable().getModel()).getDistanceMetric(); 077 if (m == null && p2 != null) m = ((TimetableModel)p2.variable().getModel()).getDistanceMetric(); 078 if (p1 == null || p2 == null || m == null) return false; 079 if (p1.variable().isCommitted() && p2.variable().isCommitted()) return false; 080 TimeLocation t1 = p1.getTimeLocation(), t2 = p2.getTimeLocation(); 081 if (!t1.shareDays(t2) || !t1.shareWeeks(t2)) return false; 082 if (m.doComputeDistanceConflictsBetweenNonBTBClasses()) { 083 if (t1.getStartSlot() + t1.getLength() <= t2.getStartSlot()) { 084 return Placement.getDistanceInMinutes(m, p1, p2) > t1.getBreakTime() + Constants.SLOT_LENGTH_MIN * (t2.getStartSlot() - t1.getStartSlot() - t1.getLength()); 085 } else if (t2.getStartSlot() + t2.getLength() <= t1.getStartSlot()) { 086 return Placement.getDistanceInMinutes(m, p1, p2) > t2.getBreakTime() + Constants.SLOT_LENGTH_MIN * (t1.getStartSlot() - t2.getStartSlot() - t2.getLength()); 087 } 088 } else { 089 if (t1.getStartSlot() + t1.getLength() == t2.getStartSlot()) { 090 return Placement.getDistanceInMinutes(m, p1, p2) > t1.getBreakTime(); 091 } else if (t2.getStartSlot() + t2.getLength() == t1.getStartSlot()) { 092 return Placement.getDistanceInMinutes(m, p1, p2) > t2.getBreakTime(); 093 } 094 } 095 return false; 096 } 097 098 public static boolean ignore(Lecture l1, Lecture l2) { 099 return l1 != null && l2 != null && l1.isToIgnoreStudentConflictsWith(l2); 100 } 101 102 public static boolean committed(Lecture l1, Lecture l2) { 103 return l1 != null && l2 != null && (l1.isCommitted() || l2.isCommitted()) && (!l1.isCommitted() || !l2.isCommitted()); 104 } 105 106 public static boolean uncommitted(Lecture l1, Lecture l2) { 107 return l1 != null && l2 != null && !l1.isCommitted() && !l2.isCommitted(); 108 } 109 110 public static boolean applicable(Lecture l1, Lecture l2) { 111 return l1 != null && l2 != null && (!l1.isCommitted() || !l2.isCommitted()); 112 } 113 114 public static boolean hard(Lecture l1, Lecture l2) { 115 return l1 != null && l2 != null && l1.isSingleSection() && l2.isSingleSection() && (!l1.isCommitted() || !l2.isCommitted()); 116 } 117 118 public boolean isApplicable(Lecture l1, Lecture l2) { 119 return l1 != null && l2 != null && !ignore(l1, l2) && uncommitted(l1, l2); // exclude committed and outside student conflicts 120 } 121 public boolean inConflict(Placement p1, Placement p2) { 122 return overlaps(p1, p2) || distance(getMetrics(), p1, p2); 123 } 124 125 @Override 126 public double getValue(Assignment<Lecture, Placement> assignment, Placement value, Set<Placement> conflicts) { 127 double ret = 0.0; 128 for (JenrlConstraint jenrl: value.variable().jenrlConstraints()) { 129 Lecture other = jenrl.another(value.variable()); 130 if (!isApplicable(value.variable(), other)) continue; 131 Placement another = assignment.getValue(other); 132 if (another == null) continue; 133 if (conflicts != null && conflicts.contains(another)) continue; 134 if (inConflict(value, another)) 135 ret += jointEnrollment(jenrl); 136 } 137 if (iIncludeConflicts && conflicts != null) 138 for (Placement conflict: conflicts) { 139 for (JenrlConstraint jenrl: conflict.variable().jenrlConstraints()) { 140 Lecture other = jenrl.another(conflict.variable()); 141 if (!isApplicable(conflict.variable(), other)) continue; 142 Placement another = assignment.getValue(other); 143 if (another == null || another.variable().equals(value.variable())) continue; 144 if (conflicts != null && conflicts.contains(another)) continue; 145 if (inConflict(conflict, another)) 146 ret -= jointEnrollment(jenrl); 147 } 148 } 149 return ret; 150 } 151 152 @Override 153 public double getValue(Assignment<Lecture, Placement> assignment, Collection<Lecture> variables) { 154 double ret = 0.0; 155 Set<JenrlConstraint> constraints = new HashSet<JenrlConstraint>(); 156 for (Lecture lect: variables) { 157 Placement plac = assignment.getValue(lect); 158 if (plac == null) continue; 159 for (JenrlConstraint jenrl: lect.jenrlConstraints()) { 160 if (!constraints.add(jenrl)) continue; 161 Lecture other = jenrl.another(lect); 162 if (!other.isCommitted() && !variables.contains(other)) continue; 163 if (!isApplicable(lect, other)) continue; 164 if (inConflict(plac, assignment.getValue(other))) 165 ret += jointEnrollment(jenrl); 166 } 167 } 168 return ret; 169 } 170 171 @Override 172 public double[] getBounds(Assignment<Lecture, Placement> assignment) { 173 double[] bounds = { 0.0, 0.0 }; 174 for (JenrlConstraint jenrl: ((TimetableModel)getModel()).getJenrlConstraints()) 175 if (isApplicable(jenrl.first(), jenrl.second())) 176 bounds[0] += jointEnrollment(jenrl); 177 return bounds; 178 } 179 180 @Override 181 public double[] getBounds(Assignment<Lecture, Placement> assignment, Collection<Lecture> variables) { 182 double[] bounds = { 0.0, 0.0 }; 183 Set<JenrlConstraint> constraints = new HashSet<JenrlConstraint>(); 184 for (Lecture lect: variables) { 185 if (assignment.getValue(lect) == null) continue; 186 for (JenrlConstraint jenrl: lect.jenrlConstraints()) { 187 if (isApplicable(jenrl.first(), jenrl.second()) && constraints.add(jenrl) && (jenrl.another(lect).isCommitted() || variables.contains(jenrl.another(lect)))) 188 bounds[0] += jointEnrollment(jenrl); 189 } 190 } 191 return bounds; 192 } 193 194 public void incJenrl(Assignment<Lecture, Placement> assignment, JenrlConstraint jenrl, double studentWeight, Double conflictPriority, Student student) { 195 if (isApplicable(jenrl.first(), jenrl.second()) && inConflict(assignment.getValue(jenrl.first()), assignment.getValue(jenrl.second()))) 196 super.inc(assignment, studentWeight); 197 } 198 199 @Override 200 public void bestRestored(Assignment<Lecture, Placement> assignment) { 201 super.bestRestored(assignment); 202 getContext(assignment).setTotal(getValue(assignment, getModel().variables())); 203 } 204}