    001package org.cpsolver.exam;
    002
    003import java.io.File;
    004import java.io.FileInputStream;
    005import java.io.FileOutputStream;
    006import java.io.FileWriter;
    007import java.io.IOException;
    008import java.io.PrintWriter;
    009import java.text.DecimalFormat;
    010import java.util.Collection;
    011import java.util.Locale;
    012import java.util.Map;
    013
    014
    015import org.cpsolver.exam.criteria.DistributionPenalty;
    016import org.cpsolver.exam.criteria.ExamRotationPenalty;
    017import org.cpsolver.exam.criteria.InstructorBackToBackConflicts;
    018import org.cpsolver.exam.criteria.InstructorDirectConflicts;
    019import org.cpsolver.exam.criteria.InstructorDistanceBackToBackConflicts;
    020import org.cpsolver.exam.criteria.InstructorMoreThan2ADayConflicts;
    021import org.cpsolver.exam.criteria.InstructorNotAvailableConflicts;
    022import org.cpsolver.exam.criteria.LargeExamsPenalty;
    023import org.cpsolver.exam.criteria.PeriodIndexPenalty;
    024import org.cpsolver.exam.criteria.PeriodPenalty;
    025import org.cpsolver.exam.criteria.PeriodSizePenalty;
    026import org.cpsolver.exam.criteria.PerturbationPenalty;
    027import org.cpsolver.exam.criteria.RoomPenalty;
    028import org.cpsolver.exam.criteria.RoomPerturbationPenalty;
    029import org.cpsolver.exam.criteria.RoomSizePenalty;
    030import org.cpsolver.exam.criteria.RoomSplitDistancePenalty;
    031import org.cpsolver.exam.criteria.RoomSplitPenalty;
    032import org.cpsolver.exam.criteria.StudentBackToBackConflicts;
    033import org.cpsolver.exam.criteria.StudentDirectConflicts;
    034import org.cpsolver.exam.criteria.StudentDistanceBackToBackConflicts;
    035import org.cpsolver.exam.criteria.StudentMoreThan2ADayConflicts;
    036import org.cpsolver.exam.criteria.StudentNotAvailableConflicts;
    037import org.cpsolver.exam.criteria.additional.DistanceToStronglyPreferredRoom;
    038import org.cpsolver.exam.criteria.additional.DistributionViolation;
    039import org.cpsolver.exam.criteria.additional.PeriodViolation;
    040import org.cpsolver.exam.criteria.additional.RoomViolation;
    041import org.cpsolver.exam.model.Exam;
    042import org.cpsolver.exam.model.ExamInstructor;
    043import org.cpsolver.exam.model.ExamModel;
    044import org.cpsolver.exam.model.ExamPlacement;
    045import org.cpsolver.exam.model.ExamStudent;
    046import org.cpsolver.exam.reports.ExamAssignments;
    047import org.cpsolver.exam.reports.ExamCourseSectionAssignments;
    048import org.cpsolver.exam.reports.ExamInstructorConflicts;
    049import org.cpsolver.exam.reports.ExamNbrMeetingsPerDay;
    050import org.cpsolver.exam.reports.ExamPeriodUsage;
    051import org.cpsolver.exam.reports.ExamRoomSchedule;
    052import org.cpsolver.exam.reports.ExamRoomSplit;
    053import org.cpsolver.exam.reports.ExamStudentBackToBackConflicts;
    054import org.cpsolver.exam.reports.ExamStudentConflicts;
    055import org.cpsolver.exam.reports.ExamStudentConflictsBySectionCourse;
    056import org.cpsolver.exam.reports.ExamStudentConflictsPerExam;
    057import org.cpsolver.exam.reports.ExamStudentDirectConflicts;
    058import org.cpsolver.exam.reports.ExamStudentMoreTwoADay;
    059import org.cpsolver.exam.split.ExamSplitter;
    060import org.cpsolver.ifs.assignment.Assignment;
    061import org.cpsolver.ifs.assignment.DefaultParallelAssignment;
    062import org.cpsolver.ifs.assignment.DefaultSingleAssignment;
    063import org.cpsolver.ifs.solution.Solution;
    064import org.cpsolver.ifs.solution.SolutionListener;
    065import org.cpsolver.ifs.solver.ParallelSolver;
    066import org.cpsolver.ifs.solver.Solver;
    067import org.cpsolver.ifs.util.DataProperties;
    068import org.cpsolver.ifs.util.Progress;
    069import org.cpsolver.ifs.util.ToolBox;
    070import org.dom4j.Document;
    071import org.dom4j.io.OutputFormat;
    072import org.dom4j.io.SAXReader;
    073import org.dom4j.io.XMLWriter;
    074
    075/**
    076 * An examination timetabling test program. The following steps are performed:
    077 * <ul>
    078 * <li>Input properties are loaded
    079 * <li>Input problem is loaded (General.Input property)
    080 * <li>Problem is solved (using the given properties)
    081 * <li>Solution is save (General.OutputFile property)
    082 * </ul>
    083 * <br>
    084 * <br>
    085 * Usage:
    086 * <pre><code>java -Xmx1024m -jar examtt-1.1.jar exam.properties input.xml output.xml</code></pre>
    087 * <br>
    088 *
    089 * @version ExamTT 1.3 (Examination Timetabling)<br>
    090 *          Copyright (C) 2007 - 2014 Tomáš Müller<br>
    091 *          <a href="mailto:muller@unitime.org">muller@unitime.org</a><br>
    092 *          <a href="http://muller.unitime.org">http://muller.unitime.org</a><br>
    093 * <br>
    094 *          This library is free software; you can redistribute it and/or modify
    095 *          it under the terms of the GNU Lesser General Public License as
    096 *          published by the Free Software Foundation; either version 3 of the
    097 *          License, or (at your option) any later version. <br>
    098 * <br>
    099 *          This library is distributed in the hope that it will be useful, but
    100 *          WITHOUT ANY WARRANTY; without even the implied warranty of
    101 *          MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
    102 *          Lesser General Public License for more details. <br>
    103 * <br>
    104 *          You should have received a copy of the GNU Lesser General Public
    105 *          License along with this library; if not see
    106 *          <a href='http://www.gnu.org/licenses/'>http://www.gnu.org/licenses/</a>.
    107 */
    108public class Test {
    109    private static org.apache.logging.log4j.Logger sLog = org.apache.logging.log4j.LogManager.getLogger(Test.class);
    110    private static java.text.DecimalFormat sDoubleFormat = new java.text.DecimalFormat("0.00", new java.text.DecimalFormatSymbols(Locale.US));
    111
    112    /** Generate exam reports
    113     * @param model problem model
    114     * @param assignment current assignment
    115     * @param outDir output folder
    116     * @param outName output file name prefix
    117     * @throws IOException may be thrown when writing fails
    118     **/
    119    public static void createReports(ExamModel model, Assignment<Exam, ExamPlacement> assignment, File outDir, String outName) throws IOException {
    120        new ExamAssignments(model).report(assignment).save(new File(outDir, outName + ".schdex.csv"));
    121
    122        new ExamCourseSectionAssignments(model).report(assignment).save(new File(outDir, outName + ".schdcs.csv"));
    123
    124        new ExamStudentConflicts(model).report(assignment).save(new File(outDir, outName + ".sconf.csv"));
    125
    126        new ExamInstructorConflicts(model).report(assignment).save(new File(outDir, outName + ".iconf.csv"));
    127
    128        new ExamStudentConflictsPerExam(model).report(assignment).save(new File(outDir, outName + ".sconfex.csv"));
    129
    130        new ExamStudentDirectConflicts(model).report(assignment).save(new File(outDir, outName + ".sdir.csv"));
    131
    132        new ExamStudentBackToBackConflicts(model).report(assignment).save(new File(outDir, outName + ".sbtb.csv"));
    133
    134        new ExamStudentMoreTwoADay(model).report(assignment).save(new File(outDir, outName + ".sm2d.csv"));
    135
    136        new ExamPeriodUsage(model).report(assignment).save(new File(outDir, outName + ".per.csv"));
    137
    138        new ExamRoomSchedule(model).report(assignment).save(new File(outDir, outName + ".schdr.csv"));
    139
    140        new ExamRoomSplit(model).report(assignment).save(new File(outDir, outName + ".rsplit.csv"));
    141
    142        new ExamNbrMeetingsPerDay(model).report(assignment).save(new File(outDir, outName + ".distmpd.csv"));
    143
    144        new ExamStudentConflictsBySectionCourse(model).report(assignment).save(new File(outDir, outName + ".sconfcs.csv"));
    145    }
    146
    147    public static class ShutdownHook extends Thread {
    148        Solver<Exam, ExamPlacement> iSolver = null;
    149
    150        public ShutdownHook(Solver<Exam, ExamPlacement> solver) {
    151            setName("ShutdownHook");
    152            iSolver = solver;
    153        }
    154
    155        @Override
    156        public void run() {
    157            try {
    158                if (iSolver.isRunning())
    159                    iSolver.stopSolver();
    160                Solution<Exam, ExamPlacement> solution = iSolver.lastSolution();
    161                Progress.removeInstance(solution.getModel());
    162                if (solution.getBestInfo() == null) {
    163                    sLog.error("No best solution found.");
    164                } else
    165                    solution.restoreBest();
    166
    167                sLog.info("Best solution:" + ToolBox.dict2string(solution.getExtendedInfo(), 1));
    168
    169                sLog.info("Best solution found after " + solution.getBestTime() + " seconds ("
    170                        + solution.getBestIteration() + " iterations).");
    171                sLog.info("Number of assigned variables is " + solution.getAssignment().nrAssignedVariables());
    172                sLog.info("Total value of the solution is " + solution.getModel().getTotalValue(solution.getAssignment()));
    173
    174                File outFile = new File(iSolver.getProperties().getProperty("General.OutputFile",
    175                        iSolver.getProperties().getProperty("General.Output") + File.separator + "solution.xml"));
    176                FileOutputStream fos = new FileOutputStream(outFile);
    177                (new XMLWriter(fos, OutputFormat.createPrettyPrint())).write(((ExamModel) solution.getModel()).save(solution.getAssignment()));
    178                fos.flush();
    179                fos.close();
    180
    181                if ("true".equals(System.getProperty("reports", "false")))
    182                    createReports((ExamModel) solution.getModel(), solution.getAssignment(), outFile.getParentFile(), outFile.getName()
    183                            .substring(0, outFile.getName().lastIndexOf('.')));
    184
    185                String baseName = new File(iSolver.getProperties().getProperty("General.Input")).getName();
    186                if (baseName.indexOf('.') > 0)
    187                    baseName = baseName.substring(0, baseName.lastIndexOf('.'));
    188                addCSVLine(new File(outFile.getParentFile(), baseName + ".csv"), outFile.getName(), iSolver.getProperties().getProperty("General.Config"), solution);
    189
    190            } catch (Exception e) {
    191                e.printStackTrace();
    192            }
    193        }
    194    }
    195
    196    private static void addCSVLine(File file, String instance, String config, Solution<Exam, ExamPlacement> solution) {
    197        try {
    198            ExamModel model = (ExamModel) solution.getModel();
    199            Assignment<Exam, ExamPlacement> assignment = solution.getAssignment();
    200            boolean ex = file.exists();
    201            PrintWriter pw = new PrintWriter(new FileWriter(file, true));
    202            boolean mpp = ((PerturbationPenalty)model.getCriterion(PerturbationPenalty.class)).isMPP();
    203            int largeSize = ((LargeExamsPenalty)model.getCriterion(LargeExamsPenalty.class)).getLargeSize();
    204            RoomSplitDistancePenalty splitDistance = (RoomSplitDistancePenalty)model.getCriterion(RoomSplitDistancePenalty.class);
    205            ExamSplitter splitter = (ExamSplitter)model.getCriterion(ExamSplitter.class);
    206            PeriodViolation violPer = (PeriodViolation)model.getCriterion(PeriodViolation.class);
    207            RoomViolation violRoom = (RoomViolation)model.getCriterion(RoomViolation.class);
    208            DistributionViolation violDist = (DistributionViolation)model.getCriterion(DistributionViolation.class);
    209            DistanceToStronglyPreferredRoom distStrPref = (DistanceToStronglyPreferredRoom)model.getCriterion(DistanceToStronglyPreferredRoom.class);
    210            ExamRotationPenalty rotation = (ExamRotationPenalty)model.getCriterion(ExamRotationPenalty.class);
    211            DecimalFormat df = new DecimalFormat("0.00");
    212            if (!ex) {
    213                pw.println("SEED"
    214                        + ",NA,DC,M2D,BTB" + (model.getBackToBackDistance() < 0 ? "" : ",dBTB")
    215                        + ",iNA,iDC,iM2D,iBTB" + (model.getBackToBackDistance() < 0 ? "" : ",diBTB")
    216                        + ",PP,RP,DP"
    217                        + ",PI,@P,PS" // Period Index, Rotation Penalty, Period Size
    218                        + ",RSz,RSp,RD" // Room Size, Room Split, Room Split Distance
    219                        + (largeSize >= 0 ? ",LP" : "")
    220                        + (mpp ? ",IP,IRP" : "")
    221                        + (distStrPref == null ? "" : ",@D")
    222                        + (splitter == null ? "" : ",XX")
    223                        + (violPer == null ? "" : ",!P")
    224                        + (violRoom == null ? "" : ",!R")
    225                        + (violDist == null ? "" : ",!D")
    226                        + ",INSTANCE,CONFIG");
    227                int nrStudentExams = 0;
    228                for (ExamStudent student : model.getStudents()) {
    229                    nrStudentExams += student.variables().size();
    230                }
    231                int nrInstructorExams = 0;
    232                for (ExamInstructor instructor : model.getInstructors()) {
    233                    nrInstructorExams += instructor.variables().size();
    234                }
    235                pw.println("MIN"
    236                        + ",#EX,#RM,#PER," + (model.getBackToBackDistance() < 0 ? "" : ",")
    237                        + ",#STD,#STDX,#INS,#INSX" + (model.getBackToBackDistance() < 0 ? "" : ",")
    238                        + "," + model.getCriterion(PeriodPenalty.class).getBounds(assignment)[0]
    239                        + "," + model.getCriterion(RoomPenalty.class).getBounds(assignment)[0]
    240                        + "," + model.getCriterion(DistributionPenalty.class).getBounds(assignment)[0]
    241                        + ",," + df.format(rotation.averagePeriod(assignment)) + ","
    242                        + ",,,"
    243                        + (largeSize >= 0 ? ",0" : "")
    244                        + (mpp ? ",," : "")
    245                        + (distStrPref == null ? "" : ",")
    246                        + (splitter == null ? "" : ",")
    247                        + (violPer == null ? "" : ",")
    248                        + (violRoom == null ? "" : ",")
    249                        + (violDist == null ? "" : ",")
    250                        + ",,");
    251                pw.println("MAX"
    252                        + "," + model.variables().size() + "," + model.getRooms().size() + "," + model.getPeriods().size() + "," + (model.getBackToBackDistance() < 0 ? "" : ",")
    253                        + "," + model.getStudents().size() + "," + nrStudentExams + "," + model.getInstructors().size() + "," + nrInstructorExams + (model.getBackToBackDistance() < 0 ? "" : ",")
    254                        + "," + model.getCriterion(PeriodPenalty.class).getBounds(assignment)[1]
    255                        + "," + model.getCriterion(RoomPenalty.class).getBounds(assignment)[1]
    256                        + "," + model.getCriterion(DistributionPenalty.class).getBounds(assignment)[1]
    257                        + ",," + rotation.nrAssignedExamsWithAvgPeriod(assignment) + ","
    258                        + ",,,"
    259                        + (largeSize >= 0 ? "," + model.getCriterion(LargeExamsPenalty.class).getBounds(assignment)[1] : "")
    260                        + (mpp ? ",," : "")
    261                        + (distStrPref == null ? "" : ",")
    262                        + (splitter == null ? "" : ",")
    263                        + (violPer == null ? "" : "," + model.getCriterion(PeriodViolation.class).getBounds(assignment)[1])
    264                        + (violRoom == null ? "" : "," + model.getCriterion(RoomViolation.class).getBounds(assignment)[1])
    265                        + (violDist == null ? "" : "," + model.getCriterion(DistributionViolation.class).getBounds(assignment)[1])
    266                        + ",,");
    267            }
    268            pw.println(ToolBox.getSeed()
    269                    + "," + model.getCriterion(StudentNotAvailableConflicts.class).getValue(assignment)
    270                    + "," + model.getCriterion(StudentDirectConflicts.class).getValue(assignment)
    271                    + "," + model.getCriterion(StudentMoreThan2ADayConflicts.class).getValue(assignment)
    272                    + "," + model.getCriterion(StudentBackToBackConflicts.class).getValue(assignment)
    273                    + (model.getBackToBackDistance() < 0 ? "" : "," + model.getCriterion(StudentDistanceBackToBackConflicts.class).getValue(assignment))
    274                    + "," + model.getCriterion(InstructorNotAvailableConflicts.class).getValue(assignment)
    275                    + "," + model.getCriterion(InstructorDirectConflicts.class).getValue(assignment)
    276                    + "," + model.getCriterion(InstructorMoreThan2ADayConflicts.class).getValue(assignment)
    277                    + "," + model.getCriterion(InstructorBackToBackConflicts.class).getValue(assignment)
    278                    + (model.getBackToBackDistance() < 0 ? "" : "," + model.getCriterion(InstructorDistanceBackToBackConflicts.class).getValue(assignment))
    279                    + "," + model.getCriterion(PeriodPenalty.class).getValue(assignment)
    280                    + "," + model.getCriterion(RoomPenalty.class).getValue(assignment)
    281                    + "," + model.getCriterion(DistributionPenalty.class).getValue(assignment)
    282                    + "," + df.format(model.getCriterion(PeriodIndexPenalty.class).getValue(assignment) / assignment.nrAssignedVariables())
    283                    + "," + df.format(Math.sqrt(rotation.getValue(assignment) / rotation.nrAssignedExamsWithAvgPeriod(assignment)) - 1)
    284                    + "," + df.format(model.getCriterion(PeriodSizePenalty.class).getValue(assignment) / assignment.nrAssignedVariables())
    285                    + "," + df.format(model.getCriterion(RoomSizePenalty.class).getValue(assignment) / assignment.nrAssignedVariables())
    286                    + "," + model.getCriterion(RoomSplitPenalty.class).getValue(assignment)
    287                    + "," + df.format(splitDistance.nrRoomSplits(assignment) <= 0 ? 0.0 : splitDistance.getValue(assignment) / splitDistance.nrRoomSplits(assignment))
    288                    + (largeSize >= 0 ? "," + model.getCriterion(LargeExamsPenalty.class).getValue(assignment) : "")
    289                    + (mpp ? "," + df.format(model.getCriterion(PerturbationPenalty.class).getValue(assignment) / assignment.nrAssignedVariables())
    290                           + "," + df.format(model.getCriterion(RoomPerturbationPenalty.class).getValue(assignment) / assignment.nrAssignedVariables()): "")
    291                    + (distStrPref == null ? "" : "," + df.format(distStrPref.getValue(assignment) / assignment.nrAssignedVariables()))
    292                    + (splitter == null ? "" : "," + df.format(splitter.getValue(assignment)))
    293                    + (violPer == null ? "" : "," + df.format(violPer.getValue(assignment)))
    294                    + (violRoom == null ? "" : "," + df.format(violRoom.getValue(assignment)))
    295                    + (violDist == null ? "" : "," + df.format(violDist.getValue(assignment)))
    296                    + "," + instance + "," + config);
    297            pw.flush();
    298            pw.close();
    299        } catch (Exception e) {
    300            sLog.error("Unable to add CSV line to " + file, e);
    301        }
    302    }
    303
    304    /**
    305     * Main program
    306     *
    307     * @param args
    308     *            problem property file, input file (optional, may be set by
    309     *            General.Input property), output file (optional, may be set by
    310     *            General.OutputFile property)
    311     */
    312    public static void main(String[] args) {
    313        try {
    314            DataProperties cfg = new DataProperties();
    315            cfg.setProperty("Termination.StopWhenComplete", "false");
    316            cfg.setProperty("Termination.TimeOut", "1800");
    317            cfg.setProperty("General.SaveBestUnassigned", "-1");
    318            cfg.setProperty("Neighbour.Class", "org.cpsolver.exam.heuristics.ExamNeighbourSelection");
    319            if (args.length >= 1) {
    320                cfg.load(new FileInputStream(args[0]));
    321                cfg.setProperty("General.Config", new File(args[0]).getName());
    322            }
    323            cfg.putAll(System.getProperties());
    324
    325            File inputFile = new File("c:\\test\\exam\\exam1070.xml");
    326            if (args.length >= 2) {
    327                inputFile = new File(args[1]);
    328            }
    329            ToolBox.setSeed(cfg.getPropertyLong("General.Seed", Math.round(Long.MAX_VALUE * Math.random())));
    330
    331            cfg.setProperty("General.Input", inputFile.toString());
    332
    333            String outName = inputFile.getName();
    334            if (outName.indexOf('.') >= 0)
    335                outName = outName.substring(0, outName.lastIndexOf('.')) + "s.xml";
    336            File outFile = new File(inputFile.getParentFile(), outName);
    337            if (args.length >= 3) {
    338                outFile = new File(args[2]);
    339                if (outFile.exists() && outFile.isDirectory())
    340                    outFile = new File(outFile, outName);
    341                if (!outFile.exists() && !outFile.getName().endsWith(".xml"))
    342                    outFile = new File(outFile, outName);
    343            }
    344            if (outFile.getParentFile() != null)
    345                outFile.getParentFile().mkdirs();
    346            cfg.setProperty("General.OutputFile", outFile.toString());
    347            cfg.setProperty("General.Output", outFile.getParent());
    348
    349            String logName = outFile.getName();
    350            if (logName.indexOf('.') >= 0)
    351                logName = logName.substring(0, logName.lastIndexOf('.')) + ".log";
    352            ToolBox.setupLogging(new File(outFile.getParent(), logName), "true".equals(System.getProperty("debug", "false")));
    353
    354            ExamModel model = new ExamModel(cfg);
    355
    356            Document document = (new SAXReader()).read(new File(cfg.getProperty("General.Input")));
    357            int nrSolvers = cfg.getPropertyInt("Parallel.NrSolvers", 1);
    358            Assignment<Exam, ExamPlacement> assignment = (nrSolvers <= 1 ? new DefaultSingleAssignment<Exam, ExamPlacement>() : new DefaultParallelAssignment<Exam, ExamPlacement>());
    359            model.load(document, assignment);
    360
    361            Solver<Exam, ExamPlacement> solver = (nrSolvers == 1 ? new Solver<Exam, ExamPlacement>(cfg) : new ParallelSolver<Exam, ExamPlacement>(cfg));
    362            solver.setInitalSolution(new Solution<Exam, ExamPlacement>(model, assignment));
    363
    364            solver.currentSolution().addSolutionListener(new SolutionListener<Exam, ExamPlacement>() {
    365                @Override
    366                public void solutionUpdated(Solution<Exam, ExamPlacement> solution) {
    367                }
    368
    369                @Override
    370                public void getInfo(Solution<Exam, ExamPlacement> solution, Map<String, String> info) {
    371                }
    372
    373                @Override
    374                public void getInfo(Solution<Exam, ExamPlacement> solution, Map<String, String> info,
    375                        Collection<Exam> variables) {
    376                }
    377
    378                @Override
    379                public void bestCleared(Solution<Exam, ExamPlacement> solution) {
    380                }
    381
    382                @Override
    383                public void bestSaved(Solution<Exam, ExamPlacement> solution) {
    384                    ExamModel m = (ExamModel) solution.getModel();
    385                    Assignment<Exam, ExamPlacement> a = solution.getAssignment();
    386                    if (sLog.isInfoEnabled()) {
    387                        sLog.info("**BEST[" + solution.getIteration() + "]** "
    388                                + (m.variables().size() > a.nrAssignedVariables() ? "V:" + a.nrAssignedVariables() + "/" + m.variables().size() + " - " : "") +
    389                                "T:" + new DecimalFormat("0.00").format(m.getTotalValue(a)) + " " + m.toString(a) +
    390                                (solution.getFailedIterations() > 0 ? ", F:" + sDoubleFormat.format(100.0 * solution.getFailedIterations() / solution.getIteration()) + "%" : ""));
    391                    }
    392                }
    393
    394                @Override
    395                public void bestRestored(Solution<Exam, ExamPlacement> solution) {
    396                }
    397            });
    398
    399            Runtime.getRuntime().addShutdownHook(new ShutdownHook(solver));
    400
    401            solver.start();
    402            try {
    403                solver.getSolverThread().join();
    404            } catch (InterruptedException e) {
    405            }
    406        } catch (Exception e) {
    407            e.printStackTrace();
    408        }
    409    }
    410}