View Javadoc

1   /*******************************************************************************
2    * SAT4J: a SATisfiability library for Java Copyright (C) 2004, 2012 Artois University and CNRS
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   * Contributors:
28   *   CRIL - initial API and implementation
29   *******************************************************************************/
30  package org.sat4j.pb;
31  
32  import static org.junit.Assert.assertEquals;
33  import static org.junit.Assert.assertTrue;
34  
35  import java.math.BigInteger;
36  
37  import org.junit.Test;
38  import org.sat4j.core.Vec;
39  import org.sat4j.core.VecInt;
40  import org.sat4j.specs.ContradictionException;
41  import org.sat4j.specs.IVec;
42  import org.sat4j.specs.IVecInt;
43  import org.sat4j.specs.TimeoutException;
44  import org.sat4j.tools.RemiUtils;
45  
46  public class BugSAT16 {
47  
48      @Test
49      public void testCNFCase() throws ContradictionException, TimeoutException {
50  
51          IPBSolver solver = SolverFactory.newDefault();
52  
53          // +1 x6 >= 1 [+1 x6 >= 1]
54          solver.addClause(transform1(new int[] { 6 }));
55  
56          // +1 x6 +1 ~x5 >= 1 [+1 x6 -1 x5 >= 0]
57          solver.addClause(transform1(new int[] { 6, -5 }));
58  
59          // +1 ~x6 +1 x5 >= 1 [-1 x6 +1 x5 >= 0]
60          solver.addClause(transform1(new int[] { -6, 5 }));
61  
62          // +1 x6 +1 ~x4 >= 1 [+1 x6 -1 x4 >= 0]
63          solver.addClause(transform1(new int[] { 6, -4 }));
64  
65          // +1 ~x6 +1 x4 >= 1 [-1 x6 +1 x4 >= 0]
66          solver.addClause(transform1(new int[] { -6, 4 }));
67  
68          // +1 x4 +1 ~x2 +1 ~x1 >= 2 [+1 x4 -1 x2 -1 x1 >= 0]
69          solver.addClause(transform1(new int[] { 4, -2, -1 }));
70  
71          // +1 ~x4 +1 x2 +1 x1 >= 1 [-1 x4 +1 x2 +1 x1 >= 0]
72          solver.addClause(transform1(new int[] { -4, 2, 1 }));
73  
74          IVecInt backbone = RemiUtils.backbone(solver);
75          assertEquals(3, backbone.size());
76          assertTrue(backbone.contains(6));
77          assertTrue(backbone.contains(5));
78          assertTrue(backbone.contains(4));
79      }
80  
81      @Test
82      public void testPBCase() throws ContradictionException, TimeoutException {
83  
84          IPBSolver solver = SolverFactory.newDefault();
85  
86          // +1 x6 >= 1 [+1 x6 >= 1]
87          solver.addPseudoBoolean(transform1(new int[] { 6 }),
88                  transform2(new int[] { 1 }), true, BigInteger.valueOf(1));
89  
90          // +1 x6 +1 ~x5 >= 1 [+1 x6 -1 x5 >= 0]
91          solver.addPseudoBoolean(transform1(new int[] { 6, -5 }),
92                  transform2(new int[] { 1, 1 }), true, BigInteger.valueOf(1));
93  
94          // +1 ~x6 +1 x5 >= 1 [-1 x6 +1 x5 >= 0]
95          solver.addPseudoBoolean(transform1(new int[] { -6, 5 }),
96                  transform2(new int[] { 1, 1 }), true, BigInteger.valueOf(1));
97  
98          // +1 x6 +1 ~x4 >= 1 [+1 x6 -1 x4 >= 0]
99          solver.addPseudoBoolean(transform1(new int[] { 6, -4 }),
100                 transform2(new int[] { 1, 1 }), true, BigInteger.valueOf(1));
101 
102         // +1 ~x6 +1 x4 >= 1 [-1 x6 +1 x4 >= 0]
103         solver.addPseudoBoolean(transform1(new int[] { -6, 4 }),
104                 transform2(new int[] { 1, 1 }), true, BigInteger.valueOf(1));
105 
106         // +1 x4 +1 ~x2 +1 ~x1 >= 2 [+1 x4 -1 x2 -1 x1 >= 0]
107         solver.addPseudoBoolean(transform1(new int[] { 4, -2, -1 }),
108                 transform2(new int[] { 1, 1, 1 }), true, BigInteger.valueOf(2));
109 
110         // +1 ~x4 +1 x2 +1 x1 >= 1 [-1 x4 +1 x2 +1 x1 >= 0]
111         solver.addPseudoBoolean(transform1(new int[] { -4, 2, 1 }),
112                 transform2(new int[] { 1, 1, 1 }), true, BigInteger.valueOf(1));
113 
114         IVecInt backbone = RemiUtils.backbone(solver);
115         assertEquals(3, backbone.size());
116         assertTrue(backbone.contains(6));
117         assertTrue(backbone.contains(5));
118         assertTrue(backbone.contains(4));
119     }
120 
121     static IVecInt transform1(int[] intArray) {
122         return new VecInt(intArray);
123     }
124 
125     static IVec<BigInteger> transform2(int[] intArray) {
126         BigInteger[] result = new BigInteger[intArray.length];
127         for (int i = 0; i < intArray.length; i++) {
128             result[i] = BigInteger.valueOf(intArray[i]);
129         }
130         return new Vec<BigInteger>(result);
131     }
132 }