001package org.cpsolver.coursett.model; 002 003import java.util.Collection; 004import java.util.HashSet; 005import java.util.HashMap; 006import java.util.Map; 007import java.util.Set; 008import java.util.TreeSet; 009 010import org.cpsolver.coursett.constraint.InstructorConstraint; 011import org.cpsolver.coursett.constraint.JenrlConstraint; 012 013 014/** 015 * Student. 016 * 017 * @author Tomáš Müller 018 * @version CourseTT 1.3 (University Course Timetabling)<br> 019 * Copyright (C) 2006 - 2014 Tomáš Müller<br> 020 * <a href="mailto:muller@unitime.org">muller@unitime.org</a><br> 021 * <a href="http://muller.unitime.org">http://muller.unitime.org</a><br> 022 * <br> 023 * This library is free software; you can redistribute it and/or modify 024 * it under the terms of the GNU Lesser General Public License as 025 * published by the Free Software Foundation; either version 3 of the 026 * License, or (at your option) any later version. <br> 027 * <br> 028 * This library is distributed in the hope that it will be useful, but 029 * WITHOUT ANY WARRANTY; without even the implied warranty of 030 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 031 * Lesser General Public License for more details. <br> 032 * <br> 033 * You should have received a copy of the GNU Lesser General Public 034 * License along with this library; if not see 035 * <a href='http://www.gnu.org/licenses/'>http://www.gnu.org/licenses/</a>. 036 */ 037public class Student implements Comparable<Student> { 038 private static org.apache.logging.log4j.Logger sLogger = org.apache.logging.log4j.LogManager.getLogger(Student.class); 039 public static boolean USE_DISTANCE_CACHE = false; 040 Long iStudentId = null; 041 HashMap<Long, Double> iOfferings = new HashMap<Long, Double>(); 042 Set<Lecture> iLectures = new HashSet<Lecture>(); 043 Set<Configuration> iConfigurations = new HashSet<Configuration>(); 044 HashMap<Long, Set<Lecture>> iCanNotEnrollSections = null; 045 HashMap<Student, Double> iDistanceCache = null; 046 HashSet<Placement> iCommitedPlacements = null; 047 private String iAcademicArea = null, iAcademicClassification = null, iMajor = null, iCurriculum = null; 048 HashMap<Long, Double> iOfferingPriority = new HashMap<Long, Double>(); 049 private InstructorConstraint iInstructor = null; 050 private Set<StudentGroup> iGroups = new HashSet<StudentGroup>(); 051 private Map<Long, Set<Long>> iAlternatives = null; 052 053 public Student(Long studentId) { 054 iStudentId = studentId; 055 } 056 057 public void addOffering(Long offeringId, double weight, Double priority) { 058 iOfferings.put(offeringId, weight); 059 if (priority != null) iOfferingPriority.put(offeringId, priority); 060 } 061 062 public void addOffering(Long offeringId, double weight) { 063 addOffering(offeringId, weight, null); 064 } 065 066 public Map<Long, Double> getOfferingsMap() { 067 return iOfferings; 068 } 069 070 public Set<Long> getOfferings() { 071 return iOfferings.keySet(); 072 } 073 074 public boolean hasOffering(Long offeringId) { 075 return iOfferings.containsKey(offeringId); 076 } 077 078 public InstructorConstraint getInstructor() { return iInstructor; } 079 080 public void setInstructor(InstructorConstraint instructor) { iInstructor = instructor; } 081 082 /** 083 * Priority of an offering (for the student). Null if not used, or between 084 * zero (no priority) and one (highest priority) 085 * @param offeringId instructional offering unique id 086 * @return student's priority 087 */ 088 public Double getPriority(Long offeringId) { 089 return offeringId == null ? null : iOfferingPriority.get(offeringId); 090 } 091 092 public Double getPriority(Configuration configuration) { 093 return configuration == null ? null : getPriority(configuration.getOfferingId()); 094 } 095 096 public Double getPriority(Lecture lecture) { 097 return lecture == null ? null : getPriority(lecture.getConfiguration()); 098 } 099 100 public Double getConflictingPriorty(Lecture l1, Lecture l2) { 101 // Conflicting priority is the lower of the two priorities 102 Double p1 = getPriority(l1); 103 Double p2 = getPriority(l2); 104 return p1 == null ? null : p2 == null ? null : Math.min(p1, p2); 105 } 106 107 public double getOfferingWeight(Configuration configuration) { 108 if (configuration == null) 109 return 1.0; 110 return getOfferingWeight(configuration.getOfferingId()); 111 } 112 113 public double getOfferingWeight(Long offeringId) { 114 Double weight = iOfferings.get(offeringId); 115 return (weight == null ? 0.0 : weight.doubleValue()); 116 } 117 118 public boolean canUnenroll(Lecture lecture) { 119 if (getInstructor() != null) 120 return !getInstructor().variables().contains(lecture); 121 return true; 122 } 123 124 public boolean canEnroll(Lecture lecture) { 125 if (iCanNotEnrollSections != null) { 126 Set<Lecture> canNotEnrollLectures = iCanNotEnrollSections.get(lecture.getConfiguration().getOfferingId()); 127 return canEnroll(canNotEnrollLectures, lecture, true); 128 } 129 return true; 130 } 131 132 private boolean canEnroll(Set<Lecture> canNotEnrollLectures, Lecture lecture, boolean checkParents) { 133 if (canNotEnrollLectures == null) 134 return true; 135 if (canNotEnrollLectures.contains(lecture)) 136 return false; 137 if (checkParents) { 138 Lecture parent = lecture.getParent(); 139 while (parent != null) { 140 if (canNotEnrollLectures.contains(parent)) 141 return false; 142 parent = parent.getParent(); 143 } 144 } 145 if (lecture.hasAnyChildren()) { 146 for (Long subpartId: lecture.getChildrenSubpartIds()) { 147 boolean canEnrollChild = false; 148 for (Lecture childLecture : lecture.getChildren(subpartId)) { 149 if (canEnroll(canNotEnrollLectures, childLecture, false)) { 150 canEnrollChild = true; 151 break; 152 } 153 } 154 if (!canEnrollChild) 155 return false; 156 } 157 } 158 return true; 159 } 160 161 public void addCanNotEnroll(Lecture lecture) { 162 if (iCanNotEnrollSections == null) 163 iCanNotEnrollSections = new HashMap<Long, Set<Lecture>>(); 164 if (lecture.getConfiguration() == null) { 165 sLogger.warn("Student.addCanNotEnroll(" + lecture 166 + ") -- given lecture has no configuration associated with."); 167 return; 168 } 169 Set<Lecture> canNotEnrollLectures = iCanNotEnrollSections.get(lecture.getConfiguration().getOfferingId()); 170 if (canNotEnrollLectures == null) { 171 canNotEnrollLectures = new HashSet<Lecture>(); 172 iCanNotEnrollSections.put(lecture.getConfiguration().getOfferingId(), canNotEnrollLectures); 173 } 174 canNotEnrollLectures.add(lecture); 175 } 176 177 public void addCanNotEnroll(Long offeringId, Collection<Lecture> lectures) { 178 if (lectures == null || lectures.isEmpty()) 179 return; 180 if (iCanNotEnrollSections == null) 181 iCanNotEnrollSections = new HashMap<Long, Set<Lecture>>(); 182 Set<Lecture> canNotEnrollLectures = iCanNotEnrollSections.get(offeringId); 183 if (canNotEnrollLectures == null) { 184 canNotEnrollLectures = new HashSet<Lecture>(); 185 iCanNotEnrollSections.put(offeringId, canNotEnrollLectures); 186 } 187 canNotEnrollLectures.addAll(lectures); 188 } 189 190 public Map<Long, Set<Lecture>> canNotEnrollSections() { 191 return iCanNotEnrollSections; 192 } 193 194 public void addLecture(Lecture lecture) { 195 iLectures.add(lecture); 196 } 197 198 public void removeLecture(Lecture lecture) { 199 iLectures.remove(lecture); 200 } 201 202 public Set<Lecture> getLectures() { 203 return iLectures; 204 } 205 206 public void addConfiguration(Configuration config) { 207 if (config != null) iConfigurations.add(config); 208 } 209 210 public void removeConfiguration(Configuration config) { 211 if (config != null) iConfigurations.remove(config); 212 } 213 214 public Set<Configuration> getConfigurations() { 215 return iConfigurations; 216 } 217 218 public Long getId() { 219 return iStudentId; 220 } 221 222 public double getDistance(Student student) { 223 Double dist = (USE_DISTANCE_CACHE && iDistanceCache != null ? iDistanceCache.get(student) : null); 224 if (dist == null) { 225 if (!getGroups().isEmpty() || !student.getGroups().isEmpty()) { 226 double total = 0.0f; 227 double same = 0.0; 228 for (StudentGroup g: getGroups()) { 229 total += g.getWeight(); 230 if (student.hasGroup(g)) 231 same += g.getWeight(); 232 } 233 for (StudentGroup g: student.getGroups()) { 234 total += g.getWeight(); 235 } 236 dist = (total - 2*same) / total; 237 } else { 238 int same = 0; 239 for (Long o : getOfferings()) { 240 if (student.getOfferings().contains(o)) 241 same++; 242 } 243 double all = student.getOfferings().size() + getOfferings().size(); 244 double dif = all - 2.0 * same; 245 dist = Double.valueOf(dif / all); 246 } 247 if (USE_DISTANCE_CACHE) { 248 if (iDistanceCache == null) 249 iDistanceCache = new HashMap<Student, Double>(); 250 iDistanceCache.put(student, dist); 251 } 252 } 253 return dist.doubleValue(); 254 } 255 256 public void clearDistanceCache() { 257 if (USE_DISTANCE_CACHE && iDistanceCache != null) 258 iDistanceCache.clear(); 259 } 260 261 @Override 262 public String toString() { 263 return String.valueOf(getId()); 264 } 265 266 @Override 267 public int hashCode() { 268 return getId().hashCode(); 269 } 270 271 @Override 272 public int compareTo(Student s) { 273 return getId().compareTo(s.getId()); 274 } 275 276 @Override 277 public boolean equals(Object o) { 278 if (o == null || !(o instanceof Student)) 279 return false; 280 return getId().equals(((Student) o).getId()); 281 } 282 283 public void addCommitedPlacement(Placement placement) { 284 if (iCommitedPlacements == null) 285 iCommitedPlacements = new HashSet<Placement>(); 286 iCommitedPlacements.add(placement); 287 } 288 289 public Set<Placement> getCommitedPlacements() { 290 return iCommitedPlacements; 291 } 292 293 public Set<Placement> conflictPlacements(Placement placement) { 294 if (iCommitedPlacements == null) 295 return null; 296 Set<Placement> ret = new HashSet<Placement>(); 297 Lecture lecture = placement.variable(); 298 for (Placement commitedPlacement : iCommitedPlacements) { 299 Lecture commitedLecture = commitedPlacement.variable(); 300 if (lecture.getSchedulingSubpartId() != null 301 && lecture.getSchedulingSubpartId().equals(commitedLecture.getSchedulingSubpartId())) 302 continue; 303 if (lecture.isToIgnoreStudentConflictsWith(commitedLecture)) continue; 304 if (JenrlConstraint.isInConflict(commitedPlacement, placement, ((TimetableModel)placement.variable().getModel()).getDistanceMetric(), 305 ((TimetableModel)placement.variable().getModel()).getStudentWorkDayLimit())) 306 ret.add(commitedPlacement); 307 } 308 return ret; 309 } 310 311 public int countConflictPlacements(Placement placement) { 312 Set<Placement> conflicts = conflictPlacements(placement); 313 double w = getOfferingWeight((placement.variable()).getConfiguration()); 314 return (int) Math.round(conflicts == null ? 0 : avg(w, 1.0) * conflicts.size()); 315 } 316 317 public double getJenrlWeight(Lecture l1, Lecture l2) { 318 if (getInstructor() != null && (getInstructor().variables().contains(l1) || getInstructor().variables().contains(l2))) 319 return 1.0; 320 if (iAlternatives != null && areAlternatives(l1, l2)) return 0.0; 321 return avg(getOfferingWeight(l1.getConfiguration()), getOfferingWeight(l2.getConfiguration())); 322 } 323 324 public void addAlternatives(Long offeringId1, Long offeringId2) { 325 if (offeringId1 == null || offeringId2 == null) return; 326 if (iAlternatives == null) 327 iAlternatives = new HashMap<Long, Set<Long>>(); 328 Set<Long> alts = iAlternatives.get(offeringId1); 329 if (alts == null) { 330 alts = new HashSet<Long>(); 331 alts.add(offeringId1); 332 iAlternatives.put(offeringId1, alts); 333 } 334 Set<Long> other = iAlternatives.get(offeringId2); 335 if (other != null) { 336 for (Long id: other) 337 iAlternatives.put(id, alts); 338 alts.addAll(other); 339 } else { 340 alts.add(offeringId2); 341 iAlternatives.put(offeringId2, alts); 342 } 343 } 344 345 public boolean areAlternatives(Lecture l1, Lecture l2) { 346 if (l1 == null || l2 == null) return false; 347 return areAlternatives(l1.getConfiguration(), l2.getConfiguration()); 348 } 349 350 public boolean areAlternatives(Configuration c1, Configuration c2) { 351 if (c1 == null || c2 == null) return false; 352 return areAlternatives(c1.getOfferingId(), c2.getOfferingId()); 353 } 354 355 public boolean areAlternatives(Long offeringId1, Long offeringId2) { 356 if (iAlternatives == null || offeringId1 == null || offeringId2 == null) return false; 357 Set<Long> alts = iAlternatives.get(offeringId1); 358 if (alts != null && alts.contains(offeringId2)) return true; 359 return false; 360 } 361 362 public Long getAlternative(Long offeringId) { 363 if (iAlternatives == null || offeringId == null) return null; 364 Set<Long> alternatives = iAlternatives.get(offeringId); 365 if (alternatives == null) return null; 366 Long bestId = null; double bestW = 0.0; 367 for (Long altId: alternatives) { 368 double w = getOfferingWeight(altId); 369 if (bestId == null || w > bestW || (bestW == w && altId < bestId)) { 370 bestId = altId; bestW = w; 371 } 372 } 373 return bestId == null || bestId.equals(offeringId) ? null : bestId; 374 } 375 376 public double avg(double w1, double w2) { 377 return Math.sqrt(w1 * w2); 378 } 379 380 public String getAcademicArea() { 381 return iAcademicArea; 382 } 383 384 public void setAcademicArea(String acadArea) { 385 iAcademicArea = acadArea; 386 } 387 388 public String getAcademicClassification() { 389 return iAcademicClassification; 390 } 391 392 public void setAcademicClassification(String acadClasf) { 393 iAcademicClassification = acadClasf; 394 } 395 396 public String getMajor() { 397 return iMajor; 398 } 399 400 public void setMajor(String major) { 401 iMajor = major; 402 } 403 404 public String getCurriculum() { 405 return iCurriculum; 406 } 407 408 public void setCurriculum(String curriculum) { 409 iCurriculum = curriculum; 410 } 411 412 public void addGroup(StudentGroup group) { 413 iGroups.add(group); 414 } 415 416 public Set<StudentGroup> getGroups() { return iGroups; } 417 418 public boolean hasGroup(StudentGroup group) { return iGroups.contains(group); } 419 420 public String getGroupNames() { 421 if (iGroups.isEmpty()) return ""; 422 if (iGroups.size() == 1) return iGroups.iterator().next().getName(); 423 String ret = ""; 424 for (StudentGroup g: new TreeSet<StudentGroup>(iGroups)) 425 ret += (ret.isEmpty() ? "" : ", ") + g.getName(); 426 return ret; 427 } 428 429 public double getSameGroupWeight(Student other) { 430 double ret = 0.0; 431 for (StudentGroup group: iGroups) 432 if (other.hasGroup(group) && group.getWeight() > ret) ret = group.getWeight(); 433 return ret; 434 } 435}