    001package org.cpsolver.ifs.example.jobshop;
    002
    003import java.io.File;
    004import java.util.Date;
    005
    006import org.cpsolver.ifs.solution.Solution;
    007import org.cpsolver.ifs.solver.Solver;
    008import org.cpsolver.ifs.util.DataProperties;
    009import org.cpsolver.ifs.util.Progress;
    010import org.cpsolver.ifs.util.ProgressWriter;
    011import org.cpsolver.ifs.util.ToolBox;
    012
    013
    014/**
    015 * Test of Job Shop problem. It takes one argument -- property file with all the
    016 * parameters. <br>
    017 * <br>
    018 * Test's parameters: <br>
    019 * <table border='1' summary='Related Solver Parameters'>
    020 * <tr>
    021 * <th>Parameter</th>
    022 * <th>Type</th>
    023 * <th>Comment</th>
    024 * </tr>
    025 * <tr>
    026 * <td>General.Input</td>
    027 * <td>{@link String}</td>
    028 * <td>Input file describing the job shop problem</td>
    029 * </tr>
    030 * <tr>
    031 * <td>General.Output</td>
    032 * <td>{@link String}</td>
    033 * <td>Output folder where a log file and a solution (solution.txt) will be
    034 * placed</td>
    035 * </tr>
    036 * </table>
    037 * <br>
    038 *
    039 * @version IFS 1.3 (Iterative Forward Search)<br>
    040 *          Copyright (C) 2006 - 2014 Tomáš Müller<br>
    041 *          <a href="mailto:muller@unitime.org">muller@unitime.org</a><br>
    042 *          <a href="http://muller.unitime.org">http://muller.unitime.org</a><br>
    043 * <br>
    044 *          This library is free software; you can redistribute it and/or modify
    045 *          it under the terms of the GNU Lesser General Public License as
    046 *          published by the Free Software Foundation; either version 3 of the
    047 *          License, or (at your option) any later version. <br>
    048 * <br>
    049 *          This library is distributed in the hope that it will be useful, but
    050 *          WITHOUT ANY WARRANTY; without even the implied warranty of
    051 *          MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
    052 *          Lesser General Public License for more details. <br>
    053 * <br>
    054 *          You should have received a copy of the GNU Lesser General Public
    055 *          License along with this library; if not see
    056 *          <a href='http://www.gnu.org/licenses/'>http://www.gnu.org/licenses/</a>.
    057 */
    058public class Test {
    059    private static java.text.SimpleDateFormat sDateFormat = new java.text.SimpleDateFormat("dd-MMM-yy_HHmmss", 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(best.getAssignment(), 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 + inputCfg.getName().substring(0, inputCfg.getName().lastIndexOf('.')) + File.separator + sDateFormat.format(new Date());
    087            (new File(outDir)).mkdirs();
    088            properties.setProperty("General.Output", outDir.toString());
    089            ToolBox.configureLogging(outDir, null);
    090            test(properties);
    091        } catch (Exception e) {
    092            e.printStackTrace();
    093        }
    094    }
    095}