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.assertFalse;
34  import static org.junit.Assert.assertTrue;
35  
36  import org.junit.Test;
37  import org.sat4j.core.VecInt;
38  import org.sat4j.specs.ContradictionException;
39  import org.sat4j.specs.IVecInt;
40  import org.sat4j.specs.TimeoutException;
41  
42  /**
43   * @author sroussel
44   * 
45   */
46  public class BugSAT21 {
47  
48      @Test
49      public void testAtLeastWithNegativeLiteralsAsText()
50              throws ContradictionException {
51  
52          IPBSolver pbSolver = new OPBStringSolver();
53          pbSolver.newVar(2);
54          pbSolver.setExpectedNumberOfClauses(1);
55  
56          int[] constr = { -1, 2 };
57  
58          pbSolver.addAtLeast(new VecInt(constr), 1);
59          String expected = "* #variable= 2 #constraint= 1 \n\n-1 x1 +1 x2 >= 0 ;\n";
60          assertEquals(expected, pbSolver.toString());
61  
62      }
63  
64      @Test
65      public void testAtLeastWithNegativeLiterals()
66              throws ContradictionException, TimeoutException {
67  
68          IPBSolver pbSolver = SolverFactory.newDefault();
69          pbSolver.newVar(2);
70          pbSolver.setExpectedNumberOfClauses(1);
71  
72          int[] constr = { -1, 2 };
73  
74          pbSolver.addAtLeast(new VecInt(constr), 1);
75          IVecInt assumps = new VecInt();
76          assumps.push(1).push(-2);
77          assertFalse(pbSolver.isSatisfiable(assumps));
78          assumps.clear();
79          assumps.push(-1).push(-2);
80          assertTrue(pbSolver.isSatisfiable(assumps));
81          assumps.clear();
82          assumps.push(1).push(2);
83          assertTrue(pbSolver.isSatisfiable(assumps));
84          assumps.clear();
85          assumps.push(-1).push(2);
86          assertTrue(pbSolver.isSatisfiable(assumps));
87  
88      }
89  
90      @Test
91      public void testAlMostWithNegativeLiteralsAsText()
92              throws ContradictionException {
93  
94          IPBSolver pbSolver = new OPBStringSolver();
95          pbSolver.newVar(2);
96          pbSolver.setExpectedNumberOfClauses(1);
97  
98          int[] constr = { -1, 2 };
99  
100         pbSolver.addAtMost(new VecInt(constr), 1);
101         String expected = "* #variable= 2 #constraint= 1 \n\n+1 x1 -1 x2 >= 0 ;\n";
102         assertEquals(expected, pbSolver.toString());
103 
104     }
105 
106     @Test
107     public void testAtMostWithNegativeLiterals() throws ContradictionException,
108             TimeoutException {
109 
110         IPBSolver pbSolver = SolverFactory.newDefault();
111         pbSolver.newVar(2);
112         pbSolver.setExpectedNumberOfClauses(1);
113 
114         int[] constr = { -1, 2 };
115 
116         pbSolver.addAtMost(new VecInt(constr), 1);
117         IVecInt assumps = new VecInt();
118         assumps.push(1).push(-2);
119         assertTrue(pbSolver.isSatisfiable(assumps));
120         assumps.clear();
121         assumps.push(-1).push(-2);
122         assertTrue(pbSolver.isSatisfiable(assumps));
123         assumps.clear();
124         assumps.push(1).push(2);
125         assertTrue(pbSolver.isSatisfiable(assumps));
126         assumps.clear();
127         assumps.push(-1).push(2);
128         assertFalse(pbSolver.isSatisfiable(assumps));
129 
130     }
131 
132 }