weka.core.SetupGenerator Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of multisearch-weka-package Show documentation
Show all versions of multisearch-weka-package Show documentation
Parameter optimization similar to GridSearch.
/*
* 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 3 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, see .
*/
/*
* SetupGenerator.java
* Copyright (C) 2008-2016 University of Waikato, Hamilton, New Zealand
*/
package weka.core;
import weka.core.PropertyPath.Path;
import weka.core.PropertyPath.PropertyContainer;
import weka.core.expressionlanguage.common.IfElseMacro;
import weka.core.expressionlanguage.common.JavaMacro;
import weka.core.expressionlanguage.common.MacroDeclarationsCompositor;
import weka.core.expressionlanguage.common.MathFunctions;
import weka.core.expressionlanguage.common.Primitives.DoubleExpression;
import weka.core.expressionlanguage.common.SimpleVariableDeclarations;
import weka.core.expressionlanguage.common.SimpleVariableDeclarations.VariableInitializer;
import weka.core.expressionlanguage.common.VariableDeclarationsCompositor;
import weka.core.expressionlanguage.core.Node;
import weka.core.expressionlanguage.parser.Parser;
import weka.core.setupgenerator.AbstractParameter;
import weka.core.setupgenerator.AbstractPropertyParameter;
import weka.core.setupgenerator.ListParameter;
import weka.core.setupgenerator.MathParameter;
import weka.core.setupgenerator.Point;
import weka.core.setupgenerator.Space;
import weka.core.setupgenerator.SpaceDimension;
import java.beans.PropertyDescriptor;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.Enumeration;
import java.util.List;
import java.util.Vector;
/**
* Generates different setups of objects (e.g., classifiers or filters) based
* on parameter settings. The parameter settings can be either based on
* mathematical functions; therefore numeric) or chosen from lists (for
* string values, SelectedTags or classnames (with optional parameters).
*
* @author fracpete (fracpete at waikato dot ac dot nz)
* @version $Revision: 4521 $
*/
public class SetupGenerator
implements Serializable, OptionHandler {
/** for serialization. */
private static final long serialVersionUID = 7683008589774888142L;
/** type: mathematical function. */
public static final int TYPE_FUNCTION = 0;
/** type: explicit, comma-separated list of values. */
public static final int TYPE_LIST = 1;
/** type of parameter. */
public static final Tag[] TAGS_TYPE = {
new Tag(TYPE_FUNCTION, "func", "Mathematical function"),
new Tag(TYPE_LIST, "list", "Comma-separated list of values")
};
/** base object. */
protected Serializable m_BaseObject;
/** the parameters. */
protected AbstractParameter[] m_Parameters;
/** whether everything has been initialized. */
protected boolean m_Initialized;
/** the parameter space to use for obtaining the setups from. */
protected Space m_Space;
/**
* Default constructor.
*/
public SetupGenerator() {
super();
m_BaseObject = null;
m_Parameters = new AbstractParameter[0];
reset();
}
/**
* Returns a string describing the object.
*
* @return a description suitable for displaying in the
* explorer/experimenter gui
*/
public String globalInfo() {
return
"Generates different setups of objects (e.g., classifiers or filters) based "
+ "on parameter settings. The parameter settings can be either based on "
+ "mathematical functions; therefore numeric) or chosen from lists (for "
+ "string values, SelectedTags or classnames (with optional parameters).";
}
/**
* Gets an enumeration describing the available options.
*
* @return an enumeration of all the available options.
*/
public Enumeration listOptions() {
Vector result;
Enumeration en;
result = new Vector();
result.addElement(new Option(
"\tThe object to generate the setups for, e.g., a classifier.\n"
+ "\tOptions for the object (in case it is an OptionHandler)\n"
+ "\thave to be provided after the '--' meta-option.",
"W", 1, "-W \"\""));
result.addElement(new Option(
"\tA parameter setup for generating the setups.\n"
+ "\tCan be supplied multiple times.\n"
+ "\t(default: " + AbstractParameter.class.getName() + ")",
"search", 1, "-search "));
result.addElement(new Option(
"",
"", 0, "\nOptions specific to search parameter class '"
+ MathParameter.class.getName() + "' ('-search'):"));
en = new MathParameter().listOptions();
while (en.hasMoreElements())
result.addElement(en.nextElement());
result.addElement(new Option(
"",
"", 0, "\nOptions specific to search parameter class '"
+ ListParameter.class.getName() + "' ('-search'):"));
en = new ListParameter().listOptions();
while (en.hasMoreElements())
result.addElement(en.nextElement());
return result.elements();
}
/**
* returns the options of the current setup.
*
* @return the current options
*/
public String[] getOptions() {
int i;
List result;
String[] options;
String tmpStr;
result = new ArrayList();
result.add("-W");
result.add("" + getBaseObject().getClass().getName());
for (i = 0; i < m_Parameters.length; i++) {
result.add("-search");
tmpStr = m_Parameters[i].getClass().getName() + " "
+ Utils.joinOptions(m_Parameters[i].getOptions());
result.add(tmpStr);
}
if (getBaseObject() instanceof OptionHandler) {
result.add("--");
options = ((OptionHandler) getBaseObject()).getOptions();
for (i = 0; i < options.length; i++)
result.add(options[i]);
}
return result.toArray(new String[result.size()]);
}
/**
* Parses the options for this object.
*
* @param options the options to use
* @throws Exception if setting of options fails
*/
public void setOptions(String[] options) throws Exception {
String tmpStr;
String[] tmpOptions;
Vector search;
int i;
AbstractParameter[] params;
Serializable base;
tmpStr = Utils.getOption('W', options);
if (tmpStr.length() != 0) {
base = (Serializable) Utils.forName(Serializable.class, tmpStr, Utils.partitionOptions(options));
setBaseObject(base);
}
else {
throw new IllegalArgumentException("No base object provided!");
}
search = new Vector();
do {
tmpStr = Utils.getOption("search", options);
if (tmpStr.length() > 0)
search.add(tmpStr);
}
while (tmpStr.length() > 0);
if (search.size() == 0)
throw new IllegalArgumentException("No search parameters provided!");
params = new AbstractParameter[search.size()];
for (i = 0; i < search.size(); i++) {
tmpOptions = Utils.splitOptions(search.get(i));
tmpStr = tmpOptions[0];
tmpOptions[0] = "";
params[i] = (AbstractParameter) Utils.forName(AbstractParameter.class, tmpStr, tmpOptions);
}
setParameters(params);
}
/**
* Returns the tip text for this property.
*
* @return tip text for this property suitable for
* displaying in the explorer/experimenter gui
*/
public String baseObjectFileTipText() {
return "The base object to set the parameters for.";
}
/**
* Sets the base object, can be single object or array of objects.
*
* @param obj the object
*/
public void setBaseObject(Serializable obj) {
m_BaseObject = obj;
reset();
}
/**
* Returns the base object.
*
* @return the object
*/
public Serializable getBaseObject() {
return m_BaseObject;
}
/**
* Returns the tip text for this property.
*
* @return tip text for this property suitable for
* displaying in the explorer/experimenter gui
*/
public String parametersFileTipText() {
return "The parameter definitions.";
}
/**
* Sets the parameters to use as basis for the setups.
*
* @param value the parameters
*/
public void setParameters(AbstractParameter[] value) {
m_Parameters = value;
reset();
}
/**
* Returns the current parameters.
*
* @return the parameters
*/
public AbstractParameter[] getParameters() {
return m_Parameters;
}
/**
* Resets the generation.
*/
public void reset() {
m_Initialized = false;
m_Space = null;
}
/**
* Updates the space to use for the setup generation.
*
* @param value the space to use
*/
public void setSpace(Space value) {
m_Space = value;
m_Initialized = false;
}
/**
* Returns the space currently in use.
*
* @return the space
*/
public Space getSpace() {
initialize();
return m_Space;
}
/**
* Performs all the necessary initializations.
*/
protected void initialize() {
SpaceDimension[] dims;
int i;
if (m_Initialized)
return;
// a few sanity checks
//if (m_Parameters.length == 0)
// throw new IllegalStateException("No parameters set!");
if (m_BaseObject == null)
throw new IllegalStateException("No base object set!");
// setup space, if necessary
if (m_Space == null) {
dims = new SpaceDimension[m_Parameters.length];
for (i = 0; i < m_Parameters.length; i++) {
try {
dims[i] = new SpaceDimension(m_Parameters[i]);
}
catch (Exception e) {
e.printStackTrace();
throw new IllegalStateException(
"Error initializing space dimension #" + (i+1) + ": " + e);
}
}
m_Space = new Space(dims);
}
m_Initialized = true;
}
/**
* evalutes the expression for the current iteration.
*
* @param values the current iteration values (from 'min' to 'max' with
* stepsize 'step') for all dimensions
* @return the generated value, NaN if the evaluation fails
*/
public Point