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

📄 smooperator.java

📁 一个数据挖掘软件ALPHAMINERR的整个过程的JAVA版源代码
💻 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.
 *

 */

/*
* $Author: XiaoguangXu 
* $Date: 2006/04/16 11:05:12 
* $Revision: 2.0 
*/

/* @author XiaoguangXu HITSZ-ICE */

package eti.bi.alphaminer.patch.standard.operation.operator;
import java.util.Vector;
import com.prudsys.pdm.Core.MiningException;
import eti.bi.alphaminer.operation.operator.ModelOperator;
import eti.bi.alphaminer.vo.IOperatorNode;
import eti.bi.exception.AppException;
import eti.bi.exception.SysException;
import com.prudsys.pdm.Core.MiningAlgorithm;
import com.prudsys.pdm.Core.MiningAlgorithmSpecification;
import com.prudsys.pdm.Core.MiningAttribute;
import com.prudsys.pdm.Core.MiningModel;
import com.prudsys.pdm.Core.NumericAttribute;
import com.prudsys.pdm.Models.Supervised.SupervisedMiningSettings;
import com.prudsys.pdm.Utils.GeneralUtils;
import eti.bi.alphaminer.core.handler.ICaseHandler;
import eti.bi.alphaminer.operation.operator.INodeInfo;
import eti.bi.alphaminer.operation.operator.Operator;
import eti.bi.common.Locale.Resource;
import eti.bi.alphaminer.vo.BIData;
import eti.bi.alphaminer.vo.BIModel;
import eti.bi.alphaminer.vo.BIObject;
import eti.bi.alphaminer.vo.IBIData;
import eti.bi.alphaminer.vo.IBIModel;

public class SMOOperator extends ModelOperator {
	/* Parameter name for MiningSettingSpecification and MiningAlgorithm */
	public static final String ALGORITHM_NAME = "SMO (Weka)";
	private static String MAP_WEKA_CLASS_PARAMETERS = "wekaClassParameters";
	
	/* Parameter name for SMO Operator in BIML */
	public static String BUILD_LOG_MODELS = "bulid logistic models";
	public static String C = "C";
	public static String CACHE_SIZE = "cache size";
	public static String DEBUG = "debug";
	public static String EPSILON = "epsilon";
	public static String EXPONENT = "exponent";
	public static String FEATRUE_SPACE_NORMOLIZATION = "feature space normolization";
	public static String FILTER_TYPE = "filter type";
	public static String GAMMA = "gamma";
	public static String LOWER_ORDER_TERMS = "lower order Terms";
	public static String NUM_FOLDERS = "num of floders";
	public static String RANDOM_SEED = "random seed";
	public static String TOLERANCE_PARAMETER = "tolerance parameter";
	public static String USE_RBF = "use RBF";
	/* Default parameter value for SMO Operator */
	public static String DEFAULT_BUILD_LOG_MODELS = "";
	public static String DEFAULT_C = String.valueOf(1.0);
	public static String DEFAULT_CACHE_SIZE = String.valueOf(250007);
	public static String DEFAULT_DEBUG = "";
	public static String DEFAULT_EPSILON = String.valueOf(0.000000000001);
	public static String DEFAULT_EXPONENT = String.valueOf(1.0);
	public static String DEFAULT_FEATRUE_SPACE_NORMOLIZATION = "";
	public static String DEFAULT_FILTER_TYPE = "0";
	public static String DEFAULT_GAMMA = String.valueOf(0.01);
	public static String DEFAULT_LOWER_ORDER_TERMS = "";
	public static String DEFAULT_NUM_FOLDERS = String.valueOf(-1);
	public static String DEFAULT_RANDOM_SEED = String.valueOf(1);
	public static String DEFAULT_TOLERANCE_PARAMETER = String.valueOf(0.001);
	public static String DEFAULT_USE_RBF = "";

	/* Parameter name for MiningSettingSpecification and MiningAlgorithm */

	public SMOOperator(String a_CaseID, INodeInfo aNodeInfo,
			ICaseHandler aCaseHandler) {
		super(a_CaseID, aNodeInfo, aCaseHandler);
		m_DefaultModelName = "SMO model";
		//2006/07/29 Xiaojun Chen
		PredictionAssessmentOperator.registerParentsDefinitionID(aNodeInfo.getDefinitionID());
		ScoreOperator.registerParentsDefinitionID(aNodeInfo.getDefinitionID());
	}
	private static final long serialVersionUID = 1456485384848913438L;

	@Override
	public void setNodeID(String a_NodeID) {
		setLabel(getDescription() + " [" + a_NodeID + "]");
		setDefaultModelName(Resource.srcStr("SMO")+"_" + a_NodeID);
		super.setNodeID(a_NodeID);
	}

	public void setDescription(String a_Description) {
		m_Description = a_Description;
		setLabel(m_Description + " [" + m_NodeID + "]");
		setDefaultModelName(Resource.srcStr("SMO")+"_" + m_NodeID);
	}

	@SuppressWarnings("unchecked")
	public void execute(IOperatorNode a_OperatorNode, Vector a_Parents)
			throws SysException, AppException, MiningException {
		/* Get parameter from user input */
		String buildLogModels = (String) a_OperatorNode
				.getParameterValue(BUILD_LOG_MODELS);
		if (buildLogModels == null) {
			buildLogModels = DEFAULT_BUILD_LOG_MODELS;
		}
		String c = (String) a_OperatorNode.getParameterValue(C);
		if (c == null) {
			c = DEFAULT_C;
		}
		String cacheSize = (String) a_OperatorNode.getParameterValue(CACHE_SIZE);
		if (cacheSize == null) {
			cacheSize = DEFAULT_CACHE_SIZE;
		}
		String debug = (String) a_OperatorNode.getParameterValue(DEBUG);
		if (debug == null) {
			debug = DEFAULT_DEBUG;
		}
		String epsilon = (String) a_OperatorNode.getParameterValue(EPSILON);
		if (epsilon == null) {
			epsilon = DEFAULT_EPSILON;
		}
		String exponent = (String) a_OperatorNode.getParameterValue(EXPONENT);
		if (exponent == null) {
			exponent = DEFAULT_EXPONENT;
		}
		String featureSpaceNormolization = (String) a_OperatorNode
				.getParameterValue(FEATRUE_SPACE_NORMOLIZATION);
		if (featureSpaceNormolization == null) {
			featureSpaceNormolization = DEFAULT_FEATRUE_SPACE_NORMOLIZATION;
		}
		String filterType = (String) a_OperatorNode.getParameterValue(FILTER_TYPE);
		if (filterType == null) {
			filterType = DEFAULT_FILTER_TYPE;
		}
		String gamma = (String) a_OperatorNode.getParameterValue(GAMMA);
		if (gamma == null) {
			gamma = DEFAULT_GAMMA;
		}
		String lowerOrderTerms = (String) a_OperatorNode
				.getParameterValue(LOWER_ORDER_TERMS);
		if (lowerOrderTerms == null) {
			lowerOrderTerms = DEFAULT_LOWER_ORDER_TERMS;
		}
		String numFolders = (String) a_OperatorNode.getParameterValue(NUM_FOLDERS);
		if (numFolders == null) {
			numFolders = DEFAULT_NUM_FOLDERS;
		}
		String randomSeed = (String) a_OperatorNode.getParameterValue(RANDOM_SEED);
		if (randomSeed == null) {
			randomSeed = DEFAULT_RANDOM_SEED;
		}
		String toleranceParameter = (String) a_OperatorNode
				.getParameterValue(TOLERANCE_PARAMETER);
		if (toleranceParameter == null) {
			toleranceParameter = DEFAULT_TOLERANCE_PARAMETER;
		}
		String useRBF = (String) a_OperatorNode.getParameterValue(USE_RBF);
		if (useRBF == null) {
			useRBF = DEFAULT_USE_RBF;
		}
		String smoParameters = buildLogModels+ lowerOrderTerms+ useRBF+featureSpaceNormolization+" -C " + c + " -A "
				+ cacheSize+ " -P " + epsilon + " -E "
				+ exponent +  " -N "
				+ filterType + " -G " + gamma 
				+ " -V " + numFolders + " -W " + randomSeed + " -T "
				+ toleranceParameter;  
		/* Get input bi object from parent node */
		Operator parentOp = (Operator) a_Parents.elementAt(0);
		setInputBIObject(parentOp.getOutputBIObject());
		IBIData aInputBIData = getInputBIObject().getBIData();
		aInputBIData.getMiningStoredData().reset();
		if (!aInputBIData.hasResult()) {
			throw new SysException("No data inputed.");
		}
		
		/* Prepare output data model */
		BIData aOutputBIData = new BIData(getCaseID(), getNodeID());
		aOutputBIData.setTargetAttribute(aInputBIData.getTargetAttribute());
		aOutputBIData.setTransformActionHistory(aInputBIData
				.getTransformActionHistory());
		aOutputBIData.setTargetAttribute(aInputBIData.getTargetAttribute());
		aOutputBIData.setMiningStoredData(aInputBIData.getMiningStoredData());
		BIModel aOutputBIModel = new BIModel(getCaseID(), getNodeID(),
				IBIModel.TYPE_CLASSIFIER);
		
		/* Check attributes */
		MiningAttribute targetAttribute = aInputBIData.getTargetAttribute();
		aOutputBIModel.setTargetAttribute(targetAttribute);
		if (targetAttribute == null) {
			m_SystemMessageHandler
					.appendMessage(
							
							"Categorical Target attribute is missing. Please add target attribute by using Data Set Attribute Node.");
			throw new AppException(
					"Categorical Target attribute is missing. Please add target attribute by using Data Set Attribute Node.");
		} else if (targetAttribute instanceof NumericAttribute) {
			m_SystemMessageHandler.appendMessage( "Attribute \""
					+ targetAttribute.getName() + "\" is not Categorical.");
			throw new AppException("Attribute \"" + targetAttribute.getName()
					+ "\" should be Categorical.");
		}
		
		/* Create MiningSettings object and assign metadata */
		SupervisedMiningSettings miningSettings = new SupervisedMiningSettings();
		miningSettings.setDataSpecification(aInputBIData.getMetaData());
		
		/* Assign settings */
		miningSettings.setTarget(targetAttribute);
		try {
			miningSettings.verifySettings();
		} catch (Exception e) {
			m_SystemMessageHandler.appendMessage(
					"Invalid parameters in building smo model.");
			throw new AppException(
					"Invalid parameters in building the smo model.");
		}
		
		/* Set MiningSettings */
		aOutputBIModel.setMiningSettings(miningSettings);
		
		/* Get default mining algorithm specification from 'algorithms.xml' */
		MiningAlgorithmSpecification miningAlgorithmSpecification = MiningAlgorithmSpecification
				.getMiningAlgorithmSpecification(ALGORITHM_NAME, getNodeInfo());
		if (miningAlgorithmSpecification == null)
			throw new MiningException("Can't find smo classification method.");
		
		/* Get class name from algorithms specification */
		String className = miningAlgorithmSpecification.getClassname();
		if (className == null)
			throw new MiningException("className attribute expected.");
		
		/* Set MiningAlgorithmSpecification */
		miningAlgorithmSpecification.setMAPValue(MAP_WEKA_CLASS_PARAMETERS,
				smoParameters);// 
		aOutputBIModel.setMiningAlgorithmSpecification(miningAlgorithmSpecification);
		displayMiningAlgSpecParameters(miningAlgorithmSpecification);
		
		/* Set and display mining parameters */
		GeneralUtils
				.displayMiningAlgSpecParameters(miningAlgorithmSpecification);
		/* Create algorithm object with default values */
		MiningAlgorithm algorithm = GeneralUtils
				.createMiningAlgorithmInstance(className, this.getClass().getClassLoader());
		algorithm.setMiningInputStream(aInputBIData.getMiningStoredData());
		algorithm.setMiningSettings(miningSettings);
		algorithm.setMiningAlgorithmSpecification(miningAlgorithmSpecification);
		try {
			algorithm.verify();
		} catch (IllegalArgumentException e) {
			throw new MiningException(e.getMessage());
		}
		MiningModel model = algorithm.buildModel();
		m_SystemMessageHandler.appendMessage( Resource
				.srcStr("calculationtime")
				+ " [s]: "
				+ algorithm.getTimeSpentToBuildModel()
				+ Resource.srcStr("ms"));
		m_SystemMessageHandler.nextLine();
		aOutputBIModel.setMiningModel(model);
		aOutputBIModel.setModelName(m_DefaultModelName);
		m_OutputBIObject.setBIData(aOutputBIData);
		m_OutputBIObject.setBIModel(aOutputBIModel);
		
		//a_OperatorNode.setParameterValue("Temporary model", aOutputBIModel.getTempBIModelPath());
		//aOutputBIModel.writeTempBIModel();
	}

	/**
	 * Test if the SMO Operator contains any results.
	 * 
	 * @return true if SMO Operator has result; false otherwise.
	 */
	/*
	 * public boolean hasResult() throws SysException { // TODO Auto-generated
	 * method stub return false; }
	 */
	public boolean hasResult() throws SysException {
		if (m_OutputBIObject != null) {
			return (m_OutputBIObject.hasResult(BIObject.DATA) && m_OutputBIObject
					.hasResult(BIObject.MODEL));
		} else {
			return false;
		}
	}

}

⌨️ 快捷键说明

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