Class EditableBayesNet

All Implemented Interfaces:
Serializable, Cloneable, Classifier, AdditionalMeasureProducer, BatchPredictor, CapabilitiesHandler, CapabilitiesIgnorer, CommandlineRunnable, 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: 10153 $
Author:
Remco Bouckaert (rrb@xm.co.nz)
See Also:
  • Constructor Details

    • 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 Details

    • setData

      public void setData(Instances instances) throws 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:
      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(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(String sNodeName) throws 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:
      Exception
    • addNode

      public void addNode(String sName, int nCardinality) throws 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:
      Exception
    • addNode

      public void addNode(String sName, int nCardinality, int nPosX, int nPosY) throws 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:
      Exception
    • deleteNode

      public void deleteNode(String sName) throws 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:
      Exception
    • deleteNode

      public void deleteNode(int nTargetNode) throws 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:
      Exception
    • deleteSelection

      public void deleteSelection(ArrayList<Integer> 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:
      Exception
    • getContent

      public String getContent(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(String sXML) throws 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:
      Exception
    • addArc

      public void addArc(String sParent, String sChild) throws 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:
      Exception - if parent or child cannot be found in network
    • addArc

      public void addArc(int nParent, int nChild) throws 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:
      Exception
    • addArc

      public void addArc(String sParent, ArrayList<Integer> nodes) throws 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:
      Exception
    • deleteArc

      public void deleteArc(String sParent, String sChild) throws 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:
      Exception - if parent or child cannot be found in network
    • deleteArc

      public void deleteArc(int nParent, int nChild) throws 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:
      Exception
    • setDistribution

      public void setDistribution(String sName, double[][] P) throws 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:
      Exception - if parent or child cannot be found in network
    • setDistribution

      public void setDistribution(int nTargetNode, double[][] P) throws 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:
      Exception - if parent or child cannot be found in network
    • getDistribution

      public double[][] getDistribution(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 String[] getValues(String sName)
      returns array of values of a node
      Parameters:
      sName - name of the node to get values from
    • getValues

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

      public 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, 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, String sValue, 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, 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, String sValue) throws 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:
      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, ArrayList<Integer> nodes)
      Set position of node. Move set of nodes with the same displacement as a specified node.
      Parameters:
      nNode - 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(ArrayList<Integer> nPosX, ArrayList<Integer> 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(ArrayList<Integer> 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(ArrayList<Integer> 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(ArrayList<Integer> 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(ArrayList<Integer> 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(ArrayList<Integer> 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(ArrayList<Integer> 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(ArrayList<Integer> 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(ArrayList<Integer> 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 ArrayList<Integer> getChildren(int nTargetNode)
      return list of children of a node
      Parameters:
      nTargetNode - index of node of interest
    • toXMLBIF03

      public 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 String toXMLBIF03(ArrayList<Integer> 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 String lastActionMsg()
      get message representing the last action performed on the network
    • undo

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

      public 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
    • getRevision

      public String getRevision()
      Returns the revision string.
      Specified by:
      getRevision in interface RevisionHandler
      Overrides:
      getRevision in class BayesNet
      Returns:
      the revision
    • main

      public static void main(String[] args)
      Parameters:
      args -