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.sat;
31
32 import org.sat4j.minisat.core.Constr;
33 import org.sat4j.minisat.core.ICDCL;
34 import org.sat4j.minisat.core.ICDCLLogger;
35 import org.sat4j.minisat.core.IPhaseSelectionStrategy;
36 import org.sat4j.minisat.core.RestartStrategy;
37 import org.sat4j.minisat.core.SearchParams;
38 import org.sat4j.minisat.orders.RSATPhaseSelectionStrategy;
39 import org.sat4j.minisat.restarts.NoRestarts;
40
41
42
43
44
45
46
47
48 public class RemoteControlStrategy implements RestartStrategy,
49 IPhaseSelectionStrategy {
50
51 private static final long serialVersionUID = 1L;
52
53 private RestartStrategy restart;
54 private IPhaseSelectionStrategy phaseSelectionStrategy;
55
56 private ICDCLLogger logger;
57
58 private boolean isInterrupted;
59
60 private boolean hasClickedOnRestart;
61 private boolean hasClickedOnClean;
62
63 private int conflictNumber;
64 private int nbClausesAtWhichWeShouldClean;
65
66 private boolean useTelecomStrategyAsLearnedConstraintsDeletionStrategy;
67
68 private ICDCL solver;
69
70 public RemoteControlStrategy(ICDCLLogger log) {
71 this.hasClickedOnClean = false;
72 this.hasClickedOnRestart = false;
73 this.restart = new NoRestarts();
74 this.phaseSelectionStrategy = new RSATPhaseSelectionStrategy();
75 this.logger = log;
76 this.isInterrupted = false;
77 this.useTelecomStrategyAsLearnedConstraintsDeletionStrategy = false;
78 }
79
80 public RemoteControlStrategy() {
81 this(null);
82 }
83
84 public boolean isHasClickedOnRestart() {
85 return this.hasClickedOnRestart;
86 }
87
88 public void setHasClickedOnRestart(boolean hasClickedOnRestart) {
89 this.hasClickedOnRestart = hasClickedOnRestart;
90 }
91
92 public boolean isHasClickedOnClean() {
93 return this.hasClickedOnClean;
94 }
95
96 public void setHasClickedOnClean(boolean hasClickedOnClean) {
97 this.hasClickedOnClean = hasClickedOnClean;
98 clickedOnClean();
99 }
100
101 public boolean isUseTelecomStrategyAsLearnedConstraintsDeletionStrategy() {
102 return this.useTelecomStrategyAsLearnedConstraintsDeletionStrategy;
103 }
104
105 public void setUseTelecomStrategyAsLearnedConstraintsDeletionStrategy(
106 boolean useTelecomStrategyAsLearnedConstraintsDeletionStrategy) {
107 this.useTelecomStrategyAsLearnedConstraintsDeletionStrategy = useTelecomStrategyAsLearnedConstraintsDeletionStrategy;
108 }
109
110 public void clickedOnClean() {
111 if (this.hasClickedOnClean) {
112 this.solver.setNeedToReduceDB(true);
113 this.hasClickedOnClean = false;
114 }
115 }
116
117 public RestartStrategy getRestartStrategy() {
118 return this.restart;
119 }
120
121 public IPhaseSelectionStrategy getPhaseSelectionStrategy() {
122 return this.phaseSelectionStrategy;
123 }
124
125 public void setPhaseSelectionStrategy(
126 IPhaseSelectionStrategy phaseSelectionStrategy) {
127 this.phaseSelectionStrategy = phaseSelectionStrategy;
128 }
129
130 public void setRestartStrategy(RestartStrategy restart) {
131 this.restart = restart;
132 }
133
134 public int getNbClausesAtWhichWeShouldClean() {
135 return this.nbClausesAtWhichWeShouldClean;
136 }
137
138 public void setNbClausesAtWhichWeShouldClean(
139 int nbClausesAtWhichWeShouldClean) {
140 this.nbClausesAtWhichWeShouldClean = nbClausesAtWhichWeShouldClean;
141 }
142
143 public ICDCLLogger getLogger() {
144 return this.logger;
145 }
146
147 public void setLogger(ICDCLLogger logger) {
148 this.logger = logger;
149 }
150
151 public void init(SearchParams params) {
152 this.restart.init(params);
153 }
154
155 public long nextRestartNumberOfConflict() {
156 return this.restart.nextRestartNumberOfConflict();
157 }
158
159 public boolean shouldRestart() {
160 if (this.hasClickedOnRestart) {
161 this.hasClickedOnRestart = false;
162 this.logger.log("Told the solver to restart");
163 return true;
164 }
165 return this.restart.shouldRestart();
166 }
167
168 public void onRestart() {
169
170 this.restart.onRestart();
171 }
172
173 public void onBackjumpToRootLevel() {
174 this.restart.onBackjumpToRootLevel();
175 }
176
177 public SearchParams getSearchParams() {
178 return this.restart.getSearchParams();
179 }
180
181 public ICDCL getSolver() {
182 return this.solver;
183 }
184
185 public void setSolver(ICDCL solver) {
186 this.solver = solver;
187 }
188
189 public void reset() {
190 this.restart.newConflict();
191 }
192
193 public void newConflict() {
194 this.restart.newConflict();
195 this.conflictNumber++;
196 if (this.useTelecomStrategyAsLearnedConstraintsDeletionStrategy) {
197 if (this.conflictNumber > this.nbClausesAtWhichWeShouldClean) {
198
199
200
201 this.conflictNumber = 0;
202 this.solver.setNeedToReduceDB(true);
203 }
204 }
205 }
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254 public void updateVar(int p) {
255 this.phaseSelectionStrategy.updateVar(p);
256 }
257
258 public void init(int nlength) {
259 this.phaseSelectionStrategy.init(nlength);
260 }
261
262 public void init(int var, int p) {
263 this.phaseSelectionStrategy.init(var, p);
264 }
265
266 public void assignLiteral(int p) {
267 while (this.isInterrupted) {
268 try {
269 Thread.sleep(1000);
270 } catch (InterruptedException e) {
271 e.printStackTrace();
272 }
273 }
274 this.phaseSelectionStrategy.assignLiteral(p);
275 }
276
277 public int select(int var) {
278 return this.phaseSelectionStrategy.select(var);
279 }
280
281 public void updateVarAtDecisionLevel(int q) {
282 this.phaseSelectionStrategy.updateVarAtDecisionLevel(q);
283 }
284
285 @Override
286 public String toString() {
287 return "RemoteControlStrategy [restartStrategy = " + this.restart
288 + ", learnedClausesDeletionStrategy = clean after "
289 + this.nbClausesAtWhichWeShouldClean
290 + " conflicts, phaseSelectionStrategy = "
291 + this.phaseSelectionStrategy + "]";
292 }
293
294 public void setInterrupted(boolean b) {
295 this.isInterrupted = b;
296 if (this.isInterrupted) {
297 this.logger.log("Solver paused");
298 } else {
299 this.logger.log("Resume solving");
300 }
301 }
302
303 public void newLearnedClause(Constr learned, int trailLevel) {
304 this.restart.newLearnedClause(learned, trailLevel);
305 }
306
307 }