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 private int[] prevfullmodel;
60
61
62
63
64 private int[] prevmodel;
65
66
67
68 private boolean[] prevboolmodel;
69
70 private 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
126 public int getNbexpectedclauses() {
127 return nbexpectedclauses;
128 }
129
130 public void setNbexpectedclauses(int nbexpectedclauses) {
131 this.nbexpectedclauses = nbexpectedclauses;
132 }
133
134 public int[] getPrevfullmodel() {
135 return prevfullmodel;
136 }
137
138 public void setPrevfullmodel(int[] prevfullmodel) {
139 this.prevfullmodel = prevfullmodel.clone();
140 }
141
142 public int[] getPrevmodel() {
143 return prevmodel;
144 }
145
146 public void setPrevmodel(int[] prevmodel) {
147 this.prevmodel = prevmodel.clone();
148 }
149
150 public boolean[] getPrevboolmodel() {
151 return prevboolmodel;
152 }
153
154 public void setPrevboolmodel(boolean[] prevboolmodel) {
155 this.prevboolmodel = prevboolmodel.clone();
156 }
157
158 public boolean isSolutionOptimal() {
159 return isSolutionOptimal;
160 }
161
162 public void setSolutionOptimal(boolean isSolutionOptimal) {
163 this.isSolutionOptimal = isSolutionOptimal;
164 }
165 }