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'><caption>Related Solver Parameters</caption>
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 * @author  Tomáš Müller
040 * @version IFS 1.3 (Iterative Forward Search)<br>
041 *          Copyright (C) 2006 - 2014 Tomáš Müller<br>
042 *          <a href="mailto:muller@unitime.org">muller@unitime.org</a><br>
043 *          <a href="http://muller.unitime.org">http://muller.unitime.org</a><br>
044 * <br>
045 *          This library is free software; you can redistribute it and/or modify
046 *          it under the terms of the GNU Lesser General Public License as
047 *          published by the Free Software Foundation; either version 3 of the
048 *          License, or (at your option) any later version. <br>
049 * <br>
050 *          This library is distributed in the hope that it will be useful, but
051 *          WITHOUT ANY WARRANTY; without even the implied warranty of
052 *          MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
053 *          Lesser General Public License for more details. <br>
054 * <br>
055 *          You should have received a copy of the GNU Lesser General Public
056 *          License along with this library; if not see
057 *          <a href='http://www.gnu.org/licenses/'>http://www.gnu.org/licenses/</a>.
058 */
059public class Test {
060    private static java.text.SimpleDateFormat sDateFormat = new java.text.SimpleDateFormat("dd-MMM-yy_HHmmss", java.util.Locale.US);
061    private static org.apache.logging.log4j.Logger sLogger = org.apache.logging.log4j.LogManager.getLogger(Test.class);
062
063    public static void test(DataProperties properties) {
064        try {
065            String inputFile = properties.getProperty("General.Input");
066            JobShopModel model = JobShopModel.loadModel(inputFile);
067            Solver<Operation, Location> s = new Solver<Operation, Location>(properties);
068            s.setInitalSolution(model);
069            s.start();
070            s.getSolverThread().join();
071            Solution<Operation, Location> best = s.currentSolution();
072            best.restoreBest();
073            sLogger.info("Best solution info:" + best.getInfo());
074            sLogger.info("Best solution:" + model.toString());
075            model.save(best.getAssignment(), properties.getProperty("General.Output") + File.separator + "solution.txt");
076        } catch (Exception e) {
077            e.printStackTrace();
078        }
079    }
080
081    public static void main(String[] args) {
082        try {
083            Progress.getInstance().addProgressListener(new ProgressWriter(System.out));
084
085            File inputCfg = new File(args[0]);
086            DataProperties properties = ToolBox.loadProperties(inputCfg);
087            String outDir = properties.getProperty("General.Output", ".") + File.separator + inputCfg.getName().substring(0, inputCfg.getName().lastIndexOf('.')) + File.separator + sDateFormat.format(new Date());
088            (new File(outDir)).mkdirs();
089            properties.setProperty("General.Output", outDir.toString());
090            ToolBox.configureLogging(outDir, null);
091            test(properties);
092        } catch (Exception e) {
093            e.printStackTrace();
094        }
095    }
096}