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