source: tags/MetisMQIDemo/src/main/java/weka/classifiers/bayes/net/search/local/TabuSearch.java

Last change on this file was 29, checked in by gnappo, 15 years ago

Taggata versione per la demo e aggiunto branch.

File size: 12.0 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 * TabuSearch.java
19 * Copyright (C) 2004 University of Waikato, Hamilton, New Zealand
20 *
21 */
22 
23package weka.classifiers.bayes.net.search.local;
24
25import weka.classifiers.bayes.BayesNet;
26import weka.core.Instances;
27import weka.core.Option;
28import weka.core.RevisionUtils;
29import weka.core.TechnicalInformation;
30import weka.core.TechnicalInformation.Type;
31import weka.core.TechnicalInformation.Field;
32import weka.core.TechnicalInformationHandler;
33import weka.core.Utils;
34
35import java.util.Enumeration;
36import java.util.Vector;
37
38/**
39 <!-- globalinfo-start -->
40 * This Bayes Network learning algorithm uses tabu search for finding a well scoring Bayes network structure. Tabu search is hill climbing till an optimum is reached. The following step is the least worst possible step. The last X steps are kept in a list and none of the steps in this so called tabu list is considered in taking the next step. The best network found in this traversal is returned.<br/>
41 * <br/>
42 * For more information see:<br/>
43 * <br/>
44 * R.R. Bouckaert (1995). Bayesian Belief Networks: from Construction to Inference. Utrecht, Netherlands.
45 * <p/>
46 <!-- globalinfo-end -->
47 *
48 <!-- technical-bibtex-start -->
49 * BibTeX:
50 * <pre>
51 * &#64;phdthesis{Bouckaert1995,
52 *    address = {Utrecht, Netherlands},
53 *    author = {R.R. Bouckaert},
54 *    institution = {University of Utrecht},
55 *    title = {Bayesian Belief Networks: from Construction to Inference},
56 *    year = {1995}
57 * }
58 * </pre>
59 * <p/>
60 <!-- technical-bibtex-end -->
61 *
62 <!-- options-start -->
63 * Valid options are: <p/>
64 *
65 * <pre> -L &lt;integer&gt;
66 *  Tabu list length</pre>
67 *
68 * <pre> -U &lt;integer&gt;
69 *  Number of runs</pre>
70 *
71 * <pre> -P &lt;nr of parents&gt;
72 *  Maximum number of parents</pre>
73 *
74 * <pre> -R
75 *  Use arc reversal operation.
76 *  (default false)</pre>
77 *
78 * <pre> -P &lt;nr of parents&gt;
79 *  Maximum number of parents</pre>
80 *
81 * <pre> -R
82 *  Use arc reversal operation.
83 *  (default false)</pre>
84 *
85 * <pre> -N
86 *  Initial structure is empty (instead of Naive Bayes)</pre>
87 *
88 * <pre> -mbc
89 *  Applies a Markov Blanket correction to the network structure,
90 *  after a network structure is learned. This ensures that all
91 *  nodes in the network are part of the Markov blanket of the
92 *  classifier node.</pre>
93 *
94 * <pre> -S [BAYES|MDL|ENTROPY|AIC|CROSS_CLASSIC|CROSS_BAYES]
95 *  Score type (BAYES, BDeu, MDL, ENTROPY and AIC)</pre>
96 *
97 <!-- options-end -->
98 *
99 * @author Remco Bouckaert (rrb@xm.co.nz)
100 * @version $Revision: 1.5 $
101 */
102public class TabuSearch 
103    extends HillClimber
104    implements TechnicalInformationHandler {
105 
106    /** for serialization */
107    static final long serialVersionUID = 1457344073228786447L;
108
109    /** number of runs **/
110    int m_nRuns = 10;
111               
112        /** size of tabu list **/
113        int m_nTabuList = 5;
114
115        /** the actual tabu list **/
116        Operation[] m_oTabuList = null;
117
118        /**
119         * Returns an instance of a TechnicalInformation object, containing
120         * detailed information about the technical background of this class,
121         * e.g., paper reference or book this class is based on.
122         *
123         * @return the technical information about this class
124         */
125        public TechnicalInformation getTechnicalInformation() {
126          TechnicalInformation  result;
127         
128          result = new TechnicalInformation(Type.PHDTHESIS);
129          result.setValue(Field.AUTHOR, "R.R. Bouckaert");
130          result.setValue(Field.YEAR, "1995");
131          result.setValue(Field.TITLE, "Bayesian Belief Networks: from Construction to Inference");
132          result.setValue(Field.INSTITUTION, "University of Utrecht");
133          result.setValue(Field.ADDRESS, "Utrecht, Netherlands");
134         
135          return result;
136        }
137
138        /**
139         * search determines the network structure/graph of the network
140         * with the Tabu search algorithm.
141         *
142         * @param bayesNet the network
143         * @param instances the data to use
144         * @throws Exception if something goes wrong
145         */
146        protected void search(BayesNet bayesNet, Instances instances) throws Exception {
147        m_oTabuList = new Operation[m_nTabuList];
148        int iCurrentTabuList = 0;
149        initCache(bayesNet, instances);
150
151                // keeps track of score pf best structure found so far
152                double fBestScore;     
153                double fCurrentScore = 0.0;
154                for (int iAttribute = 0; iAttribute < instances.numAttributes(); iAttribute++) {
155                        fCurrentScore += calcNodeScore(iAttribute);
156                }
157
158                // keeps track of best structure found so far
159                BayesNet bestBayesNet;
160
161                // initialize bestBayesNet
162                fBestScore = fCurrentScore;
163                bestBayesNet = new BayesNet();
164                bestBayesNet.m_Instances = instances;
165                bestBayesNet.initStructure();
166                copyParentSets(bestBayesNet, bayesNet);
167               
168               
169        // go do the search       
170        for (int iRun = 0; iRun < m_nRuns; iRun++) {
171            Operation oOperation = getOptimalOperation(bayesNet, instances);
172                        performOperation(bayesNet, instances, oOperation);
173            // sanity check
174            if (oOperation  == null) {
175                                throw new Exception("Panic: could not find any step to make. Tabu list too long?");
176            }
177            // update tabu list
178            m_oTabuList[iCurrentTabuList] = oOperation;
179            iCurrentTabuList = (iCurrentTabuList + 1) % m_nTabuList;
180
181                        fCurrentScore += oOperation.m_fDeltaScore;
182                        // keep track of best network seen so far
183                        if (fCurrentScore > fBestScore) {
184                                fBestScore = fCurrentScore;
185                                copyParentSets(bestBayesNet, bayesNet);
186                        }
187
188                        if (bayesNet.getDebug()) {
189                                printTabuList();
190                        }
191        }
192       
193        // restore current network to best network
194                copyParentSets(bayesNet, bestBayesNet);
195               
196                // free up memory
197                bestBayesNet = null;
198                m_Cache = null;
199    } // search
200
201
202        /**
203         * copyParentSets copies parent sets of source to dest BayesNet
204         *
205         * @param dest destination network
206         * @param source source network
207         */
208        void copyParentSets(BayesNet dest, BayesNet source) {
209                int nNodes = source.getNrOfNodes();
210                // clear parent set first
211                for (int iNode = 0; iNode < nNodes; iNode++) {
212                        dest.getParentSet(iNode).copy(source.getParentSet(iNode));
213                }               
214        } // CopyParentSets
215
216        /**
217         * check whether the operation is not in the tabu list
218         *
219         * @param oOperation operation to be checked
220         * @return true if operation is not in the tabu list
221         */
222        boolean isNotTabu(Operation oOperation) {
223                for (int iTabu = 0; iTabu < m_nTabuList; iTabu++) {
224                        if (oOperation.equals(m_oTabuList[iTabu])) {
225                                        return false;
226                                }
227                }
228                return true;
229        } // isNotTabu
230
231        /** print tabu list for debugging purposes.
232         */
233        void printTabuList() {
234                for (int i = 0; i < m_nTabuList; i++) {
235                        Operation o = m_oTabuList[i];
236                        if (o != null) {
237                                if (o.m_nOperation == 0) {System.out.print(" +(");} else {System.out.print(" -(");}
238                                System.out.print(o.m_nTail + "->" + o.m_nHead + ")");
239                        }
240                }
241                System.out.println();
242        } // printTabuList
243
244    /**
245    * @return number of runs
246    */
247    public int getRuns() {
248        return m_nRuns;
249    } // getRuns
250
251    /**
252     * Sets the number of runs
253     * @param nRuns The number of runs to set
254     */
255    public void setRuns(int nRuns) {
256        m_nRuns = nRuns;
257    } // setRuns
258
259    /**
260     * @return the Tabu List length
261     */
262    public int getTabuList() {
263        return m_nTabuList;
264    } // getTabuList
265
266    /**
267     * Sets the Tabu List length.
268     * @param nTabuList The nTabuList to set
269     */
270    public void setTabuList(int nTabuList) {
271        m_nTabuList = nTabuList;
272    } // setTabuList
273
274        /**
275         * Returns an enumeration describing the available options.
276         *
277         * @return an enumeration of all the available options.
278         */
279        public Enumeration listOptions() {
280                Vector newVector = new Vector(4);
281
282                newVector.addElement(new Option("\tTabu list length", "L", 1, "-L <integer>"));
283                newVector.addElement(new Option("\tNumber of runs", "U", 1, "-U <integer>"));
284                newVector.addElement(new Option("\tMaximum number of parents", "P", 1, "-P <nr of parents>"));
285                newVector.addElement(new Option("\tUse arc reversal operation.\n\t(default false)", "R", 0, "-R"));
286
287                Enumeration enu = super.listOptions();
288                while (enu.hasMoreElements()) {
289                        newVector.addElement(enu.nextElement());
290                }
291                return newVector.elements();
292        } // listOptions
293
294        /**
295         * Parses a given list of options. <p/>
296         *
297         <!-- options-start -->
298         * Valid options are: <p/>
299         *
300         * <pre> -L &lt;integer&gt;
301         *  Tabu list length</pre>
302         *
303         * <pre> -U &lt;integer&gt;
304         *  Number of runs</pre>
305         *
306         * <pre> -P &lt;nr of parents&gt;
307         *  Maximum number of parents</pre>
308         *
309         * <pre> -R
310         *  Use arc reversal operation.
311         *  (default false)</pre>
312         *
313         * <pre> -P &lt;nr of parents&gt;
314         *  Maximum number of parents</pre>
315         *
316         * <pre> -R
317         *  Use arc reversal operation.
318         *  (default false)</pre>
319         *
320         * <pre> -N
321         *  Initial structure is empty (instead of Naive Bayes)</pre>
322         *
323         * <pre> -mbc
324         *  Applies a Markov Blanket correction to the network structure,
325         *  after a network structure is learned. This ensures that all
326         *  nodes in the network are part of the Markov blanket of the
327         *  classifier node.</pre>
328         *
329         * <pre> -S [BAYES|MDL|ENTROPY|AIC|CROSS_CLASSIC|CROSS_BAYES]
330         *  Score type (BAYES, BDeu, MDL, ENTROPY and AIC)</pre>
331         *
332         <!-- options-end -->
333         *
334         * @param options the list of options as an array of strings
335         * @throws Exception if an option is not supported
336         */
337        public void setOptions(String[] options) throws Exception {
338                String sTabuList = Utils.getOption('L', options);
339                if (sTabuList.length() != 0) {
340                        setTabuList(Integer.parseInt(sTabuList));
341                }
342                String sRuns = Utils.getOption('U', options);
343                if (sRuns.length() != 0) {
344                        setRuns(Integer.parseInt(sRuns));
345                }
346               
347                super.setOptions(options);
348        } // setOptions
349
350        /**
351         * Gets the current settings of the search algorithm.
352         *
353         * @return an array of strings suitable for passing to setOptions
354         */
355        public String[] getOptions() {
356                String[] superOptions = super.getOptions();
357                String[] options = new String[7 + superOptions.length];
358                int current = 0;
359               
360                options[current++] = "-L";
361                options[current++] = "" + getTabuList();
362
363                options[current++] = "-U";
364                options[current++] = "" + getRuns();
365
366                // insert options from parent class
367                for (int iOption = 0; iOption < superOptions.length; iOption++) {
368                        options[current++] = superOptions[iOption];
369                }
370
371                // Fill up rest with empty strings, not nulls!
372                while (current < options.length) {
373                        options[current++] = "";
374                }
375                return options;
376        } // getOptions
377
378        /**
379         * This will return a string describing the classifier.
380         * @return The string.
381         */
382        public String globalInfo() {
383                return "This Bayes Network learning algorithm uses tabu search for finding a well scoring " +
384                "Bayes network structure. Tabu search is hill climbing till an optimum is reached. The " +
385                "following step is the least worst possible step. The last X steps are kept in a list and " +
386                "none of the steps in this so called tabu list is considered in taking the next step. " +
387                "The best network found in this traversal is returned.\n\n"
388                + "For more information see:\n\n"
389                + getTechnicalInformation().toString();
390        } // globalInfo
391       
392        /**
393         * @return a string to describe the Runs option.
394         */
395        public String runsTipText() {
396          return "Sets the number of steps to be performed.";
397        } // runsTipText
398
399        /**
400         * @return a string to describe the TabuList option.
401         */
402        public String tabuListTipText() {
403          return "Sets the length of the tabu list.";
404        } // tabuListTipText
405
406        /**
407         * Returns the revision string.
408         *
409         * @return              the revision
410         */
411        public String getRevision() {
412          return RevisionUtils.extract("$Revision: 1.5 $");
413        }
414
415} // TabuSearch
Note: See TracBrowser for help on using the repository browser.