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