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