1 |
| package org.sat4j.opt; |
2 |
| |
3 |
| import org.sat4j.core.VecInt; |
4 |
| import org.sat4j.specs.ContradictionException; |
5 |
| import org.sat4j.specs.IOptimizationProblem; |
6 |
| import org.sat4j.specs.ISolver; |
7 |
| import org.sat4j.specs.IVecInt; |
8 |
| import org.sat4j.specs.TimeoutException; |
9 |
| import org.sat4j.tools.SolverDecorator; |
10 |
| |
11 |
| |
12 |
| |
13 |
| |
14 |
| |
15 |
| |
16 |
| public class MinOneDecorator extends SolverDecorator implements |
17 |
| IOptimizationProblem { |
18 |
| |
19 |
| |
20 |
| |
21 |
| |
22 |
| private static final long serialVersionUID = 1L; |
23 |
| |
24 |
| private int[] prevmodel; |
25 |
| |
26 |
0
| public MinOneDecorator(ISolver solver) {
|
27 |
0
| super(solver);
|
28 |
| } |
29 |
| |
30 |
0
| public boolean admitABetterSolution() throws TimeoutException {
|
31 |
0
| boolean result = isSatisfiable();
|
32 |
0
| if (result) {
|
33 |
0
| prevmodel = super.model();
|
34 |
| } |
35 |
0
| return result;
|
36 |
| } |
37 |
| |
38 |
0
| public boolean hasNoObjectiveFunction() {
|
39 |
0
| return false;
|
40 |
| } |
41 |
| |
42 |
0
| public boolean nonOptimalMeansSatisfiable() {
|
43 |
0
| return true;
|
44 |
| } |
45 |
| |
46 |
| private int counter; |
47 |
| |
48 |
0
| public Number calculateObjective() {
|
49 |
0
| counter = 0;
|
50 |
0
| for (int p : prevmodel) {
|
51 |
0
| if (p > 0) {
|
52 |
0
| counter++;
|
53 |
| } |
54 |
| } |
55 |
0
| return counter;
|
56 |
| } |
57 |
| |
58 |
| private final IVecInt literals = new VecInt(); |
59 |
| |
60 |
0
| public void discard() throws ContradictionException {
|
61 |
0
| if (literals.isEmpty()) {
|
62 |
0
| for (int i = 1; i <= nVars(); i++) {
|
63 |
0
| literals.push(i);
|
64 |
| } |
65 |
| } |
66 |
0
| addAtMost(literals, counter - 1);
|
67 |
| } |
68 |
| |
69 |
0
| @Override
|
70 |
| public int[] model() { |
71 |
0
| return prevmodel;
|
72 |
| } |
73 |
| |
74 |
0
| @Override
|
75 |
| public void reset() { |
76 |
0
| literals.clear();
|
77 |
0
| super.reset();
|
78 |
| } |
79 |
| |
80 |
| } |