001 package net.sf.cpsolver.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 * @version 009 * IFS 1.1 (Iterative Forward Search)<br> 010 * Copyright (C) 2006 Tomáš Müller<br> 011 * <a href="mailto:muller@unitime.org">muller@unitime.org</a><br> 012 * Lazenska 391, 76314 Zlin, Czech Republic<br> 013 * <br> 014 * This library is free software; you can redistribute it and/or 015 * modify it under the terms of the GNU Lesser General Public 016 * License as published by the Free Software Foundation; either 017 * version 2.1 of the License, or (at your option) any later version. 018 * <br><br> 019 * This library is distributed in the hope that it will be useful, 020 * but WITHOUT ANY WARRANTY; without even the implied warranty of 021 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 022 * Lesser General Public License for more details. 023 * <br><br> 024 * You should have received a copy of the GNU Lesser General Public 025 * License along with this library; if not, write to the Free Software 026 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA 027 */ 028 public class SimpleTest { 029 /** 030 * run the test 031 */ 032 public static void main(String[] args) { 033 org.apache.log4j.BasicConfigurator.configure(); 034 int nrVariables = 25; 035 int nrValues = 12; 036 int nrConstraints = 198; 037 double tigtness = 0.25; 038 int nrAllPairs = nrValues*nrValues; 039 int nrCompatiblePairs = (int)((1.0-tigtness)*nrAllPairs); 040 long seed = System.currentTimeMillis(); 041 System.out.println("CSP("+nrVariables+","+nrValues+","+nrConstraints+"/"+((nrVariables*(nrVariables-1))/2)+","+(nrAllPairs-nrCompatiblePairs)+"/"+nrAllPairs+")"); 042 043 net.sf.cpsolver.ifs.util.DataProperties cfg = new net.sf.cpsolver.ifs.util.DataProperties(); 044 cfg.setProperty("Termination.Class","net.sf.cpsolver.ifs.termination.GeneralTerminationCondition"); 045 cfg.setProperty("Termination.StopWhenComplete","true"); 046 cfg.setProperty("Termination.TimeOut","60"); 047 cfg.setProperty("Comparator.Class","net.sf.cpsolver.ifs.solution.GeneralSolutionComparator"); 048 cfg.setProperty("Value.Class","net.sf.cpsolver.ifs.heuristics.GeneralValueSelection"); 049 cfg.setProperty("Value.WeightConflicts", "1"); 050 cfg.setProperty("Variable.Class","net.sf.cpsolver.ifs.heuristics.GeneralVariableSelection"); 051 cfg.setProperty("Extensions.Classes","net.sf.cpsolver.ifs.extension.ConflictStatistics"); 052 053 CSPModel model = new CSPModel(nrVariables,nrValues,nrConstraints,nrCompatiblePairs,seed); 054 net.sf.cpsolver.ifs.solver.Solver solver = new net.sf.cpsolver.ifs.solver.Solver(cfg); 055 solver.setInitalSolution(model); 056 057 solver.start(); 058 try { 059 solver.getSolverThread().join(); 060 } catch (InterruptedException e) {} 061 062 net.sf.cpsolver.ifs.solution.Solution solution = solver.lastSolution(); 063 solution.restoreBest(); 064 065 System.out.println("Best solution found after "+solution.getBestTime()+" seconds ("+solution.getBestIteration()+" iterations)."); 066 System.out.println("Number of assigned variables is "+solution.getModel().assignedVariables().size()); 067 System.out.println("Total value of the solution is "+solution.getModel().getTotalValue()); 068 069 int idx=1; 070 for (Enumeration e=solution.getModel().variables().elements();e.hasMoreElements();) { 071 CSPVariable v=(CSPVariable)e.nextElement(); 072 if (v.getAssignment()!=null) 073 System.out.println("Var"+(idx++)+"="+v.getAssignment().toDouble()); 074 } 075 } 076 }