ParentSet.java
Upload User: rhdiban
Upload Date: 2013-08-09
Package Size: 15085k
Code Size: 4k
Category:

Windows Develop

Development Platform:

Java

  1. /*
  2.  * This program is free software; you can redistribute it and/or modify
  3.  * it under the terms of the GNU General Public License as published by
  4.  * the Free Software Foundation; either version 2 of the License, or
  5.  * (at your option) any later version.
  6.  * 
  7.  * This program is distributed in the hope that it will be useful,
  8.  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  9.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  10.  * GNU General Public License for more details.
  11.  * 
  12.  * You should have received a copy of the GNU General Public License
  13.  * along with this program; if not, write to the Free Software
  14.  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  15.  */
  16. /*
  17.  * ParentSet.java
  18.  * Copyright (C) 2001 Remco Bouckaert
  19.  * 
  20.  */
  21. package weka.classifiers.bayes;
  22. import weka.core.*;
  23. /**
  24.  * Helper class for Bayes Network classifiers. Provides datastructures to
  25.  * represent a set of parents in a graph.
  26.  * 
  27.  * @author Remco Bouckaert (rrb@xm.co.nz)
  28.  * @version $Revision: 1.2 $
  29.  */
  30. public class ParentSet {
  31.   /**
  32.    * Holds indexes of parents
  33.    */
  34.   private int[] m_nParents;
  35.   /**
  36.    * returns index parent of parent specified by index
  37.    * 
  38.    * @param iParent Index of parent
  39.    */
  40.   public int GetParent(int iParent) {
  41.     return m_nParents[iParent];
  42.   } 
  43.   /**
  44.    * Holds number of parents
  45.    */
  46.   private int m_nNrOfParents = 0;
  47.   /**
  48.    * returns number of parents
  49.    * @ return number of parents
  50.    */
  51.   public int GetNrOfParents() {
  52.     return m_nNrOfParents;
  53.   } 
  54.   /**
  55.    * Holds cardinality  of parents (= number of instantiations the parents can take)
  56.    */
  57.   private int m_nCardinalityOfParents = 1;
  58.   /**
  59.    * returns cardinality of parents
  60.    */
  61.   public int GetCardinalityOfParents() {
  62.     return m_nCardinalityOfParents;
  63.   } 
  64.   /**
  65.    * default constructor
  66.    */
  67.   public ParentSet() {
  68.     m_nParents = new int[10];
  69.     m_nNrOfParents = 0;
  70.     m_nCardinalityOfParents = 1;
  71.   }    // ParentSet
  72.   /**
  73.    * constructor
  74.    * @param nMaxNrOfParents upper bound on nr of parents
  75.    */
  76.   public ParentSet(int nMaxNrOfParents) {
  77.     m_nParents = new int[nMaxNrOfParents];
  78.     m_nNrOfParents = 0;
  79.     m_nCardinalityOfParents = 1;
  80.   }    // ParentSet
  81.   /**
  82.    * copy constructor
  83.    * @param other other parent set
  84.    */
  85.   public ParentSet(ParentSet other) {
  86.     m_nNrOfParents = other.m_nNrOfParents;
  87.     m_nCardinalityOfParents = other.m_nCardinalityOfParents;
  88.     m_nParents = new int[m_nNrOfParents];
  89.     for (int iParent = 0; iParent < m_nNrOfParents; iParent++) {
  90.       m_nParents[iParent] = other.m_nParents[iParent];
  91.     } 
  92.   }    // ParentSet
  93.   /**
  94.    * reserve memory for parent set
  95.    * 
  96.    * @param nSize maximum size of parent set to reserver memory for
  97.    */
  98.   public void MaxParentSetSize(int nSize) {
  99.     m_nParents = new int[nSize];
  100.   }    // MaxParentSetSize
  101.  
  102.   /**
  103.    * Add parent to parent set and update internals (specifically the cardinality of the parent set)
  104.    * 
  105.    * @param nParent parent to add
  106.    * @param _Instances used for updating the internals
  107.    */
  108.   public void AddParent(int nParent, Instances _Instances) {
  109.     m_nParents[m_nNrOfParents] = nParent;
  110.     m_nNrOfParents++;
  111.     m_nCardinalityOfParents *= _Instances.attribute(nParent).numValues();
  112.   }    // AddParent
  113.  
  114.   /**
  115.    * Delete last added parent from parent set and update internals (specifically the cardinality of the parent set)
  116.    * 
  117.    * @param _Instances used for updating the internals
  118.    */
  119.   public void DeleteLastParent(Instances _Instances) {
  120.     m_nNrOfParents--;
  121.     m_nCardinalityOfParents = 
  122.       m_nCardinalityOfParents 
  123.       / _Instances.attribute(m_nParents[m_nNrOfParents]).numValues();
  124.   }    // DeleteLastParent
  125.  
  126. }      // class ParentSet