📄 randomsearch.java
字号:
/*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*/
/*
* RandomSearch.java
* Copyright (C) 1999 Mark Hall
*
*/
package weka.attributeSelection;
import java.util.BitSet;
import java.util.Enumeration;
import java.util.Random;
import java.util.Vector;
import weka.core.Instances;
import weka.core.Option;
import weka.core.OptionHandler;
import weka.core.Range;
import weka.core.Utils;
/**
* Class for performing a random search. <p>
*
* Valid options are: <p>
*
* -P <start set> <br>
* Specify a starting set of attributes. Eg 1,4,7-9. <p>
*
* -F <percent) <br>
* Percentage of the search space to consider. (default = 25). <p>
*
* -V <br>
* Verbose output. Output new best subsets as the search progresses. <p>
*
* @author Mark Hall (mhall@cs.waikato.ac.nz)
* @version $Revision$
*/
public class RandomSearch extends ASSearch
implements StartSetHandler, OptionHandler {
/**
* holds a starting set as an array of attributes.
*/
private int[] m_starting;
/** holds the start set as a range */
private Range m_startRange;
/** the best feature set found during the search */
private BitSet m_bestGroup;
/** the merit of the best subset found */
private double m_bestMerit;
/**
* only accept a feature set as being "better" than the best if its
* merit is better or equal to the best, and it contains fewer
* features than the best (this allows LVF to be implimented).
*/
private boolean m_onlyConsiderBetterAndSmaller;
/** does the data have a class */
private boolean m_hasClass;
/** holds the class index */
private int m_classIndex;
/** number of attributes in the data */
private int m_numAttribs;
/** seed for random number generation */
private int m_seed;
/** percentage of the search space to consider */
private double m_searchSize;
/** the number of iterations performed */
private int m_iterations;
/** random number object */
private Random m_random;
/** output new best subsets as the search progresses */
private boolean m_verbose;
/**
* Returns a string describing this search method
* @return a description of the search suitable for
* displaying in the explorer/experimenter gui
*/
public String globalInfo() {
return "RandomSearch : \n\nPerforms a Random search in "
+"the space of attribute subsets. If no start set is supplied, Random "
+"search starts from a random point and reports the best subset found. "
+"If a start set is supplied, Random searches randomly for subsets "
+"that are as good or better than the start point with the same or "
+"or fewer attributes. Using RandomSearch in conjunction with a start "
+"set containing all attributes equates to the LVF algorithm of Liu "
+"and Setiono (ICML-96).\n";
}
/**
* Constructor
*/
public RandomSearch () {
resetOptions();
}
/**
* Returns an enumeration describing the available options.
* @return an enumeration of all the available options.
**/
public Enumeration listOptions () {
Vector newVector = new Vector(3);
newVector.addElement(new Option("\tSpecify a starting set of attributes."
+ "\n\tEg. 1,3,5-7."
+"\n\tIf a start point is supplied,"
+"\n\trandom search evaluates the start"
+"\n\tpoint and then randomly looks for"
+"\n\tsubsets that are as good as or better"
+"\n\tthan the start point with the same"
+"\n\tor lower cardinality."
,"P",1
, "-P <start set>"));
newVector.addElement(new Option("\tPercent of search space to consider."
+"\n\t(default = 25%)."
, "F", 1
, "-F <percent> "));
newVector.addElement(new Option("\tOutput subsets as the search progresses."
+"\n\t(default = false)."
, "V", 0
, "-V"));
return newVector.elements();
}
/**
* Parses a given list of options.
*
* Valid options are: <p>
*
* -P <start set> <br>
* Specify a starting set of attributes. Eg 1,4,7-9. <p>
*
* -F <percent) <br>
* Percentage of the search space to consider. (default = 25). <p>
*
* -V <br>
* Verbose output. Output new best subsets as the search progresses. <p>
*
* @param options the list of options as an array of strings
* @exception Exception if an option is not supported
*
**/
public void setOptions (String[] options)
throws Exception {
String optionString;
resetOptions();
optionString = Utils.getOption('P', options);
if (optionString.length() != 0) {
setStartSet(optionString);
}
optionString = Utils.getOption('F',options);
if (optionString.length() != 0) {
setSearchPercent((new Double(optionString)).doubleValue());
}
setVerbose(Utils.getFlag('V',options));
}
/**
* Returns the tip text for this property
* @return tip text for this property suitable for
* displaying in the explorer/experimenter gui
*/
public String startSetTipText() {
return "Set the start point for the search. This is specified as a comma "
+"seperated list off attribute indexes starting at 1. It can include "
+"ranges. Eg. 1,2,5-9,17. If specified, Random searches for subsets "
+"of attributes that are as good as or better than the start set with "
+"the same or lower cardinality.";
}
/**
* Sets a starting set of attributes for the search. It is the
* search method's responsibility to report this start set (if any)
* in its toString() method.
* @param startSet a string containing a list of attributes (and or ranges),
* eg. 1,2,6,10-15. "" indicates no start point.
* If a start point is supplied, random search evaluates the
* start point and then looks for subsets that are as good as or better
* than the start point with the same or lower cardinality.
* @exception Exception if start set can't be set.
*/
public void setStartSet (String startSet) throws Exception {
m_startRange.setRanges(startSet);
}
/**
* Returns a list of attributes (and or attribute ranges) as a String
* @return a list of attributes (and or attribute ranges)
*/
public String getStartSet () {
return m_startRange.getRanges();
}
/**
* Returns the tip text for this property
* @return tip text for this property suitable for
* displaying in the explorer/experimenter gui
*/
public String verboseTipText() {
return "Print progress information. Sends progress info to the terminal "
+"as the search progresses.";
}
/**
* set whether or not to output new best subsets as the search proceeds
* @param v true if output is to be verbose
*/
public void setVerbose(boolean v) {
m_verbose = v;
}
/**
* get whether or not output is verbose
* @return true if output is set to verbose
*/
public boolean getVerbose() {
return m_verbose;
}
/**
* Returns the tip text for this property
* @return tip text for this property suitable for
* displaying in the explorer/experimenter gui
*/
public String searchPercentTipText() {
return "Percentage of the search space to explore.";
}
/**
* set the percentage of the search space to consider
* @param p percent of the search space ( 0 < p <= 100)
*/
public void setSearchPercent(double p) {
p = Math.abs(p);
if (p == 0) {
p = 25;
}
if (p > 100.0) {
p = 100;
}
m_searchSize = (p/100.0);
}
/**
* get the percentage of the search space to consider
* @return the percent of the search space explored
*/
public double getSearchPercent() {
return m_searchSize;
}
/**
* Gets the current settings of RandomSearch.
* @return an array of strings suitable for passing to setOptions()
*/
public String[] getOptions () {
String[] options = new String[5];
int current = 0;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -