📄 hillclimber.java
字号:
ParentSet bestParentSet = bayesNet.getParentSet(iHead);
bestParentSet.deleteParent(iTail, instances);
updateCache(iHead, instances.numAttributes(), bestParentSet);
} // applyArcAddition
/** find best (or least bad) arc addition operation
* @param bayesNet: Bayes network to add arc to
* @param instances: data set
* @return Operation containing best arc to add, or null if no arc addition is allowed
* (this can happen if any arc addition introduces a cycle, or all parent sets are filled
* up to the maximum nr of parents).
*/
Operation findBestArcToAdd(BayesNet bayesNet, Instances instances, Operation oBestOperation) {
int nNrOfAtts = instances.numAttributes();
// find best arc to add
for (int iAttributeHead = 0; iAttributeHead < nNrOfAtts; iAttributeHead++) {
if (bayesNet.getParentSet(iAttributeHead).getNrOfParents() < m_nMaxNrOfParents) {
for (int iAttributeTail = 0; iAttributeTail < nNrOfAtts; iAttributeTail++) {
if (addArcMakesSense(bayesNet, instances, iAttributeHead, iAttributeTail)) {
Operation oOperation = new Operation(iAttributeTail, iAttributeHead, Operation.OPERATION_ADD);
if (m_Cache.get(oOperation) > oBestOperation.m_fDeltaScore) {
if (isNotTabu(oOperation)) {
oBestOperation = oOperation;
oBestOperation.m_fDeltaScore = m_Cache.get(oOperation);
}
}
}
}
}
}
return oBestOperation;
} // findBestArcToAdd
/** find best (or least bad) arc deletion operation
* @param bayesNet: Bayes network to delete arc from
* @param instances: data set
* @return Operation containing best arc to delete, or null if no deletion can be made
* (happens when there is no arc in the network yet).
*/
Operation findBestArcToDelete(BayesNet bayesNet, Instances instances, Operation oBestOperation) {
int nNrOfAtts = instances.numAttributes();
// find best arc to delete
for (int iNode = 0; iNode < nNrOfAtts; iNode++) {
ParentSet parentSet = bayesNet.getParentSet(iNode);
for (int iParent = 0; iParent < parentSet.getNrOfParents(); iParent++) {
Operation oOperation = new Operation(parentSet.getParent(iParent), iNode, Operation.OPERATION_DEL);
if (m_Cache.get(oOperation) > oBestOperation.m_fDeltaScore) {
if (isNotTabu(oOperation)) {
oBestOperation = oOperation;
oBestOperation.m_fDeltaScore = m_Cache.get(oOperation);
}
}
}
}
return oBestOperation;
} // findBestArcToDelete
/** find best (or least bad) arc reversal operation
* @param bayesNet: Bayes network to reverse arc in
* @param instances: data set
* @return Operation containing best arc to reverse, or null if no reversal is allowed
* (happens if there is no arc in the network yet, or when any such reversal introduces
* a cycle).
*/
Operation findBestArcToReverse(BayesNet bayesNet, Instances instances, Operation oBestOperation) {
int nNrOfAtts = instances.numAttributes();
// find best arc to reverse
for (int iNode = 0; iNode < nNrOfAtts; iNode++) {
ParentSet parentSet = bayesNet.getParentSet(iNode);
for (int iParent = 0; iParent < parentSet.getNrOfParents(); iParent++) {
int iTail = parentSet.getParent(iParent);
// is reversal allowed?
if (reverseArcMakesSense(bayesNet, instances, iNode, iTail) &&
bayesNet.getParentSet(iTail).getNrOfParents() < m_nMaxNrOfParents) {
// go check if reversal results in the best step forward
Operation oOperation = new Operation(parentSet.getParent(iParent), iNode, Operation.OPERATION_REVERSE);
if (m_Cache.get(oOperation) > oBestOperation.m_fDeltaScore) {
if (isNotTabu(oOperation)) {
oBestOperation = oOperation;
oBestOperation.m_fDeltaScore = m_Cache.get(oOperation);
}
}
}
}
}
return oBestOperation;
} // findBestArcToReverse
/** update the cache due to change of parent set of a node
* @param iAttributeHead: node that has its parent set changed
* @param nNrOfAtts: number of nodes/attributes in data set
* @param parentSet: new parents set of node iAttributeHead
*/
void updateCache(int iAttributeHead, int nNrOfAtts, ParentSet parentSet) {
// update cache entries for arrows heading towards iAttributeHead
double fBaseScore = calcNodeScore(iAttributeHead);
int nNrOfParents = parentSet.getNrOfParents();
for (int iAttributeTail = 0; iAttributeTail < nNrOfAtts; iAttributeTail++) {
if (iAttributeTail != iAttributeHead) {
if (!parentSet.contains(iAttributeTail)) {
// add entries to cache for adding arcs
if (nNrOfParents < m_nMaxNrOfParents) {
Operation oOperation = new Operation(iAttributeTail, iAttributeHead, Operation.OPERATION_ADD);
m_Cache.put(oOperation, calcScoreWithExtraParent(iAttributeHead, iAttributeTail) - fBaseScore);
}
} else {
// add entries to cache for deleting arcs
Operation oOperation = new Operation(iAttributeTail, iAttributeHead, Operation.OPERATION_DEL);
m_Cache.put(oOperation, calcScoreWithMissingParent(iAttributeHead, iAttributeTail) - fBaseScore);
}
}
}
} // updateCache
/**
* Method declaration
*
* @param nMaxNrOfParents
*
*/
public void setMaxNrOfParents(int nMaxNrOfParents) {
m_nMaxNrOfParents = nMaxNrOfParents;
}
/**
* Method declaration
*
* @return
*
*/
public int getMaxNrOfParents() {
return m_nMaxNrOfParents;
}
/**
* Returns an enumeration describing the available options.
*
* @return an enumeration of all the available options.
*/
public Enumeration listOptions() {
Vector newVector = new Vector(2);
newVector.addElement(new Option("\tMaximum number of parents\n", "P", 1, "-P <nr of parents>"));
newVector.addElement(new Option("\tUse arc reversal operation.\n\t(default false)", "R", 0, "-R"));
Enumeration em = super.listOptions();
while (em.hasMoreElements()) {
newVector.addElement(em.nextElement());
}
return newVector.elements();
} // listOptions
/**
* Parses a given list of options. Valid options are:<p>
*
* For other options see search algorithm.
*
* @param options the list of options as an array of strings
* @exception Exception if an option is not supported
*/
public void setOptions(String[] options) throws Exception {
setUseArcReversal(Utils.getFlag('R', options));
setInitAsNaiveBayes ((Utils.getFlag('N', options)));
String sMaxNrOfParents = Utils.getOption('P', options);
if (sMaxNrOfParents.length() != 0) {
setMaxNrOfParents(Integer.parseInt(sMaxNrOfParents));
} else {
setMaxNrOfParents(100000);
}
super.setOptions(options);
} // setOptions
/**
* Gets the current settings of the search algorithm.
*
* @return an array of strings suitable for passing to setOptions
*/
public String[] getOptions() {
String[] superOptions = super.getOptions();
String[] options = new String[7 + superOptions.length];
int current = 0;
if (getUseArcReversal()) {
options[current++] = "-R";
}
if (!getInitAsNaiveBayes()) {
options[current++] = "-N";
}
if (m_nMaxNrOfParents != 10000) {
options[current++] = "-P";
options[current++] = "" + m_nMaxNrOfParents;
}
// insert options from parent class
for (int iOption = 0; iOption < superOptions.length; iOption++) {
options[current++] = superOptions[iOption];
}
// Fill up rest with empty strings, not nulls!
while (current < options.length) {
options[current++] = "";
}
return options;
} // getOptions
/**
* Method declaration
*
* @param bInitAsNaiveBayes
*
*/
public void setInitAsNaiveBayes(boolean bInitAsNaiveBayes) {
m_bInitAsNaiveBayes = bInitAsNaiveBayes;
}
/**
* Method declaration
*
* @return
*
*/
public boolean getInitAsNaiveBayes() {
return m_bInitAsNaiveBayes;
}
/** get use the arc reversal operation
* @return whether the arc reversal operation should be used
*/
public boolean getUseArcReversal() {
return m_bUseArcReversal;
} // getUseArcReversal
/** set use the arc reversal operation
* @param bUseArcReversal whether the arc reversal operation should be used
*/
public void setUseArcReversal(boolean bUseArcReversal) {
m_bUseArcReversal = bUseArcReversal;
} // setUseArcReversal
/**
* This will return a string describing the search algorithm.
* @return The string.
*/
public String globalInfo() {
return "This Bayes Network learning algorithm uses a hill climbing algorithm " +
"adding, deleting and reversing arcs. The search is not restricted by an order " +
"on the variables (unlike K2). The difference with B and B2 is that this hill " +
"climber also considers arrows part of the naive Bayes structure for deletion.";
} // globalInfo
/**
* @return a string to describe the Use Arc Reversal option.
*/
public String useArcReversalTipText() {
return "When set to true, the arc reversal operation is used in the search.";
} // useArcReversalTipText
} // HillClimber
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -