📄 bestfirst.java
字号:
/**
*
* AgentAcademy - an open source Data Mining framework for
* training intelligent agents
*
* Copyright (C) 2001-2003 AA Consortium.
*
* This library is open source software; you can redistribute it
* and/or modify it under the terms of the GNU Lesser General
* Public License as published by the Free Software Foundation;
* either version 2.0 of the License, or (at your option) any later
* version.
*
* This library 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 Lesser General Public
* License along with this library; if not, write to the Free
* Software Foundation, Inc., 59 Temple Place, Suite 330, Boston,
* MA 02111-1307 USA
*
*/
package org.agentacademy.modules.dataminer.attributeSelection;
import java.util.BitSet;
import java.util.Enumeration;
import java.util.Hashtable;
import java.util.Vector;
import org.agentacademy.modules.dataminer.core.FastVector;
import org.agentacademy.modules.dataminer.core.Instances;
import org.agentacademy.modules.dataminer.core.Option;
import org.agentacademy.modules.dataminer.core.OptionHandler;
import org.agentacademy.modules.dataminer.core.Range;
import org.agentacademy.modules.dataminer.core.SelectedTag;
import org.agentacademy.modules.dataminer.core.Tag;
import org.agentacademy.modules.dataminer.core.Utils;
import org.apache.log4j.Logger;
/**
* Class for performing a best first search. <p>
*
* Valid options are: <p>
*
* -P <start set> <br>
* Specify a starting set of attributes. Eg 1,4,7-9. <p>
*
* -D <-1 = backward | 0 = bidirectional | 1 = forward> <br>
* Direction of the search. (default = 1). <p>
*
* -N <num> <br>
* Number of non improving nodes to consider before terminating search.
* (default = 5). <p>
*
* @author Mark Hall (mhall@cs.waikato.ac.nz)
* @version $Revision: 1.3 $
*/
public class BestFirst extends ASSearch
implements OptionHandler, StartSetHandler
{
public static Logger log = Logger.getLogger(BestFirst.class);
// Inner classes
/**
* Class for a node in a linked list. Used in best first search.
* @author Mark Hall (mhall@cs.waikato.ac.nz)
**/
public class Link2 {
BitSet group;
double merit;
// Constructor
public Link2 (BitSet gr, double mer) {
group = (BitSet)gr.clone();
merit = mer;
}
/** Get a group */
public BitSet getGroup () {
return group;
}
public String toString () {
return ("Node: " + group.toString() + " " + merit);
}
}
/**
* Class for handling a linked list. Used in best first search.
* Extends the Vector class.
* @author Mark Hall (mhall@cs.waikato.ac.nz)
**/
public class LinkedList2
extends FastVector
{
// Max number of elements in the list
int m_MaxSize;
// ================
// Public methods
// ================
public LinkedList2 (int sz) {
super();
m_MaxSize = sz;
}
/**
* removes an element (Link) at a specific index from the list.
* @param index the index of the element to be removed.
**/
public void removeLinkAt (int index)
throws Exception
{
if ((index >= 0) && (index < size())) {
removeElementAt(index);
}
else {
throw new Exception("index out of range (removeLinkAt)");
}
}
/**
* returns the element (Link) at a specific index from the list.
* @param index the index of the element to be returned.
**/
public Link2 getLinkAt (int index)
throws Exception
{
if (size() == 0) {
throw new Exception("List is empty (getLinkAt)");
}
else {if ((index >= 0) && (index < size())) {
return ((Link2)(elementAt(index)));
}
else {
throw new Exception("index out of range (getLinkAt)");
}
}
}
/**
* adds an element (Link) to the list.
* @param gr the attribute set specification
* @param mer the "merit" of this attribute set
**/
public void addToList (BitSet gr, double mer)
throws Exception
{
Link2 newL = new Link2(gr, mer);
if (size() == 0) {
addElement(newL);
}
else {if (mer > ((Link2)(firstElement())).merit) {
if (size() == m_MaxSize) {
removeLinkAt(m_MaxSize - 1);
}
//----------
insertElementAt(newL, 0);
}
else {
int i = 0;
int size = size();
boolean done = false;
//------------
// don't insert if list contains max elements an this
// is worst than the last
if ((size == m_MaxSize) && (mer <= ((Link2)(lastElement())).merit)) {
}
//---------------
else {
while ((!done) && (i < size)) {
if (mer > ((Link2)(elementAt(i))).merit) {
if (size == m_MaxSize) {
removeLinkAt(m_MaxSize - 1);
}
// ---------------------
insertElementAt(newL, i);
done = true;
}
else {if (i == size - 1) {
addElement(newL);
done = true;
}
else {
i++;
}
}
}
}
}
}
}
}
// member variables
/** maximum number of stale nodes before terminating search */
private int m_maxStale;
/** 0 == backward search, 1 == forward search, 2 == bidirectional */
private int m_searchDirection;
/** search directions */
private static final int SELECTION_BACKWARD = 0;
private static final int SELECTION_FORWARD = 1;
private static final int SELECTION_BIDIRECTIONAL = 2;
public static final Tag [] TAGS_SELECTION = {
new Tag(SELECTION_BACKWARD, "Backward"),
new Tag(SELECTION_FORWARD, "Forward"),
new Tag(SELECTION_BIDIRECTIONAL, "Bi-directional"),
};
/** holds an array of starting attributes */
private int[] m_starting;
/** holds the start set for the search as a Range */
private Range m_startRange;
/** 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;
/** total number of subsets evaluated during a search */
private int m_totalEvals;
/** for debugging */
private boolean m_debug;
/** holds the merit of the best subset found */
private double m_bestMerit;
/**
* Returns a string describing this search method
* @return a description of the search method suitable for
* displaying in the explorer/experimenter gui
*/
public String globalInfo() {
return "BestFirst:\n\n"
+"Searches the space of attribute subsets by greedy hillclimbing "
+"augmented with a backtracking facility. Setting the number of "
+"consecutive non-improving nodes allowed controls the level of "
+"backtracking done. Best first may start with the empty set of "
+"attributes and search forward, or start with the full set of "
+"attributes and search backward, or start at any point and search "
+"in both directions (by considering all possible single attribute "
+"additions and deletions at a given point).\n";
}
/**
*Constructor
*/
public BestFirst () {
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."
,"P",1
, "-P <start set>"));
newVector.addElement(new Option("\tDirection of search. (default = 1)."
, "D", 1
, "-D <0 = backward | 1 = forward "
+ "| 2 = bi-directional>"));
newVector.addElement(new Option("\tNumber of non-improving nodes to"
+ "\n\tconsider before terminating search."
, "N", 1, "-N <num>"));
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>
*
* -D <-1 = backward | 0 = bidirectional | 1 = forward> <br>
* Direction of the search. (default = 1). <p>
*
* -N <num> <br>
* Number of non improving nodes to consider before terminating search.
* (default = 5). <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('D', options);
if (optionString.length() != 0) {
setDirection(new SelectedTag(Integer.parseInt(optionString),
TAGS_SELECTION));
} else {
setDirection(new SelectedTag(SELECTION_FORWARD, TAGS_SELECTION));
}
optionString = Utils.getOption('N', options);
if (optionString.length() != 0) {
setSearchTermination(Integer.parseInt(optionString));
}
m_debug = Utils.getFlag('Z', 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.";
}
/**
* 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.
* @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 searchTerminationTipText() {
return "Set the amount of backtracking. Specify the number of ";
}
/**
* Set the numnber of non-improving nodes to consider before terminating
* search.
*
* @param t the number of non-improving nodes
* @exception Exception if t is less than 1
*/
public void setSearchTermination (int t)
throws Exception
{
if (t < 1) {
throw new Exception("Value of -N must be > 0.");
}
m_maxStale = t;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -