📄 tertius.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. *//* * Tertius.java * Copyright (C) 2003 Peter A. Flach, Nicolas Lachiche * * Thanks to Amelie Deltour for porting the original C code to Java * and integrating it into Weka. */package weka.associations;import weka.associations.tertius.AttributeValueLiteral;import weka.associations.tertius.IndividualInstances;import weka.associations.tertius.IndividualLiteral;import weka.associations.tertius.Literal;import weka.associations.tertius.Predicate;import weka.associations.tertius.Rule;import weka.associations.tertius.SimpleLinkedList;import weka.core.Attribute;import weka.core.Capabilities;import weka.core.Instances;import weka.core.Option;import weka.core.OptionHandler;import weka.core.SelectedTag;import weka.core.Tag;import weka.core.TechnicalInformation;import weka.core.Capabilities.Capability;import weka.core.TechnicalInformation.Type;import weka.core.TechnicalInformation.Field;import weka.core.TechnicalInformationHandler;import weka.core.Utils;import java.awt.BorderLayout;import java.awt.Button;import java.awt.Font;import java.awt.Frame;import java.awt.Label;import java.awt.TextField;import java.awt.event.ActionEvent;import java.awt.event.ActionListener;import java.io.BufferedReader;import java.io.File;import java.io.FileReader;import java.io.Reader;import java.text.SimpleDateFormat;import java.util.ArrayList;import java.util.Date;import java.util.Enumeration;import java.util.Vector;/** <!-- globalinfo-start --> * Finds rules according to confirmation measure (Tertius-type algorithm).<br/> * <br/> * For more information see:<br/> * <br/> * P. A. Flach, N. Lachiche (1999). Confirmation-Guided Discovery of first-order rules with Tertius. Machine Learning. 42:61-95. * <p/> <!-- globalinfo-end --> * <!-- technical-bibtex-start --> * BibTeX: * <pre> * @article{Flach1999, * author = {P. A. Flach and N. Lachiche}, * journal = {Machine Learning}, * pages = {61-95}, * title = {Confirmation-Guided Discovery of first-order rules with Tertius}, * volume = {42}, * year = {1999} * } * </pre> * <p/> <!-- technical-bibtex-end --> * <!-- options-start --> * Valid options are: <p/> * * <pre> -K <number of values in result> * Set maximum number of confirmation values in the result. (default: 10)</pre> * * <pre> -F <frequency threshold> * Set frequency threshold for pruning. (default: 0)</pre> * * <pre> -C <confirmation threshold> * Set confirmation threshold. (default: 0)</pre> * * <pre> -N <noise threshold> * Set noise threshold : maximum frequency of counter-examples. * 0 gives only satisfied rules. (default: 1)</pre> * * <pre> -R * Allow attributes to be repeated in a same rule.</pre> * * <pre> -L <number of literals> * Set maximum number of literals in a rule. (default: 4)</pre> * * <pre> -G <0=no negation | 1=body | 2=head | 3=body and head> * Set the negations in the rule. (default: 0)</pre> * * <pre> -S * Consider only classification rules.</pre> * * <pre> -c <class index> * Set index of class attribute. (default: last).</pre> * * <pre> -H * Consider only horn clauses.</pre> * * <pre> -E * Keep equivalent rules.</pre> * * <pre> -M * Keep same clauses.</pre> * * <pre> -T * Keep subsumed rules.</pre> * * <pre> -I <0=always match | 1=never match | 2=significant> * Set the way to handle missing values. (default: 0)</pre> * * <pre> -O * Use ROC analysis. </pre> * * <pre> -p <name of file> * Set the file containing the parts of the individual for individual-based learning.</pre> * * <pre> -P <0=no output | 1=on stdout | 2=in separate window> * Set output of current values. (default: 0)</pre> * <!-- options-end --> * * @author <a href="mailto:adeltour@netcourrier.com">Amelie Deltour</a> * @version $Revision: 1.6 $ */public class Tertius extends Associator implements OptionHandler, Runnable, TechnicalInformationHandler { /** for serialization */ static final long serialVersionUID = 5556726848380738179L; /** The results. */ private SimpleLinkedList m_results; /** Number of hypotheses considered. */ private int m_hypotheses; /** Number of hypotheses explored. */ private int m_explored; /** Time needed for the search. */ private Date m_time; /** Field to output the current values. */ private TextField m_valuesText; /** Instances used for the search. */ private Instances m_instances; /** Predicates used in the rules. */ private ArrayList m_predicates; /** Status of the search. */ private int m_status; /** Status of the search: normal */ private static final int NORMAL = 0; /** Status of the search: memory problem */ private static final int MEMORY = 1; /** Status of the search: user interruption */ private static final int STOP = 2; /* Pruning options. */ /** Number of best confirmation values to search. */ private int m_best; /** Frequency threshold for the body and the negation of the head. */ private double m_frequencyThreshold; /** Confirmation threshold for the rules. */ private double m_confirmationThreshold; /** Maximal number of counter-instances. */ private double m_noiseThreshold; /* Search space & language bias options. */ /** Repeat attributes ? */ private boolean m_repeat; /** Number of literals in a rule. */ private int m_numLiterals; /** Type of negation: none */ private static final int NONE = 0; /** Type of negation: body */ private static final int BODY = 1; /** Type of negation: head */ private static final int HEAD = 2; /** Type of negation: all */ private static final int ALL = 3; /** Types of negation. */ private static final Tag [] TAGS_NEGATION = { new Tag(NONE, "None"), new Tag(BODY, "Body"), new Tag(HEAD, "Head"), new Tag(ALL, "Both") }; /** Type of negation used in the rules. */ private int m_negation; /** Classification bias. */ private boolean m_classification; /** Index of class attribute. */ private int m_classIndex; /** Horn clauses bias. */ private boolean m_horn; /* Subsumption tests options. */ /** Perform test on equivalent rules ? */ private boolean m_equivalent; /** Perform test on same clauses ? */ private boolean m_sameClause; /** Perform subsumption test ? */ private boolean m_subsumption; /** Way of handling missing values: min counterinstances */ public static final int EXPLICIT = 0; /** Way of handling missing values: max counterinstances */ public static final int IMPLICIT = 1; /** Way of handling missing values: missing as a particular value */ public static final int SIGNIFICANT = 2; /** Ways of handling missing values. */ private static final Tag [] TAGS_MISSING = { new Tag(EXPLICIT, "Matches all"), new Tag(IMPLICIT, "Matches none"), new Tag(SIGNIFICANT, "Significant") }; /** Way of handling missing values in the search. */ private int m_missing; /** Perform ROC analysis ? */ private boolean m_roc; /** Name of the file containing the parts for individual-based learning. */ private String m_partsString; /** Part instances for individual-based learning. */ private Instances m_parts; /** Type of values output: No */ private static final int NO = 0; /** Type of values output: stdout */ private static final int OUT = 1; /** Type of values output: Window */ private static final int WINDOW = 2; /** Types of values output. */ private static final Tag [] TAGS_VALUES = { new Tag(NO, "No"), new Tag(OUT, "stdout"), new Tag(WINDOW, "Window") }; /** Type of values output. */ private int m_printValues; /** * Constructor that sets the options to the default values. */ public Tertius() { resetOptions(); } /** * Returns a string describing this associator. * * @return A description of the evaluator suitable for * displaying in the explorer/experimenter gui. */ public String globalInfo() { return "Finds rules according to confirmation measure (Tertius-type " + "algorithm).\n\n" + "For more information see:\n\n" + getTechnicalInformation().toString(); } /** * Returns an instance of a TechnicalInformation object, containing * detailed information about the technical background of this class, * e.g., paper reference or book this class is based on. * * @return the technical information about this class */ public TechnicalInformation getTechnicalInformation() { TechnicalInformation result; result = new TechnicalInformation(Type.ARTICLE); result.setValue(Field.AUTHOR, "P. A. Flach and N. Lachiche"); result.setValue(Field.YEAR, "1999"); result.setValue(Field.TITLE, "Confirmation-Guided Discovery of first-order rules with Tertius"); result.setValue(Field.JOURNAL, "Machine Learning"); result.setValue(Field.VOLUME, "42"); result.setValue(Field.PAGES, "61-95"); return result; } /** * Resets the options to the default values. */ public void resetOptions() { /* Pruning options. */ m_best = 10; m_frequencyThreshold = 0; m_confirmationThreshold = 0; m_noiseThreshold = 1; /* Search space & language bias options. */ m_repeat = false; m_numLiterals = 4; m_negation = NONE; m_classification = false; m_classIndex = 0; m_horn = false; /* Subsumption tests options. */ m_equivalent = true; m_sameClause = true; m_subsumption = true; /* Missing values. */ m_missing = EXPLICIT; /* ROC analysis. */ m_roc = false; /* Individual-based learning. */ m_partsString = ""; m_parts = null; /* Values output. */ m_printValues = NO; } /** * Returns an enumeration describing the available options. * * @return An enumeration of all the available options. */ public Enumeration listOptions() { Vector newVector = new Vector(17); /* Pruning options. */ newVector.addElement(new Option("\tSet maximum number of confirmation " + "values in the result. (default: 10)", "K", 1, "-K <number of values in result>")); newVector.addElement(new Option("\tSet frequency threshold for pruning. " + "(default: 0)", "F", 1, "-F <frequency threshold>")); newVector.addElement(new Option("\tSet confirmation threshold. " + "(default: 0)", "C", 1, "-C <confirmation threshold>")); newVector.addElement(new Option("\tSet noise threshold : maximum frequency " + "of counter-examples.\n\t0 gives only " + "satisfied rules. (default: 1)", "N", 1, "-N <noise threshold>")); /* Search space & language bias options. */ newVector.addElement(new Option("\tAllow attributes to be repeated in a " + "same rule.", "R", 0, "-R")); newVector.addElement(new Option("\tSet maximum number of literals in a " + "rule. (default: 4)", "L", 1, "-L <number of literals>")); newVector.addElement(new Option("\tSet the negations in the rule. " + "(default: 0)", "G", 1, "-G <0=no negation | " + "1=body | " + "2=head | " + "3=body and head>")); newVector.addElement(new Option("\tConsider only classification rules.", "S", 0, "-S")); newVector.addElement(new Option("\tSet index of class attribute. " + "(default: last).", "c", 1, "-c <class index>")); newVector.addElement(new Option("\tConsider only horn clauses.", "H", 0, "-H")); /* Subsumption tests options. */ newVector.addElement(new Option("\tKeep equivalent rules.", "E", 0, "-E")); newVector.addElement(new Option("\tKeep same clauses.", "M", 0, "-M")); newVector.addElement(new Option("\tKeep subsumed rules.", "T", 0, "-T")); /* Missing values options. */ newVector.addElement(new Option("\tSet the way to handle missing values. " + "(default: 0)", "I", 1, "-I <0=always match | " + "1=never match | " + "2=significant>")); /* ROC analysis. */ newVector.addElement(new Option("\tUse ROC analysis. ", "O", 0, "-O")); /* Individual-based learning. */ newVector.addElement(new Option("\tSet the file containing the parts of " + "the individual for individual-based " + "learning.", "p", 1, "-p <name of file>")); /* Values output. */ newVector.addElement(new Option("\tSet output of current values. " + "(default: 0)", "P", 1, "-P <0=no output | " + "1=on stdout | " + "2=in separate window>")); return newVector.elements(); } /** * Parses a given list of options. <p/> * <!-- options-start --> * Valid options are: <p/> * * <pre> -K <number of values in result> * Set maximum number of confirmation values in the result. (default: 10)</pre> * * <pre> -F <frequency threshold> * Set frequency threshold for pruning. (default: 0)</pre> * * <pre> -C <confirmation threshold> * Set confirmation threshold. (default: 0)</pre> * * <pre> -N <noise threshold> * Set noise threshold : maximum frequency of counter-examples. * 0 gives only satisfied rules. (default: 1)</pre> * * <pre> -R * Allow attributes to be repeated in a same rule.</pre> * * <pre> -L <number of literals> * Set maximum number of literals in a rule. (default: 4)</pre> * * <pre> -G <0=no negation | 1=body | 2=head | 3=body and head> * Set the negations in the rule. (default: 0)</pre> * * <pre> -S * Consider only classification rules.</pre> * * <pre> -c <class index> * Set index of class attribute. (default: last).</pre> * * <pre> -H * Consider only horn clauses.</pre> * * <pre> -E * Keep equivalent rules.</pre> * * <pre> -M * Keep same clauses.</pre> * * <pre> -T * Keep subsumed rules.</pre> * * <pre> -I <0=always match | 1=never match | 2=significant> * Set the way to handle missing values. (default: 0)</pre> * * <pre> -O * Use ROC analysis. </pre> * * <pre> -p <name of file> * Set the file containing the parts of the individual for individual-based learning.</pre> * * <pre> -P <0=no output | 1=on stdout | 2=in separate window> * Set output of current values. (default: 0)</pre> *
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -