source: branches/MetisMQI/src/main/java/weka/classifiers/ParallelMultipleClassifiersCombiner.java @ 38

Last change on this file since 38 was 29, checked in by gnappo, 14 years ago

Taggata versione per la demo e aggiunto branch.

File size: 7.8 KB
Line 
1/*
2 *    This program is free software; you can redistribute it and/or modify
3 *    it under the terms of the GNU General Public License as published by
4 *    the Free Software Foundation; either version 2 of the License, or
5 *    (at your option) any later version.
6 *
7 *    This program is distributed in the hope that it will be useful,
8 *    but WITHOUT ANY WARRANTY; without even the implied warranty of
9 *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
10 *    GNU General Public License for more details.
11 *
12 *    You should have received a copy of the GNU General Public License
13 *    along with this program; if not, write to the Free Software
14 *    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
15 */
16
17/*
18 *    ParallelMultipleClassifiersCombiner.java
19 *    Copyright (C) 2009 University of Waikato, Hamilton, New Zealand
20 *
21 */
22
23package weka.classifiers;
24
25import java.util.Enumeration;
26import java.util.Vector;
27import java.util.concurrent.LinkedBlockingQueue;
28import java.util.concurrent.ThreadPoolExecutor;
29import java.util.concurrent.TimeUnit;
30
31import weka.core.Instances;
32import weka.core.Option;
33import weka.core.Utils;
34
35/**
36 * Abstract utility class for handling settings common to
37 * meta classifiers that build an ensemble in parallel using multiple
38 * classifiers.
39 *
40 * @author Mark Hall (mhall{[at]}pentaho{[dot]}com)
41 * @version $Revision: 6041 $
42 */
43public abstract class ParallelMultipleClassifiersCombiner extends
44    MultipleClassifiersCombiner {
45
46  /** For serialization */
47  private static final long serialVersionUID = 728109028953726626L;
48
49  /** The number of threads to have executing at any one time */
50  protected int m_numExecutionSlots = 1;
51
52  /** Pool of threads to train models with */
53  protected transient ThreadPoolExecutor m_executorPool;
54
55  /** The number of classifiers completed so far */
56  protected int m_completed;
57
58  /**
59   * The number of classifiers that experienced a failure of some sort
60   * during construction
61   */
62  protected int m_failed;
63
64  /**
65   * Returns an enumeration describing the available options.
66   *
67   * @return an enumeration of all the available options.
68   */
69  public Enumeration listOptions() {
70
71    Vector newVector = new Vector(2);
72
73    newVector.addElement(new Option(
74              "\tNumber of execution slots.\n"
75              + "\t(default 1 - i.e. no parallelism)",
76              "num-slots", 1, "-num-slots <num>"));
77
78    Enumeration enu = super.listOptions();
79    while (enu.hasMoreElements()) {
80      newVector.addElement(enu.nextElement());
81    }
82    return newVector.elements();
83  }
84
85  /**
86   * Parses a given list of options. Valid options are:<p>
87   *
88   * -Z num <br>
89   * Set the number of execution slots to use (default 1 - i.e. no parallelism). <p>
90   *
91   * Options after -- are passed to the designated classifier.<p>
92   *
93   * @param options the list of options as an array of strings
94   * @exception Exception if an option is not supported
95   */
96  public void setOptions(String[] options) throws Exception {
97
98    String iterations = Utils.getOption("num-slots", options);
99    if (iterations.length() != 0) {
100      setNumExecutionSlots(Integer.parseInt(iterations));
101    } else {
102      setNumExecutionSlots(1);
103    }
104
105    super.setOptions(options);
106  }
107
108  /**
109   * Gets the current settings of the classifier.
110   *
111   * @return an array of strings suitable for passing to setOptions
112   */
113  public String [] getOptions() {
114
115    String [] superOptions = super.getOptions();
116    String [] options = new String [superOptions.length + 2];
117
118    int current = 0;
119    options[current++] = "-num-slots";
120    options[current++] = "" + getNumExecutionSlots();
121
122    System.arraycopy(superOptions, 0, options, current,
123                     superOptions.length);
124
125    return options;
126  }
127
128  /**
129   * Set the number of execution slots (threads) to use for building the
130   * members of the ensemble.
131   *
132   * @param numSlots the number of slots to use.
133   */
134  public void setNumExecutionSlots(int numSlots) {
135    m_numExecutionSlots = numSlots;
136  }
137
138  /**
139   * Get the number of execution slots (threads) to use for building
140   * the members of the ensemble.
141   *
142   * @return the number of slots to use
143   */
144  public int getNumExecutionSlots() {
145    return m_numExecutionSlots;
146  }
147
148  /**
149   * Returns the tip text for this property
150   * @return tip text for this property suitable for
151   * displaying in the explorer/experimenter gui
152   */
153  public String numExecutionSlotsTipText() {
154    return "The number of execution slots (threads) to use for " +
155      "constructing the ensemble.";
156  }
157
158  /**
159   * Stump method for building the classifiers
160   *
161   * @param data the training data to be used for generating the ensemble
162   * @exception Exception if the classifier could not be built successfully
163   */
164  public void buildClassifier(Instances data) throws Exception {
165
166    if (m_numExecutionSlots < 1) {
167      throw new Exception("Number of execution slots needs to be >= 1!");
168    }
169
170    if (m_numExecutionSlots > 1) {
171      if (m_Debug) {
172        System.out.println("Starting executor pool with " + m_numExecutionSlots
173            + " slots...");
174      }
175      startExecutorPool();
176    }
177    m_completed = 0;
178    m_failed = 0;
179  }
180
181  /**
182   * Start the pool of execution threads
183   */
184  protected void startExecutorPool() {
185    if (m_executorPool != null) {
186      m_executorPool.shutdownNow();
187    }
188
189    m_executorPool = new ThreadPoolExecutor(m_numExecutionSlots, m_numExecutionSlots,
190        120, TimeUnit.SECONDS, new LinkedBlockingQueue<Runnable>());
191  }
192
193  private synchronized void block(boolean tf) {
194    if (tf) {
195      try {
196        wait();
197      } catch (InterruptedException ex) {
198      }
199    } else {
200      notifyAll();
201    }
202  }
203
204  /**
205   * Does the actual construction of the ensemble
206   *
207   * @throws Exception if something goes wrong during the training
208   * process
209   */
210  protected synchronized void buildClassifiers(final Instances data) throws Exception {
211
212    for (int i = 0; i < m_Classifiers.length; i++) {
213      if (m_numExecutionSlots > 1) {
214        final Classifier currentClassifier = m_Classifiers[i];
215        final int iteration = i;
216        Runnable newTask = new Runnable() {
217          public void run() {
218            try {
219              if (m_Debug) {
220                System.out.println("Training classifier (" + (iteration +1) + ")");
221              }
222              currentClassifier.buildClassifier(data);
223              if (m_Debug) {
224                System.out.println("Finished classifier (" + (iteration +1) + ")");
225              }
226              completedClassifier(iteration, true);
227            } catch (Exception ex) {
228              ex.printStackTrace();
229              completedClassifier(iteration, false);
230            }
231          }
232        };
233
234        // launch this task
235        m_executorPool.execute(newTask);
236      } else {
237        m_Classifiers[i].buildClassifier(data);
238      }
239    }
240
241    if (m_numExecutionSlots > 1 && m_completed + m_failed < m_Classifiers.length) {
242      block(true);
243    }
244  }
245
246  /**
247   * Records the completion of the training of a single classifier. Unblocks if
248   * all classifiers have been trained.
249   *
250   * @param iteration the iteration that has completed
251   * @param success whether the classifier trained successfully
252   */
253  protected synchronized void completedClassifier(int iteration,
254      boolean success) {
255    m_completed++;
256
257    if (!success) {
258      m_failed++;
259      if (m_Debug) {
260        System.err.println("Iteration " + iteration + " failed!");
261      }
262    }
263
264    if (m_completed + m_failed == m_Classifiers.length) {
265      if (m_failed > 0) {
266        if (m_Debug) {
267          System.err.println("Problem building classifiers - some iterations failed.");
268        }
269      }
270
271      // have to shut the pool down or program executes as a server
272      // and when running from the command line does not return to the
273      // prompt
274      m_executorPool.shutdown();
275      block(false);
276    }
277  }
278}
Note: See TracBrowser for help on using the repository browser.