Class EditableBayesNet

  • All Implemented Interfaces:
    java.io.Serializable, java.lang.Cloneable, AdditionalMeasureProducer, CapabilitiesHandler, Drawable, OptionHandler, RevisionHandler, WeightedInstancesHandler
    Direct Known Subclasses:
    BayesNetGenerator

    public class EditableBayesNet
    extends BayesNet
    Bayes Network learning using various search algorithms and quality measures.
    Base class for a Bayes Network classifier. Provides datastructures (network structure, conditional probability distributions, etc.) and facilities common to Bayes Network learning algorithms like K2 and B.

    For more information see:

    http://www.cs.waikato.ac.nz/~remco/weka.pdf

    Valid options are:

     -D
      Do not use ADTree data structure
     
     -B <BIF file>
      BIF file to compare with
     
     -Q weka.classifiers.bayes.net.search.SearchAlgorithm
      Search algorithm
     
     -E weka.classifiers.bayes.net.estimate.SimpleEstimator
      Estimator algorithm
     
    Version:
    $Revision: 7836 $
    Author:
    Remco Bouckaert (rrb@xm.co.nz)
    See Also:
    Serialized Form
    • Constructor Detail

      • EditableBayesNet

        public EditableBayesNet()
        standard constructor *
      • EditableBayesNet

        public EditableBayesNet​(Instances instances)
        constructor, creates empty network with nodes based on the attributes in a data set
      • EditableBayesNet

        public EditableBayesNet​(BIFReader other)
        constructor, copies Bayesian network structure from a Bayesian network encapsulated in a BIFReader
      • EditableBayesNet

        public EditableBayesNet​(boolean bSetInstances)
        constructor that potentially initializes instances as well
        Parameters:
        bSetInstances - flag indicating whether to initialize instances or not
    • Method Detail

      • setData

        public void setData​(Instances instances)
                     throws java.lang.Exception
        Assuming a network structure is defined and we want to learn from data, the data set must be put if correct order first and possibly discretized/missing values filled in before proceeding to CPT learning.
        Parameters:
        instances - data set to learn from
        Throws:
        java.lang.Exception - when data sets are not compatible, e.g., a variable is missing or a variable has different nr of values.
      • getNode2

        public int getNode2​(java.lang.String sNodeName)
        returns index of node with given name, or -1 if no such node exists
        Parameters:
        sNodeName - name of the node to get index for
      • getNode

        public int getNode​(java.lang.String sNodeName)
                    throws java.lang.Exception
        returns index of node with given name. Throws exception if no such node exists
        Parameters:
        sNodeName - name of the node to get index for
        Throws:
        java.lang.Exception
      • addNode

        public void addNode​(java.lang.String sName,
                            int nCardinality)
                     throws java.lang.Exception
        Add new node to the network, initializing instances, parentsets, distributions. Used for manual manipulation of the Bayesian network.
        Parameters:
        sName - name of the node. If the name already exists, an x is appended to the name
        nCardinality - number of values for this node
        Throws:
        java.lang.Exception
      • addNode

        public void addNode​(java.lang.String sName,
                            int nCardinality,
                            int nPosX,
                            int nPosY)
                     throws java.lang.Exception
        Add node to network at a given position, initializing instances, parentsets, distributions. Used for manual manipulation of the Bayesian network.
        Parameters:
        sName - name of the node. If the name already exists, an x is appended to the name
        nCardinality - number of values for this node
        nPosX - x-coordiate of the position to place this node
        nPosY - y-coordiate of the position to place this node
        Throws:
        java.lang.Exception
      • deleteNode

        public void deleteNode​(java.lang.String sName)
                        throws java.lang.Exception
        Delete node from the network, updating instances, parentsets, distributions Conditional distributions are condensed by taking the values for the target node to be its first value. Used for manual manipulation of the Bayesian network.
        Parameters:
        sName - name of the node. If the name does not exists an exception is thrown
        Throws:
        java.lang.Exception
      • deleteNode

        public void deleteNode​(int nTargetNode)
                        throws java.lang.Exception
        Delete node from the network, updating instances, parentsets, distributions Conditional distributions are condensed by taking the values for the target node to be its first value. Used for manual manipulation of the Bayesian network.
        Parameters:
        nTargetNode - index of the node to delete.
        Throws:
        java.lang.Exception
      • deleteSelection

        public void deleteSelection​(FastVector nodes)
        Delete nodes with indexes in selection from the network, updating instances, parentsets, distributions Conditional distributions are condensed by taking the values for the target node to be its first value. Used for manual manipulation of the Bayesian network.
        Parameters:
        nodes - array of indexes of nodes to delete.
        Throws:
        java.lang.Exception
      • getContent

        public java.lang.String getContent​(org.w3c.dom.Element node)
        XML helper function. Returns all TEXT children of the given node in one string. Between the node values new lines are inserted.
        Parameters:
        node - the node to return the content for
        Returns:
        the content of the node
      • paste

        public void paste​(java.lang.String sXML)
                   throws java.lang.Exception
        Apply paste operation with XMLBIF fragment. This adds nodes in the XMLBIF fragment to the network, together with its parents. First, paste in test mode to verify no problems occur, then execute paste operation. If a problem occurs (e.g. parent does not exist) then a exception is thrown.
        Parameters:
        sXML - XMLBIF fragment to paste into the network
        Throws:
        java.lang.Exception
      • addArc

        public void addArc​(java.lang.String sParent,
                           java.lang.String sChild)
                    throws java.lang.Exception
        Add arc between two nodes Distributions are updated by duplication for every value of the parent node.
        Parameters:
        sParent - name of the parent node
        sChild - name of the child node
        Throws:
        java.lang.Exception - if parent or child cannot be found in network
      • addArc

        public void addArc​(int nParent,
                           int nChild)
                    throws java.lang.Exception
        Add arc between two nodes Distributions are updated by duplication for every value of the parent node.
        Parameters:
        nParent - index of the parent node
        nChild - index of the child node
        Throws:
        java.lang.Exception
      • addArc

        public void addArc​(java.lang.String sParent,
                           FastVector nodes)
                    throws java.lang.Exception
        Add arc between parent node and each of the nodes in a given list. Distributions are updated as above.
        Parameters:
        sParent - name of the parent node
        nodes - array of indexes of child nodes
        Throws:
        java.lang.Exception
      • deleteArc

        public void deleteArc​(java.lang.String sParent,
                              java.lang.String sChild)
                       throws java.lang.Exception
        Delete arc between two nodes. Distributions are updated by condensing for the parent node taking its first value.
        Parameters:
        sParent - name of the parent node
        sChild - name of the child node
        Throws:
        java.lang.Exception - if parent or child cannot be found in network
      • deleteArc

        public void deleteArc​(int nParent,
                              int nChild)
                       throws java.lang.Exception
        Delete arc between two nodes. Distributions are updated by condensing for the parent node taking its first value.
        Parameters:
        nParent - index of the parent node
        nChild - index of the child node
        Throws:
        java.lang.Exception
      • setDistribution

        public void setDistribution​(java.lang.String sName,
                                    double[][] P)
                             throws java.lang.Exception
        specify distribution of a node
        Parameters:
        sName - name of the node to specify distribution for
        P - matrix representing distribution with P[i][j] = P(node = j | parent configuration = i)
        Throws:
        java.lang.Exception - if parent or child cannot be found in network
      • setDistribution

        public void setDistribution​(int nTargetNode,
                                    double[][] P)
                             throws java.lang.Exception
        specify distribution of a node
        Parameters:
        nTargetNode - index of the node to specify distribution for
        P - matrix representing distribution with P[i][j] = P(node = j | parent configuration = i)
        Throws:
        java.lang.Exception - if parent or child cannot be found in network
      • getDistribution

        public double[][] getDistribution​(java.lang.String sName)
        returns distribution of a node in matrix form with matrix representing distribution with P[i][j] = P(node = j | parent configuration = i)
        Parameters:
        sName - name of the node to get distribution from
      • getDistribution

        public double[][] getDistribution​(int nTargetNode)
        returns distribution of a node in matrix form with matrix representing distribution with P[i][j] = P(node = j | parent configuration = i)
        Parameters:
        nTargetNode - index of the node to get distribution from
      • getValues

        public java.lang.String[] getValues​(java.lang.String sName)
        returns array of values of a node
        Parameters:
        sName - name of the node to get values from
      • getValues

        public java.lang.String[] getValues​(int nTargetNode)
        returns array of values of a node
        Parameters:
        nTargetNode - index of the node to get values from
      • getValueName

        public java.lang.String getValueName​(int nTargetNode,
                                             int iValue)
        returns value of a node
        Parameters:
        nTargetNode - index of the node to get values from
        iValue - index of the value
      • setNodeName

        public void setNodeName​(int nTargetNode,
                                java.lang.String sName)
        change the name of a node
        Parameters:
        nTargetNode - index of the node to set name for
        sName - new name to assign
      • renameNodeValue

        public void renameNodeValue​(int nTargetNode,
                                    java.lang.String sValue,
                                    java.lang.String sNewValue)
        change the name of a value of a node
        Parameters:
        nTargetNode - index of the node to set name for
        sValue - current name of the value
        sNewValue - new name of the value
      • addNodeValue

        public void addNodeValue​(int nTargetNode,
                                 java.lang.String sNewValue)
        Add node value to a node. Distributions for the node assign zero probability to the new value. Child nodes duplicate CPT conditioned on the new value.
        Parameters:
        nTargetNode - index of the node to add value for
        sNewValue - name of the value
      • delNodeValue

        public void delNodeValue​(int nTargetNode,
                                 java.lang.String sValue)
                          throws java.lang.Exception
        Delete node value from a node. Distributions for the node are scaled up proportional to existing distribution (or made uniform if zero probability is assigned to remainder of values). .* Child nodes delete CPTs conditioned on the new value.
        Parameters:
        nTargetNode - index of the node to delete value from
        sValue - name of the value to delete
        Throws:
        java.lang.Exception
      • setPosition

        public void setPosition​(int iNode,
                                int nX,
                                int nY)
        set position of node
        Parameters:
        iNode - index of node to set position for
        nX - x position of new position
        nY - y position of new position
      • setPosition

        public void setPosition​(int nNode,
                                int nX,
                                int nY,
                                FastVector nodes)
        Set position of node. Move set of nodes with the same displacement as a specified node.
        Parameters:
        iNode - index of node to set position for
        nX - x position of new position
        nY - y position of new position
        nodes - array of indexes of nodes to move
      • layoutGraph

        public void layoutGraph​(FastVector nPosX,
                                FastVector nPosY)
        set positions of all nodes
        Parameters:
        nPosX - new x positions for all nodes
        nPosY - new y positions for all nodes
      • getPositionX

        public int getPositionX​(int iNode)
        get x position of a node
        Parameters:
        iNode - index of node of interest
      • getPositionY

        public int getPositionY​(int iNode)
        get y position of a node
        Parameters:
        iNode - index of node of interest
      • alignLeft

        public void alignLeft​(FastVector nodes)
        align set of nodes with the left most node in the list
        Parameters:
        nodes - list of indexes of nodes to align
      • alignRight

        public void alignRight​(FastVector nodes)
        align set of nodes with the right most node in the list
        Parameters:
        nodes - list of indexes of nodes to align
      • alignTop

        public void alignTop​(FastVector nodes)
        align set of nodes with the top most node in the list
        Parameters:
        nodes - list of indexes of nodes to align
      • alignBottom

        public void alignBottom​(FastVector nodes)
        align set of nodes with the bottom most node in the list
        Parameters:
        nodes - list of indexes of nodes to align
      • centerHorizontal

        public void centerHorizontal​(FastVector nodes)
        center set of nodes half way between left and right most node in the list
        Parameters:
        nodes - list of indexes of nodes to center
      • centerVertical

        public void centerVertical​(FastVector nodes)
        center set of nodes half way between top and bottom most node in the list
        Parameters:
        nodes - list of indexes of nodes to center
      • spaceHorizontal

        public void spaceHorizontal​(FastVector nodes)
        space out set of nodes evenly between left and right most node in the list
        Parameters:
        nodes - list of indexes of nodes to space out
      • spaceVertical

        public void spaceVertical​(FastVector nodes)
        space out set of nodes evenly between top and bottom most node in the list
        Parameters:
        nodes - list of indexes of nodes to space out
      • getMargin

        public double[] getMargin​(int iNode)
        return marginal distibution for a node
        Parameters:
        iNode - index of node of interest
      • setMargin

        public void setMargin​(int iNode,
                              double[] fMarginP)
        set marginal distibution for a node
        Parameters:
        iNode - index of node to set marginal distribution for
        fMarginP - marginal distribution
      • getEvidence

        public int getEvidence​(int iNode)
        get evidence state of a node. -1 represents no evidence set, otherwise the index of a value of the node
        Parameters:
        iNode - index of node of interest
      • setEvidence

        public void setEvidence​(int iNode,
                                int iValue)
        set evidence state of a node. -1 represents no evidence set, otherwise the index of a value of the node
        Parameters:
        iNode - index of node of interest
        iValue - evidence value to set
      • getChildren

        public FastVector getChildren​(int nTargetNode)
        return list of children of a node
        Parameters:
        iNode - index of node of interest
      • toXMLBIF03

        public java.lang.String toXMLBIF03()
        returns network in XMLBIF format
        Overrides:
        toXMLBIF03 in class BayesNet
        Returns:
        an XML BIF 0.3 description of the classifier as a string.
      • toXMLBIF03

        public java.lang.String toXMLBIF03​(FastVector nodes)
        return fragment of network in XMLBIF format
        Parameters:
        nodes - array of indexes of nodes that should be in the fragment
      • canUndo

        public boolean canUndo()
        return whether there is something on the undo stack that can be performed
      • canRedo

        public boolean canRedo()
        return whether there is something on the undo stack that can be performed
      • isChanged

        public boolean isChanged()
        return true when current state differs from the state the network was last saved
      • isSaved

        public void isSaved()
        indicate the network state was saved
      • lastActionMsg

        public java.lang.String lastActionMsg()
        get message representing the last action performed on the network
      • undo

        public java.lang.String undo()
        undo the last edit action performed on the network. returns message representing the action performed.
      • redo

        public java.lang.String redo()
        redo the last edit action performed on the network. returns message representing the action performed.
      • clearUndoStack

        public void clearUndoStack()
        remove all actions from the undo stack
      • main

        public static void main​(java.lang.String[] args)
        Parameters:
        args -