    001package net.sf.cpsolver.ifs.example.jobshop;
    002
    003import java.io.File;
    004import java.util.Date;
    005
    006import net.sf.cpsolver.ifs.solution.Solution;
    007import net.sf.cpsolver.ifs.solver.Solver;
    008import net.sf.cpsolver.ifs.util.DataProperties;
    009import net.sf.cpsolver.ifs.util.Progress;
    010import net.sf.cpsolver.ifs.util.ProgressWriter;
    011import net.sf.cpsolver.ifs.util.ToolBox;
    012
    013/**
    014 * Test of Job Shop problem. It takes one argument -- property file with all the
    015 * parameters. <br>
    016 * <br>
    017 * Test's parameters: <br>
    018 * <table border='1'>
    019 * <tr>
    020 * <th>Parameter</th>
    021 * <th>Type</th>
    022 * <th>Comment</th>
    023 * </tr>
    024 * <tr>
    025 * <td>General.Input</td>
    026 * <td>{@link String}</td>
    027 * <td>Input file describing the job shop problem</td>
    028 * </tr>
    029 * <tr>
    030 * <td>General.Output</td>
    031 * <td>{@link String}</td>
    032 * <td>Output folder where a log file and a solution (solution.txt) will be
    033 * placed</td>
    034 * </tr>
    035 * </table>
    036 * <br>
    037 *
    038 * @version IFS 1.2 (Iterative Forward Search)<br>
    039 *          Copyright (C) 2006 - 2010 Tomáš Müller<br>
    040 *          <a href="mailto:muller@unitime.org">muller@unitime.org</a><br>
    041 *          <a href="http://muller.unitime.org">http://muller.unitime.org</a><br>
    042 * <br>
    043 *          This library is free software; you can redistribute it and/or modify
    044 *          it under the terms of the GNU Lesser General Public License as
    045 *          published by the Free Software Foundation; either version 3 of the
    046 *          License, or (at your option) any later version. <br>
    047 * <br>
    048 *          This library is distributed in the hope that it will be useful, but
    049 *          WITHOUT ANY WARRANTY; without even the implied warranty of
    050 *          MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
    051 *          Lesser General Public License for more details. <br>
    052 * <br>
    053 *          You should have received a copy of the GNU Lesser General Public
    054 *          License along with this library; if not see
    055 *          <a href='http://www.gnu.org/licenses/'>http://www.gnu.org/licenses/</a>.
    056 */
    057public class Test {
    058    private static java.text.SimpleDateFormat sDateFormat = new java.text.SimpleDateFormat("dd-MMM-yy_HHmmss",
    059            java.util.Locale.US);
    060    private static org.apache.log4j.Logger sLogger = org.apache.log4j.Logger.getLogger(Test.class);
    061
    062    public static void test(DataProperties properties) {
    063        try {
    064            String inputFile = properties.getProperty("General.Input");
    065            JobShopModel model = JobShopModel.loadModel(inputFile);
    066            Solver<Operation, Location> s = new Solver<Operation, Location>(properties);
    067            s.setInitalSolution(model);
    068            s.start();
    069            s.getSolverThread().join();
    070            Solution<Operation, Location> best = s.currentSolution();
    071            best.restoreBest();
    072            sLogger.info("Best solution info:" + best.getInfo());
    073            sLogger.info("Best solution:" + model.toString());
    074            model.save(properties.getProperty("General.Output") + File.separator + "solution.txt");
    075        } catch (Exception e) {
    076            e.printStackTrace();
    077        }
    078    }
    079
    080    public static void main(String[] args) {
    081        try {
    082            Progress.getInstance().addProgressListener(new ProgressWriter(System.out));
    083
    084            File inputCfg = new File(args[0]);
    085            DataProperties properties = ToolBox.loadProperties(inputCfg);
    086            String outDir = properties.getProperty("General.Output", ".") + File.separator
    087                    + inputCfg.getName().substring(0, inputCfg.getName().lastIndexOf('.')) + File.separator
    088                    + sDateFormat.format(new Date());
    089            (new File(outDir)).mkdirs();
    090            properties.setProperty("General.Output", outDir.toString());
    091            ToolBox.configureLogging(outDir, null);
    092            test(properties);
    093        } catch (Exception e) {
    094            e.printStackTrace();
    095        }
    096    }
    097}