    001package org.cpsolver.ifs.example.csp;
    002
    003import org.cpsolver.ifs.solution.Solution;
    004import org.cpsolver.ifs.solver.Solver;
    005import org.cpsolver.ifs.util.ToolBox;
    006
    007/**
    008 * Simple test of IFS CBS algorithm on random binary CSP problem
    009 * CSP(25,12,198/300,36/144).
    010 *
    011 * @author  Tomáš Müller
    012 * @version IFS 1.3 (Iterative Forward Search)<br>
    013 *          Copyright (C) 2006 - 2014 Tomáš Müller<br>
    014 *          <a href="mailto:muller@unitime.org">muller@unitime.org</a><br>
    015 *          <a href="http://muller.unitime.org">http://muller.unitime.org</a><br>
    016 * <br>
    017 *          This library is free software; you can redistribute it and/or modify
    018 *          it under the terms of the GNU Lesser General Public License as
    019 *          published by the Free Software Foundation; either version 3 of the
    020 *          License, or (at your option) any later version. <br>
    021 * <br>
    022 *          This library is distributed in the hope that it will be useful, but
    023 *          WITHOUT ANY WARRANTY; without even the implied warranty of
    024 *          MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
    025 *          Lesser General Public License for more details. <br>
    026 * <br>
    027 *          You should have received a copy of the GNU Lesser General Public
    028 *          License along with this library; if not see
    029 *          <a href='http://www.gnu.org/licenses/'>http://www.gnu.org/licenses/</a>.
    030 */
    031public class SimpleTest {
    032    /**
    033     * run the test
    034     * @param args program arguments
    035     */
    036    public static void main(String[] args) {
    037        ToolBox.configureLogging();
    038        int nrVariables = 25;
    039        int nrValues = 12;
    040        int nrConstraints = 198;
    041        double tigtness = 0.25;
    042        int nrAllPairs = nrValues * nrValues;
    043        int nrCompatiblePairs = (int) ((1.0 - tigtness) * nrAllPairs);
    044        long seed = System.currentTimeMillis();
    045        System.out.println("CSP(" + nrVariables + "," + nrValues + "," + nrConstraints + "/"
    046                + ((nrVariables * (nrVariables - 1)) / 2) + "," + (nrAllPairs - nrCompatiblePairs) + "/" + nrAllPairs
    047                + ")");
    048
    049        org.cpsolver.ifs.util.DataProperties cfg = new org.cpsolver.ifs.util.DataProperties();
    050        cfg.setProperty("Termination.Class", "org.cpsolver.ifs.termination.GeneralTerminationCondition");
    051        cfg.setProperty("Termination.StopWhenComplete", "false");
    052        cfg.setProperty("Termination.TimeOut", "60");
    053        cfg.setProperty("Comparator.Class", "org.cpsolver.ifs.solution.GeneralSolutionComparator");
    054        cfg.setProperty("Value.Class", "org.cpsolver.ifs.heuristics.GeneralValueSelection");
    055        cfg.setProperty("Value.WeightConflicts", "1");
    056        cfg.setProperty("Variable.Class", "org.cpsolver.ifs.heuristics.GeneralVariableSelection");
    057        cfg.setProperty("Extensions.Classes", "org.cpsolver.ifs.extension.ConflictStatistics");
    058
    059        CSPModel model = new CSPModel(nrVariables, nrValues, nrConstraints, nrCompatiblePairs, seed);
    060        Solver<CSPVariable, CSPValue> solver = new Solver<CSPVariable, CSPValue>(cfg);
    061        solver.setInitalSolution(model);
    062
    063        solver.start();
    064        try {
    065            solver.getSolverThread().join();
    066        } catch (InterruptedException e) {
    067        }
    068
    069        Solution<CSPVariable, CSPValue> solution = solver.lastSolution();
    070        solution.restoreBest();
    071
    072        System.out.println("Best solution found after " + solution.getBestTime() + " seconds ("
    073                + solution.getBestIteration() + " iterations).");
    074        System.out.println("Number of assigned variables is " + solution.getAssignment().nrAssignedVariables());
    075        System.out.println("Total value of the solution is " + solution.getModel().getTotalValue(solution.getAssignment()));
    076
    077        int idx = 1;
    078        for (CSPVariable v : solution.getModel().variables()) {
    079            CSPValue a = solution.getAssignment().getValue(v);
    080            if (a != null)
    081                System.out.println("Var" + (idx++) + "=" + a.toDouble());
    082        }
    083    }
    084}