View Javadoc

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 pseudo boolean algorithms described in:
20   * A fast pseudo-Boolean constraint solver Chai, D.; Kuehlmann, A.
21   * Computer-Aided Design of Integrated Circuits and Systems, IEEE Transactions on
22   * Volume 24, Issue 3, March 2005 Page(s): 305 - 317
23   * 
24   * and 
25   * Heidi E. Dixon, 2004. Automating Pseudo-Boolean Inference within a DPLL 
26   * Framework. Ph.D. Dissertation, University of Oregon.
27   *******************************************************************************/
28  package org.sat4j.pb;
29  
30  import java.io.FileNotFoundException;
31  import java.io.IOException;
32  import java.math.BigInteger;
33  
34  import org.sat4j.core.VecInt;
35  import org.sat4j.pb.reader.OPBReader2007;
36  import org.sat4j.reader.ParseFormatException;
37  import org.sat4j.specs.ContradictionException;
38  import org.sat4j.specs.IConstr;
39  import org.sat4j.specs.IProblem;
40  import org.sat4j.specs.IVec;
41  import org.sat4j.specs.IVecInt;
42  import org.sat4j.specs.TimeoutException;
43  import org.sat4j.tools.GateTranslator;
44  import org.sat4j.tools.SolverDecorator;
45  
46  /**
47   * A decorator that computes minimal pseudo boolean models.
48   * 
49   * @author daniel
50   * 
51   */
52  public class PseudoBitsAdderDecorator extends SolverDecorator<IPBSolver>
53  		implements IPBSolver {
54  
55  	/**
56       * 
57       */
58  	private static final long serialVersionUID = 1L;
59  
60  	private ObjectiveFunction objfct;
61  
62  	private final GateTranslator gator;
63  	private final IPBSolver solver;
64  	private IVecInt bitsLiterals;
65  	private IVecInt fixedLiterals;
66  
67  	public PseudoBitsAdderDecorator(IPBSolver solver) {
68  		super(solver);
69  		gator = new GateTranslator(solver);
70  		this.solver = solver;
71  	}
72  
73  	public void setObjectiveFunction(ObjectiveFunction objf) {
74  		objfct = objf;
75  	}
76  
77  	@Override
78  	public boolean isSatisfiable() throws TimeoutException {
79  		return isSatisfiable(VecInt.EMPTY);
80  	}
81  
82  	@Override
83  	public boolean isSatisfiable(IVecInt assumps) throws TimeoutException {
84  		if (objfct == null) {
85  			return gator.isSatisfiable(assumps);
86  		}
87  		System.out.println("c Original number of variables and constraints");
88  		System.out.println("c #vars: " + gator.nVars() + " #constraints: "
89  				+ gator.nConstraints());
90  		bitsLiterals = new VecInt();
91  		System.out.println("c Creating optimization constraints ....");
92  		try {
93  			gator.optimisationFunction(objfct.getVars(), objfct.getCoeffs(),
94  					bitsLiterals);
95  		} catch (ContradictionException e) {
96  			return false;
97  		}
98  		System.out.println("c ... done. " + bitsLiterals);
99  		System.out.println("c New number of variables and constraints");
100 		System.out.println("c #vars: " + gator.nVars() + " #constraints: "
101 				+ gator.nConstraints());
102 		fixedLiterals = new VecInt(bitsLiterals.size());
103 		IVecInt nAssumpts = new VecInt(assumps.size() + bitsLiterals.size());
104 		boolean result;
105 		for (int litIndex = bitsLiterals.size() - 1; litIndex >= 0;) {
106 			assumps.copyTo(nAssumpts);
107 			fixedLiterals.copyTo(nAssumpts);
108 			nAssumpts.push(-bitsLiterals.get(litIndex));
109 			for (int j = litIndex - 1; j >= 0; j--) {
110 				nAssumpts.push(bitsLiterals.get(j));
111 			}
112 			System.out.println("c assumptions " + nAssumpts);
113 			result = gator.isSatisfiable(nAssumpts, true);
114 			if (result) {
115 				// int var = bitsLiterals.get(litIndex);
116 				// while (!gator.model(var)) {
117 				// fixedLiterals.push(-var);
118 				// if (litIndex == 0) {
119 				// litIndex--;
120 				// break;
121 				// }
122 				// var = bitsLiterals.get(--litIndex);
123 				// }
124 				fixedLiterals.push(-bitsLiterals.get(litIndex--));
125 				Number value = objfct.calculateDegree(gator);
126 				System.out.println("o " + value);
127 				System.out.println("c current objective value with fixed lits "
128 						+ fixedLiterals);
129 			} else {
130 				fixedLiterals.push(bitsLiterals.get(litIndex--));
131 				System.out.println("c unsat. fixed lits " + fixedLiterals);
132 			}
133 			nAssumpts.clear();
134 		}
135 		assert fixedLiterals.size() == bitsLiterals.size();
136 		assumps.copyTo(nAssumpts);
137 		fixedLiterals.copyTo(nAssumpts);
138 		return gator.isSatisfiable(nAssumpts);
139 	}
140 
141 	public static void main(String[] args) {
142 		PseudoBitsAdderDecorator decorator = new PseudoBitsAdderDecorator(
143 				SolverFactory.newDefault());
144 		decorator.setVerbose(false);
145 		OPBReader2007 reader = new OPBReader2007(decorator);
146 		long begin = System.currentTimeMillis();
147 		try {
148 			IProblem problem = reader.parseInstance(args[0]);
149 			if (problem.isSatisfiable()) {
150 				System.out.println("s OPTIMUM FOUND");
151 				System.out.println("v " + reader.decode(problem.model()));
152 				if (decorator.objfct != null) {
153 					System.out
154 							.println("c objective function="
155 									+ decorator.objfct
156 											.calculateDegree(decorator.gator));
157 				}
158 			} else {
159 				System.out.println("s UNSAT");
160 			}
161 		} catch (FileNotFoundException e) {
162 			// TODO Auto-generated catch block
163 			e.printStackTrace();
164 		} catch (ParseFormatException e) {
165 			// TODO Auto-generated catch block
166 			e.printStackTrace();
167 		} catch (IOException e) {
168 			// TODO Auto-generated catch block
169 			e.printStackTrace();
170 		} catch (ContradictionException e) {
171 			System.out.println("s UNSAT");
172 			System.out.println("c trivial inconsistency");
173 		} catch (TimeoutException e) {
174 			System.out.println("s UNKNOWN");
175 		}
176 		long end = System.currentTimeMillis();
177 		System.out.println("c Total wall clock time: " + (end - begin) / 1000.0
178 				+ " seconds");
179 	}
180 
181 	public IConstr addPseudoBoolean(IVecInt lits, IVec<BigInteger> coeffs,
182 			boolean moreThan, BigInteger d) throws ContradictionException {
183 		return solver.addPseudoBoolean(lits, coeffs, moreThan, d);
184 	}
185 
186 	public ObjectiveFunction getObjectiveFunction() {
187 		return objfct;
188 	}
189 
190 	public IConstr addAtMost(IVecInt literals, IVecInt coeffs, int degree)
191 			throws ContradictionException {
192 		throw new UnsupportedOperationException();
193 	}
194 
195 	public IConstr addAtMost(IVecInt literals, IVec<BigInteger> coeffs,
196 			BigInteger degree) throws ContradictionException {
197 		throw new UnsupportedOperationException();
198 	}
199 
200 	public IConstr addAtLeast(IVecInt literals, IVecInt coeffs, int degree)
201 			throws ContradictionException {
202 		throw new UnsupportedOperationException();
203 	}
204 
205 	public IConstr addAtLeast(IVecInt literals, IVec<BigInteger> coeffs,
206 			BigInteger degree) throws ContradictionException {
207 		throw new UnsupportedOperationException();
208 	}
209 
210 	public IConstr addExactly(IVecInt literals, IVecInt coeffs, int weight)
211 			throws ContradictionException {
212 		throw new UnsupportedOperationException();
213 	}
214 
215 	public IConstr addExactly(IVecInt literals, IVec<BigInteger> coeffs,
216 			BigInteger weight) throws ContradictionException {
217 		throw new UnsupportedOperationException();
218 	}
219 }