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.opt;
31
32 import org.sat4j.core.VecInt;
33 import org.sat4j.specs.IOptimizationProblem;
34 import org.sat4j.specs.ISolver;
35 import org.sat4j.specs.IVecInt;
36 import org.sat4j.specs.TimeoutException;
37 import org.sat4j.tools.SolverDecorator;
38
39
40
41
42
43
44
45
46
47
48
49 public abstract class AbstractSelectorVariablesDecorator extends
50 SolverDecorator<ISolver> implements IOptimizationProblem {
51
52
53
54
55 private static final long serialVersionUID = 1L;
56
57 private int nbexpectedclauses;
58
59 protected int[] prevfullmodel;
60
61
62
63
64 protected int[] prevmodel;
65
66
67
68 protected boolean[] prevboolmodel;
69
70 protected boolean isSolutionOptimal;
71
72 public AbstractSelectorVariablesDecorator(ISolver solver) {
73 super(solver);
74 }
75
76 @Override
77 public void setExpectedNumberOfClauses(int nb) {
78 this.nbexpectedclauses = nb;
79 }
80
81 public int getExpectedNumberOfClauses() {
82 return this.nbexpectedclauses;
83 }
84
85 public boolean admitABetterSolution() throws TimeoutException {
86 return admitABetterSolution(VecInt.EMPTY);
87 }
88
89
90
91
92 public boolean admitABetterSolution(IVecInt assumps)
93 throws TimeoutException {
94 this.isSolutionOptimal = false;
95 boolean result = super.isSatisfiable(assumps, true);
96 if (result) {
97 this.prevboolmodel = new boolean[nVars()];
98 for (int i = 0; i < nVars(); i++) {
99 this.prevboolmodel[i] = decorated().model(i + 1);
100 }
101 this.prevfullmodel = super.modelWithInternalVariables();
102 this.prevmodel = super.model();
103 calculateObjectiveValue();
104 } else {
105 this.isSolutionOptimal = true;
106 }
107 return result;
108 }
109
110 abstract void calculateObjectiveValue();
111
112 @Override
113 public int[] model() {
114 return this.prevmodel;
115 }
116
117 @Override
118 public boolean model(int var) {
119 return this.prevboolmodel[var - 1];
120 }
121
122 public boolean isOptimal() {
123 return this.isSolutionOptimal;
124 }
125 }