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