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.tools.DimacsStringSolver;
38  
39  /**
40   * Solver used to display in a string the pb-instance in OPB format.
41   * 
42   * That solver is useful to produce OPB files to be used by third party solvers.
43   * 
44   * @author parrain
45   * 
46   */
47  public class OPBStringSolver extends DimacsStringSolver implements 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  	/**
82  	 * 
83  	 */
84  	public OPBStringSolver() {
85  	}
86  
87  	/**
88  	 * @param initSize
89  	 */
90  	public OPBStringSolver(int initSize) {
91  		super(initSize);
92  	}
93  
94  	public IConstr addPseudoBoolean(IVecInt lits, IVec<BigInteger> coeffs,
95  			boolean moreThan, BigInteger d) throws ContradictionException {
96  		StringBuffer out = getOut();
97  		assert lits.size() == coeffs.size();
98  		nbOfConstraints++;
99  		if (moreThan) {
100 			for (int i = 0; i < lits.size(); i++)
101 				out.append(coeffs.get(i) + " x" + lits.get(i) + " ");
102 			out.append(">= " + d + " ;\n");
103 		} else {
104 			for (int i = 0; i < lits.size(); i++)
105 				out.append(coeffs.get(i).negate() + " x" + lits.get(i) + " ");
106 			out.append(">= " + d.negate() + " ;\n");
107 		}
108 		return FAKE_CONSTR;
109 	}
110 
111 	public void setObjectiveFunction(ObjectiveFunction obj) {
112 		this.obj = obj;
113 	}
114 
115 	@Override
116 	public IConstr addAtLeast(IVecInt literals, int degree)
117 			throws ContradictionException {
118 		StringBuffer out = getOut();
119 		nbOfConstraints++;
120 		for (IteratorInt iterator = literals.iterator(); iterator.hasNext();)
121 			out.append("+1 x" + iterator.next() + " ");
122 		out.append(">= " + degree + " ;\n");
123 		return FAKE_CONSTR;
124 	}
125 
126 	@Override
127 	public IConstr addAtMost(IVecInt literals, int degree)
128 			throws ContradictionException {
129 		StringBuffer out = getOut();
130 		nbOfConstraints++;
131 		for (IteratorInt iterator = literals.iterator(); iterator.hasNext();)
132 			out.append("-1 x" + iterator.next() + " ");
133 		out.append(">= " + (-degree) + " ;\n");
134 		return FAKE_CONSTR;
135 	}
136 
137 	@Override
138 	public IConstr addClause(IVecInt literals) throws ContradictionException {
139 		StringBuffer out = getOut();
140 		nbOfConstraints++;
141 		int lit;
142 		for (IteratorInt iterator = literals.iterator(); iterator.hasNext();) {
143 			lit = iterator.next();
144 			if (lit > 0) {
145 				out.append("+1 x" + lit + " ");
146 			} else {
147 				out.append("+1 ~x" + -lit + " ");
148 			}
149 		}
150 		out.append(">= 1 ;\n");
151 		return FAKE_CONSTR;
152 	}
153 
154 	/*
155 	 * (non-Javadoc)
156 	 * 
157 	 * @see org.sat4j.pb.IPBSolver#getExplanation()
158 	 */
159 	public String getExplanation() {
160 		// TODO Auto-generated method stub
161 		return null;
162 	}
163 
164 	/*
165 	 * (non-Javadoc)
166 	 * 
167 	 * @see
168 	 * org.sat4j.pb.IPBSolver#setListOfVariablesForExplanation(org.sat4j.specs
169 	 * .IVecInt)
170 	 */
171 	public void setListOfVariablesForExplanation(IVecInt listOfVariables) {
172 		// TODO Auto-generated method stub
173 
174 	}
175 
176 	@Override
177 	public String toString() {
178 		StringBuffer out = getOut();
179 		if (!inserted) {
180 			StringBuffer tmp = new StringBuffer();
181 			tmp.append("* #variable= " + nVars());
182 			tmp.append(" #constraint= " + nbOfConstraints + " \n");
183 			if (obj != null) {
184 				tmp.append("min: ");
185 				tmp.append(obj);
186 				tmp.append(" ;\n");
187 			}
188 			out.insert(indxConstrObj, tmp.toString());
189 			inserted = true;
190 		}
191 		return out.toString();
192 	}
193 
194 	@Override
195 	public String toString(String prefix) {
196 		return "OPB output solver";
197 	}
198 
199 	@Override
200 	public int newVar(int howmany) {
201 		StringBuffer out = getOut();
202 		setNbVars(howmany);
203 		// to add later the number of constraints
204 		indxConstrObj = out.length();
205 		out.append("\n");
206 		return howmany;
207 	}
208 
209 	@Override
210 	public void setExpectedNumberOfClauses(int nb) {
211 	}
212 
213 	public ObjectiveFunction getObjectiveFunction() {
214 		return obj;
215 	}
216 
217 }