1 |
| |
2 |
| |
3 |
| |
4 |
| |
5 |
| |
6 |
| |
7 |
| package org.sat4j.opt; |
8 |
| |
9 |
| import org.sat4j.core.VecInt; |
10 |
| import org.sat4j.specs.ContradictionException; |
11 |
| import org.sat4j.specs.IConstr; |
12 |
| import org.sat4j.specs.IOptimizationProblem; |
13 |
| import org.sat4j.specs.ISolver; |
14 |
| import org.sat4j.specs.IVecInt; |
15 |
| import org.sat4j.specs.TimeoutException; |
16 |
| import org.sat4j.tools.SolverDecorator; |
17 |
| |
18 |
| public class MaxSatDecorator extends SolverDecorator implements |
19 |
| IOptimizationProblem { |
20 |
| |
21 |
| |
22 |
| |
23 |
| |
24 |
| private static final long serialVersionUID = 1L; |
25 |
| |
26 |
| private int nborigvars; |
27 |
| |
28 |
| private int nbexpectedclauses; |
29 |
| |
30 |
| private int nbnewvar; |
31 |
| |
32 |
| private int[] prevfullmodel; |
33 |
| |
34 |
0
| public MaxSatDecorator(ISolver solver) {
|
35 |
0
| super(solver);
|
36 |
| } |
37 |
| |
38 |
0
| @Override
|
39 |
| public int[] model() { |
40 |
0
| int[] shortmodel = new int[nborigvars];
|
41 |
0
| for (int i = 0; i < nborigvars; i++) {
|
42 |
0
| shortmodel[i] = prevfullmodel[i];
|
43 |
| } |
44 |
0
| return shortmodel;
|
45 |
| } |
46 |
| |
47 |
0
| @Override
|
48 |
| public int newVar(int howmany) { |
49 |
0
| nborigvars = super.newVar(howmany);
|
50 |
0
| return nborigvars;
|
51 |
| } |
52 |
| |
53 |
0
| @Override
|
54 |
| public void setExpectedNumberOfClauses(int nb) { |
55 |
0
| nbexpectedclauses = nb;
|
56 |
0
| super.setExpectedNumberOfClauses(nb);
|
57 |
0
| super.newVar(nborigvars + nbexpectedclauses);
|
58 |
| } |
59 |
| |
60 |
0
| @Override
|
61 |
| public IConstr addClause(IVecInt literals) throws ContradictionException { |
62 |
0
| literals.push(nborigvars + ++nbnewvar);
|
63 |
0
| return super.addClause(literals);
|
64 |
| } |
65 |
| |
66 |
0
| @Override
|
67 |
| public void reset() { |
68 |
0
| nbnewvar = 0;
|
69 |
0
| vec.clear();
|
70 |
0
| super.reset();
|
71 |
| } |
72 |
| |
73 |
0
| public boolean admitABetterSolution() throws TimeoutException {
|
74 |
0
| boolean result = super.isSatisfiable();
|
75 |
0
| if (result)
|
76 |
0
| prevfullmodel = super.model();
|
77 |
0
| return result;
|
78 |
| } |
79 |
| |
80 |
0
| public boolean hasNoObjectiveFunction() {
|
81 |
0
| return false;
|
82 |
| } |
83 |
| |
84 |
0
| public boolean nonOptimalMeansSatisfiable() {
|
85 |
0
| return false;
|
86 |
| } |
87 |
| |
88 |
0
| public Number calculateObjective() {
|
89 |
0
| counter = 0;
|
90 |
0
| for (int q : prevfullmodel) {
|
91 |
0
| if (q > nborigvars) {
|
92 |
0
| counter++;
|
93 |
| } |
94 |
| } |
95 |
0
| return counter;
|
96 |
| } |
97 |
| |
98 |
| private final IVecInt vec = new VecInt(); |
99 |
| |
100 |
| private int counter; |
101 |
| |
102 |
0
| public void discard() throws ContradictionException {
|
103 |
0
| if (vec.isEmpty()) {
|
104 |
0
| for (int i = nborigvars + 1; i <= nVars(); i++) {
|
105 |
0
| vec.push(i);
|
106 |
| } |
107 |
| } |
108 |
0
| super.addAtMost(vec, counter - 1);
|
109 |
| } |
110 |
| |
111 |
| } |