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 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.pb;
29  
30  import java.math.BigInteger;
31  
32  import org.sat4j.specs.ContradictionException;
33  import org.sat4j.specs.IConstr;
34  import org.sat4j.specs.IVec;
35  import org.sat4j.specs.IVecInt;
36  import org.sat4j.specs.IteratorInt;
37  import org.sat4j.specs.TimeoutException;
38  import org.sat4j.tools.DimacsStringSolver;
39  
40  /**
41   * Solver used to display in a string the pb-instance in OPB format.
42   * 
43   * That solver is useful to produce OPB files to be used by third party solvers.
44   * 
45   * @author parrain
46   * 
47   */
48  public class OPBStringSolver extends DimacsStringSolver implements IPBSolver {
49  
50  	private static final String FAKE_I_CONSTR_MSG = "Fake IConstr";
51  
52  	/**
53  	 * 
54  	 */
55  	private static final long serialVersionUID = 1L;
56  
57  	private int indxConstrObj;
58  
59  	private int nbOfConstraints;
60  
61  	private ObjectiveFunction obj;
62  
63  	private boolean inserted = false;
64  
65  	private static final IConstr FAKE_CONSTR = new IConstr() {
66  
67  		public int size() {
68  			throw new UnsupportedOperationException(FAKE_I_CONSTR_MSG);
69  		}
70  
71  		public boolean learnt() {
72  			throw new UnsupportedOperationException(FAKE_I_CONSTR_MSG);
73  		}
74  
75  		public double getActivity() {
76  			throw new UnsupportedOperationException(FAKE_I_CONSTR_MSG);
77  		}
78  
79  		public int get(int i) {
80  			throw new UnsupportedOperationException(FAKE_I_CONSTR_MSG);
81  		}
82  	};
83  
84  	/**
85  	 * 
86  	 */
87  	public OPBStringSolver() {
88  	}
89  
90  	/**
91  	 * @param initSize
92  	 */
93  	public OPBStringSolver(int initSize) {
94  		super(initSize);
95  	}
96  
97  	@Override
98  	public boolean isSatisfiable(IVecInt assumps) throws TimeoutException {
99  		for (IteratorInt it = assumps.iterator(); it.hasNext();) {
100 			int p = it.next();
101 			if (p > 0) {
102 				getOut().append("+1 x" + p + " >= 1 ;\n");
103 			} else {
104 				getOut().append("-1 x" + (-p) + " >= 0 ;\n");
105 			}
106 			nbOfConstraints++;
107 		}
108 		throw new TimeoutException();
109 	}
110 
111 	@Override
112 	public boolean isSatisfiable(IVecInt assumps, boolean global)
113 			throws TimeoutException {
114 		// TODO Auto-generated method stub
115 		return super.isSatisfiable(assumps, global);
116 	}
117 
118 	public IConstr addPseudoBoolean(IVecInt lits, IVec<BigInteger> coeffs,
119 			boolean moreThan, BigInteger d) throws ContradictionException {
120 		StringBuffer out = getOut();
121 		assert lits.size() == coeffs.size();
122 		nbOfConstraints++;
123 		if (moreThan) {
124 			for (int i = 0; i < lits.size(); i++) {
125 				out.append(coeffs.get(i));
126 				out.append(" x");
127 				out.append(lits.get(i));
128 				out.append(" ");
129 			}
130 			out.append(">= ");
131 			out.append(d);
132 			out.append(" ;\n");
133 		} else {
134 			for (int i = 0; i < lits.size(); i++) {
135 				out.append(coeffs.get(i).negate());
136 				out.append(" x");
137 				out.append(lits.get(i));
138 				out.append(" ");
139 			}
140 			out.append(">= ");
141 			out.append(d.negate());
142 			out.append(" ;\n");
143 		}
144 		return FAKE_CONSTR;
145 	}
146 
147 	public void setObjectiveFunction(ObjectiveFunction obj) {
148 		this.obj = obj;
149 	}
150 
151 	@Override
152 	public IConstr addAtLeast(IVecInt literals, int degree)
153 			throws ContradictionException {
154 		StringBuffer out = getOut();
155 		nbOfConstraints++;
156 		for (IteratorInt iterator = literals.iterator(); iterator.hasNext();)
157 			out.append("+1 x" + iterator.next() + " ");
158 		out.append(">= " + degree + " ;\n");
159 		return FAKE_CONSTR;
160 	}
161 
162 	@Override
163 	public IConstr addAtMost(IVecInt literals, int degree)
164 			throws ContradictionException {
165 		StringBuffer out = getOut();
166 		nbOfConstraints++;
167 		for (IteratorInt iterator = literals.iterator(); iterator.hasNext();)
168 			out.append("-1 x" + iterator.next() + " ");
169 		out.append(">= " + (-degree) + " ;\n");
170 		return FAKE_CONSTR;
171 	}
172 
173 	@Override
174 	public IConstr addClause(IVecInt literals) throws ContradictionException {
175 		StringBuffer out = getOut();
176 		nbOfConstraints++;
177 		int lit;
178 		for (IteratorInt iterator = literals.iterator(); iterator.hasNext();) {
179 			lit = iterator.next();
180 			if (lit > 0) {
181 				out.append("+1 x" + lit + " ");
182 			} else {
183 				out.append("+1 ~x" + -lit + " ");
184 			}
185 		}
186 		out.append(">= 1 ;\n");
187 		return FAKE_CONSTR;
188 	}
189 
190 	/*
191 	 * (non-Javadoc)
192 	 * 
193 	 * @see org.sat4j.pb.IPBSolver#getExplanation()
194 	 */
195 	public String getExplanation() {
196 		// TODO Auto-generated method stub
197 		return null;
198 	}
199 
200 	/*
201 	 * (non-Javadoc)
202 	 * 
203 	 * @see
204 	 * org.sat4j.pb.IPBSolver#setListOfVariablesForExplanation(org.sat4j.specs
205 	 * .IVecInt)
206 	 */
207 	public void setListOfVariablesForExplanation(IVecInt listOfVariables) {
208 		// TODO Auto-generated method stub
209 
210 	}
211 
212 	@Override
213 	public String toString() {
214 		StringBuffer out = getOut();
215 		if (!inserted) {
216 			StringBuffer tmp = new StringBuffer();
217 			tmp.append("* #variable= ");
218 			tmp.append(nVars());
219 			tmp.append(" #constraint= ");
220 			tmp.append(nbOfConstraints);
221 			tmp.append(" \n");
222 			if (obj != null) {
223 				tmp.append("min: ");
224 				tmp.append(obj);
225 				tmp.append(" ;\n");
226 			}
227 			out.insert(indxConstrObj, tmp.toString());
228 			inserted = true;
229 		}
230 		return out.toString();
231 	}
232 
233 	@Override
234 	public String toString(String prefix) {
235 		return "OPB output solver";
236 	}
237 
238 	@Override
239 	public int newVar(int howmany) {
240 		StringBuffer out = getOut();
241 		setNbVars(howmany);
242 		// to add later the number of constraints
243 		indxConstrObj = out.length();
244 		out.append("\n");
245 		return howmany;
246 	}
247 
248 	@Override
249 	public void setExpectedNumberOfClauses(int nb) {
250 	}
251 
252 	public ObjectiveFunction getObjectiveFunction() {
253 		return obj;
254 	}
255 
256 }