001 package net.sf.cpsolver.coursett; 002 003 import java.io.File; 004 import java.text.SimpleDateFormat; 005 import java.util.ArrayList; 006 import java.util.BitSet; 007 import java.util.Calendar; 008 import java.util.Date; 009 import java.util.HashSet; 010 import java.util.HashMap; 011 import java.util.Hashtable; 012 import java.util.Iterator; 013 import java.util.List; 014 import java.util.Locale; 015 import java.util.Map; 016 import java.util.Set; 017 018 import net.sf.cpsolver.coursett.constraint.ClassLimitConstraint; 019 import net.sf.cpsolver.coursett.constraint.DepartmentSpreadConstraint; 020 import net.sf.cpsolver.coursett.constraint.DiscouragedRoomConstraint; 021 import net.sf.cpsolver.coursett.constraint.GroupConstraint; 022 import net.sf.cpsolver.coursett.constraint.InstructorConstraint; 023 import net.sf.cpsolver.coursett.constraint.JenrlConstraint; 024 import net.sf.cpsolver.coursett.constraint.MinimizeNumberOfUsedGroupsOfTime; 025 import net.sf.cpsolver.coursett.constraint.MinimizeNumberOfUsedRoomsConstraint; 026 import net.sf.cpsolver.coursett.constraint.RoomConstraint; 027 import net.sf.cpsolver.coursett.constraint.SpreadConstraint; 028 import net.sf.cpsolver.coursett.model.Configuration; 029 import net.sf.cpsolver.coursett.model.InitialSectioning; 030 import net.sf.cpsolver.coursett.model.Lecture; 031 import net.sf.cpsolver.coursett.model.Placement; 032 import net.sf.cpsolver.coursett.model.RoomLocation; 033 import net.sf.cpsolver.coursett.model.RoomSharingModel; 034 import net.sf.cpsolver.coursett.model.Student; 035 import net.sf.cpsolver.coursett.model.TimeLocation; 036 import net.sf.cpsolver.coursett.model.TimetableModel; 037 import net.sf.cpsolver.ifs.model.Constraint; 038 import net.sf.cpsolver.ifs.solution.Solution; 039 import net.sf.cpsolver.ifs.solver.Solver; 040 import net.sf.cpsolver.ifs.util.Progress; 041 import net.sf.cpsolver.ifs.util.ToolBox; 042 043 import org.dom4j.Document; 044 import org.dom4j.Element; 045 import org.dom4j.io.SAXReader; 046 047 /** 048 * This class loads the input model from XML file. <br> 049 * <br> 050 * Parameters: 051 * <table border='1'> 052 * <tr> 053 * <th>Parameter</th> 054 * <th>Type</th> 055 * <th>Comment</th> 056 * </tr> 057 * <tr> 058 * <td>General.Input</td> 059 * <td>{@link String}</td> 060 * <td>Input XML file</td> 061 * </tr> 062 * <tr> 063 * <td>General.DeptBalancing</td> 064 * <td>{@link Boolean}</td> 065 * <td>Use {@link DepartmentSpreadConstraint}</td> 066 * </tr> 067 * <tr> 068 * <td>General.InteractiveMode</td> 069 * <td>{@link Boolean}</td> 070 * <td>Interactive mode (see {@link Lecture#purgeInvalidValues(boolean)})</td> 071 * </tr> 072 * <tr> 073 * <td>General.ForcedPerturbances</td> 074 * <td>{@link Integer}</td> 075 * <td>For testing of MPP: number of input perturbations, i.e., classes with 076 * prohibited intial assignment</td> 077 * </tr> 078 * <tr> 079 * <td>General.UseDistanceConstraints</td> 080 * <td>{@link Boolean}</td> 081 * <td>Consider distances between buildings</td> 082 * </tr> 083 * </table> 084 * 085 * @version CourseTT 1.2 (University Course Timetabling)<br> 086 * Copyright (C) 2006 - 2010 Tomas Muller<br> 087 * <a href="mailto:muller@unitime.org">muller@unitime.org</a><br> 088 * <a href="http://muller.unitime.org">http://muller.unitime.org</a><br> 089 * <br> 090 * This library is free software; you can redistribute it and/or modify 091 * it under the terms of the GNU Lesser General Public License as 092 * published by the Free Software Foundation; either version 3 of the 093 * License, or (at your option) any later version. <br> 094 * <br> 095 * This library is distributed in the hope that it will be useful, but 096 * WITHOUT ANY WARRANTY; without even the implied warranty of 097 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 098 * Lesser General Public License for more details. <br> 099 * <br> 100 * You should have received a copy of the GNU Lesser General Public 101 * License along with this library; if not see 102 * <a href='http://www.gnu.org/licenses/'>http://www.gnu.org/licenses/</a>. 103 */ 104 105 public class TimetableXMLLoader extends TimetableLoader { 106 private static org.apache.log4j.Logger sLogger = org.apache.log4j.Logger.getLogger(TimetableXMLLoader.class); 107 private static SimpleDateFormat sDF = new SimpleDateFormat("MM/dd"); 108 109 private boolean iDeptBalancing = true; 110 private int iForcedPerturbances = 0; 111 112 private boolean iInteractiveMode = false; 113 private File iInputFile; 114 115 private Progress iProgress = null; 116 117 public TimetableXMLLoader(TimetableModel model) { 118 super(model); 119 iProgress = Progress.getInstance(getModel()); 120 iInputFile = new File(getModel().getProperties().getProperty("General.Input", 121 "." + File.separator + "solution.xml")); 122 iForcedPerturbances = getModel().getProperties().getPropertyInt("General.ForcedPerturbances", 0); 123 iDeptBalancing = getModel().getProperties().getPropertyBoolean("General.DeptBalancing", true); 124 iInteractiveMode = getModel().getProperties().getPropertyBoolean("General.InteractiveMode", iInteractiveMode); 125 } 126 127 private Solver<Lecture, Placement> iSolver = null; 128 129 public void setSolver(Solver<Lecture, Placement> solver) { 130 iSolver = solver; 131 } 132 133 public Solver<Lecture, Placement> getSolver() { 134 return iSolver; 135 } 136 137 public void setInputFile(File inputFile) { 138 iInputFile = inputFile; 139 } 140 141 @Override 142 public void load() throws Exception { 143 load(null); 144 } 145 146 public void load(Solution<Lecture, Placement> currentSolution) throws Exception { 147 sLogger.debug("Reading XML data from " + iInputFile); 148 iProgress.setPhase("Reading " + iInputFile.getName() + " ..."); 149 150 Document document = (new SAXReader()).read(iInputFile); 151 Element root = document.getRootElement(); 152 sLogger.debug("Root element: " + root.getName()); 153 if (!"llrt".equals(root.getName()) && !"timetable".equals(root.getName())) { 154 sLogger.error("Given XML file is not large lecture room timetabling problem."); 155 return; 156 } 157 158 iProgress.load(root, true); 159 iProgress.message(Progress.MSGLEVEL_STAGE, "Restoring from backup ..."); 160 161 if (root.element("input") != null) 162 root = root.element("input"); 163 164 if (root.attributeValue("term") != null) 165 getModel().getProperties().setProperty("Data.Term", root.attributeValue("term")); 166 if (root.attributeValue("year") != null) 167 getModel().setYear(Integer.parseInt(root.attributeValue("year"))); 168 else if (root.attributeValue("term") != null) 169 getModel().setYear(Integer.parseInt(root.attributeValue("term").substring(0, 4))); 170 if (root.attributeValue("initiative") != null) 171 getModel().getProperties().setProperty("Data.Initiative", root.attributeValue("initiative")); 172 if (root.attributeValue("semester") != null && root.attributeValue("year") != null) 173 getModel().getProperties().setProperty("Data.Term", 174 root.attributeValue("semester") + root.attributeValue("year")); 175 if (root.attributeValue("session") != null) 176 getModel().getProperties().setProperty("General.SessionId", root.attributeValue("session")); 177 if (root.attributeValue("solverGroup") != null) 178 getModel().getProperties().setProperty("General.SolverGroupId", root.attributeValue("solverGroup")); 179 String version = root.attributeValue("version"); 180 181 // Student sectioning considers the whole course (including committed classes), since 2.5 182 boolean sectionWholeCourse = true; 183 184 if (version != null && version.indexOf('.') >= 0) { 185 int majorVersion = Integer.parseInt(version.substring(0, version.indexOf('.'))); 186 int minorVersion = Integer.parseInt(version.substring(1 + version.indexOf('.'))); 187 188 sectionWholeCourse = (majorVersion == 2 && minorVersion >= 5) || majorVersion > 2; 189 } 190 191 HashMap<Long, TimeLocation> perts = new HashMap<Long, TimeLocation>(); 192 if (getModel().getProperties().getPropertyInt("MPP.TimePert", 0) > 0) { 193 int nrChanges = getModel().getProperties().getPropertyInt("MPP.TimePert", 0); 194 int idx = 0; 195 for (Iterator<?> i = root.element("perturbations").elementIterator("class"); i.hasNext() && idx < nrChanges; idx++) { 196 Element pertEl = (Element) i.next(); 197 Long classId = Long.valueOf(pertEl.attributeValue("id")); 198 TimeLocation tl = new TimeLocation(Integer.parseInt(pertEl.attributeValue("days"), 2), Integer 199 .parseInt(pertEl.attributeValue("start")), Integer.parseInt(pertEl.attributeValue("length")), 200 0, 0.0, 0, null, null, null, 0); 201 perts.put(classId, tl); 202 } 203 } 204 205 iProgress.setPhase("Creating rooms ...", root.element("rooms").elements("room").size()); 206 HashMap<String, Element> roomElements = new HashMap<String, Element>(); 207 HashMap<String, RoomConstraint> roomConstraints = new HashMap<String, RoomConstraint>(); 208 HashMap<Long, List<Lecture>> sameLectures = new HashMap<Long, List<Lecture>>(); 209 for (Iterator<?> i = root.element("rooms").elementIterator("room"); i.hasNext();) { 210 Element roomEl = (Element) i.next(); 211 iProgress.incProgress(); 212 roomElements.put(roomEl.attributeValue("id"), roomEl); 213 if ("false".equals(roomEl.attributeValue("constraint"))) 214 continue; 215 RoomSharingModel sharingModel = null; 216 Element sharingEl = roomEl.element("sharing"); 217 if (sharingEl != null) { 218 Character freeForAllPrefChar = null; 219 Element freeForAllEl = sharingEl.element("freeForAll"); 220 if (freeForAllEl != null) 221 freeForAllPrefChar = freeForAllEl.attributeValue("value", "F").charAt(0); 222 Character notAvailablePrefChar = null; 223 Element notAvailableEl = sharingEl.element("notAvailable"); 224 if (notAvailableEl != null) 225 notAvailablePrefChar = notAvailableEl.attributeValue("value", "X").charAt(0); 226 String pattern = sharingEl.element("pattern").getText(); 227 java.util.List<?> depts = sharingEl.elements("department"); 228 Long departmentIds[] = new Long[depts.size()]; 229 for (int j = 0; j < departmentIds.length; j++) 230 departmentIds[j] = Long.valueOf(((Element) depts.get(j)).attributeValue("id")); 231 sharingModel = new RoomSharingModel(departmentIds, pattern, freeForAllPrefChar, notAvailablePrefChar); 232 } 233 boolean ignoreTooFar = false; 234 if ("true".equals(roomEl.attributeValue("ignoreTooFar"))) 235 ignoreTooFar = true; 236 boolean fake = false; 237 if ("true".equals(roomEl.attributeValue("fake"))) 238 fake = true; 239 Double posX = null, posY = null; 240 if (roomEl.attributeValue("location") != null) { 241 String loc = roomEl.attributeValue("location"); 242 posX = Double.valueOf(loc.substring(0, loc.indexOf(','))); 243 posY = Double.valueOf(loc.substring(loc.indexOf(',') + 1)); 244 } 245 boolean discouraged = "true".equals(roomEl.attributeValue("discouraged")); 246 RoomConstraint constraint = (discouraged ? new DiscouragedRoomConstraint( 247 getModel().getProperties(), 248 Long.valueOf(roomEl.attributeValue("id")), 249 (roomEl.attributeValue("name") != null ? roomEl.attributeValue("name") : "r" 250 + roomEl.attributeValue("id")), 251 (roomEl.attributeValue("building") == null ? null : Long.valueOf(roomEl.attributeValue("building"))), 252 Integer.parseInt(roomEl.attributeValue("capacity")), sharingModel, posX, posY, ignoreTooFar, !fake) 253 : new RoomConstraint(Long.valueOf(roomEl.attributeValue("id")), 254 (roomEl.attributeValue("name") != null ? roomEl.attributeValue("name") : "r" 255 + roomEl.attributeValue("id")), (roomEl.attributeValue("building") == null ? null 256 : Long.valueOf(roomEl.attributeValue("building"))), Integer.parseInt(roomEl 257 .attributeValue("capacity")), sharingModel, posX, posY, ignoreTooFar, !fake)); 258 if (roomEl.attributeValue("type") != null) 259 constraint.setType(Long.valueOf(roomEl.attributeValue("type"))); 260 getModel().addConstraint(constraint); 261 roomConstraints.put(roomEl.attributeValue("id"), constraint); 262 263 for (Iterator<?> j = roomEl.elementIterator("travel-time"); j.hasNext();) { 264 Element travelTimeEl = (Element)j.next(); 265 getModel().getDistanceMetric().addTravelTime(constraint.getResourceId(), 266 Long.valueOf(travelTimeEl.attributeValue("id")), 267 Integer.valueOf(travelTimeEl.attributeValue("minutes"))); 268 } 269 } 270 271 HashMap<String, InstructorConstraint> instructorConstraints = new HashMap<String, InstructorConstraint>(); 272 if (root.element("instructors") != null) { 273 for (Iterator<?> i = root.element("instructors").elementIterator("instructor"); i.hasNext();) { 274 Element instructorEl = (Element) i.next(); 275 InstructorConstraint instructorConstraint = new InstructorConstraint(Long.valueOf(instructorEl 276 .attributeValue("id")), instructorEl.attributeValue("puid"), (instructorEl 277 .attributeValue("name") != null ? instructorEl.attributeValue("name") : "i" 278 + instructorEl.attributeValue("id")), "true".equals(instructorEl.attributeValue("ignDist"))); 279 if (instructorEl.attributeValue("type") != null) 280 instructorConstraint.setType(Long.valueOf(instructorEl.attributeValue("type"))); 281 instructorConstraints.put(instructorEl.attributeValue("id"), instructorConstraint); 282 283 getModel().addConstraint(instructorConstraint); 284 } 285 } 286 HashMap<Long, String> depts = new HashMap<Long, String>(); 287 if (root.element("departments") != null) { 288 for (Iterator<?> i = root.element("departments").elementIterator("department"); i.hasNext();) { 289 Element deptEl = (Element) i.next(); 290 depts.put(Long.valueOf(deptEl.attributeValue("id")), (deptEl.attributeValue("name") != null ? deptEl 291 .attributeValue("name") : "d" + deptEl.attributeValue("id"))); 292 } 293 } 294 295 HashMap<Long, Configuration> configs = new HashMap<Long, Configuration>(); 296 HashMap<Long, List<Configuration>> alternativeConfigurations = new HashMap<Long, List<Configuration>>(); 297 if (root.element("configurations") != null) { 298 for (Iterator<?> i = root.element("configurations").elementIterator("config"); i.hasNext();) { 299 Element configEl = (Element) i.next(); 300 Long configId = Long.valueOf(configEl.attributeValue("id")); 301 int limit = Integer.parseInt(configEl.attributeValue("limit")); 302 Long offeringId = Long.valueOf(configEl.attributeValue("offering")); 303 Configuration config = new Configuration(offeringId, configId, limit); 304 configs.put(configId, config); 305 List<Configuration> altConfigs = alternativeConfigurations.get(offeringId); 306 if (altConfigs == null) { 307 altConfigs = new ArrayList<Configuration>(); 308 alternativeConfigurations.put(offeringId, altConfigs); 309 } 310 altConfigs.add(config); 311 config.setAltConfigurations(altConfigs); 312 } 313 } 314 315 iProgress.setPhase("Creating variables ...", root.element("classes").elements("class").size()); 316 317 HashMap<String, Element> classElements = new HashMap<String, Element>(); 318 HashMap<String, Lecture> lectures = new HashMap<String, Lecture>(); 319 HashMap<Lecture, Placement> assignedPlacements = new HashMap<Lecture, Placement>(); 320 HashMap<Lecture, String> parents = new HashMap<Lecture, String>(); 321 int ord = 0; 322 for (Iterator<?> i1 = root.element("classes").elementIterator("class"); i1.hasNext();) { 323 Element classEl = (Element) i1.next(); 324 325 Configuration config = null; 326 if (classEl.attributeValue("config") != null) { 327 config = configs.get(Long.valueOf(classEl.attributeValue("config"))); 328 } 329 if (config == null && classEl.attributeValue("offering") != null) { 330 Long offeringId = Long.valueOf(classEl.attributeValue("offering")); 331 Long configId = Long.valueOf(classEl.attributeValue("config")); 332 List<Configuration> altConfigs = alternativeConfigurations.get(offeringId); 333 if (altConfigs == null) { 334 altConfigs = new ArrayList<Configuration>(); 335 alternativeConfigurations.put(offeringId, altConfigs); 336 } 337 for (Configuration c : altConfigs) { 338 if (c.getConfigId().equals(configId)) { 339 config = c; 340 break; 341 } 342 } 343 if (config == null) { 344 config = new Configuration(offeringId, configId, -1); 345 altConfigs.add(config); 346 config.setAltConfigurations(altConfigs); 347 } 348 } 349 350 DatePattern defaultDatePattern = new DatePattern(); 351 if (classEl.attributeValue("dates") == null) { 352 int startDay = Integer.parseInt(classEl.attributeValue("startDay", "0")); 353 int endDay = Integer.parseInt(classEl.attributeValue("endDay", "1")); 354 defaultDatePattern.setPattern(startDay, endDay); 355 defaultDatePattern.setName(sDF.format(getDate(getModel().getYear(), startDay)) + "-" + sDF.format(getDate(getModel().getYear(), endDay))); 356 } else { 357 defaultDatePattern.setId(classEl.attributeValue("datePattern") == null ? null : Long.valueOf(classEl.attributeValue("datePattern"))); 358 defaultDatePattern.setName("datePatternName"); 359 defaultDatePattern.setPattern(classEl.attributeValue("dates")); 360 } 361 Hashtable<Long, DatePattern> datePatterns = new Hashtable<Long, TimetableXMLLoader.DatePattern>(); 362 for (Iterator<?> i2 = classEl.elementIterator("date"); i2.hasNext();) { 363 Element dateEl = (Element) i2.next(); 364 Long id = Long.valueOf(dateEl.attributeValue("id")); 365 datePatterns.put(id, new DatePattern( 366 id, 367 dateEl.attributeValue("name"), 368 dateEl.attributeValue("pattern"))); 369 } 370 classElements.put(classEl.attributeValue("id"), classEl); 371 List<InstructorConstraint> ics = new ArrayList<InstructorConstraint>(); 372 for (Iterator<?> i2 = classEl.elementIterator("instructor"); i2.hasNext();) { 373 Element instructorEl = (Element) i2.next(); 374 InstructorConstraint instructorConstraint = instructorConstraints 375 .get(instructorEl.attributeValue("id")); 376 if (instructorConstraint == null) { 377 instructorConstraint = new InstructorConstraint(Long.valueOf(instructorEl.attributeValue("id")), 378 instructorEl.attributeValue("puid"), 379 (instructorEl.attributeValue("name") != null ? instructorEl.attributeValue("name") : "i" 380 + instructorEl.attributeValue("id")), "true".equals(instructorEl 381 .attributeValue("ignDist"))); 382 instructorConstraints.put(instructorEl.attributeValue("id"), instructorConstraint); 383 getModel().addConstraint(instructorConstraint); 384 } 385 ics.add(instructorConstraint); 386 } 387 List<RoomLocation> roomLocations = new ArrayList<RoomLocation>(); 388 List<RoomConstraint> roomConstraintsThisClass = new ArrayList<RoomConstraint>(); 389 List<RoomLocation> initialRoomLocations = new ArrayList<RoomLocation>(); 390 List<RoomLocation> assignedRoomLocations = new ArrayList<RoomLocation>(); 391 List<RoomLocation> bestRoomLocations = new ArrayList<RoomLocation>(); 392 for (Iterator<?> i2 = classEl.elementIterator("room"); i2.hasNext();) { 393 Element roomLocationEl = (Element) i2.next(); 394 Element roomEl = roomElements.get(roomLocationEl.attributeValue("id")); 395 RoomConstraint roomConstraint = roomConstraints.get(roomLocationEl.attributeValue("id")); 396 397 Long roomId = null; 398 String roomName = null; 399 Long bldgId = null; 400 401 if (roomConstraint != null) { 402 roomConstraintsThisClass.add(roomConstraint); 403 roomId = roomConstraint.getResourceId(); 404 roomName = roomConstraint.getRoomName(); 405 bldgId = roomConstraint.getBuildingId(); 406 } else { 407 roomId = Long.valueOf(roomEl.attributeValue("id")); 408 roomName = (roomEl.attributeValue("name") != null ? roomEl.attributeValue("name") : "r" 409 + roomEl.attributeValue("id")); 410 bldgId = (roomEl.attributeValue("building") == null ? null : Long.valueOf(roomEl 411 .attributeValue("building"))); 412 } 413 414 boolean ignoreTooFar = false; 415 if ("true".equals(roomEl.attributeValue("ignoreTooFar"))) 416 ignoreTooFar = true; 417 Double posX = null, posY = null; 418 if (roomEl.attributeValue("location") != null) { 419 String loc = roomEl.attributeValue("location"); 420 posX = Double.valueOf(loc.substring(0, loc.indexOf(','))); 421 posY = Double.valueOf(loc.substring(loc.indexOf(',') + 1)); 422 } 423 RoomLocation rl = new RoomLocation(roomId, roomName, bldgId, Integer.parseInt(roomLocationEl 424 .attributeValue("pref")), Integer.parseInt(roomEl.attributeValue("capacity")), posX, posY, 425 ignoreTooFar, roomConstraint); 426 if ("true".equals(roomLocationEl.attributeValue("initial"))) 427 initialRoomLocations.add(rl); 428 if ("true".equals(roomLocationEl.attributeValue("solution"))) 429 assignedRoomLocations.add(rl); 430 if ("true".equals(roomLocationEl.attributeValue("best"))) 431 bestRoomLocations.add(rl); 432 roomLocations.add(rl); 433 } 434 List<TimeLocation> timeLocations = new ArrayList<TimeLocation>(); 435 TimeLocation initialTimeLocation = null; 436 TimeLocation assignedTimeLocation = null; 437 TimeLocation bestTimeLocation = null; 438 TimeLocation prohibitedTime = perts.get(Long.valueOf(classEl.attributeValue("id"))); 439 440 for (Iterator<?> i2 = classEl.elementIterator("time"); i2.hasNext();) { 441 Element timeLocationEl = (Element) i2.next(); 442 DatePattern dp = defaultDatePattern; 443 if (timeLocationEl.attributeValue("date") != null) 444 dp = datePatterns.get(Long.valueOf(timeLocationEl.attributeValue("date"))); 445 TimeLocation tl = new TimeLocation( 446 Integer.parseInt(timeLocationEl.attributeValue("days"), 2), 447 Integer.parseInt(timeLocationEl.attributeValue("start")), 448 Integer.parseInt(timeLocationEl.attributeValue("length")), 449 (int) Double.parseDouble(timeLocationEl.attributeValue("pref")), 450 Double.parseDouble(timeLocationEl.attributeValue("npref", timeLocationEl.attributeValue("pref"))), 451 Integer.parseInt(timeLocationEl.attributeValue("datePref", "0")), 452 dp.getId(), dp.getName(), dp.getPattern(), 453 Integer.parseInt(timeLocationEl.attributeValue("breakTime") == null ? "-1" : timeLocationEl.attributeValue("breakTime"))); 454 if (tl.getBreakTime() < 0) tl.setBreakTime(tl.getLength() == 18 ? 15 : 10); 455 if (timeLocationEl.attributeValue("pattern") != null) 456 tl.setTimePatternId(Long.valueOf(timeLocationEl.attributeValue("pattern"))); 457 /* 458 * if (timePatternTransform) tl = 459 * transformTimePattern(Long.valueOf 460 * (classEl.attributeValue("id")),tl); 461 */ 462 if (prohibitedTime != null && prohibitedTime.getDayCode() == tl.getDayCode() 463 && prohibitedTime.getStartSlot() == tl.getStartSlot() 464 && prohibitedTime.getLength() == tl.getLength()) { 465 sLogger.info("Time " + tl.getLongName() + " is prohibited for class " 466 + classEl.attributeValue("id")); 467 continue; 468 } 469 if ("true".equals(timeLocationEl.attributeValue("solution"))) 470 assignedTimeLocation = tl; 471 if ("true".equals(timeLocationEl.attributeValue("initial"))) 472 initialTimeLocation = tl; 473 if ("true".equals(timeLocationEl.attributeValue("best"))) 474 bestTimeLocation = tl; 475 timeLocations.add(tl); 476 } 477 if (timeLocations.isEmpty()) { 478 sLogger.error(" ERROR: No time."); 479 continue; 480 } 481 482 int minClassLimit = 0; 483 int maxClassLimit = 0; 484 double room2limitRatio = 1.0; 485 if (!"true".equals(classEl.attributeValue("committed"))) { 486 if (classEl.attributeValue("expectedCapacity") != null) { 487 minClassLimit = maxClassLimit = Integer.parseInt(classEl.attributeValue("expectedCapacity")); 488 int roomCapacity = Integer.parseInt(classEl.attributeValue("roomCapacity", classEl 489 .attributeValue("expectedCapacity"))); 490 if (minClassLimit == 0) 491 minClassLimit = maxClassLimit = roomCapacity; 492 room2limitRatio = (minClassLimit == 0 ? 1.0 : ((double) roomCapacity) / minClassLimit); 493 } else { 494 if (classEl.attribute("classLimit") != null) { 495 minClassLimit = maxClassLimit = Integer.parseInt(classEl.attributeValue("classLimit")); 496 } else { 497 minClassLimit = Integer.parseInt(classEl.attributeValue("minClassLimit")); 498 maxClassLimit = Integer.parseInt(classEl.attributeValue("maxClassLimit")); 499 } 500 room2limitRatio = Double.parseDouble(classEl.attributeValue("roomToLimitRatio", "1.0")); 501 } 502 } 503 504 Lecture lecture = new Lecture(Long.valueOf(classEl.attributeValue("id")), 505 (classEl.attributeValue("solverGroup") != null ? Long 506 .valueOf(classEl.attributeValue("solverGroup")) : null), Long.valueOf(classEl 507 .attributeValue("subpart", classEl.attributeValue("course", "-1"))), (classEl 508 .attributeValue("name") != null ? classEl.attributeValue("name") : "c" 509 + classEl.attributeValue("id")), timeLocations, roomLocations, Integer.parseInt(classEl 510 .attributeValue("nrRooms", roomLocations.isEmpty() ? "0" : "1")), null, minClassLimit, maxClassLimit, room2limitRatio); 511 lecture.setNote(classEl.attributeValue("note")); 512 513 if ("true".equals(classEl.attributeValue("committed"))) 514 lecture.setCommitted(true); 515 516 if (!lecture.isCommitted() && classEl.attributeValue("ord") != null) 517 lecture.setOrd(Integer.parseInt(classEl.attributeValue("ord"))); 518 else 519 lecture.setOrd(ord++); 520 521 if (config != null) 522 lecture.setConfiguration(config); 523 524 if (initialTimeLocation != null && initialRoomLocations.size() == lecture.getNrRooms()) { 525 lecture.setInitialAssignment(new Placement(lecture, initialTimeLocation, initialRoomLocations)); 526 } 527 if (assignedTimeLocation != null && assignedRoomLocations.size() == lecture.getNrRooms()) { 528 assignedPlacements.put(lecture, new Placement(lecture, assignedTimeLocation, assignedRoomLocations)); 529 } else if (lecture.getInitialAssignment() != null) { 530 assignedPlacements.put(lecture, lecture.getInitialAssignment()); 531 } 532 if (bestTimeLocation != null && bestRoomLocations.size() == lecture.getNrRooms()) { 533 lecture.setBestAssignment(new Placement(lecture, bestTimeLocation, bestRoomLocations)); 534 } else if (assignedTimeLocation != null && assignedRoomLocations.size() == lecture.getNrRooms()) { 535 lecture.setBestAssignment(assignedPlacements.get(lecture)); 536 } 537 538 lectures.put(classEl.attributeValue("id"), lecture); 539 if (classEl.attributeValue("department") != null) 540 lecture.setDepartment(Long.valueOf(classEl.attributeValue("department"))); 541 if (classEl.attribute("scheduler") != null) 542 lecture.setScheduler(Long.valueOf(classEl.attributeValue("scheduler"))); 543 if ((sectionWholeCourse || !lecture.isCommitted()) && classEl.attributeValue("subpart", classEl.attributeValue("course")) != null) { 544 Long subpartId = Long.valueOf(classEl.attributeValue("subpart", classEl.attributeValue("course"))); 545 List<Lecture> sames = sameLectures.get(subpartId); 546 if (sames == null) { 547 sames = new ArrayList<Lecture>(); 548 sameLectures.put(subpartId, sames); 549 } 550 sames.add(lecture); 551 } 552 String parent = classEl.attributeValue("parent"); 553 if (parent != null) 554 parents.put(lecture, parent); 555 556 getModel().addVariable(lecture); 557 558 if (lecture.isCommitted()) { 559 Placement placement = assignedPlacements.get(lecture); 560 if (classEl.attribute("assignment") != null) 561 placement.setAssignmentId(Long.valueOf(classEl.attributeValue("assignment"))); 562 for (InstructorConstraint ic : ics) 563 ic.setNotAvailable(placement); 564 for (RoomConstraint rc : roomConstraintsThisClass) 565 rc.setNotAvailable(placement); 566 } else { 567 for (InstructorConstraint ic : ics) 568 ic.addVariable(lecture); 569 for (RoomConstraint rc : roomConstraintsThisClass) 570 rc.addVariable(lecture); 571 } 572 573 iProgress.incProgress(); 574 } 575 576 for (Map.Entry<Lecture, String> entry : parents.entrySet()) { 577 Lecture lecture = entry.getKey(); 578 Lecture parent = lectures.get(entry.getValue()); 579 if (parent == null) { 580 System.out.println("Unknown parent class: " + entry.getValue()); 581 } 582 lecture.setParent(parent); 583 } 584 585 iProgress.setPhase("Creating constraints ...", root.element("groupConstraints").elements("constraint").size()); 586 HashMap<String, Element> grConstraintElements = new HashMap<String, Element>(); 587 HashMap<String, Constraint<Lecture, Placement>> groupConstraints = new HashMap<String, Constraint<Lecture, Placement>>(); 588 for (Iterator<?> i1 = root.element("groupConstraints").elementIterator("constraint"); i1.hasNext();) { 589 Element grConstraintEl = (Element) i1.next(); 590 Constraint<Lecture, Placement> c = null; 591 if ("SPREAD".equals(grConstraintEl.attributeValue("type"))) { 592 c = new SpreadConstraint(getModel().getProperties(), grConstraintEl.attributeValue("name", "spread")); 593 } else if ("MIN_ROOM_USE".equals(grConstraintEl.attributeValue("type"))) { 594 c = new MinimizeNumberOfUsedRoomsConstraint(getModel().getProperties()); 595 } else if ("CLASS_LIMIT".equals(grConstraintEl.attributeValue("type"))) { 596 if (grConstraintEl.element("parentClass") == null) { 597 c = new ClassLimitConstraint(Integer.parseInt(grConstraintEl.attributeValue("courseLimit")), 598 grConstraintEl.attributeValue("name", "class-limit")); 599 } else { 600 String classId = grConstraintEl.element("parentClass").attributeValue("id"); 601 c = new ClassLimitConstraint(lectures.get(classId), grConstraintEl.attributeValue("name", 602 "class-limit")); 603 } 604 if (grConstraintEl.attributeValue("delta") != null) 605 ((ClassLimitConstraint) c).setClassLimitDelta(Integer.parseInt(grConstraintEl 606 .attributeValue("delta"))); 607 } else if ("MIN_GRUSE(10x1h)".equals(grConstraintEl.attributeValue("type"))) { 608 c = new MinimizeNumberOfUsedGroupsOfTime(getModel().getProperties(), "10x1h", 609 MinimizeNumberOfUsedGroupsOfTime.sGroups10of1h); 610 } else if ("MIN_GRUSE(5x2h)".equals(grConstraintEl.attributeValue("type"))) { 611 c = new MinimizeNumberOfUsedGroupsOfTime(getModel().getProperties(), "5x2h", 612 MinimizeNumberOfUsedGroupsOfTime.sGroups5of2h); 613 } else if ("MIN_GRUSE(3x3h)".equals(grConstraintEl.attributeValue("type"))) { 614 c = new MinimizeNumberOfUsedGroupsOfTime(getModel().getProperties(), "3x3h", 615 MinimizeNumberOfUsedGroupsOfTime.sGroups3of3h); 616 } else if ("MIN_GRUSE(2x5h)".equals(grConstraintEl.attributeValue("type"))) { 617 c = new MinimizeNumberOfUsedGroupsOfTime(getModel().getProperties(), "2x5h", 618 MinimizeNumberOfUsedGroupsOfTime.sGroups2of5h); 619 } else { 620 c = new GroupConstraint( 621 Long.valueOf(grConstraintEl.attributeValue("id")), 622 GroupConstraint.ConstraintType.get(grConstraintEl.attributeValue("type")), 623 grConstraintEl.attributeValue("pref")); 624 } 625 getModel().addConstraint(c); 626 for (Iterator<?> i2 = grConstraintEl.elementIterator("class"); i2.hasNext();) { 627 String classId = ((Element) i2.next()).attributeValue("id"); 628 Lecture other = lectures.get(classId); 629 if (other != null) 630 c.addVariable(other); 631 else 632 iProgress.warn("Class " + classId + " does not exists, but it is referred from group constraint " + c.getId() + " (" + c.getName() + ")"); 633 } 634 grConstraintElements.put(grConstraintEl.attributeValue("id"), grConstraintEl); 635 groupConstraints.put(grConstraintEl.attributeValue("id"), c); 636 iProgress.incProgress(); 637 } 638 639 iProgress.setPhase("Loading students ...", root.element("students").elements("student").size()); 640 boolean initialSectioning = true; 641 HashMap<Long, Student> students = new HashMap<Long, Student>(); 642 HashMap<Long, Set<Student>> offering2students = new HashMap<Long, Set<Student>>(); 643 for (Iterator<?> i1 = root.element("students").elementIterator("student"); i1.hasNext();) { 644 Element studentEl = (Element) i1.next(); 645 List<Lecture> lecturesThisStudent = new ArrayList<Lecture>(); 646 Long studentId = Long.valueOf(studentEl.attributeValue("id")); 647 Student student = students.get(studentId); 648 if (student == null) { 649 student = new Student(studentId); 650 students.put(studentId, student); 651 getModel().addStudent(student); 652 } 653 student.setAcademicArea(studentEl.attributeValue("area")); 654 student.setAcademicClassification(studentEl.attributeValue("classification")); 655 student.setMajor(studentEl.attributeValue("major")); 656 student.setCurriculum(studentEl.attributeValue("curriculum")); 657 for (Iterator<?> i2 = studentEl.elementIterator("offering"); i2.hasNext();) { 658 Element ofEl = (Element) i2.next(); 659 Long offeringId = Long.valueOf(ofEl.attributeValue("id")); 660 String priority = ofEl.attributeValue("priority"); 661 student.addOffering(offeringId, Double.parseDouble(ofEl.attributeValue("weight", "1.0")), priority == null ? null : Double.valueOf(priority)); 662 Set<Student> studentsThisOffering = offering2students.get(offeringId); 663 if (studentsThisOffering == null) { 664 studentsThisOffering = new HashSet<Student>(); 665 offering2students.put(offeringId, studentsThisOffering); 666 } 667 studentsThisOffering.add(student); 668 } 669 for (Iterator<?> i2 = studentEl.elementIterator("class"); i2.hasNext();) { 670 String classId = ((Element) i2.next()).attributeValue("id"); 671 Lecture lecture = lectures.get(classId); 672 if (lecture == null) { 673 iProgress.warn("Class " + classId + " does not exists, but it is referred from student " + student.getId()); 674 continue; 675 } 676 if (lecture.isCommitted()) { 677 if (sectionWholeCourse && (lecture.getParent() != null || lecture.getConfiguration() != null)) { 678 // committed, but with course structure -- sectioning can be used 679 student.addLecture(lecture); 680 lecture.addStudent(student); 681 lecturesThisStudent.add(lecture); 682 initialSectioning = false; 683 } else { 684 Placement placement = assignedPlacements.get(lecture); 685 student.addCommitedPlacement(placement); 686 } 687 } else { 688 student.addLecture(lecture); 689 lecture.addStudent(student); 690 lecturesThisStudent.add(lecture); 691 initialSectioning = false; 692 } 693 } 694 695 for (Iterator<?> i2 = studentEl.elementIterator("prohibited-class"); i2.hasNext();) { 696 String classId = ((Element) i2.next()).attributeValue("id"); 697 Lecture lecture = lectures.get(classId); 698 if (lecture != null) 699 student.addCanNotEnroll(lecture); 700 else 701 iProgress.warn("Class " + classId + " does not exists, but it is referred from student " + student.getId()); 702 } 703 704 iProgress.incProgress(); 705 } 706 707 for (List<Lecture> sames: sameLectures.values()) { 708 for (Lecture lect : sames) { 709 lect.setSameSubpartLectures(sames); 710 } 711 } 712 713 if (initialSectioning) { 714 iProgress.setPhase("Initial sectioning ...", offering2students.size()); 715 for (Map.Entry<Long, Set<Student>> entry : offering2students.entrySet()) { 716 Long offeringId = entry.getKey(); 717 Set<Student> studentsThisOffering = entry.getValue(); 718 List<Configuration> altConfigs = alternativeConfigurations.get(offeringId); 719 InitialSectioning.initialSectioningCfg(iProgress, offeringId, String.valueOf(offeringId), 720 studentsThisOffering, altConfigs); 721 iProgress.incProgress(); 722 } 723 for (Student student: students.values()) 724 student.clearDistanceCache(); 725 } 726 727 iProgress.setPhase("Computing jenrl ...", students.size()); 728 HashMap<Lecture, HashMap<Lecture, JenrlConstraint>> jenrls = new HashMap<Lecture, HashMap<Lecture, JenrlConstraint>>(); 729 for (Iterator<Student> i1 = students.values().iterator(); i1.hasNext();) { 730 Student st = i1.next(); 731 for (Iterator<Lecture> i2 = st.getLectures().iterator(); i2.hasNext();) { 732 Lecture l1 = i2.next(); 733 for (Iterator<Lecture> i3 = st.getLectures().iterator(); i3.hasNext();) { 734 Lecture l2 = i3.next(); 735 if (l1.getId() >= l2.getId()) 736 continue; 737 HashMap<Lecture, JenrlConstraint> x = jenrls.get(l1); 738 if (x == null) { 739 x = new HashMap<Lecture, JenrlConstraint>(); 740 jenrls.put(l1, x); 741 } 742 JenrlConstraint jenrl = x.get(l2); 743 if (jenrl == null) { 744 jenrl = new JenrlConstraint(); 745 jenrl.addVariable(l1); 746 jenrl.addVariable(l2); 747 getModel().addConstraint(jenrl); 748 x.put(l2, jenrl); 749 } 750 jenrl.incJenrl(st); 751 } 752 } 753 iProgress.incProgress(); 754 } 755 756 if (iDeptBalancing) { 757 iProgress.setPhase("Creating dept. spread constraints ...", getModel().variables().size()); 758 HashMap<Long, DepartmentSpreadConstraint> depSpreadConstraints = new HashMap<Long, DepartmentSpreadConstraint>(); 759 for (Lecture lecture : getModel().variables()) { 760 if (lecture.getDepartment() == null) 761 continue; 762 DepartmentSpreadConstraint deptConstr = depSpreadConstraints.get(lecture.getDepartment()); 763 if (deptConstr == null) { 764 String name = depts.get(lecture.getDepartment()); 765 deptConstr = new DepartmentSpreadConstraint(getModel().getProperties(), lecture.getDepartment(), 766 (name != null ? name : "d" + lecture.getDepartment())); 767 depSpreadConstraints.put(lecture.getDepartment(), deptConstr); 768 getModel().addConstraint(deptConstr); 769 } 770 deptConstr.addVariable(lecture); 771 iProgress.incProgress(); 772 } 773 } 774 775 if (getModel().getProperties().getPropertyBoolean("General.PurgeInvalidPlacements", true)) { 776 iProgress.setPhase("Purging invalid placements ...", getModel().variables().size()); 777 for (Lecture lecture : getModel().variables()) { 778 lecture.purgeInvalidValues(iInteractiveMode); 779 iProgress.incProgress(); 780 } 781 } 782 783 if (getModel().hasConstantVariables() && getModel().constantVariables().size() > 0) { 784 iProgress.setPhase("Assigning committed classes ...", assignedPlacements.size()); 785 for (Map.Entry<Lecture, Placement> entry : assignedPlacements.entrySet()) { 786 Lecture lecture = entry.getKey(); 787 Placement placement = entry.getValue(); 788 if (!lecture.isCommitted()) { iProgress.incProgress(); continue; } 789 Map<Constraint<Lecture, Placement>, Set<Placement>> conflictConstraints = getModel().conflictConstraints(placement); 790 if (conflictConstraints.isEmpty()) { 791 lecture.assign(0, placement); 792 } else { 793 sLogger.warn("WARNING: Unable to assign " + lecture.getName() + " := " + placement.getName()); 794 sLogger.debug(" Reason:"); 795 for (Constraint<Lecture, Placement> c : conflictConstraints.keySet()) { 796 Set<Placement> vals = conflictConstraints.get(c); 797 for (Placement v : vals) { 798 sLogger.debug(" " + v.variable().getName() + " = " + v.getName()); 799 } 800 sLogger.debug(" in constraint " + c); 801 } 802 } 803 iProgress.incProgress(); 804 } 805 } 806 807 if (currentSolution != null) { 808 iProgress.setPhase("Creating best assignment ...", 2 * getModel().variables().size()); 809 for (Lecture lecture : getModel().variables()) { 810 iProgress.incProgress(); 811 Placement placement = lecture.getBestAssignment(); 812 if (placement == null) 813 continue; 814 lecture.assign(0, placement); 815 } 816 817 currentSolution.saveBest(); 818 for (Lecture lecture : getModel().variables()) { 819 iProgress.incProgress(); 820 if (lecture.getAssignment() != null) 821 lecture.unassign(0); 822 } 823 } 824 825 iProgress.setPhase("Creating initial assignment ...", assignedPlacements.size()); 826 for (Map.Entry<Lecture, Placement> entry : assignedPlacements.entrySet()) { 827 Lecture lecture = entry.getKey(); 828 Placement placement = entry.getValue(); 829 if (lecture.isCommitted()) { iProgress.incProgress(); continue; } 830 Map<Constraint<Lecture, Placement>, Set<Placement>> conflictConstraints = getModel().conflictConstraints(placement); 831 if (conflictConstraints.isEmpty()) { 832 if (!placement.isValid()) { 833 sLogger.warn("WARNING: Lecture " + lecture.getName() + " does not contain assignment " 834 + placement.getLongName() + " in its domain (" + placement.getNotValidReason() + ")."); 835 } else 836 lecture.assign(0, placement); 837 } else { 838 sLogger.warn("WARNING: Unable to assign " + lecture.getName() + " := " + placement.getName()); 839 sLogger.debug(" Reason:"); 840 for (Constraint<Lecture, Placement> c : conflictConstraints.keySet()) { 841 Set<Placement> vals = conflictConstraints.get(c); 842 for (Placement v : vals) { 843 sLogger.debug(" " + v.variable().getName() + " = " + v.getName()); 844 } 845 sLogger.debug(" in constraint " + c); 846 } 847 } 848 iProgress.incProgress(); 849 } 850 851 if (initialSectioning && !getModel().assignedVariables().isEmpty() 852 && !getModel().getProperties().getPropertyBoolean("Global.LoadStudentEnrlsFromSolution", false)) 853 getModel().switchStudents(); 854 855 if (iForcedPerturbances > 0) { 856 iProgress.setPhase("Forcing perturbances", iForcedPerturbances); 857 for (int i = 0; i < iForcedPerturbances; i++) { 858 iProgress.setProgress(i); 859 Lecture var = null; 860 do { 861 var = ToolBox.random(getModel().variables()); 862 } while (var.getInitialAssignment() == null || var.values().size() <= 1); 863 var.removeInitialValue(); 864 } 865 } 866 867 for (Constraint<Lecture, Placement> c : getModel().constraints()) { 868 if (c instanceof SpreadConstraint) 869 ((SpreadConstraint) c).init(); 870 if (c instanceof DiscouragedRoomConstraint) 871 ((DiscouragedRoomConstraint) c).setEnabled(true); 872 if (c instanceof MinimizeNumberOfUsedRoomsConstraint) 873 ((MinimizeNumberOfUsedRoomsConstraint) c).setEnabled(true); 874 if (c instanceof MinimizeNumberOfUsedGroupsOfTime) 875 ((MinimizeNumberOfUsedGroupsOfTime) c).setEnabled(true); 876 } 877 878 try { 879 getSolver().getClass().getMethod("load", new Class[] { Element.class }).invoke(getSolver(), 880 new Object[] { root }); 881 } catch (Exception e) { 882 } 883 884 iProgress.setPhase("Done", 1); 885 iProgress.incProgress(); 886 887 sLogger.debug("Model successfully loaded."); 888 iProgress.info("Model successfully loaded."); 889 } 890 891 public static Date getDate(int year, int dayOfYear) { 892 Calendar c = Calendar.getInstance(Locale.US); 893 c.set(year, 1, 1, 0, 0, 0); 894 c.set(Calendar.DAY_OF_YEAR, dayOfYear); 895 return c.getTime(); 896 } 897 898 public static class DatePattern { 899 Long iId; 900 String iName; 901 BitSet iPattern; 902 public DatePattern() {} 903 public DatePattern(Long id, String name, BitSet pattern) { 904 setId(id); setName(name); setPattern(pattern); 905 } 906 public DatePattern(Long id, String name, String pattern) { 907 setId(id); setName(name); setPattern(pattern); 908 } 909 public Long getId() { return iId; } 910 public void setId(Long id) { iId = id; } 911 public String getName() { return iName; } 912 public void setName(String name) { iName = name; } 913 public BitSet getPattern() { return iPattern; } 914 public void setPattern(BitSet pattern) { iPattern = pattern; } 915 public void setPattern(String pattern) { 916 iPattern = new BitSet(pattern.length()); 917 for (int i = 0; i < pattern.length(); i++) 918 if (pattern.charAt(i) == '1') 919 iPattern.set(i); 920 } 921 public void setPattern(int startDay, int endDay) { 922 iPattern = new BitSet(366); 923 for (int d = startDay; d <= endDay; d++) 924 iPattern.set(d); 925 } 926 } 927 }