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