📄 hillclimber.java
字号:
/* * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 2 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program; if not, write to the Free Software * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. *//* * HillClimber.java * Copyright (C) 2004 University of Waikato, Hamilton, New Zealand * */ package weka.classifiers.bayes.net.search.global;import weka.classifiers.bayes.BayesNet;import weka.classifiers.bayes.net.ParentSet;import weka.core.Instances;import weka.core.Option;import weka.core.RevisionHandler;import weka.core.RevisionUtils;import weka.core.Utils;import java.io.Serializable;import java.util.Enumeration;import java.util.Vector;/** <!-- globalinfo-start --> * 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. * <p/> <!-- globalinfo-end --> * <!-- options-start --> * Valid options are: <p/> * * <pre> -P <nr of parents> * Maximum number of parents</pre> * * <pre> -R * Use arc reversal operation. * (default false)</pre> * * <pre> -N * Initial structure is empty (instead of Naive Bayes)</pre> * * <pre> -mbc * Applies a Markov Blanket correction to the network structure, * after a network structure is learned. This ensures that all * nodes in the network are part of the Markov blanket of the * classifier node.</pre> * * <pre> -S [LOO-CV|k-Fold-CV|Cumulative-CV] * Score type (LOO-CV,k-Fold-CV,Cumulative-CV)</pre> * * <pre> -Q * Use probabilistic or 0/1 scoring. * (default probabilistic scoring)</pre> * <!-- options-end --> * * @author Remco Bouckaert (rrb@xm.co.nz) * @version $Revision: 1.9 $ */public class HillClimber extends GlobalScoreSearchAlgorithm { /** for serialization */ static final long serialVersionUID = -3885042888195820149L; /** * the Operation class contains info on operations performed * on the current Bayesian network. */ class Operation implements Serializable, RevisionHandler { /** for serialization */ static final long serialVersionUID = -2934970456587374967L; // constants indicating the type of an operation final static int OPERATION_ADD = 0; final static int OPERATION_DEL = 1; final static int OPERATION_REVERSE = 2; /** c'tor **/ public Operation() { } /** c'tor + initializers * * @param nTail * @param nHead * @param nOperation */ public Operation(int nTail, int nHead, int nOperation) { m_nHead = nHead; m_nTail = nTail; m_nOperation = nOperation; } /** compare this operation with another * @param other operation to compare with * @return true if operation is the same */ public boolean equals(Operation other) { if (other == null) { return false; } return (( m_nOperation == other.m_nOperation) && (m_nHead == other.m_nHead) && (m_nTail == other.m_nTail)); } // equals /** number of the tail node **/ public int m_nTail; /** number of the head node **/ public int m_nHead; /** type of operation (ADD, DEL, REVERSE) **/ public int m_nOperation; /** change of score due to this operation **/ public double m_fScore = -1E100; /** * Returns the revision string. * * @return the revision */ public String getRevision() { return RevisionUtils.extract("$Revision: 1.9 $"); } } // class Operation /** use the arc reversal operator **/ boolean m_bUseArcReversal = false; /** * search determines the network structure/graph of the network * with the Taby algorithm. * * @param bayesNet the network to search * @param instances the instances to work with * @throws Exception if something goes wrong */ protected void search(BayesNet bayesNet, Instances instances) throws Exception { m_BayesNet = bayesNet; double fScore = calcScore(bayesNet); // go do the search Operation oOperation = getOptimalOperation(bayesNet, instances); while ((oOperation != null) && (oOperation.m_fScore > fScore)) { performOperation(bayesNet, instances, oOperation); fScore = oOperation.m_fScore; oOperation = getOptimalOperation(bayesNet, instances); } } // search /** check whether the operation is not in the forbidden. * For base hill climber, there are no restrictions on operations, * so we always return true. * @param oOperation operation to be checked * @return true if operation is not in the tabu list */ boolean isNotTabu(Operation oOperation) { return true; } // isNotTabu /** * getOptimalOperation finds the optimal operation that can be performed * on the Bayes network that is not in the tabu list. * * @param bayesNet Bayes network to apply operation on * @param instances data set to learn from * @return optimal operation found * @throws Exception if something goes wrong */ Operation getOptimalOperation(BayesNet bayesNet, Instances instances) throws Exception { Operation oBestOperation = new Operation(); // Add??? oBestOperation = findBestArcToAdd(bayesNet, instances, oBestOperation); // Delete??? oBestOperation = findBestArcToDelete(bayesNet, instances, oBestOperation); // Reverse??? if (getUseArcReversal()) { oBestOperation = findBestArcToReverse(bayesNet, instances, oBestOperation); } // did we find something? if (oBestOperation.m_fScore == -1E100) { return null; } return oBestOperation; } // getOptimalOperation /** performOperation applies an operation * on the Bayes network and update the cache. * * @param bayesNet Bayes network to apply operation on * @param instances data set to learn from * @param oOperation operation to perform * @throws Exception if something goes wrong */ void performOperation(BayesNet bayesNet, Instances instances, Operation oOperation) throws Exception { // perform operation switch (oOperation.m_nOperation) { case Operation.OPERATION_ADD: applyArcAddition(bayesNet, oOperation.m_nHead, oOperation.m_nTail, instances); if (bayesNet.getDebug()) { System.out.print("Add " + oOperation.m_nHead + " -> " + oOperation.m_nTail); } break; case Operation.OPERATION_DEL: applyArcDeletion(bayesNet, oOperation.m_nHead, oOperation.m_nTail, instances); if (bayesNet.getDebug()) { System.out.print("Del " + oOperation.m_nHead + " -> " + oOperation.m_nTail); } break; case Operation.OPERATION_REVERSE: applyArcDeletion(bayesNet, oOperation.m_nHead, oOperation.m_nTail, instances); applyArcAddition(bayesNet, oOperation.m_nTail, oOperation.m_nHead, instances); if (bayesNet.getDebug()) { System.out.print("Rev " + oOperation.m_nHead+ " -> " + oOperation.m_nTail); } break; } } // performOperation /** * * @param bayesNet * @param iHead * @param iTail * @param instances */ void applyArcAddition(BayesNet bayesNet, int iHead, int iTail, Instances instances) { ParentSet bestParentSet = bayesNet.getParentSet(iHead); bestParentSet.addParent(iTail, instances); } // applyArcAddition /** * * @param bayesNet * @param iHead * @param iTail * @param instances */ void applyArcDeletion(BayesNet bayesNet, int iHead, int iTail, Instances instances) { ParentSet bestParentSet = bayesNet.getParentSet(iHead); bestParentSet.deleteParent(iTail, instances); } // applyArcAddition /** * find best (or least bad) arc addition operation * * @param bayesNet Bayes network to add arc to * @param instances data set * @param oBestOperation * @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
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -