001: /*
002: * This program is free software; you can redistribute it and/or modify
003: * it under the terms of the GNU General Public License as published by
004: * the Free Software Foundation; either version 2 of the License, or
005: * (at your option) any later version.
006: *
007: * This program is distributed in the hope that it will be useful,
008: * but WITHOUT ANY WARRANTY; without even the implied warranty of
009: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
010: * GNU General Public License for more details.
011: *
012: * You should have received a copy of the GNU General Public License
013: * along with this program; if not, write to the Free Software
014: * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
015: */
016:
017: /*
018: * VaryNode.java
019: * Copyright (C) 2002 University of Waikato, Hamilton, New Zealand
020: *
021: */
022:
023: package weka.classifiers.bayes.net;
024:
025: import java.io.Serializable;
026:
027: /**
028: * Part of ADTree implementation. See ADNode.java for more details.
029: *
030: * @author Remco Bouckaert (rrb@xm.co.nz)
031: * @version $Revision: 1.5 $
032: */
033: public class VaryNode implements Serializable {
034:
035: /** for serialization */
036: private static final long serialVersionUID = -6196294370675872424L;
037:
038: /** index of the node varied **/
039: public int m_iNode;
040:
041: /** most common value **/
042: public int m_nMCV;
043:
044: /** list of ADNode children **/
045: public ADNode[] m_ADNodes;
046:
047: /** Creates new VaryNode */
048: public VaryNode(int iNode) {
049: m_iNode = iNode;
050: }
051:
052: /**
053: * get counts for specific instantiation of a set of nodes
054: *
055: * @param nCounts array for storing counts
056: * @param nNodes array of node indexes
057: * @param nOffsets offset for nodes in nNodes in nCounts
058: * @param iNode index into nNode indicating current node
059: * @param iOffset Offset into nCounts due to nodes below iNode
060: * @param parent parant ADNode of this VaryNode
061: * @param bSubstract indicate whether counts should be added or substracted
062: */
063: public void getCounts(int[] nCounts, int[] nNodes, int[] nOffsets,
064: int iNode, int iOffset, ADNode parent, boolean bSubstract) {
065: int nCurrentNode = nNodes[iNode];
066: for (int iValue = 0; iValue < m_ADNodes.length; iValue++) {
067: if (iValue != m_nMCV) {
068: if (m_ADNodes[iValue] != null) {
069: m_ADNodes[iValue].getCounts(nCounts, nNodes,
070: nOffsets, iNode + 1, iOffset
071: + nOffsets[iNode] * iValue,
072: bSubstract);
073: }
074: } else {
075: parent.getCounts(nCounts, nNodes, nOffsets, iNode + 1,
076: iOffset + nOffsets[iNode] * iValue, bSubstract);
077: for (int iValue2 = 0; iValue2 < m_ADNodes.length; iValue2++) {
078: if (iValue2 != m_nMCV && m_ADNodes[iValue2] != null) {
079: m_ADNodes[iValue2].getCounts(nCounts, nNodes,
080: nOffsets, iNode + 1, iOffset
081: + nOffsets[iNode] * iValue,
082: !bSubstract);
083: }
084: }
085: }
086: }
087: }
088:
089: /**
090: * print is used for debugging only, called from ADNode
091: *
092: * @param sTab amount of space.
093: */
094: public void print(String sTab) {
095: for (int iValue = 0; iValue < m_ADNodes.length; iValue++) {
096: System.out.print(sTab + iValue + ": ");
097: if (m_ADNodes[iValue] == null) {
098: if (iValue == m_nMCV) {
099: System.out.println("MCV");
100: } else {
101: System.out.println("null");
102: }
103: } else {
104: System.out.println();
105: m_ADNodes[iValue].print();
106: }
107: }
108: }
109: }
|