001    package net.sf.cpsolver.ifs.example.csp;
002    
003    import net.sf.cpsolver.ifs.solution.Solution;
004    import net.sf.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.2 (Iterative Forward Search)<br>
011     *          Copyright (C) 2006 - 2010 Tomas Muller<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     */
029    public class SimpleTest {
030        /**
031         * run the test
032         */
033        public static void main(String[] args) {
034            org.apache.log4j.BasicConfigurator.configure();
035            int nrVariables = 25;
036            int nrValues = 12;
037            int nrConstraints = 198;
038            double tigtness = 0.25;
039            int nrAllPairs = nrValues * nrValues;
040            int nrCompatiblePairs = (int) ((1.0 - tigtness) * nrAllPairs);
041            long seed = System.currentTimeMillis();
042            System.out.println("CSP(" + nrVariables + "," + nrValues + "," + nrConstraints + "/"
043                    + ((nrVariables * (nrVariables - 1)) / 2) + "," + (nrAllPairs - nrCompatiblePairs) + "/" + nrAllPairs
044                    + ")");
045    
046            net.sf.cpsolver.ifs.util.DataProperties cfg = new net.sf.cpsolver.ifs.util.DataProperties();
047            cfg.setProperty("Termination.Class", "net.sf.cpsolver.ifs.termination.GeneralTerminationCondition");
048            cfg.setProperty("Termination.StopWhenComplete", "true");
049            cfg.setProperty("Termination.TimeOut", "60");
050            cfg.setProperty("Comparator.Class", "net.sf.cpsolver.ifs.solution.GeneralSolutionComparator");
051            cfg.setProperty("Value.Class", "net.sf.cpsolver.ifs.heuristics.GeneralValueSelection");
052            cfg.setProperty("Value.WeightConflicts", "1");
053            cfg.setProperty("Variable.Class", "net.sf.cpsolver.ifs.heuristics.GeneralVariableSelection");
054            cfg.setProperty("Extensions.Classes", "net.sf.cpsolver.ifs.extension.ConflictStatistics");
055    
056            CSPModel model = new CSPModel(nrVariables, nrValues, nrConstraints, nrCompatiblePairs, seed);
057            Solver<CSPVariable, CSPValue> solver = new Solver<CSPVariable, CSPValue>(cfg);
058            solver.setInitalSolution(model);
059    
060            solver.start();
061            try {
062                solver.getSolverThread().join();
063            } catch (InterruptedException e) {
064            }
065    
066            Solution<CSPVariable, CSPValue> solution = solver.lastSolution();
067            solution.restoreBest();
068    
069            System.out.println("Best solution found after " + solution.getBestTime() + " seconds ("
070                    + solution.getBestIteration() + " iterations).");
071            System.out.println("Number of assigned variables is " + solution.getModel().assignedVariables().size());
072            System.out.println("Total value of the solution is " + solution.getModel().getTotalValue());
073    
074            int idx = 1;
075            for (CSPVariable v : ((CSPModel) solution.getModel()).variables()) {
076                if (v.getAssignment() != null)
077                    System.out.println("Var" + (idx++) + "=" + v.getAssignment().toDouble());
078            }
079        }
080    }