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 | /* |
---|
18 | * VaryNode.java |
---|
19 | * Copyright (C) 2002 University of Waikato, Hamilton, New Zealand |
---|
20 | * |
---|
21 | */ |
---|
22 | |
---|
23 | package weka.classifiers.bayes.net; |
---|
24 | |
---|
25 | import weka.core.RevisionHandler; |
---|
26 | import weka.core.RevisionUtils; |
---|
27 | |
---|
28 | import java.io.Serializable; |
---|
29 | |
---|
30 | /** |
---|
31 | * Part of ADTree implementation. See ADNode.java for more details. |
---|
32 | * |
---|
33 | * @author Remco Bouckaert (rrb@xm.co.nz) |
---|
34 | * @version $Revision: 1.6 $ |
---|
35 | */ |
---|
36 | public class VaryNode |
---|
37 | implements Serializable, RevisionHandler { |
---|
38 | |
---|
39 | /** for serialization */ |
---|
40 | private static final long serialVersionUID = -6196294370675872424L; |
---|
41 | |
---|
42 | /** index of the node varied **/ |
---|
43 | public int m_iNode; |
---|
44 | |
---|
45 | /** most common value **/ |
---|
46 | public int m_nMCV; |
---|
47 | |
---|
48 | /** list of ADNode children **/ |
---|
49 | public ADNode [] m_ADNodes; |
---|
50 | |
---|
51 | /** Creates new VaryNode */ |
---|
52 | public VaryNode(int iNode) { |
---|
53 | m_iNode = iNode; |
---|
54 | } |
---|
55 | |
---|
56 | /** |
---|
57 | * get counts for specific instantiation of a set of nodes |
---|
58 | * |
---|
59 | * @param nCounts array for storing counts |
---|
60 | * @param nNodes array of node indexes |
---|
61 | * @param nOffsets offset for nodes in nNodes in nCounts |
---|
62 | * @param iNode index into nNode indicating current node |
---|
63 | * @param iOffset Offset into nCounts due to nodes below iNode |
---|
64 | * @param parent parant ADNode of this VaryNode |
---|
65 | * @param bSubstract indicate whether counts should be added or substracted |
---|
66 | */ |
---|
67 | public void getCounts( |
---|
68 | int [] nCounts, |
---|
69 | int [] nNodes, |
---|
70 | int [] nOffsets, |
---|
71 | int iNode, |
---|
72 | int iOffset, |
---|
73 | ADNode parent, |
---|
74 | boolean bSubstract) { |
---|
75 | int nCurrentNode = nNodes[iNode]; |
---|
76 | for (int iValue = 0 ; iValue < m_ADNodes.length; iValue++) { |
---|
77 | if (iValue != m_nMCV) { |
---|
78 | if (m_ADNodes[iValue] != null) { |
---|
79 | m_ADNodes[iValue].getCounts(nCounts, |
---|
80 | nNodes, |
---|
81 | nOffsets, |
---|
82 | iNode + 1, |
---|
83 | iOffset + nOffsets[iNode] * iValue, |
---|
84 | bSubstract); |
---|
85 | } |
---|
86 | } else { |
---|
87 | parent.getCounts(nCounts, |
---|
88 | nNodes, |
---|
89 | nOffsets, |
---|
90 | iNode + 1, |
---|
91 | iOffset + nOffsets[iNode] * iValue, |
---|
92 | bSubstract); |
---|
93 | for (int iValue2 = 0; iValue2 < m_ADNodes.length; iValue2++) { |
---|
94 | if (iValue2 != m_nMCV && m_ADNodes[iValue2] != null) { |
---|
95 | m_ADNodes[iValue2].getCounts(nCounts, |
---|
96 | nNodes, |
---|
97 | nOffsets, |
---|
98 | iNode + 1, |
---|
99 | iOffset + nOffsets[iNode] * iValue, |
---|
100 | !bSubstract); |
---|
101 | } |
---|
102 | } |
---|
103 | } |
---|
104 | } |
---|
105 | } |
---|
106 | |
---|
107 | /** |
---|
108 | * print is used for debugging only, called from ADNode |
---|
109 | * |
---|
110 | * @param sTab amount of space. |
---|
111 | */ |
---|
112 | public void print(String sTab) { |
---|
113 | for (int iValue = 0; iValue < m_ADNodes.length; iValue++) { |
---|
114 | System.out.print(sTab + iValue + ": "); |
---|
115 | if (m_ADNodes[iValue] == null) { |
---|
116 | if (iValue == m_nMCV) { |
---|
117 | System.out.println("MCV"); |
---|
118 | } else { |
---|
119 | System.out.println("null"); |
---|
120 | } |
---|
121 | } else { |
---|
122 | System.out.println(); |
---|
123 | m_ADNodes[iValue].print(); |
---|
124 | } |
---|
125 | } |
---|
126 | } |
---|
127 | |
---|
128 | /** |
---|
129 | * Returns the revision string. |
---|
130 | * |
---|
131 | * @return the revision |
---|
132 | */ |
---|
133 | public String getRevision() { |
---|
134 | return RevisionUtils.extract("$Revision: 1.6 $"); |
---|
135 | } |
---|
136 | } |
---|