View Javadoc

1   /*******************************************************************************
2    * SAT4J: a SATisfiability library for Java Copyright (C) 2004, 2012 Artois University and CNRS
3    *
4    * All rights reserved. This program and the accompanying materials
5    * are made available under the terms of the Eclipse Public License v1.0
6    * which accompanies this distribution, and is available at
7    *  http://www.eclipse.org/legal/epl-v10.html
8    *
9    * Alternatively, the contents of this file may be used under the terms of
10   * either the GNU Lesser General Public License Version 2.1 or later (the
11   * "LGPL"), in which case the provisions of the LGPL are applicable instead
12   * of those above. If you wish to allow use of your version of this file only
13   * under the terms of the LGPL, and not to allow others to use your version of
14   * this file under the terms of the EPL, indicate your decision by deleting
15   * the provisions above and replace them with the notice and other provisions
16   * required by the LGPL. If you do not delete the provisions above, a recipient
17   * may use your version of this file under the terms of the EPL or the LGPL.
18   *
19   * Based on the original MiniSat specification from:
20   *
21   * An extensible SAT solver. Niklas Een and Niklas Sorensson. Proceedings of the
22   * Sixth International Conference on Theory and Applications of Satisfiability
23   * Testing, LNCS 2919, pp 502-518, 2003.
24   *
25   * See www.minisat.se for the original solver in C++.
26   *
27   * Contributors:
28   *   CRIL - initial API and implementation
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   * Strategy used by the solver when launched with the remote control.
44   * 
45   * @author sroussel
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         // logger.log("Has restarted");
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                 // logger.log("we should clean now because " + conflictNumber +
199                 // " > " + nbClausesAtWhichWeShouldClean);
200                 // hasClickedOnClean=true;
201                 this.conflictNumber = 0;
202                 this.solver.setNeedToReduceDB(true);
203             }
204         }
205     }
206 
207     // public void init() {
208     //
209     // }
210     //
211     // public ConflictTimer getTimer() {
212     // return this;
213     // }
214 
215     // public void reduce(IVec<Constr> learnts) {
216     // //System.out.println("je suis l� ??");
217     // //assert hasClickedOnClean;
218     //
219     // int i, j;
220     // for (i = j = 0; i < learnts.size() / 2; i++) {
221     // Constr c = learnts.get(i);
222     // if (c.locked() || c.size() == 2) {
223     // learnts.set(j++, learnts.get(i));
224     // } else {
225     // c.remove(getSolver());
226     // }
227     // }
228     // for (; i < learnts.size(); i++) {
229     // learnts.set(j++, learnts.get(i));
230     // }
231     // if (true) {
232     //			logger.log("cleaning " + (learnts.size() - j) //$NON-NLS-1$
233     //					+ " clauses out of " + learnts.size()); //$NON-NLS-1$ //$NON-NLS-2$
234     // }
235     // learnts.shrinkTo(j);
236     //
237     // hasClickedOnClean=false;
238     // }
239 
240     // public void onConflict(Constr outLearnt) {
241     // conflictNumber++;
242     // if(conflictNumber>nbClausesAtWhichWeShouldClean){
243     // //hasClickedOnClean=true;
244     // conflictNumber=0;
245     // solver.setNeedToReduceDB(true);
246     // }
247     // }
248     //
249     // public void onConflictAnalysis(Constr reason) {
250     // // TODO Auto-generated method stub
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 }