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

📄 experimentlogoperator.java

📁 一个很好的LIBSVM的JAVA源码。对于要研究和改进SVM算法的学者。可以参考。来自数据挖掘工具YALE工具包。
💻 JAVA
字号:
/*
 *  YALE - Yet Another Learning Environment
 *  Copyright (C) 2001-2004
 *      Simon Fischer, Ralf Klinkenberg, Ingo Mierswa, 
 *          Katharina Morik, Oliver Ritthoff
 *      Artificial Intelligence Unit
 *      Computer Science Department
 *      University of Dortmund
 *      44221 Dortmund,  Germany
 *  email: yale-team@lists.sourceforge.net
 *  web:   http://yale.cs.uni-dortmund.de/
 *
 *  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., 59 Temple Place, Suite 330, Boston, MA 02111-1307
 *  USA.
 */
package edu.udo.cs.yale.operator;

import edu.udo.cs.yale.operator.ResultObjectAdapter;
import edu.udo.cs.yale.operator.parameter.*;
import edu.udo.cs.yale.tools.LogService;
import edu.udo.cs.yale.gui.Plotter;

import java.util.StringTokenizer;
import java.util.List;
import java.util.LinkedList;
import java.util.Iterator;
import java.io.FileWriter;
import java.io.PrintWriter;
import java.io.IOException;
import java.awt.Component;

/**
 *  This operator records almost arbitrary data. It can written to a file which can be
 *  read e.g. by gnuplot or plotted by the GUI.<br/>
 *
 *  Parameters in the list <code>log</code> are interpreted as follows: The <var>key</var> gives
 *  the name for the column name (e.g. for use in the plotter). The <var>value</var> specifies 
 *  where to retrieve the value from. This is best explained by an example:
 *  <ul>
 *   <li>If the value is <code>operator.Evaluator.value.absolute</code>, the ExperimentLogOperator looks 
 *       up the operator with the name <code>Evaluator</code>. If this operator is a 
 *       {@link edu.udo.cs.yale.operator.performance.PerformanceEvaluator}, it has a value named
 *        <var>absolute</var> which gives the absolute error of the last evaluation. This value
 *       is queried by the ExperimentLogOperator</li>
 *   <li>If the value is <code>operator.SVMLearner.parameter.C</code>, the ExperimentLogOperator looks
 *       up the parameter <var>C</var> of the operator named <code>SVMLearner</code>.</li>
 *  </ul>
 *  Each time the ExperimentLogOperator is applied, all the values and parameters specified by the
 *  list <var>log</var> are collected and stored in a data row. When the experiment finishes, the
 *  operator writes the collected data rows to a file (if specified). In GUI mode a 2D plot is automatically
 *  generated and displayed in the result viewer.
 *  <br/>
 *  Please refer to 
 *  section {@yale.ref sec:parameter_optimization|Advanced experiments/Parameter and performance analysis} 
 *  for an example application. 
 *
 *  @yale.xmlclass ExperimentLog
 *  @yale.todo Use IOObjects for logging as well (e.g. {@link edu.udo.cs.yale.operator.performance.PerformanceVector})
 *  @author Simon
 *  @version $Id: ExperimentLogOperator.java,v 2.19 2004/08/27 11:57:34 ingomierswa Exp $
 */
public class ExperimentLogOperator extends Operator {

    private static final Class[] OUTPUT_CLASSES = { };

    private String[] valueNames;

    private Object fetchValue(String name) {
	StringTokenizer reader = new StringTokenizer(name, ".");
	String type = reader.nextToken();
	if (type.equals("operator")) {
	    String opName = reader.nextToken();
	    Operator operator = getExperiment().getOperator(opName);
	    if (operator != null) {
		type = reader.nextToken();
		if (type.equals("value")) {
		    String valueName = reader.nextToken();
		    double value = operator.getValue(valueName);
		    if (Double.isNaN(value)) {
			LogService.logMessage(getName()+": No such value in '"+name+"'", LogService.WARNING);
			return null;
		    }
		    return new Double(value);
		} else if (type.equals("parameter")) {
		    String parameterName = reader.nextToken();
		    Object value = operator.getParameter(parameterName);
		    if (value == null) {
			LogService.logMessage(getName()+": No such parameter in '"+name+"'", LogService.WARNING);
			return null;
		    }
		    return value;
		} else {
		    LogService.logMessage(getName()+": Unknown token '"+type+"' in '"+name+"'",LogService.WARNING);
		    return null;
		}
	    } else {
		LogService.logMessage(getName()+": Unknown operator '"+opName+"' in '"+name+"'",LogService.WARNING);
		return null;
	    }
	} else {
	    LogService.logMessage(getName()+": Unknown token '"+type+"' in '"+name+"'", LogService.WARNING);
	    return null;
	}
    }


    public void experimentStarts() {
	List parameters = getParameterList("log");
	String columnNames[] = new String[parameters.size()];
	valueNames = new String[parameters.size()];
	Iterator i = parameters.iterator();
	int j = 0;
	while (i.hasNext()) {
	    Object[] parameter = (Object[])i.next();
	    columnNames[j] = (String)parameter[0];
	    valueNames[j]  = (String)parameter[1];
	    j++;
	}
	getExperiment().getStatistics(getName()).init(columnNames);
    }

    public Class[] getInputClasses() {
	return new Class[0];
    }

    public Class[] getOutputClasses() {
	return OUTPUT_CLASSES;
    }

    public IOObject[] apply() {
	fetchAllValues();
	return new IOObject[] {};
    }

    private void fetchAllValues() {
	Object[] row = new Object[valueNames.length];
	for (int i = 0; i < valueNames.length; i++) {
	    Object value = fetchValue(valueNames[i]);
	    row[i] = value;
	}
	getExperiment().getStatistics(getName()).add(row);
    }

    public void experimentFinished() {
	super.experimentFinished();

	String filename = getParameterAsString("filename");
	if (filename != null) {
	    LogService.logMessage(getName()+": writing data to '"+filename+"'", LogService.INIT);
	    try {
		PrintWriter out = new PrintWriter(new FileWriter(getExperiment().resolveFileName(filename)));
		getExperiment().getStatistics(getName()).write(out);
		out.close();
	    } catch (IOException e) {
		LogService.logMessage(getName() + ": Could not write to file ["+filename+"]", LogService.ERROR);
	    }
	}
    }


    public List getParameterTypes() {
	List types = super.getParameterTypes();
	ParameterType type = new ParameterTypeFile("filename", "File to save the data to.", true);
	type.setExpert(false);
	types.add(type);
	type = new ParameterTypeList("log", "List of key value pairs where the key is the column name and the value specifies the experiment value to log.", new ParameterTypeValue("column_name", "operator.OPERATORNAME.[value|parameter].VALUE_NAME"));
	type.setExpert(false);
	types.add(type);
	return types;
    }
}

⌨️ 快捷键说明

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