001    package ifs.example.csp;
002    
003    import java.util.*;
004    
005    /**
006     * Simple test of IFS CBS algorithm on random binary CSP problem CSP(25,12,198/300,36/144).
007     *
008     * @author <a href="mailto:muller@ktiml.mff.cuni.cz">Tomáš Müller</a>
009     * @version 1.0
010     */
011    public class SimpleTest {
012        /**
013         * run the test
014         */
015        public static void main(String[] args) {
016            org.apache.log4j.BasicConfigurator.configure();
017            int nrVariables = 25;
018            int nrValues = 12;
019            int nrConstraints = 198;
020            double tigtness = 0.25;
021            int nrAllPairs = nrValues*nrValues;
022            int nrCompatiblePairs = (int)((1.0-tigtness)*nrAllPairs);
023            long seed = System.currentTimeMillis();
024            System.out.println("CSP("+nrVariables+","+nrValues+","+nrConstraints+"/"+((nrVariables*(nrVariables-1))/2)+","+(nrAllPairs-nrCompatiblePairs)+"/"+nrAllPairs+")");
025            
026            ifs.util.DataProperties cfg = new ifs.util.DataProperties();
027            cfg.setProperty("Termination.Class","ifs.termination.GeneralTerminationCondition");
028            cfg.setProperty("Termination.StopWhenComplete","true");
029            cfg.setProperty("Termination.TimeOut","60");
030            cfg.setProperty("Comparator.Class","ifs.solution.GeneralSolutionComparator");
031            cfg.setProperty("Value.Class","ifs.heuristics.GeneralValueSelection");
032            cfg.setProperty("Value.WeightConflicts", "1");
033            cfg.setProperty("Variable.Class","ifs.heuristics.GeneralVariableSelection");
034            cfg.setProperty("Extensions.Classes","ifs.extension.ConflictStatistics");
035            
036            CSPModel model = new CSPModel(nrVariables,nrValues,nrConstraints,nrCompatiblePairs,seed);
037            ifs.solver.Solver solver = new ifs.solver.Solver(cfg);
038            solver.setInitalSolution(model);
039            
040            solver.start();
041            try {
042                solver.getSolverThread().join();
043            } catch (InterruptedException e) {}
044            
045            ifs.solution.Solution solution = solver.lastSolution();
046            solution.restoreBest();
047            
048            System.out.println("Best solution found after "+solution.getBestTime()+" seconds ("+solution.getBestIteration()+" iterations).");
049            System.out.println("Number of assigned variables is "+solution.getModel().assignedVariables().size());
050            System.out.println("Total value of the solution is "+solution.getModel().getTotalValue());
051            
052            int idx=1;
053            for (Enumeration e=solution.getModel().variables().elements();e.hasMoreElements();) {
054                CSPVariable v=(CSPVariable)e.nextElement();
055                if (v.getAssignment()!=null)
056                    System.out.println("Var"+(idx++)+"="+v.getAssignment().toInt());
057            }
058        }
059    }