1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
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 }