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  
29  package org.sat4j.tools.encoding;
30  
31  import org.sat4j.core.ConstrGroup;
32  import org.sat4j.core.VecInt;
33  import org.sat4j.specs.ContradictionException;
34  import org.sat4j.specs.IConstr;
35  import org.sat4j.specs.ISolver;
36  import org.sat4j.specs.IVecInt;
37  
38  /**
39   * 
40   * For the case "at most one", we can use the binary encoding (also called
41   * birwise encoding) first described in A. M. Frisch, T. J. Peugniez, A. J.
42   * Dogget and P. Nightingale, "Solving Non-Boolean Satisfiability Problems With
43   * Stochastic Local Search: A Comparison of Encodings" in Journal of Automated
44   * Reasoning, vol. 35, n� 1-3, 2005
45   * 
46   * @author sroussel
47   * @since 2.3.1
48   */
49  public class Binary extends EncodingStrategyAdapter {
50  
51  	/**
52  	 * p being the smaller integer greater than log_2(n), this encoding adds p
53  	 * variables and n*p clauses.
54  	 * 
55  	 */
56  	@Override
57  	public IConstr addAtMostOne(ISolver solver, IVecInt literals)
58  			throws ContradictionException {
59  		ConstrGroup group = new ConstrGroup(false);
60  
61  		final int n = literals.size();
62  		final int p = (int) Math.ceil(Math.log(n) / Math.log(2));
63  		final int k = (int) Math.pow(2, p) - n;
64  
65  		int y[] = new int[p];
66  		for (int i = 0; i < p; i++) {
67  			y[i] = solver.nextFreeVarId(true);
68  		}
69  
70  		IVecInt clause = new VecInt();
71  		String binary = "";
72  
73  		for (int i = 0; i < k; i++) {
74  			binary = Integer.toBinaryString(i);
75  			while (binary.length() != p - 1) {
76  				binary = "0" + binary;
77  			}
78  
79  			for (int j = 0; j < p - 1; j++) {
80  				clause.push(-literals.get(i));
81  				if (binary.charAt(j) == '0') {
82  					clause.push(-y[j]);
83  				} else {
84  					clause.push(y[j]);
85  				}
86  				group.add(solver.addClause(clause));
87  				clause.clear();
88  
89  			}
90  		}
91  
92  		for (int i = k; i < n; i++) {
93  			binary = Integer.toBinaryString(2 * k + i - k);
94  			while (binary.length() != p) {
95  				binary = "0" + binary;
96  			}
97  			for (int j = 0; j < p; j++) {
98  				clause.push(-literals.get(i));
99  				if (binary.charAt(j) == '0') {
100 					clause.push(-y[j]);
101 				} else {
102 					clause.push(y[j]);
103 				}
104 				group.add(solver.addClause(clause));
105 				clause.clear();
106 			}
107 
108 		}
109 
110 		return group;
111 	}
112 
113 	@Override
114 	public IConstr addAtMost(ISolver solver, IVecInt literals, int degree)
115 			throws ContradictionException {
116 		// TODO Implement binary at most k method
117 		return super.addAtMost(solver, literals, degree);
118 	}
119 }