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