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  import static org.junit.Assert.fail;
36  
37  import java.math.BigInteger;
38  
39  import org.junit.Test;
40  import org.sat4j.core.Vec;
41  import org.sat4j.core.VecInt;
42  import org.sat4j.specs.ContradictionException;
43  import org.sat4j.specs.IConstr;
44  import org.sat4j.specs.IProblem;
45  import org.sat4j.specs.IVec;
46  import org.sat4j.specs.IVecInt;
47  import org.sat4j.specs.TimeoutException;
48  import org.sat4j.tools.ModelIterator;
49  
50  public class TestLonca {
51  
52      @Test
53      public void testIteratingWithNoObjectiveFunction() {
54          IPBSolver solver = buildSolver1();
55  
56          IProblem problem = solver;
57          int nbModel = 0;
58          try {
59              while (problem.isSatisfiable()) {
60                  int[] mod = problem.model();
61                  solver.addBlockingClause(new VecInt(invert(mod)));
62                  nbModel++;
63              }
64          } catch (TimeoutException e) {
65              fail();
66          } catch (ContradictionException e) {
67              fail();
68          }
69          assertEquals(4, nbModel);
70      }
71  
72      @Test
73      public void testIteratingWithObjectiveFunctionCard() {
74          IPBSolver solver = buildSolver2();
75          IProblem problem = solver;
76          int nbModel = 0;
77          try {
78              while (problem.isSatisfiable()) {
79                  int[] mod = problem.model();
80                  solver.addBlockingClause(new VecInt(invert(mod)));
81                  nbModel++;
82              }
83          } catch (TimeoutException e) {
84              fail();
85          } catch (ContradictionException e) {
86              fail();
87          }
88          assertEquals(4, nbModel);
89      }
90  
91      @Test
92      public void testIteratingWithObjectiveFunctionPseudo() {
93          IPBSolver solver = buildSolver3();
94          IProblem problem = solver;
95          int nbModel = 0;
96          try {
97              while (problem.isSatisfiable()) {
98                  int[] mod = problem.model();
99                  solver.addBlockingClause(new VecInt(invert(mod)));
100                 nbModel++;
101             }
102         } catch (TimeoutException e) {
103             fail();
104         } catch (ContradictionException e) {
105             fail();
106         }
107         assertEquals(4, nbModel);
108     }
109 
110     @Test
111     public void testIteratingWithObjectiveFunctionWithDecorator() {
112         IPBSolver solver = buildSolver2();
113 
114         IProblem problem = new ModelIterator(solver);
115         int nbModel = 0;
116         try {
117             while (problem.isSatisfiable()) {
118                 problem.model(); // needed to discard that model
119                 nbModel++;
120             }
121         } catch (TimeoutException e) {
122             fail();
123         }
124         assertEquals(4, nbModel);
125     }
126 
127     private static int[] invert(int[] mod) {
128         int[] res = new int[mod.length];
129         for (int i = 0; i < res.length; i++) {
130             res[i] = -mod[i];
131         }
132         return res;
133     }
134 
135     private static IPBSolver buildSolver1() {
136         IPBSolver solver = new OptToPBSATAdapter(new PseudoOptDecorator(
137                 SolverFactory.newResolution()));
138 
139         try {
140             solver.addClause(new VecInt(new int[] { 1, 2, 3 }));
141             solver.addClause(new VecInt(new int[] { -1, -2 }));
142             solver.addClause(new VecInt(new int[] { -2, -3 }));
143         } catch (ContradictionException e) {
144             fail();
145         }
146         return solver;
147     }
148 
149     private static IPBSolver buildSolver2() {
150         IPBSolver solver = buildSolver1();
151         IVecInt vars = new VecInt(new int[] { 1, 2, 3 });
152         IVec<BigInteger> coeffs = new Vec<BigInteger>(new BigInteger[] {
153                 BigInteger.valueOf(1), BigInteger.valueOf(1),
154                 BigInteger.valueOf(1) });
155         ObjectiveFunction func = new ObjectiveFunction(vars, coeffs);
156         solver.setObjectiveFunction(func);
157         return solver;
158     }
159 
160     private static IPBSolver buildSolver3() {
161         IPBSolver solver = buildSolver1();
162         IVecInt vars = new VecInt(new int[] { 1, 2, 3 });
163         IVec<BigInteger> coeffs = new Vec<BigInteger>(new BigInteger[] {
164                 BigInteger.valueOf(8), BigInteger.valueOf(4),
165                 BigInteger.valueOf(2) });
166         ObjectiveFunction func = new ObjectiveFunction(vars, coeffs);
167 
168         solver.setObjectiveFunction(func);
169         return solver;
170     }
171 
172     @Test
173     public void testRemovalOfConstraintsPropagatingLiterals()
174             throws ContradictionException, TimeoutException {
175         IPBSolver solver = buildSolver1();
176         IVecInt literals = new VecInt(new int[] { 4, 5, 6, 7 });
177         IVecInt coeffs = new VecInt(new int[] { 12, 10, 8, 6 });
178         IConstr c1 = solver.addAtMost(literals, coeffs, 8);
179         IConstr c2 = solver.addAtMost(literals, coeffs, 6);
180         assertTrue(solver.isSatisfiable());
181         assertFalse(solver.isSatisfiable(new VecInt(new int[] { 4 })));
182         assertFalse(solver.isSatisfiable(new VecInt(new int[] { 5 })));
183         assertFalse(solver.isSatisfiable(new VecInt(new int[] { 6 })));
184         assertTrue(solver.isSatisfiable(new VecInt(new int[] { 7 })));
185         solver.removeConstr(c2);
186         assertTrue(solver.isSatisfiable());
187         assertFalse(solver.isSatisfiable(new VecInt(new int[] { 4 })));
188         assertFalse(solver.isSatisfiable(new VecInt(new int[] { 5 })));
189         assertTrue(solver.isSatisfiable(new VecInt(new int[] { 6 })));
190         assertTrue(solver.isSatisfiable(new VecInt(new int[] { 7 })));
191         solver.removeConstr(c1);
192         assertTrue(solver.isSatisfiable());
193         assertTrue(solver.isSatisfiable(new VecInt(new int[] { 4 })));
194         assertTrue(solver.isSatisfiable(new VecInt(new int[] { 5 })));
195         assertTrue(solver.isSatisfiable(new VecInt(new int[] { 6 })));
196         assertTrue(solver.isSatisfiable(new VecInt(new int[] { 7 })));
197 
198     }
199 
200     @Test
201     public void testRemovalOfConstraintsPropagatingLiteralsBis()
202             throws ContradictionException, TimeoutException {
203         IPBSolver solver = buildSolver1();
204         IVecInt literals = new VecInt(new int[] { 4, 5, 6, 7 });
205         IVecInt coeffs = new VecInt(new int[] { 12, 10, 8, 6 });
206         IConstr c1 = solver.addAtMost(literals, coeffs, 6);
207         IConstr c2 = solver.addAtMost(literals, coeffs, 8);
208         assertTrue(solver.isSatisfiable());
209         assertFalse(solver.isSatisfiable(new VecInt(new int[] { 4 })));
210         assertFalse(solver.isSatisfiable(new VecInt(new int[] { 5 })));
211         assertFalse(solver.isSatisfiable(new VecInt(new int[] { 6 })));
212         assertTrue(solver.isSatisfiable(new VecInt(new int[] { 7 })));
213         solver.removeConstr(c2);
214         assertTrue(solver.isSatisfiable());
215         assertFalse(solver.isSatisfiable(new VecInt(new int[] { 4 })));
216         assertFalse(solver.isSatisfiable(new VecInt(new int[] { 5 })));
217         assertFalse(solver.isSatisfiable(new VecInt(new int[] { 6 })));
218         assertTrue(solver.isSatisfiable(new VecInt(new int[] { 7 })));
219         solver.removeConstr(c1);
220         assertTrue(solver.isSatisfiable());
221         assertTrue(solver.isSatisfiable(new VecInt(new int[] { 4 })));
222         assertTrue(solver.isSatisfiable(new VecInt(new int[] { 5 })));
223         assertTrue(solver.isSatisfiable(new VecInt(new int[] { 6 })));
224         assertTrue(solver.isSatisfiable(new VecInt(new int[] { 7 })));
225 
226     }
227 }