001package org.cpsolver.coursett;
002
003import java.io.File;
004import java.io.FileWriter;
005import java.io.IOException;
006import java.io.PrintWriter;
007import java.text.DecimalFormat;
008import java.util.ArrayList;
009import java.util.Collection;
010import java.util.Date;
011import java.util.HashSet;
012import java.util.HashMap;
013import java.util.List;
014import java.util.Locale;
015import java.util.Map;
016import java.util.TreeSet;
017
018import org.apache.log4j.ConsoleAppender;
019import org.apache.log4j.FileAppender;
020import org.apache.log4j.Level;
021import org.apache.log4j.Logger;
022import org.apache.log4j.PatternLayout;
023import org.cpsolver.coursett.constraint.DepartmentSpreadConstraint;
024import org.cpsolver.coursett.constraint.GroupConstraint;
025import org.cpsolver.coursett.constraint.InstructorConstraint;
026import org.cpsolver.coursett.constraint.JenrlConstraint;
027import org.cpsolver.coursett.constraint.RoomConstraint;
028import org.cpsolver.coursett.constraint.SpreadConstraint;
029import org.cpsolver.coursett.criteria.BackToBackInstructorPreferences;
030import org.cpsolver.coursett.criteria.BrokenTimePatterns;
031import org.cpsolver.coursett.criteria.DepartmentBalancingPenalty;
032import org.cpsolver.coursett.criteria.DistributionPreferences;
033import org.cpsolver.coursett.criteria.Perturbations;
034import org.cpsolver.coursett.criteria.RoomPreferences;
035import org.cpsolver.coursett.criteria.SameSubpartBalancingPenalty;
036import org.cpsolver.coursett.criteria.StudentCommittedConflict;
037import org.cpsolver.coursett.criteria.StudentConflict;
038import org.cpsolver.coursett.criteria.StudentDistanceConflict;
039import org.cpsolver.coursett.criteria.StudentHardConflict;
040import org.cpsolver.coursett.criteria.TimePreferences;
041import org.cpsolver.coursett.criteria.TooBigRooms;
042import org.cpsolver.coursett.criteria.UselessHalfHours;
043import org.cpsolver.coursett.heuristics.UniversalPerturbationsCounter;
044import org.cpsolver.coursett.model.Lecture;
045import org.cpsolver.coursett.model.Placement;
046import org.cpsolver.coursett.model.RoomLocation;
047import org.cpsolver.coursett.model.Student;
048import org.cpsolver.coursett.model.TimeLocation;
049import org.cpsolver.coursett.model.TimetableModel;
050import org.cpsolver.ifs.assignment.Assignment;
051import org.cpsolver.ifs.assignment.DefaultParallelAssignment;
052import org.cpsolver.ifs.assignment.DefaultSingleAssignment;
053import org.cpsolver.ifs.extension.ConflictStatistics;
054import org.cpsolver.ifs.extension.Extension;
055import org.cpsolver.ifs.extension.MacPropagation;
056import org.cpsolver.ifs.model.Constraint;
057import org.cpsolver.ifs.solution.Solution;
058import org.cpsolver.ifs.solution.SolutionListener;
059import org.cpsolver.ifs.solver.ParallelSolver;
060import org.cpsolver.ifs.solver.Solver;
061import org.cpsolver.ifs.util.DataProperties;
062import org.cpsolver.ifs.util.Progress;
063import org.cpsolver.ifs.util.ProgressWriter;
064import org.cpsolver.ifs.util.ToolBox;
065
066
067/**
068 * A main class for running of the solver from command line. <br>
069 * <br>
070 * Usage:<br>
071 * java -Xmx1024m -jar coursett1.1.jar config.properties [input_file]
072 * [output_folder]<br>
073 * <br>
074 * See http://www.unitime.org for example configuration files and banchmark data
075 * sets.<br>
076 * <br>
077 * 
078 * The test does the following steps:
079 * <ul>
080 * <li>Provided property file is loaded (see {@link DataProperties}).
081 * <li>Output folder is created (General.Output property) and loggings is setup
082 * (using log4j).
083 * <li>Input data are loaded (calling {@link TimetableLoader#load()}).
084 * <li>Solver is executed (see {@link Solver}).
085 * <li>Resultant solution is saved (calling {@link TimetableSaver#save()}, when
086 * General.Save property is set to true.
087 * </ul>
088 * Also, a log and a CSV (comma separated text file) is created in the output
089 * folder.
090 * 
091 * @version CourseTT 1.3 (University Course Timetabling)<br>
092 *          Copyright (C) 2006 - 2014 Tomas Muller<br>
093 *          <a href="mailto:muller@unitime.org">muller@unitime.org</a><br>
094 *          <a href="http://muller.unitime.org">http://muller.unitime.org</a><br>
095 * <br>
096 *          This library is free software; you can redistribute it and/or modify
097 *          it under the terms of the GNU Lesser General Public License as
098 *          published by the Free Software Foundation; either version 3 of the
099 *          License, or (at your option) any later version. <br>
100 * <br>
101 *          This library is distributed in the hope that it will be useful, but
102 *          WITHOUT ANY WARRANTY; without even the implied warranty of
103 *          MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
104 *          Lesser General Public License for more details. <br>
105 * <br>
106 *          You should have received a copy of the GNU Lesser General Public
107 *          License along with this library; if not see
108 *          <a href='http://www.gnu.org/licenses/'>http://www.gnu.org/licenses/</a>.
109 */
110
111public class Test implements SolutionListener<Lecture, Placement> {
112    private static java.text.SimpleDateFormat sDateFormat = new java.text.SimpleDateFormat("yyMMdd_HHmmss",
113            java.util.Locale.US);
114    private static java.text.DecimalFormat sDoubleFormat = new java.text.DecimalFormat("0.000",
115            new java.text.DecimalFormatSymbols(Locale.US));
116    private static org.apache.log4j.Logger sLogger = org.apache.log4j.Logger.getLogger(Test.class);
117
118    private PrintWriter iCSVFile = null;
119
120    private MacPropagation<Lecture, Placement> iProp = null;
121    private ConflictStatistics<Lecture, Placement> iStat = null;
122    private int iLastNotified = -1;
123
124    private boolean initialized = false;
125    private Solver<Lecture, Placement> iSolver = null;
126
127    /** Current version 
128     * @return version string
129     **/
130    public static String getVersionString() {
131        return "IFS Timetable Solver v" + Constants.getVersion() + " build" + Constants.getBuildNumber() + ", "
132                + Constants.getReleaseDate();
133    }
134
135    /** Solver initialization 
136     * @param solver current solver
137     **/
138    public void init(Solver<Lecture, Placement> solver) {
139        iSolver = solver;
140        solver.currentSolution().addSolutionListener(this);
141    }
142    
143    /**
144     * Setup log4j logging
145     * 
146     * @param logFile  log file
147     * @param debug true if debug messages should be logged (use -Ddebug=true to enable debug message)
148     */
149    public static void setupLogging(File logFile, boolean debug) {
150        Logger root = Logger.getRootLogger();
151        ConsoleAppender console = new ConsoleAppender(new PatternLayout("[%t] %m%n"));
152        console.setThreshold(Level.INFO);
153        root.addAppender(console);
154        if (logFile != null) {
155            try {
156                FileAppender file = new FileAppender(new PatternLayout("%d{dd-MMM-yy HH:mm:ss.SSS} [%t] %-5p %c{2}> %m%n"), logFile.getPath(), false);
157                file.setThreshold(Level.DEBUG);
158                root.addAppender(file);
159            } catch (IOException e) {
160                sLogger.fatal("Unable to configure logging, reason: " + e.getMessage(), e);
161            }
162        }
163        if (!debug)
164            root.setLevel(Level.INFO);
165    }
166
167    /**
168     * Return name of the class that is used for loading the data. This class
169     * needs to extend class {@link TimetableLoader}. It can be also defined in
170     * configuration, using TimetableLoader property.
171     **/
172    private String getTimetableLoaderClass(DataProperties properties) {
173        String loader = properties.getProperty("TimetableLoader");
174        if (loader != null)
175            return loader;
176        if (properties.getPropertyInt("General.InputVersion", -1) >= 0)
177            return "org.unitime.timetable.solver.TimetableDatabaseLoader";
178        else
179            return "org.cpsolver.coursett.TimetableXMLLoader";
180    }
181
182    /**
183     * Return name of the class that is used for loading the data. This class
184     * needs to extend class {@link TimetableSaver}. It can be also defined in
185     * configuration, using TimetableSaver property.
186     **/
187    private String getTimetableSaverClass(DataProperties properties) {
188        String saver = properties.getProperty("TimetableSaver");
189        if (saver != null)
190            return saver;
191        if (properties.getPropertyInt("General.InputVersion", -1) >= 0)
192            return "org.unitime.timetable.solver.TimetableDatabaseSaver";
193        else
194            return "org.cpsolver.coursett.TimetableXMLSaver";
195    }
196
197    /**
198     * Solver Test
199     * 
200     * @param args
201     *            command line arguments
202     */
203    public Test(String[] args) {
204        try {
205            DataProperties properties = ToolBox.loadProperties(new java.io.File(args[0]));
206            properties.putAll(System.getProperties());
207            properties.setProperty("General.Output", properties.getProperty("General.Output", ".") + File.separator + sDateFormat.format(new Date()));
208            if (args.length > 1)
209                properties.setProperty("General.Input", args[1]);
210            if (args.length > 2)
211                properties.setProperty("General.Output", args[2] + File.separator + (sDateFormat.format(new Date())));
212            System.out.println("Output folder: " + properties.getProperty("General.Output"));
213            File outDir = new File(properties.getProperty("General.Output", "."));
214            outDir.mkdirs();
215            setupLogging(new File(outDir, "debug.log"), "true".equals(System.getProperty("debug", "false")));
216
217            TimetableModel model = new TimetableModel(properties);
218            int nrSolvers = properties.getPropertyInt("Parallel.NrSolvers", 1);
219            Assignment<Lecture, Placement> assignment = (nrSolvers <= 1 ? new DefaultSingleAssignment<Lecture, Placement>() : new DefaultParallelAssignment<Lecture, Placement>());
220            Progress.getInstance(model).addProgressListener(new ProgressWriter(System.out));
221            Solver<Lecture, Placement> solver = (nrSolvers == 1 ? new Solver<Lecture, Placement>(properties) : new ParallelSolver<Lecture, Placement>(properties));
222
223            TimetableLoader loader = null;
224            try {
225                loader = (TimetableLoader) Class.forName(getTimetableLoaderClass(properties))
226                        .getConstructor(new Class[] { TimetableModel.class, Assignment.class }).newInstance(new Object[] { model, assignment });
227            } catch (ClassNotFoundException e) {
228                System.err.println(e.getClass().getSimpleName() + ": " + e.getMessage());
229                loader = new TimetableXMLLoader(model, assignment);
230            }
231            loader.load();
232
233            solver.setInitalSolution(new Solution<Lecture, Placement>(model, assignment));
234            init(solver);
235
236            iCSVFile = new PrintWriter(new FileWriter(outDir.toString() + File.separator + "stat.csv"));
237            String colSeparator = ";";
238            iCSVFile.println("Assigned"
239                    + colSeparator
240                    + "Assigned[%]"
241                    + colSeparator
242                    + "Time[min]"
243                    + colSeparator
244                    + "Iter"
245                    + colSeparator
246                    + "IterYield[%]"
247                    + colSeparator
248                    + "Speed[it/s]"
249                    + colSeparator
250                    + "AddedPert"
251                    + colSeparator
252                    + "AddedPert[%]"
253                    + colSeparator
254                    + "HardStudentConf"
255                    + colSeparator
256                    + "StudentConf"
257                    + colSeparator
258                    + "DistStudentConf"
259                    + colSeparator
260                    + "CommitStudentConf"
261                    + colSeparator
262                    + "TimePref"
263                    + colSeparator
264                    + "RoomPref"
265                    + colSeparator
266                    + "DistInstrPref"
267                    + colSeparator
268                    + "GrConstPref"
269                    + colSeparator
270                    + "UselessHalfHours"
271                    + colSeparator
272                    + "BrokenTimePat"
273                    + colSeparator
274                    + "TooBigRooms"
275                    + (iProp != null ? colSeparator + "GoodVars" + colSeparator + "GoodVars[%]" + colSeparator
276                            + "GoodVals" + colSeparator + "GoodVals[%]" : ""));
277            iCSVFile.flush();
278            
279            Runtime.getRuntime().addShutdownHook(new ShutdownHook(solver));
280
281            solver.start();
282            try {
283                solver.getSolverThread().join();
284            } catch (InterruptedException e) {
285            }
286        } catch (Throwable t) {
287            sLogger.error("Test failed.", t);
288        }
289    }
290
291    public static void main(String[] args) {
292        new Test(args);
293    }
294
295    @Override
296    public void bestCleared(Solution<Lecture, Placement> solution) {
297    }
298
299    @Override
300    public void bestRestored(Solution<Lecture, Placement> solution) {
301    }
302
303    @Override
304    public void bestSaved(Solution<Lecture, Placement> solution) {
305        notify(solution);
306        if (sLogger.isInfoEnabled())
307            sLogger.info("**BEST[" + solution.getIteration() + "]** " + ((TimetableModel)solution.getModel()).toString(solution.getAssignment()) +
308                    (solution.getFailedIterations() > 0 ? ", F:" + sDoubleFormat.format(100.0 * solution.getFailedIterations() / solution.getIteration()) + "%" : ""));
309    }
310
311    @Override
312    public void getInfo(Solution<Lecture, Placement> solution, Map<String, String> info) {
313    }
314
315    @Override
316    public void getInfo(Solution<Lecture, Placement> solution, Map<String, String> info, Collection<Lecture> variables) {
317    }
318
319    @Override
320    public void solutionUpdated(Solution<Lecture, Placement> solution) {
321        if (!initialized) {
322            for (Extension<Lecture, Placement> extension : iSolver.getExtensions()) {
323                if (MacPropagation.class.isInstance(extension))
324                    iProp = (MacPropagation<Lecture, Placement>) extension;
325                if (ConflictStatistics.class.isInstance(extension)) {
326                    iStat = (ConflictStatistics<Lecture, Placement>) extension;
327                }
328            }
329        }
330    }
331
332    /** Add a line into the output CSV file when a enw best solution is found. 
333     * @param solution current solution
334     **/
335    public void notify(Solution<Lecture, Placement> solution) {
336        String colSeparator = ";";
337        Assignment<Lecture, Placement> assignment = solution.getAssignment();
338        if (assignment.nrAssignedVariables() < solution.getModel().countVariables() && iLastNotified == assignment.nrAssignedVariables())
339            return;
340        iLastNotified = assignment.nrAssignedVariables();
341        if (iCSVFile != null) {
342            TimetableModel model = (TimetableModel) solution.getModel();
343            iCSVFile.print(model.variables().size() - model.nrUnassignedVariables(assignment));
344            iCSVFile.print(colSeparator);
345            iCSVFile.print(sDoubleFormat.format(100.0 * assignment.nrAssignedVariables() / model.variables().size()));
346            iCSVFile.print(colSeparator);
347            iCSVFile.print(sDoubleFormat.format((solution.getTime()) / 60.0));
348            iCSVFile.print(colSeparator);
349            iCSVFile.print(solution.getIteration());
350            iCSVFile.print(colSeparator);
351            iCSVFile.print(sDoubleFormat.format(100.0 * assignment.nrAssignedVariables() / solution.getIteration()));
352            iCSVFile.print(colSeparator);
353            iCSVFile.print(sDoubleFormat.format((solution.getIteration()) / solution.getTime()));
354            iCSVFile.print(colSeparator);
355            iCSVFile.print(model.perturbVariables(assignment).size());
356            iCSVFile.print(colSeparator);
357            iCSVFile.print(sDoubleFormat.format(100.0 * model.perturbVariables(assignment).size() / model.variables().size()));
358            iCSVFile.print(colSeparator);
359            iCSVFile.print(Math.round(solution.getModel().getCriterion(StudentHardConflict.class).getValue(assignment)));
360            iCSVFile.print(colSeparator);
361            iCSVFile.print(Math.round(solution.getModel().getCriterion(StudentConflict.class).getValue(assignment)));
362            iCSVFile.print(colSeparator);
363            iCSVFile.print(Math.round(solution.getModel().getCriterion(StudentDistanceConflict.class).getValue(assignment)));
364            iCSVFile.print(colSeparator);
365            iCSVFile.print(Math.round(solution.getModel().getCriterion(StudentCommittedConflict.class).getValue(assignment)));
366            iCSVFile.print(colSeparator);
367            iCSVFile.print(sDoubleFormat.format(solution.getModel().getCriterion(TimePreferences.class).getValue(assignment)));
368            iCSVFile.print(colSeparator);
369            iCSVFile.print(Math.round(solution.getModel().getCriterion(RoomPreferences.class).getValue(assignment)));
370            iCSVFile.print(colSeparator);
371            iCSVFile.print(Math.round(solution.getModel().getCriterion(BackToBackInstructorPreferences.class).getValue(assignment)));
372            iCSVFile.print(colSeparator);
373            iCSVFile.print(Math.round(solution.getModel().getCriterion(DistributionPreferences.class).getValue(assignment)));
374            iCSVFile.print(colSeparator);
375            iCSVFile.print(Math.round(solution.getModel().getCriterion(UselessHalfHours.class).getValue(assignment)));
376            iCSVFile.print(colSeparator);
377            iCSVFile.print(Math.round(solution.getModel().getCriterion(BrokenTimePatterns.class).getValue(assignment)));
378            iCSVFile.print(colSeparator);
379            iCSVFile.print(Math.round(solution.getModel().getCriterion(TooBigRooms.class).getValue(assignment)));
380            if (iProp != null) {
381                if (solution.getModel().nrUnassignedVariables(assignment) > 0) {
382                    int goodVariables = 0;
383                    long goodValues = 0;
384                    long allValues = 0;
385                    for (Lecture variable : ((TimetableModel) solution.getModel()).unassignedVariables(assignment)) {
386                        goodValues += iProp.goodValues(assignment, variable).size();
387                        allValues += variable.values(solution.getAssignment()).size();
388                        if (!iProp.goodValues(assignment, variable).isEmpty())
389                            goodVariables++;
390                    }
391                    iCSVFile.print(colSeparator);
392                    iCSVFile.print(goodVariables);
393                    iCSVFile.print(colSeparator);
394                    iCSVFile.print(sDoubleFormat.format(100.0 * goodVariables / solution.getModel().nrUnassignedVariables(assignment)));
395                    iCSVFile.print(colSeparator);
396                    iCSVFile.print(goodValues);
397                    iCSVFile.print(colSeparator);
398                    iCSVFile.print(sDoubleFormat.format(100.0 * goodValues / allValues));
399                } else {
400                    iCSVFile.print(colSeparator);
401                    iCSVFile.print(colSeparator);
402                    iCSVFile.print(colSeparator);
403                    iCSVFile.print(colSeparator);
404                }
405            }
406            iCSVFile.println();
407            iCSVFile.flush();
408        }
409    }
410
411    /** Print room utilization 
412     * @param pw writer
413     * @param model problem model
414     * @param assignment current assignment
415     **/
416    public static void printRoomInfo(PrintWriter pw, TimetableModel model, Assignment<Lecture, Placement> assignment) {
417        pw.println("Room info:");
418        pw.println("id, name, size, used_day, used_total");
419        int firstDaySlot = model.getProperties().getPropertyInt("General.FirstDaySlot", Constants.DAY_SLOTS_FIRST);
420        int lastDaySlot = model.getProperties().getPropertyInt("General.LastDaySlot", Constants.DAY_SLOTS_LAST);
421        int firstWorkDay = model.getProperties().getPropertyInt("General.FirstWorkDay", 0);
422        int lastWorkDay = model.getProperties().getPropertyInt("General.LastWorkDay", Constants.NR_DAYS_WEEK - 1);
423        for (RoomConstraint rc : model.getRoomConstraints()) {
424            int used_day = 0;
425            int used_total = 0;
426            for (int day = firstWorkDay; day <= lastWorkDay; day++) {
427                for (int time = firstDaySlot; time <= lastDaySlot; time++) {
428                    if (!rc.getContext(assignment).getPlacements(day * Constants.SLOTS_PER_DAY + time).isEmpty())
429                        used_day++;
430                }
431            }
432            for (int day = 0; day < Constants.DAY_CODES.length; day++) {
433                for (int time = 0; time < Constants.SLOTS_PER_DAY; time++) {
434                    if (!rc.getContext(assignment).getPlacements(day * Constants.SLOTS_PER_DAY + time).isEmpty())
435                        used_total++;
436                }
437            }
438            pw.println(rc.getResourceId() + "," + rc.getName() + "," + rc.getCapacity() + "," + used_day + "," + used_total);
439        }
440    }
441
442    /** Class information 
443     * @param pw writer
444     * @param model problem model
445     **/
446    public static void printClassInfo(PrintWriter pw, TimetableModel model) {
447        pw.println("Class info:");
448        pw.println("id, name, min_class_limit, max_class_limit, room2limit_ratio, half_hours");
449        for (Lecture lecture : model.variables()) {
450            TimeLocation time = lecture.timeLocations().get(0);
451            pw.println(lecture.getClassId() + "," + lecture.getName() + "," + lecture.minClassLimit() + ","
452                    + lecture.maxClassLimit() + "," + lecture.roomToLimitRatio() + ","
453                    + (time.getNrSlotsPerMeeting() * time.getNrMeetings()));
454        }
455    }
456
457    /** Create info.txt with some more information about the problem 
458     * @param solution current solution
459     * @throws IOException an exception that may be thrown
460     **/
461    public static void printSomeStuff(Solution<Lecture, Placement> solution) throws IOException {
462        TimetableModel model = (TimetableModel) solution.getModel();
463        Assignment<Lecture, Placement> assignment = solution.getAssignment();
464        File outDir = new File(model.getProperties().getProperty("General.Output", "."));
465        PrintWriter pw = new PrintWriter(new FileWriter(outDir.toString() + File.separator + "info.txt"));
466        PrintWriter pwi = new PrintWriter(new FileWriter(outDir.toString() + File.separator + "info.csv"));
467        String name = new File(model.getProperties().getProperty("General.Input")).getName();
468        pwi.println("Instance," + name.substring(0, name.lastIndexOf('.')));
469        pw.println("Solution info: " + ToolBox.dict2string(solution.getInfo(), 1));
470        pw.println("Bounds: " + ToolBox.dict2string(model.getBounds(assignment), 1));
471        Map<String, String> info = solution.getInfo();
472        for (String key : new TreeSet<String>(info.keySet())) {
473            if (key.equals("Memory usage"))
474                continue;
475            if (key.equals("Iteration"))
476                continue;
477            if (key.equals("Time"))
478                continue;
479            String value = info.get(key);
480            if (value.indexOf(' ') > 0)
481                value = value.substring(0, value.indexOf(' '));
482            pwi.println(key + "," + value);
483        }
484        printRoomInfo(pw, model, assignment);
485        printClassInfo(pw, model);
486        long nrValues = 0;
487        long nrTimes = 0;
488        long nrRooms = 0;
489        double totalMaxNormTimePref = 0.0;
490        double totalMinNormTimePref = 0.0;
491        double totalNormTimePref = 0.0;
492        int totalMaxRoomPref = 0;
493        int totalMinRoomPref = 0;
494        int totalRoomPref = 0;
495        long nrStudentEnrls = 0;
496        long nrInevitableStudentConflicts = 0;
497        long nrJenrls = 0;
498        int nrHalfHours = 0;
499        int nrMeetings = 0;
500        int totalMinLimit = 0;
501        int totalMaxLimit = 0;
502        long nrReqRooms = 0;
503        int nrSingleValueVariables = 0;
504        int nrSingleTimeVariables = 0;
505        int nrSingleRoomVariables = 0;
506        long totalAvailableMinRoomSize = 0;
507        long totalAvailableMaxRoomSize = 0;
508        long totalRoomSize = 0;
509        long nrOneOrMoreRoomVariables = 0;
510        long nrOneRoomVariables = 0;
511        HashSet<Student> students = new HashSet<Student>();
512        HashSet<Long> offerings = new HashSet<Long>();
513        HashSet<Long> configs = new HashSet<Long>();
514        HashSet<Long> subparts = new HashSet<Long>();
515        int[] sizeLimits = new int[] { 0, 25, 50, 75, 100, 150, 200, 400 };
516        int[] nrRoomsOfSize = new int[sizeLimits.length];
517        int[] minRoomOfSize = new int[sizeLimits.length];
518        int[] maxRoomOfSize = new int[sizeLimits.length];
519        int[] totalUsedSlots = new int[sizeLimits.length];
520        int[] totalUsedSeats = new int[sizeLimits.length];
521        int[] totalUsedSeats2 = new int[sizeLimits.length];
522        int firstDaySlot = model.getProperties().getPropertyInt("General.FirstDaySlot", Constants.DAY_SLOTS_FIRST);
523        int lastDaySlot = model.getProperties().getPropertyInt("General.LastDaySlot", Constants.DAY_SLOTS_LAST);
524        int firstWorkDay = model.getProperties().getPropertyInt("General.FirstWorkDay", 0);
525        int lastWorkDay = model.getProperties().getPropertyInt("General.LastWorkDay", Constants.NR_DAYS_WEEK - 1);
526        for (Lecture lect : model.variables()) {
527            if (lect.getConfiguration() != null) {
528                offerings.add(lect.getConfiguration().getOfferingId());
529                configs.add(lect.getConfiguration().getConfigId());
530            }
531            subparts.add(lect.getSchedulingSubpartId());
532            nrStudentEnrls += (lect.students() == null ? 0 : lect.students().size());
533            students.addAll(lect.students());
534            nrValues += lect.values(solution.getAssignment()).size();
535            nrReqRooms += lect.getNrRooms();
536            for (RoomLocation room: lect.roomLocations())
537                if (room.getPreference() < Constants.sPreferenceLevelProhibited / 2)
538                    nrRooms++;
539            for (TimeLocation time: lect.timeLocations())
540                if (time.getPreference() < Constants.sPreferenceLevelProhibited / 2)
541                    nrTimes ++;
542            totalMinLimit += lect.minClassLimit();
543            totalMaxLimit += lect.maxClassLimit();
544            if (!lect.values(solution.getAssignment()).isEmpty()) {
545                Placement p = lect.values(solution.getAssignment()).get(0);
546                nrMeetings += p.getTimeLocation().getNrMeetings();
547                nrHalfHours += p.getTimeLocation().getNrMeetings() * p.getTimeLocation().getNrSlotsPerMeeting();
548                totalMaxNormTimePref += lect.getMinMaxTimePreference()[1];
549                totalMinNormTimePref += lect.getMinMaxTimePreference()[0];
550                totalNormTimePref += Math.abs(lect.getMinMaxTimePreference()[1] - lect.getMinMaxTimePreference()[0]);
551                totalMaxRoomPref += lect.getMinMaxRoomPreference()[1];
552                totalMinRoomPref += lect.getMinMaxRoomPreference()[0];
553                totalRoomPref += Math.abs(lect.getMinMaxRoomPreference()[1] - lect.getMinMaxRoomPreference()[0]);
554                TimeLocation time = p.getTimeLocation();
555                boolean hasRoomConstraint = false;
556                for (RoomLocation roomLocation : lect.roomLocations()) {
557                    if (roomLocation.getRoomConstraint().getConstraint())
558                        hasRoomConstraint = true;
559                }
560                if (hasRoomConstraint && lect.getNrRooms() > 0) {
561                    for (int d = firstWorkDay; d <= lastWorkDay; d++) {
562                        if ((time.getDayCode() & Constants.DAY_CODES[d]) == 0)
563                            continue;
564                        for (int t = Math.max(time.getStartSlot(), firstDaySlot); t <= Math.min(time.getStartSlot() + time.getLength() - 1, lastDaySlot); t++) {
565                            for (int l = 0; l < sizeLimits.length; l++) {
566                                if (sizeLimits[l] <= lect.minRoomSize()) {
567                                    totalUsedSlots[l] += lect.getNrRooms();
568                                    totalUsedSeats[l] += lect.classLimit(assignment);
569                                    totalUsedSeats2[l] += lect.minRoomSize() * lect.getNrRooms();
570                                }
571                            }
572                        }
573                    }
574                }
575            }
576            if (lect.values(solution.getAssignment()).size() == 1) {
577                nrSingleValueVariables++;
578            }
579            if (lect.timeLocations().size() == 1) {
580                nrSingleTimeVariables++;
581            }
582            if (lect.roomLocations().size() == 1) {
583                nrSingleRoomVariables++;
584            }
585            if (lect.getNrRooms() == 1) {
586                nrOneRoomVariables++;
587            }
588            if (lect.getNrRooms() > 0) {
589                nrOneOrMoreRoomVariables++;
590            }
591            if (!lect.roomLocations().isEmpty()) {
592                int minRoomSize = Integer.MAX_VALUE;
593                int maxRoomSize = Integer.MIN_VALUE;
594                for (RoomLocation rl : lect.roomLocations()) {
595                    minRoomSize = Math.min(minRoomSize, rl.getRoomSize());
596                    maxRoomSize = Math.max(maxRoomSize, rl.getRoomSize());
597                    totalRoomSize += rl.getRoomSize();
598                }
599                totalAvailableMinRoomSize += minRoomSize;
600                totalAvailableMaxRoomSize += maxRoomSize;
601            }
602        }
603        for (JenrlConstraint jenrl : model.getJenrlConstraints()) {
604            nrJenrls += jenrl.getJenrl();
605            if ((jenrl.first()).timeLocations().size() == 1 && (jenrl.second()).timeLocations().size() == 1) {
606                TimeLocation t1 = jenrl.first().timeLocations().get(0);
607                TimeLocation t2 = jenrl.second().timeLocations().get(0);
608                if (t1.hasIntersection(t2)) {
609                    nrInevitableStudentConflicts += jenrl.getJenrl();
610                    pw.println("Inevitable " + jenrl.getJenrl() + " student conflicts between " + jenrl.first() + " "
611                            + t1 + " and " + jenrl.second() + " " + t2);
612                } else if (jenrl.first().values(solution.getAssignment()).size() == 1 && jenrl.second().values(solution.getAssignment()).size() == 1) {
613                    Placement p1 = jenrl.first().values(solution.getAssignment()).get(0);
614                    Placement p2 = jenrl.second().values(solution.getAssignment()).get(0);
615                    if (JenrlConstraint.isInConflict(p1, p2, ((TimetableModel)p1.variable().getModel()).getDistanceMetric())) {
616                        nrInevitableStudentConflicts += jenrl.getJenrl();
617                        pw.println("Inevitable " + jenrl.getJenrl()
618                                + (p1.getTimeLocation().hasIntersection(p2.getTimeLocation()) ? "" : " distance")
619                                + " student conflicts between " + p1 + " and " + p2);
620                    }
621                }
622            }
623        }
624        int totalCommitedPlacements = 0;
625        for (Student student : students) {
626            if (student.getCommitedPlacements() != null)
627                totalCommitedPlacements += student.getCommitedPlacements().size();
628        }
629        pw.println("Total number of classes: " + model.variables().size());
630        pwi.println("Number of classes," + model.variables().size());
631        pw.println("Total number of instructional offerings: " + offerings.size() + " ("
632                + sDoubleFormat.format(100.0 * offerings.size() / model.variables().size()) + "%)");
633        // pwi.println("Number of instructional offerings,"+offerings.size());
634        pw.println("Total number of configurations: " + configs.size() + " ("
635                + sDoubleFormat.format(100.0 * configs.size() / model.variables().size()) + "%)");
636        pw.println("Total number of scheduling subparts: " + subparts.size() + " ("
637                + sDoubleFormat.format(100.0 * subparts.size() / model.variables().size()) + "%)");
638        // pwi.println("Number of scheduling subparts,"+subparts.size());
639        pw.println("Average number classes per subpart: "
640                + sDoubleFormat.format(1.0 * model.variables().size() / subparts.size()));
641        pwi.println("Avg. classes per instruction,"
642                + sDoubleFormat.format(1.0 * model.variables().size() / subparts.size()));
643        pw.println("Average number classes per config: "
644                + sDoubleFormat.format(1.0 * model.variables().size() / configs.size()));
645        pw.println("Average number classes per offering: "
646                + sDoubleFormat.format(1.0 * model.variables().size() / offerings.size()));
647        pw.println("Total number of classes with only one value: " + nrSingleValueVariables + " ("
648                + sDoubleFormat.format(100.0 * nrSingleValueVariables / model.variables().size()) + "%)");
649        pw.println("Total number of classes with only one time: " + nrSingleTimeVariables + " ("
650                + sDoubleFormat.format(100.0 * nrSingleTimeVariables / model.variables().size()) + "%)");
651        pw.println("Total number of classes with only one room: " + nrSingleRoomVariables + " ("
652                + sDoubleFormat.format(100.0 * nrSingleRoomVariables / model.variables().size()) + "%)");
653        pwi.println("Classes with single value," + nrSingleValueVariables);
654        // pwi.println("Classes with only one time/room,"+nrSingleTimeVariables+"/"+nrSingleRoomVariables);
655        pw.println("Total number of classes requesting no room: "
656                + (model.variables().size() - nrOneOrMoreRoomVariables)
657                + " ("
658                + sDoubleFormat.format(100.0 * (model.variables().size() - nrOneOrMoreRoomVariables)
659                        / model.variables().size()) + "%)");
660        pw.println("Total number of classes requesting one room: " + nrOneRoomVariables + " ("
661                + sDoubleFormat.format(100.0 * nrOneRoomVariables / model.variables().size()) + "%)");
662        pw.println("Total number of classes requesting one or more rooms: " + nrOneOrMoreRoomVariables + " ("
663                + sDoubleFormat.format(100.0 * nrOneOrMoreRoomVariables / model.variables().size()) + "%)");
664        // pwi.println("% classes requesting no room,"+sDoubleFormat.format(100.0*(model.variables().size()-nrOneOrMoreRoomVariables)/model.variables().size())+"%");
665        // pwi.println("% classes requesting one room,"+sDoubleFormat.format(100.0*nrOneRoomVariables/model.variables().size())+"%");
666        // pwi.println("% classes requesting two or more rooms,"+sDoubleFormat.format(100.0*(nrOneOrMoreRoomVariables-nrOneRoomVariables)/model.variables().size())+"%");
667        pw.println("Average number of requested rooms: "
668                + sDoubleFormat.format(1.0 * nrReqRooms / model.variables().size()));
669        pw.println("Average minimal class limit: "
670                + sDoubleFormat.format(1.0 * totalMinLimit / model.variables().size()));
671        pw.println("Average maximal class limit: "
672                + sDoubleFormat.format(1.0 * totalMaxLimit / model.variables().size()));
673        // pwi.println("Average class limit,"+sDoubleFormat.format(1.0*(totalMinLimit+totalMaxLimit)/(2*model.variables().size())));
674        pw.println("Average number of placements: " + sDoubleFormat.format(1.0 * nrValues / model.variables().size()));
675        // pwi.println("Average domain size,"+sDoubleFormat.format(1.0*nrValues/model.variables().size()));
676        pwi.println("Avg. domain size," + sDoubleFormat.format(1.0 * nrValues / model.variables().size()));
677        pw.println("Average number of time locations: "
678                + sDoubleFormat.format(1.0 * nrTimes / model.variables().size()));
679        pwi.println("Avg. number of avail. times/rooms,"
680                + sDoubleFormat.format(1.0 * nrTimes / model.variables().size()) + "/"
681                + sDoubleFormat.format(1.0 * nrRooms / model.variables().size()));
682        pw.println("Average number of room locations: "
683                + sDoubleFormat.format(1.0 * nrRooms / model.variables().size()));
684        pw.println("Average minimal requested room size: "
685                + sDoubleFormat.format(1.0 * totalAvailableMinRoomSize / nrOneOrMoreRoomVariables));
686        pw.println("Average maximal requested room size: "
687                + sDoubleFormat.format(1.0 * totalAvailableMaxRoomSize / nrOneOrMoreRoomVariables));
688        pw.println("Average requested room sizes: " + sDoubleFormat.format(1.0 * totalRoomSize / nrRooms));
689        pwi.println("Average requested room size," + sDoubleFormat.format(1.0 * totalRoomSize / nrRooms));
690        pw.println("Average maximum normalized time preference: "
691                + sDoubleFormat.format(totalMaxNormTimePref / model.variables().size()));
692        pw.println("Average minimum normalized time preference: "
693                + sDoubleFormat.format(totalMinNormTimePref / model.variables().size()));
694        pw.println("Average normalized time preference,"
695                + sDoubleFormat.format(totalNormTimePref / model.variables().size()));
696        pw.println("Average maximum room preferences: "
697                + sDoubleFormat.format(1.0 * totalMaxRoomPref / nrOneOrMoreRoomVariables));
698        pw.println("Average minimum room preferences: "
699                + sDoubleFormat.format(1.0 * totalMinRoomPref / nrOneOrMoreRoomVariables));
700        pw.println("Average room preferences," + sDoubleFormat.format(1.0 * totalRoomPref / nrOneOrMoreRoomVariables));
701        pw.println("Total number of students:" + students.size());
702        pwi.println("Number of students," + students.size());
703        pwi.println("Number of inevitable student conflicts," + nrInevitableStudentConflicts);
704        pw.println("Total amount of student enrollments: " + nrStudentEnrls);
705        pwi.println("Number of student enrollments," + nrStudentEnrls);
706        pw.println("Total amount of joined enrollments: " + nrJenrls);
707        pwi.println("Number of joint student enrollments," + nrJenrls);
708        pw.println("Average number of students: "
709                + sDoubleFormat.format(1.0 * students.size() / model.variables().size()));
710        pw.println("Average number of enrollemnts (per student): "
711                + sDoubleFormat.format(1.0 * nrStudentEnrls / students.size()));
712        pwi.println("Avg. number of classes per student,"
713                + sDoubleFormat.format(1.0 * nrStudentEnrls / students.size()));
714        pwi.println("Avg. number of committed classes per student,"
715                + sDoubleFormat.format(1.0 * totalCommitedPlacements / students.size()));
716        pw.println("Total amount of inevitable student conflicts: " + nrInevitableStudentConflicts + " ("
717                + sDoubleFormat.format(100.0 * nrInevitableStudentConflicts / nrStudentEnrls) + "%)");
718        pw.println("Average number of meetings (per class): "
719                + sDoubleFormat.format(1.0 * nrMeetings / model.variables().size()));
720        pw.println("Average number of hours per class: "
721                + sDoubleFormat.format(1.0 * nrHalfHours / model.variables().size() / 12.0));
722        pwi.println("Avg. number of meetings per class,"
723                + sDoubleFormat.format(1.0 * nrMeetings / model.variables().size()));
724        pwi.println("Avg. number of hours per class,"
725                + sDoubleFormat.format(1.0 * nrHalfHours / model.variables().size() / 12.0));
726        int minRoomSize = Integer.MAX_VALUE;
727        int maxRoomSize = Integer.MIN_VALUE;
728        int nrDistancePairs = 0;
729        double maxRoomDistance = Double.MIN_VALUE;
730        double totalRoomDistance = 0.0;
731        int[] totalAvailableSlots = new int[sizeLimits.length];
732        int[] totalAvailableSeats = new int[sizeLimits.length];
733        int nrOfRooms = 0;
734        totalRoomSize = 0;
735        for (RoomConstraint rc : model.getRoomConstraints()) {
736            if (rc.variables().isEmpty()) continue;
737            nrOfRooms++;
738            minRoomSize = Math.min(minRoomSize, rc.getCapacity());
739            maxRoomSize = Math.max(maxRoomSize, rc.getCapacity());
740            for (int l = 0; l < sizeLimits.length; l++) {
741                if (sizeLimits[l] <= rc.getCapacity()
742                        && (l + 1 == sizeLimits.length || rc.getCapacity() < sizeLimits[l + 1])) {
743                    nrRoomsOfSize[l]++;
744                    if (minRoomOfSize[l] == 0)
745                        minRoomOfSize[l] = rc.getCapacity();
746                    else
747                        minRoomOfSize[l] = Math.min(minRoomOfSize[l], rc.getCapacity());
748                    if (maxRoomOfSize[l] == 0)
749                        maxRoomOfSize[l] = rc.getCapacity();
750                    else
751                        maxRoomOfSize[l] = Math.max(maxRoomOfSize[l], rc.getCapacity());
752                }
753            }
754            totalRoomSize += rc.getCapacity();
755            if (rc.getPosX() != null && rc.getPosY() != null) {
756                for (RoomConstraint rc2 : model.getRoomConstraints()) {
757                    if (rc2.getResourceId().compareTo(rc.getResourceId()) > 0 && rc2.getPosX() != null && rc2.getPosY() != null) {
758                        double distance = ((TimetableModel)solution.getModel()).getDistanceMetric().getDistanceInMinutes(rc.getId(), rc.getPosX(), rc.getPosY(), rc2.getId(), rc2.getPosX(), rc2.getPosY());
759                        totalRoomDistance += distance;
760                        nrDistancePairs++;
761                        maxRoomDistance = Math.max(maxRoomDistance, distance);
762                    }
763                }
764            }
765            for (int d = firstWorkDay; d <= lastWorkDay; d++) {
766                for (int t = firstDaySlot; t <= lastDaySlot; t++) {
767                    if (rc.isAvailable(d * Constants.SLOTS_PER_DAY + t)) {
768                        for (int l = 0; l < sizeLimits.length; l++) {
769                            if (sizeLimits[l] <= rc.getCapacity()) {
770                                totalAvailableSlots[l]++;
771                                totalAvailableSeats[l] += rc.getCapacity();
772                            }
773                        }
774                    }
775                }
776            }
777        }
778        pw.println("Total number of rooms: " + nrOfRooms);
779        pwi.println("Number of rooms," + nrOfRooms);
780        pw.println("Minimal room size: " + minRoomSize);
781        pw.println("Maximal room size: " + maxRoomSize);
782        pwi.println("Room size min/max," + minRoomSize + "/" + maxRoomSize);
783        pw.println("Average room size: "
784                + sDoubleFormat.format(1.0 * totalRoomSize / model.getRoomConstraints().size()));
785        pw.println("Maximal distance between two rooms: " + sDoubleFormat.format(maxRoomDistance));
786        pw.println("Average distance between two rooms: "
787                + sDoubleFormat.format(totalRoomDistance / nrDistancePairs));
788        pwi.println("Average distance between two rooms [min],"
789                + sDoubleFormat.format(totalRoomDistance / nrDistancePairs));
790        pwi.println("Maximal distance between two rooms [min]," + sDoubleFormat.format(maxRoomDistance));
791        for (int l = 0; l < sizeLimits.length; l++) {// sizeLimits.length;l++) {
792            pwi.println("\"Room frequency (size>=" + sizeLimits[l] + ", used/avaiable times)\","
793                    + sDoubleFormat.format(100.0 * totalUsedSlots[l] / totalAvailableSlots[l]) + "%");
794            pwi.println("\"Room utilization (size>=" + sizeLimits[l] + ", used/available seats)\","
795                    + sDoubleFormat.format(100.0 * totalUsedSeats[l] / totalAvailableSeats[l]) + "%");
796            pwi.println("\"Number of rooms (size>=" + sizeLimits[l] + ")\"," + nrRoomsOfSize[l]);
797            pwi.println("\"Min/max room size (size>=" + sizeLimits[l] + ")\"," + minRoomOfSize[l] + "-"
798                    + maxRoomOfSize[l]);
799            // pwi.println("\"Room utilization (size>="+sizeLimits[l]+", minRoomSize)\","+sDoubleFormat.format(100.0*totalUsedSeats2[l]/totalAvailableSeats[l])+"%");
800        }
801        pw.println("Average hours available: "
802                + sDoubleFormat.format(1.0 * totalAvailableSlots[0] / nrOfRooms / 12.0));
803        int totalInstructedClasses = 0;
804        for (InstructorConstraint ic : model.getInstructorConstraints()) {
805            totalInstructedClasses += ic.variables().size();
806        }
807        pw.println("Total number of instructors: " + model.getInstructorConstraints().size());
808        pwi.println("Number of instructors," + model.getInstructorConstraints().size());
809        pw.println("Total class-instructor assignments: " + totalInstructedClasses + " ("
810                + sDoubleFormat.format(100.0 * totalInstructedClasses / model.variables().size()) + "%)");
811        pwi.println("Number of class-instructor assignments," + totalInstructedClasses);
812        pw.println("Average classes per instructor: "
813                + sDoubleFormat.format(1.0 * totalInstructedClasses / model.getInstructorConstraints().size()));
814        pwi.println("Average classes per instructor,"
815                + sDoubleFormat.format(1.0 * totalInstructedClasses / model.getInstructorConstraints().size()));
816        // pw.println("Average hours available: "+sDoubleFormat.format(1.0*totalAvailableSlots/model.getInstructorConstraints().size()/12.0));
817        // pwi.println("Instructor availability [h],"+sDoubleFormat.format(1.0*totalAvailableSlots/model.getInstructorConstraints().size()/12.0));
818        int nrGroupConstraints = model.getGroupConstraints().size() + model.getSpreadConstraints().size();
819        int nrHardGroupConstraints = 0;
820        int nrVarsInGroupConstraints = 0;
821        for (GroupConstraint gc : model.getGroupConstraints()) {
822            if (gc.isHard())
823                nrHardGroupConstraints++;
824            nrVarsInGroupConstraints += gc.variables().size();
825        }
826        for (SpreadConstraint sc : model.getSpreadConstraints()) {
827            nrVarsInGroupConstraints += sc.variables().size();
828        }
829        pw.println("Total number of group constraints: " + nrGroupConstraints + " ("
830                + sDoubleFormat.format(100.0 * nrGroupConstraints / model.variables().size()) + "%)");
831        // pwi.println("Number of group constraints,"+nrGroupConstraints);
832        pw.println("Total number of hard group constraints: " + nrHardGroupConstraints + " ("
833                + sDoubleFormat.format(100.0 * nrHardGroupConstraints / model.variables().size()) + "%)");
834        // pwi.println("Number of hard group constraints,"+nrHardGroupConstraints);
835        pw.println("Average classes per group constraint: "
836                + sDoubleFormat.format(1.0 * nrVarsInGroupConstraints / nrGroupConstraints));
837        // pwi.println("Average classes per group constraint,"+sDoubleFormat.format(1.0*nrVarsInGroupConstraints/nrGroupConstraints));
838        pwi.println("Avg. number distribution constraints per class,"
839                + sDoubleFormat.format(1.0 * nrVarsInGroupConstraints / model.variables().size()));
840        pwi.println("Joint enrollment constraints," + model.getJenrlConstraints().size());
841        pw.flush();
842        pw.close();
843        pwi.flush();
844        pwi.close();
845    }
846
847    public static void saveOutputCSV(Solution<Lecture, Placement> s, File file) {
848        try {
849            DecimalFormat dx = new DecimalFormat("000");
850            PrintWriter w = new PrintWriter(new FileWriter(file));
851            TimetableModel m = (TimetableModel) s.getModel();
852            int firstDaySlot = m.getProperties().getPropertyInt("General.FirstDaySlot", Constants.DAY_SLOTS_FIRST);
853            int lastDaySlot = m.getProperties().getPropertyInt("General.LastDaySlot", Constants.DAY_SLOTS_LAST);
854            int firstWorkDay = m.getProperties().getPropertyInt("General.FirstWorkDay", 0);
855            int lastWorkDay = m.getProperties().getPropertyInt("General.LastWorkDay", Constants.NR_DAYS_WEEK - 1);
856            Assignment<Lecture, Placement> a = s.getAssignment();
857            int idx = 1;
858            w.println("000." + dx.format(idx++) + " Assigned variables," + a.nrAssignedVariables());
859            w.println("000." + dx.format(idx++) + " Time [sec]," + sDoubleFormat.format(s.getBestTime()));
860            w.println("000." + dx.format(idx++) + " Hard student conflicts," + Math.round(m.getCriterion(StudentHardConflict.class).getValue(a)));
861            if (m.getProperties().getPropertyBoolean("General.UseDistanceConstraints", true))
862                w.println("000." + dx.format(idx++) + " Distance student conf.," + Math.round(m.getCriterion(StudentDistanceConflict.class).getValue(a)));
863            w.println("000." + dx.format(idx++) + " Student conflicts," + Math.round(m.getCriterion(StudentConflict.class).getValue(a)));
864            w.println("000." + dx.format(idx++) + " Committed student conflicts," + Math.round(m.getCriterion(StudentCommittedConflict.class).getValue(a)));
865            w.println("000." + dx.format(idx++) + " All Student conflicts,"
866                    + Math.round(m.getCriterion(StudentConflict.class).getValue(a) + m.getCriterion(StudentCommittedConflict.class).getValue(a)));
867            w.println("000." + dx.format(idx++) + " Time preferences,"
868                    + sDoubleFormat.format( m.getCriterion(TimePreferences.class).getValue(a)));
869            w.println("000." + dx.format(idx++) + " Room preferences," + Math.round(m.getCriterion(RoomPreferences.class).getValue(a)));
870            w.println("000." + dx.format(idx++) + " Useless half-hours," + Math.round(m.getCriterion(UselessHalfHours.class).getValue(a)));
871            w.println("000." + dx.format(idx++) + " Broken time patterns," + Math.round(m.getCriterion(BrokenTimePatterns.class).getValue(a)));
872            w.println("000." + dx.format(idx++) + " Too big room," + Math.round(m.getCriterion(TooBigRooms.class).getValue(a)));
873            w.println("000." + dx.format(idx++) + " Distribution preferences," + sDoubleFormat.format(m.getCriterion(DistributionPreferences.class).getValue(a)));
874            if (m.getProperties().getPropertyBoolean("General.UseDistanceConstraints", true))
875                w.println("000." + dx.format(idx++) + " Back-to-back instructor pref.," + Math.round(m.getCriterion(BackToBackInstructorPreferences.class).getValue(a)));
876            if (m.getProperties().getPropertyBoolean("General.DeptBalancing", true)) {
877                w.println("000." + dx.format(idx++) + " Dept. balancing penalty," + sDoubleFormat.format(m.getCriterion(DepartmentBalancingPenalty.class).getValue(a)));
878            }
879            w.println("000." + dx.format(idx++) + " Same subpart balancing penalty," + sDoubleFormat.format(m.getCriterion(SameSubpartBalancingPenalty.class).getValue(a)));
880            if (m.getProperties().getPropertyBoolean("General.MPP", false)) {
881                Map<String, Double> mppInfo = ((UniversalPerturbationsCounter)((Perturbations)m.getCriterion(Perturbations.class)).getPerturbationsCounter()).getCompactInfo(a, m, false, false);
882                int pidx = 51;
883                w.println("000." + dx.format(pidx++) + " Perturbation penalty," + sDoubleFormat.format(m.getCriterion(Perturbations.class).getValue(a)));
884                w.println("000." + dx.format(pidx++) + " Additional perturbations," + m.perturbVariables(a).size());
885                int nrPert = 0, nrStudentPert = 0;
886                for (Lecture lecture : m.variables()) {
887                    if (lecture.getInitialAssignment() != null)
888                        continue;
889                    nrPert++;
890                    nrStudentPert += lecture.classLimit(a);
891                }
892                w.println("000." + dx.format(pidx++) + " Given perturbations," + nrPert);
893                w.println("000." + dx.format(pidx++) + " Given student perturbations," + nrStudentPert);
894                for (String key : new TreeSet<String>(mppInfo.keySet())) {
895                    Double value = mppInfo.get(key);
896                    w.println("000." + dx.format(pidx++) + " " + key + "," + sDoubleFormat.format(value));
897                }
898            }
899            HashSet<Student> students = new HashSet<Student>();
900            int enrls = 0;
901            int minRoomPref = 0, maxRoomPref = 0;
902            int minGrPref = 0, maxGrPref = 0;
903            int minTimePref = 0, maxTimePref = 0;
904            int worstInstrPref = 0;
905            HashSet<Constraint<Lecture, Placement>> used = new HashSet<Constraint<Lecture, Placement>>();
906            for (Lecture lecture : m.variables()) {
907                enrls += (lecture.students() == null ? 0 : lecture.students().size());
908                students.addAll(lecture.students());
909
910                int[] minMaxRoomPref = lecture.getMinMaxRoomPreference();
911                maxRoomPref += minMaxRoomPref[1] - minMaxRoomPref[0];
912
913                double[] minMaxTimePref = lecture.getMinMaxTimePreference();
914                maxTimePref += minMaxTimePref[1] - minMaxTimePref[0];
915                for (Constraint<Lecture, Placement> c : lecture.constraints()) {
916                    if (!used.add(c))
917                        continue;
918
919                    if (c instanceof InstructorConstraint) {
920                        InstructorConstraint ic = (InstructorConstraint) c;
921                        worstInstrPref += ic.getWorstPreference();
922                    }
923
924                    if (c instanceof GroupConstraint) {
925                        GroupConstraint gc = (GroupConstraint) c;
926                        if (gc.isHard())
927                            continue;
928                        maxGrPref += Math.abs(gc.getPreference()) * (1 + (gc.variables().size() * (gc.variables().size() - 1)) / 2);
929                    }
930                }
931            }
932            int totalCommitedPlacements = 0;
933            for (Student student : students) {
934                if (student.getCommitedPlacements() != null)
935                    totalCommitedPlacements += student.getCommitedPlacements().size();
936            }
937            HashMap<Long, List<Lecture>> subs = new HashMap<Long, List<Lecture>>();
938            for (Lecture lecture : m.variables()) {
939                if (lecture.isCommitted() || lecture.getScheduler() == null)
940                    continue;
941                List<Lecture> vars = subs.get(lecture.getScheduler());
942                if (vars == null) {
943                    vars = new ArrayList<Lecture>();
944                    subs.put(lecture.getScheduler(), vars);
945                }
946                vars.add(lecture);
947            }
948            int bidx = 101;
949            w.println("000." + dx.format(bidx++) + " Assigned variables max," + m.variables().size());
950            w.println("000." + dx.format(bidx++) + " Student enrollments," + enrls);
951            w.println("000." + dx.format(bidx++) + " Student commited enrollments," + totalCommitedPlacements);
952            w.println("000." + dx.format(bidx++) + " All student enrollments," + (enrls + totalCommitedPlacements));
953            w.println("000." + dx.format(bidx++) + " Time preferences min," + minTimePref);
954            w.println("000." + dx.format(bidx++) + " Time preferences max," + maxTimePref);
955            w.println("000." + dx.format(bidx++) + " Room preferences min," + minRoomPref);
956            w.println("000." + dx.format(bidx++) + " Room preferences max," + maxRoomPref);
957            w.println("000." + dx.format(bidx++) + " Useless half-hours max," +
958                    (Constants.sPreferenceLevelStronglyDiscouraged * m.getRoomConstraints().size() * (lastDaySlot - firstDaySlot + 1) * (lastWorkDay - firstWorkDay + 1)));
959            w.println("000." + dx.format(bidx++) + " Too big room max," + (Constants.sPreferenceLevelStronglyDiscouraged * m.variables().size()));
960            w.println("000." + dx.format(bidx++) + " Distribution preferences min," + minGrPref);
961            w.println("000." + dx.format(bidx++) + " Distribution preferences max," + maxGrPref);
962            w.println("000." + dx.format(bidx++) + " Back-to-back instructor pref max," + worstInstrPref);
963            for (Long scheduler: new TreeSet<Long>(subs.keySet())) {
964                List<Lecture> vars = subs.get(scheduler);
965                idx = 001;
966                bidx = 101;
967                int nrAssg = 0;
968                enrls = 0;
969                int roomPref = 0;
970                minRoomPref = 0;
971                maxRoomPref = 0;
972                double timePref = 0;
973                minTimePref = 0;
974                maxTimePref = 0;
975                double grPref = 0;
976                minGrPref = 0;
977                maxGrPref = 0;
978                long allSC = 0, hardSC = 0, distSC = 0;
979                int instPref = 0;
980                worstInstrPref = 0;
981                int spreadPen = 0, deptSpreadPen = 0;
982                int tooBigRooms = 0;
983                int rcs = 0, uselessSlots = 0;
984                used = new HashSet<Constraint<Lecture, Placement>>();
985                for (Lecture lecture : vars) {
986                    if (lecture.isCommitted())
987                        continue;
988                    enrls += lecture.students().size();
989                    Placement placement = a.getValue(lecture);
990                    if (placement != null) {
991                        nrAssg++;
992                    }
993
994                    int[] minMaxRoomPref = lecture.getMinMaxRoomPreference();
995                    minRoomPref += minMaxRoomPref[0];
996                    maxRoomPref += minMaxRoomPref[1];
997
998                    double[] minMaxTimePref = lecture.getMinMaxTimePreference();
999                    minTimePref += minMaxTimePref[0];
1000                    maxTimePref += minMaxTimePref[1];
1001
1002                    if (placement != null) {
1003                        roomPref += placement.getRoomPreference();
1004                        timePref += placement.getTimeLocation().getNormalizedPreference();
1005                        tooBigRooms += TooBigRooms.getTooBigRoomPreference(placement);
1006                    }
1007
1008                    for (Constraint<Lecture, Placement> c : lecture.constraints()) {
1009                        if (!used.add(c))
1010                            continue;
1011
1012                        if (c instanceof InstructorConstraint) {
1013                            InstructorConstraint ic = (InstructorConstraint) c;
1014                            instPref += ic.getPreference(a);
1015                            worstInstrPref += ic.getWorstPreference();
1016                        }
1017
1018                        if (c instanceof DepartmentSpreadConstraint) {
1019                            DepartmentSpreadConstraint dsc = (DepartmentSpreadConstraint) c;
1020                            deptSpreadPen += dsc.getPenalty(a);
1021                        } else if (c instanceof SpreadConstraint) {
1022                            SpreadConstraint sc = (SpreadConstraint) c;
1023                            spreadPen += sc.getPenalty(a);
1024                        }
1025
1026                        if (c instanceof GroupConstraint) {
1027                            GroupConstraint gc = (GroupConstraint) c;
1028                            if (gc.isHard())
1029                                continue;
1030                            minGrPref -= Math.abs(gc.getPreference());
1031                            maxGrPref += 0;
1032                            grPref += Math.min(0, gc.getCurrentPreference(a));
1033                            // minGrPref += Math.min(gc.getPreference(), 0);
1034                            // maxGrPref += Math.max(gc.getPreference(), 0);
1035                            // grPref += gc.getCurrentPreference();
1036                        }
1037
1038                        if (c instanceof JenrlConstraint) {
1039                            JenrlConstraint jc = (JenrlConstraint) c;
1040                            if (!jc.isInConflict(a) || !jc.isOfTheSameProblem())
1041                                continue;
1042                            Lecture l1 = jc.first();
1043                            Lecture l2 = jc.second();
1044                            allSC += jc.getJenrl();
1045                            if (l1.areStudentConflictsHard(l2))
1046                                hardSC += jc.getJenrl();
1047                            Placement p1 = a.getValue(l1);
1048                            Placement p2 = a.getValue(l2);
1049                            if (!p1.getTimeLocation().hasIntersection(p2.getTimeLocation()))
1050                                distSC += jc.getJenrl();
1051                        }
1052
1053                        if (c instanceof RoomConstraint) {
1054                            RoomConstraint rc = (RoomConstraint) c;
1055                            uselessSlots += UselessHalfHours.countUselessSlotsHalfHours(rc.getContext(a)) + BrokenTimePatterns.countUselessSlotsBrokenTimePatterns(rc.getContext(a));
1056                            rcs++;
1057                        }
1058                    }
1059                }
1060                w.println(dx.format(scheduler) + "." + dx.format(idx++) + " Assigned variables," + nrAssg);
1061                w.println(dx.format(scheduler) + "." + dx.format(bidx++) + " Assigned variables max," + vars.size());
1062                w.println(dx.format(scheduler) + "." + dx.format(idx++) + " Hard student conflicts," + hardSC);
1063                w.println(dx.format(scheduler) + "." + dx.format(bidx++) + " Student enrollments," + enrls);
1064                if (m.getProperties().getPropertyBoolean("General.UseDistanceConstraints", true))
1065                    w.println(dx.format(scheduler) + "." + dx.format(idx++) + " Distance student conf.," + distSC);
1066                w.println(dx.format(scheduler) + "." + dx.format(idx++) + " Student conflicts," + allSC);
1067                w.println(dx.format(scheduler) + "." + dx.format(idx++) + " Time preferences," + timePref);
1068                w.println(dx.format(scheduler) + "." + dx.format(bidx++) + " Time preferences min," + minTimePref);
1069                w.println(dx.format(scheduler) + "." + dx.format(bidx++) + " Time preferences max," + maxTimePref);
1070                w.println(dx.format(scheduler) + "." + dx.format(idx++) + " Room preferences," + roomPref);
1071                w.println(dx.format(scheduler) + "." + dx.format(bidx++) + " Room preferences min," + minRoomPref);
1072                w.println(dx.format(scheduler) + "." + dx.format(bidx++) + " Room preferences max," + maxRoomPref);
1073                w.println(dx.format(scheduler) + "." + dx.format(idx++) + " Useless half-hours," + uselessSlots);
1074                w.println(dx.format(scheduler) + "." + dx.format(bidx++) + " Useless half-hours max," +
1075                        (Constants.sPreferenceLevelStronglyDiscouraged * rcs * (lastDaySlot - firstDaySlot + 1) * (lastWorkDay - firstWorkDay + 1)));
1076                w.println(dx.format(scheduler) + "." + dx.format(idx++) + " Too big room," + tooBigRooms);
1077                w.println(dx.format(scheduler) + "." + dx.format(bidx++) + " Too big room max," + (Constants.sPreferenceLevelStronglyDiscouraged * vars.size()));
1078                w.println(dx.format(scheduler) + "." + dx.format(idx++) + " Distribution preferences," + grPref);
1079                w.println(dx.format(scheduler) + "." + dx.format(bidx++) + " Distribution preferences min," + minGrPref);
1080                w.println(dx.format(scheduler) + "." + dx.format(bidx++) + " Distribution preferences max," + maxGrPref);
1081                if (m.getProperties().getPropertyBoolean("General.UseDistanceConstraints", true))
1082                    w.println(dx.format(scheduler) + "." + dx.format(idx++) + " Back-to-back instructor pref," + instPref);
1083                w.println(dx.format(scheduler) + "." + dx.format(bidx++) + " Back-to-back instructor pref max," + worstInstrPref);
1084                if (m.getProperties().getPropertyBoolean("General.DeptBalancing", true)) {
1085                    w.println(dx.format(scheduler) + "." + dx.format(idx++) + " Department balancing penalty," + sDoubleFormat.format((deptSpreadPen) / 12.0));
1086                }
1087                w.println(dx.format(scheduler) + "." + dx.format(idx++) + " Same subpart balancing penalty," + sDoubleFormat.format((spreadPen) / 12.0));
1088            }
1089            w.flush();
1090            w.close();
1091        } catch (java.io.IOException io) {
1092            sLogger.error(io.getMessage(), io);
1093        }
1094    }
1095    
1096    private class ShutdownHook extends Thread {
1097        Solver<Lecture, Placement> iSolver = null;
1098
1099        private ShutdownHook(Solver<Lecture, Placement> solver) {
1100            setName("ShutdownHook");
1101            iSolver = solver;
1102        }
1103        
1104        @Override
1105        public void run() {
1106            try {
1107                if (iSolver.isRunning()) iSolver.stopSolver();
1108                Solution<Lecture, Placement> solution = iSolver.lastSolution();
1109                long lastIt = solution.getIteration();
1110                double lastTime = solution.getTime();
1111                DataProperties properties = iSolver.getProperties();
1112                TimetableModel model = (TimetableModel) solution.getModel();
1113                File outDir = new File(properties.getProperty("General.Output", "."));
1114
1115                if (solution.getBestInfo() != null) {
1116                    Solution<Lecture, Placement> bestSolution = solution;// .cloneBest();
1117                    sLogger.info("Last solution: " + ToolBox.dict2string(bestSolution.getExtendedInfo(), 1));
1118                    sLogger.info("Best solution (before restore): " + ToolBox.dict2string(bestSolution.getBestInfo(), 1));
1119                    bestSolution.restoreBest();
1120                    sLogger.info("Best solution: " + ToolBox.dict2string(bestSolution.getExtendedInfo(), 1));
1121                    if (properties.getPropertyBoolean("General.SwitchStudents", true))
1122                        ((TimetableModel) bestSolution.getModel()).switchStudents(bestSolution.getAssignment());
1123                    sLogger.info("Best solution: " + ToolBox.dict2string(bestSolution.getExtendedInfo(), 1));
1124                    saveOutputCSV(bestSolution, new File(outDir, "output.csv"));
1125
1126                    printSomeStuff(bestSolution);
1127
1128                    if (properties.getPropertyBoolean("General.Save", true)) {
1129                        TimetableSaver saver = null;
1130                        try {
1131                            saver = (TimetableSaver) Class.forName(getTimetableSaverClass(properties))
1132                                .getConstructor(new Class[] { Solver.class }).newInstance(new Object[] { iSolver });
1133                        } catch (ClassNotFoundException e) {
1134                            System.err.println(e.getClass().getSimpleName() + ": " + e.getMessage());
1135                            saver = new TimetableXMLSaver(iSolver);
1136                        }
1137                        if ((saver instanceof TimetableXMLSaver) && properties.getProperty("General.SolutionFile") != null)
1138                            ((TimetableXMLSaver) saver).save(new File(properties.getProperty("General.SolutionFile")));
1139                        else
1140                            saver.save();
1141                    }
1142                } else {
1143                    sLogger.info("Last solution:" + ToolBox.dict2string(solution.getExtendedInfo(), 1));
1144                }
1145
1146                iCSVFile.close();
1147
1148                sLogger.info("Total number of done iteration steps:" + lastIt);
1149                sLogger.info("Achieved speed: " + sDoubleFormat.format(lastIt / lastTime) + " iterations/second");
1150                
1151                PrintWriter out = new PrintWriter(new FileWriter(new File(outDir, "solver.html")));
1152                out.println("<html><title>Save log</title><body>");
1153                out.println(Progress.getInstance(model).getHtmlLog(Progress.MSGLEVEL_TRACE, true));
1154                out.println("</html>");
1155                out.flush();
1156                out.close();
1157                Progress.removeInstance(model);
1158
1159                if (iStat != null) {
1160                    PrintWriter cbs = new PrintWriter(new FileWriter(new File(outDir, "cbs.txt")));
1161                    cbs.println(iStat.toString());
1162                    cbs.flush(); cbs.close();
1163                }
1164
1165                System.out.println("Unassigned variables: " + model.nrUnassignedVariables(solution.getAssignment()));
1166            } catch (Throwable t) {
1167                sLogger.error("Test failed.", t);
1168            }
1169        }
1170    }
1171}