1 /******************************************************************************* 2 * SAT4J: a SATisfiability library for Java Copyright (C) 2004-2008 Daniel Le Berre 3 * 4 * All rights reserved. This program and the accompanying materials 5 * are made available under the terms of the Eclipse Public License v1.0 6 * which accompanies this distribution, and is available at 7 * http://www.eclipse.org/legal/epl-v10.html 8 * 9 * Alternatively, the contents of this file may be used under the terms of 10 * either the GNU Lesser General Public License Version 2.1 or later (the 11 * "LGPL"), in which case the provisions of the LGPL are applicable instead 12 * of those above. If you wish to allow use of your version of this file only 13 * under the terms of the LGPL, and not to allow others to use your version of 14 * this file under the terms of the EPL, indicate your decision by deleting 15 * the provisions above and replace them with the notice and other provisions 16 * required by the LGPL. If you do not delete the provisions above, a recipient 17 * may use your version of this file under the terms of the EPL or the LGPL. 18 * 19 * Based on the original MiniSat specification from: 20 * 21 * An extensible SAT solver. Niklas Een and Niklas Sorensson. Proceedings of the 22 * Sixth International Conference on Theory and Applications of Satisfiability 23 * Testing, LNCS 2919, pp 502-518, 2003. 24 * 25 * See www.minisat.se for the original solver in C++. 26 * 27 *******************************************************************************/ 28 package org.sat4j.specs; 29 30 31 /** 32 * Represents an optimization problem. The SAT solver will find suboptimal solutions 33 * of the problem until no more solutions are available. The latest solution found 34 * will be the optimal one. 35 * 36 * Such kind of problem is supposed to be handled: 37 * <pre> 38 boolean isSatisfiable = false; 39 40 IOptimizationProblem optproblem = (IOptimizationProblem) problem; 41 42 try { 43 while (optproblem.admitABetterSolution()) { 44 if (!isSatisfiable) { 45 if (optproblem.nonOptimalMeansSatisfiable()) { 46 setExitCode(ExitCode.SATISFIABLE); 47 if (optproblem.hasNoObjectiveFunction()) { 48 return; 49 } 50 log("SATISFIABLE"); //$NON-NLS-1$ 51 } 52 isSatisfiable = true; 53 log("OPTIMIZING..."); //$NON-NLS-1$ 54 } 55 log("Got one! Elapsed wall clock time (in seconds):" //$NON-NLS-1$ 56 + (System.currentTimeMillis() - getBeginTime()) 57 / 1000.0); 58 getLogWriter().println( 59 CURRENT_OPTIMUM_VALUE_PREFIX 60 + optproblem.calculateObjective()); 61 optproblem.discard(); 62 } 63 if (isSatisfiable) { 64 setExitCode(ExitCode.OPTIMUM_FOUND); 65 } else { 66 setExitCode(ExitCode.UNSATISFIABLE); 67 } 68 } catch (ContradictionException ex) { 69 assert isSatisfiable; 70 setExitCode(ExitCode.OPTIMUM_FOUND); 71 } 72 </pre> 73 * 74 * @author leberre 75 * 76 */ 77 public interface IOptimizationProblem extends IProblem { 78 79 /** 80 * Look for a solution of the optimization problem. 81 * 82 * @return true if a better solution than current one can be found. 83 * @throws TimeoutException if the solver cannot answer in reasonable time. 84 * @see ISolver#setTimeout(int) 85 */ 86 boolean admitABetterSolution() throws TimeoutException; 87 88 /** 89 * If the optimization problem has no objective function, then it is a simple 90 * decision problem. 91 * 92 * @return true if the problem is a decision problem, false if the problem is 93 * an optimization problem. 94 */ 95 boolean hasNoObjectiveFunction(); 96 97 /** 98 * A suboptimal solution has different meaning depending of the optimization problem 99 * considered. 100 * 101 * For instance, in the case of MAXSAT, a suboptimal solution does not mean that the problem is 102 * satisfiable, while in pseudo boolean optimization, it is true. 103 * 104 * @return true if founding a suboptimal solution means that the problem is satisfiable. 105 */ 106 boolean nonOptimalMeansSatisfiable(); 107 108 /** 109 * Compute the value of the objective function for the current solution. 110 * A call to that method only makes sense if hasNoObjectiveFunction()==false. 111 * 112 * @return the value of the objective function. 113 */ 114 Number calculateObjective(); 115 116 /** 117 * Discard the current solution in the optimization problem. 118 * 119 * @throws ContradictionException if a trivial inconsistency is detected. 120 */ 121 void discard() throws ContradictionException; 122 123 }