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.tools;
31  
32  import org.sat4j.specs.IConstr;
33  import org.sat4j.specs.ISolverService;
34  import org.sat4j.specs.Lbool;
35  
36  public class SpeedTracing extends SearchListenerAdapter<ISolverService> {
37  
38      private static final long serialVersionUID = 1L;
39  
40      private final IVisualizationTool visuTool;
41      private final IVisualizationTool cleanVisuTool;
42      private final IVisualizationTool restartVisuTool;
43  
44      private long begin, end;
45      private int counter;
46      private long index;
47  
48      private double maxY;
49  
50      public SpeedTracing(IVisualizationTool visuTool,
51              IVisualizationTool cleanVisuTool, IVisualizationTool restartVisuTool) {
52          this.visuTool = visuTool;
53          this.cleanVisuTool = cleanVisuTool;
54          this.restartVisuTool = restartVisuTool;
55  
56          visuTool.init();
57          cleanVisuTool.init();
58          restartVisuTool.init();
59  
60          this.begin = System.currentTimeMillis();
61          this.counter = 0;
62          this.index = 0;
63          this.maxY = 0;
64      }
65  
66      @Override
67      public void propagating(int p, IConstr reason) {
68          this.end = System.currentTimeMillis();
69          double y;
70          if (this.end - this.begin >= 2000) {
71              long tmp = this.end - this.begin;
72              this.index += tmp;
73              y = this.counter / tmp * 1000;
74              if (y > this.maxY) {
75                  this.maxY = y;
76              }
77              this.visuTool.addPoint(this.index / 1000.0, y);
78              this.cleanVisuTool.addPoint(this.index / 1000.0, 0);
79              this.restartVisuTool.addPoint(this.index / 1000.0, 0);
80              this.begin = System.currentTimeMillis();
81              this.counter = 0;
82          }
83          this.counter++;
84      }
85  
86      @Override
87      public void end(Lbool result) {
88          this.visuTool.end();
89          this.cleanVisuTool.end();
90          this.restartVisuTool.end();
91      }
92  
93      @Override
94      public void cleaning() {
95          this.end = System.currentTimeMillis();
96          long indexClean = this.index + this.end - this.begin;
97          this.visuTool.addPoint(indexClean / 1000.0, this.counter
98                  / (this.end - this.begin) * 1000);
99          this.cleanVisuTool.addPoint(indexClean / 1000.0, this.maxY);
100         this.restartVisuTool.addInvisiblePoint(indexClean, 0);
101     }
102 
103     @Override
104     public void restarting() {
105         this.end = System.currentTimeMillis();
106         long indexRestart = this.index + this.end - this.begin;
107         double y = this.counter / (this.end - this.begin) * 1000;
108         this.visuTool.addPoint(indexRestart / 1000.0, y);
109         if (y > this.maxY) {
110             this.maxY = y;
111         }
112         this.restartVisuTool.addPoint(indexRestart / 1000.0, this.maxY);
113         this.cleanVisuTool.addInvisiblePoint(indexRestart, 0);
114     }
115 
116     @Override
117     public void start() {
118         this.visuTool.init();
119         this.cleanVisuTool.init();
120         this.restartVisuTool.init();
121 
122         this.begin = System.currentTimeMillis();
123         this.counter = 0;
124         this.index = 0;
125     }
126 }