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.maxsat;
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.opt.MaxSatDecorator;
39  import org.sat4j.pb.OptToPBSATAdapter;
40  import org.sat4j.pb.PseudoOptDecorator;
41  import org.sat4j.specs.ContradictionException;
42  import org.sat4j.specs.IVecInt;
43  import org.sat4j.specs.TimeoutException;
44  import org.sat4j.tools.OptToSatAdapter;
45  
46  public class TestDavid {
47  
48      @Test
49      public void testMaxsat() throws ContradictionException, TimeoutException {
50          MaxSatDecorator maxsat = new MaxSatDecorator(SolverFactory.newLight());
51          maxsat.newVar(3);
52          IVecInt literals = new VecInt();
53          literals.push(1).push(-2).push(3);
54          maxsat.addClause(literals);
55          literals.clear();
56          literals.push(-1).push(-2);
57          maxsat.addClause(literals);
58          literals.clear();
59          literals.push(2);
60          maxsat.addClause(literals);
61          literals.clear();
62          literals.push(-3);
63          maxsat.addClause(literals);
64          OptToSatAdapter opt = new OptToSatAdapter(maxsat);
65          assertTrue(opt.isSatisfiable());
66          assertEquals(1, maxsat.calculateObjective());
67      }
68  
69      @Test
70      public void testMaxsatBis() throws ContradictionException, TimeoutException {
71          MaxSatDecorator maxsat = new MaxSatDecorator(SolverFactory.newLight());
72          maxsat.newVar(3);
73          IVecInt literals = new VecInt();
74          literals.push(1).push(-2);
75          maxsat.addClause(literals);
76          literals.clear();
77          literals.push(-1).push(-2);
78          maxsat.addClause(literals);
79          literals.clear();
80          literals.push(2);
81          maxsat.addClause(literals);
82          literals.clear();
83          literals.push(-3);
84          maxsat.addClause(literals);
85          OptToSatAdapter opt = new OptToSatAdapter(maxsat);
86          assertTrue(opt.isSatisfiable());
87          assertTrue(maxsat.calculateObjective().equals(1));
88          assertFalse(opt.model(3));
89      }
90  
91      @Test
92      public void testPartialWeightedMaxsat() throws ContradictionException,
93              TimeoutException {
94          WeightedMaxSatDecorator maxsat = new WeightedMaxSatDecorator(
95                  SolverFactory.newLight());
96          maxsat.newVar(3);
97          IVecInt literals = new VecInt();
98          literals.push(1).push(-2).push(3);
99          maxsat.addHardClause(literals);
100         literals.clear();
101         literals.push(-1).push(-2);
102         maxsat.addHardClause(literals);
103         literals.clear();
104         literals.push(2);
105         maxsat.addSoftClause(10, literals);
106         literals.clear();
107         literals.push(-3);
108         maxsat.addSoftClause(5, literals);
109         OptToPBSATAdapter opt = new OptToPBSATAdapter(new PseudoOptDecorator(
110                 maxsat));
111         assertTrue(opt.isSatisfiable());
112         assertTrue(opt.model(2));
113         assertTrue(opt.model(3));
114     }
115 
116     @Test
117     public void testWeightedMinimization() throws ContradictionException,
118             TimeoutException {
119         WeightedMaxSatDecorator maxsat = new WeightedMaxSatDecorator(
120                 SolverFactory.newLight());
121         maxsat.newVar(3);
122         IVecInt literals = new VecInt();
123         literals.push(1).push(-2).push(3);
124         maxsat.addHardClause(literals);
125         literals.clear();
126         literals.push(-1).push(-2);
127         maxsat.addHardClause(literals);
128         literals.clear();
129         literals.push(-2).push(3);
130         IVecInt coefs = new VecInt().push(10).push(5);
131         maxsat.addWeightedLiteralsToMinimize(literals, coefs);
132         OptToPBSATAdapter opt = new OptToPBSATAdapter(new PseudoOptDecorator(
133                 maxsat));
134         assertTrue(opt.isSatisfiable());
135         assertTrue(opt.model(2));
136         assertTrue(opt.model(3));
137     }
138 
139     @Test
140     public void testExampleDavid() throws ContradictionException,
141             TimeoutException {
142         WeightedMaxSatDecorator maxsat = new WeightedMaxSatDecorator(
143                 SolverFactory.newLight());
144         maxsat.newVar(3);
145         IVecInt literals = new VecInt();
146         literals.push(1).push(-2).push(3);
147         maxsat.addHardClause(literals);
148         literals.clear();
149         literals.push(-1).push(-2);
150         maxsat.addHardClause(literals);
151         literals.clear();
152         literals.push(1).push(2).push(3);
153         maxsat.addLiteralsToMinimize(literals);
154         OptToPBSATAdapter opt = new OptToPBSATAdapter(new PseudoOptDecorator(
155                 maxsat));
156         assertTrue(opt.isSatisfiable());
157         assertFalse(opt.model(1));
158         assertFalse(opt.model(2));
159         assertFalse(opt.model(3));
160     }
161 }