|
1 |
| |
|
2 |
| |
|
3 |
| |
|
4 |
| |
|
5 |
| |
|
6 |
| |
|
7 |
| package org.sat4j.tools; |
|
8 |
| |
|
9 |
| import org.sat4j.core.VecInt; |
|
10 |
| import org.sat4j.specs.ContradictionException; |
|
11 |
| import org.sat4j.specs.IConstr; |
|
12 |
| import org.sat4j.specs.ISolver; |
|
13 |
| import org.sat4j.specs.IVecInt; |
|
14 |
| import org.sat4j.specs.TimeoutException; |
|
15 |
| |
|
16 |
| public class MaxSatDecorator extends SolverDecorator { |
|
17 |
| |
|
18 |
| private int nborigvars; |
|
19 |
| |
|
20 |
| private int nbexpectedclauses; |
|
21 |
| |
|
22 |
| private int nbnewvar; |
|
23 |
| |
|
24 |
| private int[] prevmodel; |
|
25 |
| |
|
26 |
0
| public MaxSatDecorator(ISolver solver) {
|
|
27 |
0
| super(solver);
|
|
28 |
| } |
|
29 |
| |
|
30 |
0
| @Override
|
|
31 |
| public boolean isSatisfiable() throws TimeoutException { |
|
32 |
0
| if (super.isSatisfiable()) {
|
|
33 |
0
| prevmodel = null;
|
|
34 |
0
| IVecInt vec = new VecInt();
|
|
35 |
0
| try {
|
|
36 |
0
| do {
|
|
37 |
0
| prevmodel = super.model();
|
|
38 |
0
| vec.clear();
|
|
39 |
0
| for (int i = nborigvars+1; i <= nVars(); i++) {
|
|
40 |
0
| vec.push(i);
|
|
41 |
| } |
|
42 |
0
| int counter = 0;
|
|
43 |
0
| for (int q : prevmodel) {
|
|
44 |
0
| if (q > nborigvars) {
|
|
45 |
0
| counter++;
|
|
46 |
| } |
|
47 |
| } |
|
48 |
0
| if (counter == 0) {
|
|
49 |
0
| return true;
|
|
50 |
| } |
|
51 |
0
| super.addAtMost(vec, counter - 1);
|
|
52 |
0
| System.out.println("o " + counter);
|
|
53 |
0
| } while (super.isSatisfiable());
|
|
54 |
| } catch (TimeoutException e) { |
|
55 |
0
| System.err.println("Solver timed out");
|
|
56 |
| } catch (ContradictionException e) { |
|
57 |
0
| System.err.println("added trivial unsat clauses?" + vec);
|
|
58 |
| } |
|
59 |
| } |
|
60 |
| |
|
61 |
0
| return false;
|
|
62 |
| } |
|
63 |
| |
|
64 |
0
| @Override
|
|
65 |
| public int[] model() { |
|
66 |
0
| return prevmodel;
|
|
67 |
| } |
|
68 |
| |
|
69 |
0
| @Override
|
|
70 |
| public int newVar(int howmany) { |
|
71 |
0
| nborigvars = super.newVar(howmany);
|
|
72 |
0
| return nborigvars;
|
|
73 |
| } |
|
74 |
| |
|
75 |
0
| @Override
|
|
76 |
| public void setExpectedNumberOfClauses(int nb) { |
|
77 |
0
| nbexpectedclauses = nb;
|
|
78 |
0
| super.setExpectedNumberOfClauses(nb);
|
|
79 |
0
| super.newVar(nborigvars + nbexpectedclauses);
|
|
80 |
| } |
|
81 |
| |
|
82 |
0
| @Override
|
|
83 |
| public IConstr addClause(IVecInt literals) throws ContradictionException { |
|
84 |
0
| literals.push(nborigvars + nbnewvar++);
|
|
85 |
0
| return super.addClause(literals);
|
|
86 |
| } |
|
87 |
| |
|
88 |
0
| @Override
|
|
89 |
| public void reset() { |
|
90 |
0
| nbnewvar = 0;
|
|
91 |
0
| super.reset();
|
|
92 |
| } |
|
93 |
| |
|
94 |
| } |