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