View Javadoc

1   package org.sat4j.tools;
2   
3   /*******************************************************************************
4    * SAT4J: a SATisfiability library for Java Copyright (C) 2004, 2012 Artois University and CNRS
5    *
6    * All rights reserved. This program and the accompanying materials
7    * are made available under the terms of the Eclipse Public License v1.0
8    * which accompanies this distribution, and is available at
9    *  http://www.eclipse.org/legal/epl-v10.html
10   *
11   * Alternatively, the contents of this file may be used under the terms of
12   * either the GNU Lesser General Public License Version 2.1 or later (the
13   * "LGPL"), in which case the provisions of the LGPL are applicable instead
14   * of those above. If you wish to allow use of your version of this file only
15   * under the terms of the LGPL, and not to allow others to use your version of
16   * this file under the terms of the EPL, indicate your decision by deleting
17   * the provisions above and replace them with the notice and other provisions
18   * required by the LGPL. If you do not delete the provisions above, a recipient
19   * may use your version of this file under the terms of the EPL or the LGPL.
20   *
21   * Based on the original MiniSat specification from:
22   *
23   * An extensible SAT solver. Niklas Een and Niklas Sorensson. Proceedings of the
24   * Sixth International Conference on Theory and Applications of Satisfiability
25   * Testing, LNCS 2919, pp 502-518, 2003.
26   *
27   * See www.minisat.se for the original solver in C++.
28   *
29   * Contributors:
30   *   CRIL - initial API and implementation
31   *******************************************************************************/
32  
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  import org.sat4j.tools.encoding.EncodingStrategyAdapter;
38  import org.sat4j.tools.encoding.Policy;
39  
40  /**
41   * 
42   * A decorator for clausal cardinalities constraints.
43   * 
44   * @author stephanieroussel
45   * @since 2.3.1
46   * @param <T>
47   */
48  public class ClausalCardinalitiesDecorator<T extends ISolver> extends
49          SolverDecorator<T> {
50  
51      /**
52  	 * 
53  	 */
54      private static final long serialVersionUID = 1L;
55  
56      private final EncodingStrategyAdapter encodingAdapter;
57  
58      public ClausalCardinalitiesDecorator(T solver) {
59          super(solver);
60          this.encodingAdapter = new Policy();
61      }
62  
63      public ClausalCardinalitiesDecorator(T solver,
64              EncodingStrategyAdapter encodingAd) {
65          super(solver);
66          this.encodingAdapter = encodingAd;
67      }
68  
69      @Override
70      public IConstr addAtLeast(IVecInt literals, int k)
71              throws ContradictionException {
72          if (k == 1) {
73              return this.encodingAdapter.addAtLeastOne(decorated(), literals);
74          } else {
75              return this.encodingAdapter.addAtLeast(decorated(), literals, k);
76          }
77      }
78  
79      @Override
80      public IConstr addAtMost(IVecInt literals, int k)
81              throws ContradictionException {
82          if (k == 1) {
83              return this.encodingAdapter.addAtMostOne(decorated(), literals);
84          } else {
85              return this.encodingAdapter.addAtMost(decorated(), literals, k);
86          }
87      }
88  
89      @Override
90      public IConstr addExactly(IVecInt literals, int k)
91              throws ContradictionException {
92  
93          if (k == 1) {
94              return this.encodingAdapter.addExactlyOne(decorated(), literals);
95          } else {
96              return this.encodingAdapter.addExactly(decorated(), literals, k);
97          }
98      }
99  
100     @Override
101     public String toString() {
102         return toString("");
103     }
104 
105     @Override
106     public String toString(String prefix) {
107         return super.toString(prefix) + "\n"
108                 + "Cardinality to SAT encoding: \n" + "Encoding: "
109                 + this.encodingAdapter + "\n";
110     }
111 
112 }