⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 geneticsearch.java

📁 数据挖掘聚类算法:bayes源代码,使用JAVA语言实现
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
/* * 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. *//* * GeneticSearch.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.util.Enumeration;import java.util.Random;import java.util.Vector;/**  <!-- globalinfo-start --> * This Bayes Network learning algorithm uses genetic search for finding a well scoring Bayes network structure. Genetic search works by having a population of Bayes network structures and allow them to mutate and apply cross over to get offspring. The best network structure found during the process is returned. * <p/> <!-- globalinfo-end --> * <!-- options-start --> * Valid options are: <p/> *  * <pre> -L &lt;integer&gt; *  Population size</pre> *  * <pre> -A &lt;integer&gt; *  Descendant population size</pre> *  * <pre> -U &lt;integer&gt; *  Number of runs</pre> *  * <pre> -M *  Use mutation. *  (default true)</pre> *  * <pre> -C *  Use cross-over. *  (default true)</pre> *  * <pre> -O *  Use tournament selection (true) or maximum subpopulatin (false). *  (default false)</pre> *  * <pre> -R &lt;seed&gt; *  Random number seed</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.5 $ */public class GeneticSearch     extends GlobalScoreSearchAlgorithm {    /** for serialization */    static final long serialVersionUID = 4236165533882462203L;      /** number of runs **/    int m_nRuns = 10;	/** size of population **/	int m_nPopulationSize = 10;	/** size of descendant population **/	int m_nDescendantPopulationSize = 100;	/** use cross-over? **/	boolean m_bUseCrossOver = true;	/** use mutation? **/	boolean m_bUseMutation = true;		/** use tournament selection or take best sub-population **/	boolean m_bUseTournamentSelection = false;			/** random number seed **/	int m_nSeed = 1;		/** random number generator **/	Random m_random = null;	/** used in BayesNetRepresentation for efficiently determining	 * whether a number is square  	 */	static boolean [] g_bIsSquare;		class BayesNetRepresentation implements RevisionHandler {		/** number of nodes in network **/				int m_nNodes = 0;		/** bit representation of parent sets 		 * m_bits[iTail + iHead * m_nNodes] represents arc iTail->iHead		 */		boolean [] m_bits;				/** score of represented network structure **/		double m_fScore = 0.0f;				/** 		 * return score of represented network structure		 * 		 * @return the score		 */		public double getScore() {			return m_fScore;		} // getScore		/** 		 * c'tor 		 * 		 * @param nNodes the number of nodes		 */		BayesNetRepresentation (int nNodes) {			m_nNodes = nNodes;		} // c'tor				/** initialize with a random structure by randomly placing		 * m_nNodes arcs.		 */		public void randomInit() {			do {				m_bits = new boolean [m_nNodes * m_nNodes];				for (int i = 0; i < m_nNodes; i++) {					int iPos;					do {						iPos = m_random.nextInt(m_nNodes * m_nNodes);					} while (isSquare(iPos));					m_bits[iPos] = true;				}			} while (hasCycles());			calcGlobalScore();		}		/** calculate score of current network representation		 * As a side effect, the parent sets are set		 */		void calcGlobalScore() {			// clear current network			for (int iNode = 0; iNode < m_nNodes; iNode++) {				ParentSet parentSet = m_BayesNet.getParentSet(iNode);				while (parentSet.getNrOfParents() > 0) {					parentSet.deleteLastParent(m_BayesNet.m_Instances);				}			}			// insert arrows			for (int iNode = 0; iNode < m_nNodes; iNode++) {				ParentSet parentSet = m_BayesNet.getParentSet(iNode);				for (int iNode2 = 0; iNode2 < m_nNodes; iNode2++) {					if (m_bits[iNode2 + iNode * m_nNodes]) {						parentSet.addParent(iNode2, m_BayesNet.m_Instances);					}				}			}			// calc score			try {				m_fScore = calcScore(m_BayesNet);			} catch (Exception e) {				// ignore			}		} // calcScore		/** check whether there are cycles in the network 		*  		* @return true if a cycle is found, false otherwise		 */		public boolean hasCycles() {			// check for cycles			boolean[] bDone = new boolean[m_nNodes];			for (int iNode = 0; iNode < m_nNodes; iNode++) {				// find a node for which all parents are 'done'				boolean bFound = false;				for (int iNode2 = 0; !bFound && iNode2 < m_nNodes; iNode2++) {					if (!bDone[iNode2]) {						boolean bHasNoParents = true;						for (int iParent = 0; iParent < m_nNodes; iParent++) {							if (m_bits[iParent + iNode2 * m_nNodes] && !bDone[iParent]) {								bHasNoParents = false;							}						}						if (bHasNoParents) {							bDone[iNode2] = true;							bFound = true;						}					}				}				if (!bFound) {					return true;				}			}			return false;		} // hasCycles		/** create clone of current object 		 * @return cloned object		 */		BayesNetRepresentation copy() {			BayesNetRepresentation b = new BayesNetRepresentation(m_nNodes);			b.m_bits = new boolean [m_bits.length];			for (int i = 0; i < m_nNodes * m_nNodes; i++) {				b.m_bits[i] = m_bits[i];			}			b.m_fScore = m_fScore;			return b;				} // copy		/** Apply mutation operation to BayesNet		 * Calculate score and as a side effect sets BayesNet parent sets.		 */		void mutate() {			// flip a bit			do {								int iBit;				do {					iBit = m_random.nextInt(m_nNodes * m_nNodes);				} while (isSquare(iBit));								m_bits[iBit] = !m_bits[iBit];			} while (hasCycles());			calcGlobalScore();		} // mutate		/** Apply cross-over operation to BayesNet 		 * Calculate score and as a side effect sets BayesNet parent sets.		 * @param other BayesNetRepresentation to cross over with		 */		void crossOver(BayesNetRepresentation other) {			boolean [] bits = new boolean [m_bits.length];			for (int i = 0; i < m_bits.length; i++) {				bits[i] = m_bits[i];			}			int iCrossOverPoint = m_bits.length;			do {				// restore to original state				for (int i = iCrossOverPoint; i < m_bits.length; i++) {					m_bits[i] = bits[i];				}				// take all bits from cross-over points onwards				iCrossOverPoint = m_random.nextInt(m_bits.length);				for (int i = iCrossOverPoint; i < m_bits.length; i++) {					m_bits[i] = other.m_bits[i];				}			} while (hasCycles());			calcGlobalScore();		} // crossOver						/** check if number is square and initialize g_bIsSquare structure		 * if necessary		 * @param nNum number to check (should be below m_nNodes * m_nNodes)		 * @return true if number is square		 */		boolean isSquare(int nNum) {			if (g_bIsSquare == null || g_bIsSquare.length < nNum) {				g_bIsSquare = new boolean [m_nNodes * m_nNodes];				for (int i = 0; i < m_nNodes; i++) {					g_bIsSquare[i * m_nNodes + i] = true;				}			}			return g_bIsSquare[nNum];		} // isSquare		/**		 * Returns the revision string.		 * 		 * @return		the revision		 */		public String getRevision() {		  return RevisionUtils.extract("$Revision: 1.5 $");		}	} // class BayesNetRepresentation 	    		/**	 * search determines the network structure/graph of the network	 * with a genetic search algorithm.	 * 	 * @param bayesNet the network to search	 * @param instances the instances to use	 * @throws Exception if population size doesn fit or neither cross-over or mutation was chosen	 */	protected void search(BayesNet bayesNet, Instances instances) throws Exception {		// sanity check		if (getDescendantPopulationSize() < getPopulationSize()) {			throw new Exception ("Descendant PopulationSize should be at least Population Size");		}		if (!getUseCrossOver() && !getUseMutation()) {			throw new Exception ("At least one of mutation or cross-over should be used");		}				m_random = new Random(m_nSeed);		// keeps track of best structure found so far 		BayesNet bestBayesNet;		// keeps track of score pf best structure found so far 		double fBestScore = calcScore(bayesNet);			// initialize bestBayesNet		bestBayesNet = new BayesNet();		bestBayesNet.m_Instances = instances;		bestBayesNet.initStructure();		copyParentSets(bestBayesNet, bayesNet);		                        // initialize population        		BayesNetRepresentation  [] population = new BayesNetRepresentation [getPopulationSize()];        for (int i = 0; i < getPopulationSize(); i++) {        	population[i] = new BayesNetRepresentation (instances.numAttributes());			population[i].randomInit();			if (population[i].getScore() > fBestScore) {				copyParentSets(bestBayesNet, bayesNet);				fBestScore = population[i].getScore();							}        }                // go do the search                for (int iRun = 0; iRun < m_nRuns; iRun++) {        	// create descendants			BayesNetRepresentation  [] descendantPopulation = new BayesNetRepresentation  [getDescendantPopulationSize()];			for (int i = 0; i < getDescendantPopulationSize(); i++) {				descendantPopulation[i] = population[m_random.nextInt(getPopulationSize())].copy();				if (getUseMutation()) {					if (getUseCrossOver() && m_random.nextBoolean()) {						descendantPopulation[i].crossOver(population[m_random.nextInt(getPopulationSize())]);											} else {						descendantPopulation[i].mutate();													}				} else {					// use crossover					descendantPopulation[i].crossOver(population[m_random.nextInt(getPopulationSize())]);				}				if (descendantPopulation[i].getScore() > fBestScore) {					copyParentSets(bestBayesNet, bayesNet);					fBestScore = descendantPopulation[i].getScore();				}

⌨️ 快捷键说明

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