editablebayesnet.java

来自「Weka」· Java 代码 · 共 1,981 行 · 第 1/5 页

JAVA
1,981
字号
		}		// update distributions		// condense distribution, use values for targetnode = 0		int nParentCard = m_ParentSets[nChild].getCardinalityOfParents();		int nTargetCard = m_Instances.attribute(nChild).numValues();		nParentCard = nParentCard / nTargetCard;		Estimator[] distribution2 = new Estimator[nParentCard];		for (int iParent = 0; iParent < nParentCard; iParent++) {			distribution2[iParent] = m_Distributions[nChild][iParent];		}		m_Distributions[nChild] = distribution2;		// update parentsets		m_ParentSets[nChild].deleteParent(nParent, m_Instances);	} // deleteArc	/** specify distribution of a node	 * @param sName name of the node to specify distribution for	 * @param 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	 */	public void setDistribution(String sName, double[][] P) throws Exception {		int nTargetNode = getNode(sName);		setDistribution(nTargetNode, P);	} // setDistribution	/** specify distribution of a node	 * @param nTargetNode index of the node to specify distribution for	 * @param 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	 */	public void setDistribution(int nTargetNode, double[][] P) throws Exception {		// update undo stack		if (m_bNeedsUndoAction) {			addUndoAction(new SetDistributionAction(nTargetNode, P));		}		Estimator[] distributions = m_Distributions[nTargetNode];		for (int iParent = 0; iParent < distributions.length; iParent++) {			DiscreteEstimatorBayes distribution = new DiscreteEstimatorBayes(P[0].length, 0);			for (int iValue = 0; iValue < distribution.getNumSymbols(); iValue++) {				distribution.addValue(iValue, P[iParent][iValue]);			}			distributions[iParent] = distribution;		}		// m_Distributions[nTargetNode] = distributions;	} // setDistribution	/** returns distribution of a node in matrix form with matrix representing distribution	 * with P[i][j] = P(node = j | parent configuration = i)	 * @param sName name of the node to get distribution from	 */	public double[][] getDistribution(String sName) {		int nTargetNode = getNode2(sName);		return getDistribution(nTargetNode);	} // getDistribution	/** returns distribution of a node in matrix form with matrix representing distribution	 * with P[i][j] = P(node = j | parent configuration = i)	 * @param nTargetNode index of the node to get distribution from	 */	public double[][] getDistribution(int nTargetNode) {		int nParentCard = m_ParentSets[nTargetNode].getCardinalityOfParents();		int nCard = m_Instances.attribute(nTargetNode).numValues();		double[][] P = new double[nParentCard][nCard];		for (int iParent = 0; iParent < nParentCard; iParent++) {			for (int iValue = 0; iValue < nCard; iValue++) {				P[iParent][iValue] = m_Distributions[nTargetNode][iParent].getProbability(iValue);			}		}		return P;	} // getDistribution	/** returns array of values of a node	 * @param sName name of the node to get values from	 */	public String[] getValues(String sName) {		int nTargetNode = getNode2(sName);		return getValues(nTargetNode);	} // getValues	/** returns array of values of a node	 * @param nTargetNode index of the node to get values from	 */	public String[] getValues(int nTargetNode) {		String[] values = new String[getCardinality(nTargetNode)];		for (int iValue = 0; iValue < values.length; iValue++) {			values[iValue] = m_Instances.attribute(nTargetNode).value(iValue);		}		return values;	} // getValues	/** returns value of a node	 * @param nTargetNode index of the node to get values from	 * @param iValue index of the value	 */	public String getValueName(int nTargetNode, int iValue) {		return m_Instances.attribute(nTargetNode).value(iValue);	} // getNodeValue	/** change the name of a node	 * @param nTargetNode index of the node to set name for	 * @param sName new name to assign	 */	public void setNodeName(int nTargetNode, String sName) {		// update undo stack		if (m_bNeedsUndoAction) {			addUndoAction(new RenameAction(nTargetNode, getNodeName(nTargetNode), sName));		}		Attribute att = m_Instances.attribute(nTargetNode);		int nCardinality = att.numValues();		FastVector values = new FastVector(nCardinality);		for (int iValue = 0; iValue < nCardinality; iValue++) {			values.addElement(att.value(iValue));		}		replaceAtt(nTargetNode, sName, values);	} // setNodeName	/** change the name of a value of a node	 * @param nTargetNode index of the node to set name for	 * @param sValue current name of the value	 * @param sNewValue new name of the value	 */	public void renameNodeValue(int nTargetNode, String sValue, String sNewValue) {		// update undo stack		if (m_bNeedsUndoAction) {			addUndoAction(new RenameValueAction(nTargetNode, sValue, sNewValue));		}		Attribute att = m_Instances.attribute(nTargetNode);		int nCardinality = att.numValues();		FastVector values = new FastVector(nCardinality);		for (int iValue = 0; iValue < nCardinality; iValue++) {			if (att.value(iValue).equals(sValue)) {				values.addElement(sNewValue);			} else {				values.addElement(att.value(iValue));			}		}		replaceAtt(nTargetNode, att.name(), values);	} // renameNodeValue	/** 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.	 * @param nTargetNode index of the node to add value for	 * @param sNewValue name of the value	 */	public void addNodeValue(int nTargetNode, String sNewValue) {		// update undo stack		if (m_bNeedsUndoAction) {			addUndoAction(new AddValueAction(nTargetNode, sNewValue));		}		Attribute att = m_Instances.attribute(nTargetNode);		int nCardinality = att.numValues();		FastVector values = new FastVector(nCardinality);		for (int iValue = 0; iValue < nCardinality; iValue++) {			values.addElement(att.value(iValue));		}		values.addElement(sNewValue);		replaceAtt(nTargetNode, att.name(), values);		// update distributions of this node		Estimator[] distributions = m_Distributions[nTargetNode];		int nNewCard = values.size();		for (int iParent = 0; iParent < distributions.length; iParent++) {			DiscreteEstimatorBayes distribution = new DiscreteEstimatorBayes(nNewCard, 0);			for (int iValue = 0; iValue < nNewCard - 1; iValue++) {				distribution.addValue(iValue, distributions[iParent].getProbability(iValue));			}			distributions[iParent] = distribution;		}		// update distributions of all children		for (int iNode = 0; iNode < getNrOfNodes(); iNode++) {			if (m_ParentSets[iNode].contains(nTargetNode)) {				distributions = m_Distributions[iNode];				ParentSet parentSet = m_ParentSets[iNode];				int nParentCard = parentSet.getFreshCardinalityOfParents(m_Instances);				Estimator[] newDistributions = new Estimator[nParentCard];				int nCard = getCardinality(iNode);				int nParents = parentSet.getNrOfParents();				int[] values2 = new int[nParents];				int iOldPos = 0;				int iTargetNode = 0;				while (parentSet.getParent(iTargetNode) != nTargetNode) {					iTargetNode++;				}				for (int iPos = 0; iPos < nParentCard; iPos++) {					DiscreteEstimatorBayes distribution = new DiscreteEstimatorBayes(nCard, 0);					for (int iValue = 0; iValue < nCard; iValue++) {						distribution.addValue(iValue, distributions[iOldPos].getProbability(iValue));					}					newDistributions[iPos] = distribution;					// update values					int i = 0;					values2[i]++;					while (i < nParents && values2[i] == getCardinality(parentSet.getParent(i))) {						values2[i] = 0;						i++;						if (i < nParents) {							values2[i]++;						}					}					if (values2[iTargetNode] != nNewCard - 1) {						iOldPos++;					}				}				m_Distributions[iNode] = newDistributions;			}		}	} // addNodeValue	/** 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.	 * @param nTargetNode index of the node to delete value from	 * @param sValue name of the value to delete	 */	public void delNodeValue(int nTargetNode, String sValue) throws Exception {		// update undo stack		if (m_bNeedsUndoAction) {			addUndoAction(new DelValueAction(nTargetNode, sValue));		}		Attribute att = m_Instances.attribute(nTargetNode);		int nCardinality = att.numValues();		FastVector values = new FastVector(nCardinality);		int nValue = -1;		for (int iValue = 0; iValue < nCardinality; iValue++) {			if (att.value(iValue).equals(sValue)) {				nValue = iValue;			} else {				values.addElement(att.value(iValue));			}		}		if (nValue < 0) {			// could not find value			throw new Exception("Node " + nTargetNode + " does not have value (" + sValue + ")");		}		replaceAtt(nTargetNode, att.name(), values);		// update distributions		Estimator[] distributions = m_Distributions[nTargetNode];		int nCard = values.size();		for (int iParent = 0; iParent < distributions.length; iParent++) {			DiscreteEstimatorBayes distribution = new DiscreteEstimatorBayes(nCard, 0);			double sum = 0;			for (int iValue = 0; iValue < nCard; iValue++) {				sum += distributions[iParent].getProbability(iValue);			}			if (sum > 0) {				for (int iValue = 0; iValue < nCard; iValue++) {					distribution.addValue(iValue, distributions[iParent].getProbability(iValue) / sum);				}			} else {				for (int iValue = 0; iValue < nCard; iValue++) {					distribution.addValue(iValue, 1.0 / nCard);				}			}			distributions[iParent] = distribution;		}		// update distributions of all children		for (int iNode = 0; iNode < getNrOfNodes(); iNode++) {			if (m_ParentSets[iNode].contains(nTargetNode)) {				ParentSet parentSet = m_ParentSets[iNode];				distributions = m_Distributions[iNode];				Estimator[] newDistributions = new Estimator[distributions.length * nCard / (nCard + 1)];				int iCurrentDist = 0;				int nParents = parentSet.getNrOfParents();				int[] values2 = new int[nParents];				// fill in the values				int nParentCard = parentSet.getFreshCardinalityOfParents(m_Instances) * (nCard + 1) / nCard;				int iTargetNode = 0;				while (parentSet.getParent(iTargetNode) != nTargetNode) {					iTargetNode++;				}				int[] nCards = new int[nParents];				for (int iParent = 0; iParent < nParents; iParent++) {					nCards[iParent] = getCardinality(parentSet.getParent(iParent));				}				nCards[iTargetNode]++;				for (int iPos = 0; iPos < nParentCard; iPos++) {					if (values2[iTargetNode] != nValue) {						newDistributions[iCurrentDist++] = distributions[iPos];					}					// update values					int i = 0;					values2[i]++;					while (i < nParents && values2[i] == nCards[i]) {						values2[i] = 0;						i++;						if (i < nParents) {							values2[i]++;						}					}				}				m_Distributions[iNode] = newDistributions;			}		}		// update evidence		if (getEvidence(nTargetNode) > nValue) {			setEvidence(nTargetNode, getEvidence(nTargetNode) - 1);		}	} // delNodeValue	/** set position of node	 * @param iNode index of node to set position for	 * @param nX x position of new position	 * @param nY y position of new position	 */	public void setPosition(int iNode, int nX, int nY) {		// update undo stack		if (m_bNeedsUndoAction) {			boolean isUpdate = false;			UndoAction undoAction = null;			try {				if (m_undoStack.size() > 0) {					undoAction = (UndoAction) m_undoStack.elementAt(m_undoStack.size() - 1);					SetPositionAction posAction = (SetPositionAction) undoAction;					if (posAction.m_nTargetNode == iNode) {						isUpdate = true;						posAction.setUndoPosition(nX, nY);					}				}			} catch (Exception e) {				// ignore. it's not a SetPositionAction			}			if (!isUpdate) {				addUndoAction(new SetPositionAction(iNode, nX, nY));			}		}		m_nPositionX.setElementAt(nX, iNode);		m_nPositionY.setElementAt(nY, iNode);	} // setPosition	/** Set position of node. Move set of nodes with the same displacement	 * as a specified node.	 * @param iNode index of node to set position for	 * @param nX x position of new position	 * @param nY y position of new position	 * @param nodes array of indexes of nodes to move	 */	public void setPosition(int nNode, int nX, int nY, FastVector nodes) {		int dX = nX - getPositionX(nNode);		int dY = nY - getPositionY(nNode);		// update undo stack		if (m_bNeedsUndoAction) {			boolean isUpdate = false;			try {				UndoAction undoAction = null;				if (m_undoStack.size() > 0) {					undoAction = (UndoAction) m_undoStack.elementAt(m_undoStack.size() - 1);						SetGroupPositionAction posAction = (SetGroupPositionAction) undoAction;						isUpdate = true;						int iNode = 0;						while (isUpdate && iNode < posAction.m_nodes.size()) {							if ((Integer)posAction.m_nodes.elementAt(iNode) != (Integer) nodes.elementAt(iNode)) {								isUpdate = false;							}							iNode++;						}						if (isUpdate == true) {							posAction.setUndoPosition(dX, dY);						}				}			} catch (Exception e) {				// ignore. it's not a SetPositionAction			}			if (!isUpdate) {				addUndoAction(new SetGroupPositionAction(nodes, dX, dY));			}		}		for (int iNode = 0; iNode < nodes.size(); iNode++) {			nNode = (Integer) nodes.elementAt(iNode);			m_nPositionX.setElementAt(getPositionX(nNode) + dX, nNode);			m_nPositionY.setElementAt(getPositionY(nNode) + dY, nNode);		}	} // setPosition	/** set positions of all nodes	 * @param nPosX new x positions for all nodes	 * @param nPosY new y positions for all nodes	 */	public void layoutGraph(FastVector nPosX, FastVector nPosY) {		if (m_bNeedsUndoAction) {			addUndoAction(new LayoutGraphAction(nPosX, nPosY));		}		m_nPositionX = nPosX;		m_nPositionY = nPosY;	} // layoutGraph	/** get x position of a node

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?