📄 parameterset.java
字号:
/* * YALE - Yet Another Learning Environment * Copyright (C) 2002, 2003 * Simon Fischer, Ralf Klinkenberg, Ingo Mierswa, * Katharina Morik, Oliver Ritthoff * Artificial Intelligence Unit * Computer Science Department * University of Dortmund * 44221 Dortmund, Germany * email: yale@ls8.cs.uni-dortmund.de * 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.performance.PerformanceVector;import edu.udo.cs.yale.operator.Operator;import edu.udo.cs.yale.gui.SwingTools;import edu.udo.cs.yale.Experiment;import edu.udo.cs.yale.tools.LogService;import java.util.Iterator;import java.util.List;import java.util.LinkedList;import java.util.Map;import java.io.File;import java.io.FileWriter;import java.io.PrintWriter;import java.io.IOException;import java.io.BufferedReader;/** A set of parameters generated by a <code>ParameterOptimizationOperator</code>. * @version $Id: ParameterSet.java,v 2.4 2003/06/18 17:57:12 fischer Exp $ */public class ParameterSet extends ResultObjectAdapter implements Saveable { private static class ParameterValue { private String operator; private String parameterKey; private String parameterValue; private ParameterValue(String operator, String parameterKey, String parameterValue) { this.operator = operator; this.parameterKey = parameterKey; this.parameterValue = parameterValue; } public String toString() { return operator + "." + parameterKey + "\t= " + parameterValue; } public void apply(Experiment experiment, Map nameMap) { String opName = (String)nameMap.get(operator); if (opName == null) opName = operator; LogService.logMessage("Setting parameter '"+parameterKey+"' of operator '"+opName+ "' to '"+parameterValue+"'.", LogService.MINIMUM); Operator operator = experiment.getOperator(opName); if (operator == null) { LogService.logMessage("No such operator: '"+opName+"'.", LogService.MINIMUM); } else { operator.getParameters().setParameter(parameterKey, parameterValue); } } } private List parameterValues = new LinkedList(); private PerformanceVector performance; /** Private constructor for the read() method. */ private ParameterSet() { } /** Constructs a new ParameterSet. The three arrays must have equal length. * For each <i>i</i>, the ParameterSet specifies the value <code>values[i]</code> * for the parameter <code>parameters[i]</code> of the operator named * <code>operators[i]</code>. */ public ParameterSet(Operator[] operators, String[] parameters, String[] values, PerformanceVector value) { if ((operators.length != parameters.length) || (operators.length != values.length)) { throw new IllegalArgumentException("The arrays operators, parameters, and values must be of equal size!"); } for (int i = 0; i < operators.length; i++) { parameterValues.add(new ParameterValue(operators[i].getName(), parameters[i], values[i])); } this.performance = value; } public String getName() { return "ParameterSet"; } public String getParameterString() { StringBuffer str = new StringBuffer(); Iterator i = parameterValues.iterator(); while (i.hasNext()) { str.append(i.next().toString()+"\n"); } return str.toString(); } public String toString() { String str = "Parameter set:\n"; str += getParameterString(); str += "\nPerformance: " + performance; return str; } /** Returns the performance of this parameter set. * Attension: This may be null if the ParameterSet was read from a file. */ public PerformanceVector getPerformance() { return performance; } /** Applies all parameters in the set to their operators. * The entries in the nameMap can be * used if the names of the operators in two experiments (one parameter optimisation * experiment and one application experiment) are not the same. * Each entry in nameMap maps the name read from the file to the name of the operator * in the experiment. */ public void applyAll(Experiment experiment, Map nameMap) { Iterator i = parameterValues.iterator(); while (i.hasNext()) { ((ParameterValue)i.next()).apply(experiment, nameMap); } } public void save(File file) throws IOException { write(new PrintWriter(new FileWriter(file))); } public void write(PrintWriter writer) { writer.println("# Generated by "+getClass().getName()); writer.println(getParameterString()); writer.flush(); } /** Reads a parameter set from a file. */ public static ParameterSet read(BufferedReader reader) throws IOException { ParameterSet parameterSet = new ParameterSet(); String line = null; while ((line = reader.readLine()) != null) { line = line.trim(); if ((line.length() == 0) || (line.startsWith("#"))) continue; int colon = line.indexOf("."); int equals = line.indexOf("="); if ((colon < 0) || (equals < 0) || (colon > equals)) throw new IOException("Malformed line: "+line); parameterSet.parameterValues.add(new ParameterValue(line.substring(0, colon).trim(), line.substring(colon+1, equals).trim(), line.substring(equals+1).trim())); } reader.close(); return parameterSet; }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -