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  	/**
59  	 * 
60  	 */
61  	public OPBStringSolver() {
62  	}
63  
64  	/**
65  	 * @param initSize
66  	 */
67  	public OPBStringSolver(int initSize) {
68  		super(initSize);
69  	}
70  
71  	public IConstr addPseudoBoolean(IVecInt lits, IVec<BigInteger> coeffs,
72  			boolean moreThan, BigInteger d) throws ContradictionException {
73  		StringBuffer out = getOut();
74  		assert lits.size() == coeffs.size();
75  		nbOfConstraints ++;
76  		if (moreThan) {
77  			for (int i = 0; i <lits.size();i++)
78  				out.append(coeffs.get(i)+" x"+ lits.get(i) + " ");
79          	out.append(">= "+d+" ;\n");
80  		}
81          else {
82  			for (int i = 0; i <lits.size();i++)
83  				out.append(coeffs.get(i).negate()+" x"+ lits.get(i) + " ");
84          	out.append(">= "+d.negate()+" ;\n");        	
85          }
86          return null;        
87  	}
88  
89  	public void setObjectiveFunction(ObjectiveFunction obj) {
90  		StringBuffer out = getOut();
91  		StringBuffer tmp = new StringBuffer();
92  		tmp.append(" #constraint= "+nbOfConstraints+" \n");
93  		tmp.append("min : ");
94  		IVecInt lits = obj.getVars();
95  		IVec<BigInteger> coeffs = obj.getCoeffs();
96  		for (int i = 0; i <lits.size();i++)
97  			tmp.append(coeffs.get(i)+" x"+ lits.get(i) + " ");
98  		tmp.append(" ;\n");
99  		out.insert(indxConstrObj,tmp);
100 	}
101 
102 	@Override
103 	public IConstr addAtLeast(IVecInt literals, int degree)
104 			throws ContradictionException {
105 		StringBuffer out = getOut();
106 		nbOfConstraints++;
107         for (IteratorInt iterator = literals.iterator();iterator.hasNext();)
108             out.append("+1 x"+iterator.next() + " ");
109         out.append(">= "+degree+" ;\n");
110         return null;        
111 	}
112 
113 	@Override
114 	public IConstr addAtMost(IVecInt literals, int degree)
115 			throws ContradictionException {
116 		StringBuffer out = getOut();
117 		nbOfConstraints++;
118         for (IteratorInt iterator = literals.iterator();iterator.hasNext();)
119             out.append("-1 x"+iterator.next() + " ");
120         out.append(">= "+(-degree)+" ;\n");
121         return null;        
122 	}
123 
124 	@Override
125 	public IConstr addClause(IVecInt literals) throws ContradictionException {
126 		StringBuffer out = getOut();
127 		nbOfConstraints++;
128         for (IteratorInt iterator = literals.iterator();iterator.hasNext();)
129             out.append("+1 x"+iterator.next() + " ");
130         out.append(">= 1 ;\n");
131         return null;        
132 	}
133 
134 	/* (non-Javadoc)
135 	 * @see org.sat4j.pb.IPBSolver#getExplanation()
136 	 */
137 	public String getExplanation() {
138 		// TODO Auto-generated method stub
139 		return null;
140 	}
141 
142 	/* (non-Javadoc)
143 	 * @see org.sat4j.pb.IPBSolver#setListOfVariablesForExplanation(org.sat4j.specs.IVecInt)
144 	 */
145 	public void setListOfVariablesForExplanation(IVecInt listOfVariables) {
146 		// TODO Auto-generated method stub
147 
148 	}
149 	
150 	@Override
151 	public String toString(){
152 		return getOut().toString();
153 	}
154 
155 	@Override
156 	public String toString(String prefix) {
157         return "OPB output solver";
158 	}
159 
160     @Override
161     public int newVar(int howmany) {
162 		StringBuffer out = getOut();
163         out.append("* #variable= " + howmany);
164         setNbVars(howmany);
165         // to add later the number of constraints
166         indxConstrObj = out.length();
167         return 0;
168     }
169     
170     @Override
171     public void setExpectedNumberOfClauses(int nb) {
172     }
173 
174 
175 
176 }