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