Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
   /*
    *   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 <http://www.gnu.org/licenses/>.
   */
  
  /*
   * GridSearch.java
   * Copyright (C) 2006-2010 University of Waikato, Hamilton, New Zealand
   */
  
  package weka.classifiers.meta;
  
  import java.io.File;
  import java.util.HashMap;
  import java.util.Random;
  import java.util.Vector;
  
  import weka.core.Debug;
  import weka.core.Option;
  import weka.core.Tag;
  import weka.core.Utils;
Performs a grid search of parameter pairs for the a classifier (Y-axis, default is LinearRegression with the "Ridge" parameter) and the PLSFilter (X-axis, "# of Components") and chooses the best pair found for the actual predicting.

The initial grid is worked on with 2-fold CV to determine the values of the parameter pairs for the selected type of evaluation (e.g., accuracy). The best point in the grid is then taken and a 10-fold CV is performed with the adjacent parameter pairs. If a better pair is found, then this will act as new center and another 10-fold CV will be performed (kind of hill-climbing). This process is repeated until no better pair is found or the best pair is on the border of the grid.
In case the best pair is on the border, one can let GridSearch automatically extend the grid and continue the search. Check out the properties 'gridIsExtendable' (option '-extend-grid') and 'maxGridExtensions' (option '-max-grid-extensions <num>').

GridSearch can handle doubles, integers (values are just cast to int) and booleans (0 is false, otherwise true). float, char and long are supported as well.

The best filter/classifier setup can be accessed after the buildClassifier call via the getBestFilter/getBestClassifier methods.
Note on the implementation: after the data has been passed through the filter, a default NumericCleaner filter is applied to the data in order to avoid numbers that are getting too small and might produce NaNs in other schemes.

Note: with -num-slots/numExecutionSlots you can specify how many setups are evaluated in parallel, taking advantage of multi-cpu/core architectures.

Valid options are:

 -E <CC|RMSE|RRSE|MAE|RAE|COMB|ACC|KAP|WAUC>
  Determines the parameter used for evaluation:
  CC = Correlation coefficient
  RMSE = Root mean squared error
  RRSE = Root relative squared error
  MAE = Mean absolute error
  RAE = Root absolute error
  COMB = Combined = (1-abs(CC)) + RRSE + RAE
  ACC = Accuracy
  WAUC = Weighted AUC
  KAP = Kappa
  (default: CC)
 -y-property <option>
  The Y option to test (without leading dash).
  (default: classifier.ridge)
 -y-min <num>
  The minimum for Y.
  (default: -10)
 -y-max <num>
  The maximum for Y.
  (default: +5)
 -y-step <num>
  The step size for Y.
  (default: 1)
 -y-base <num>
  The base for Y.
  (default: 10)
 -y-expression <expr>
  The expression for Y.
  Available parameters:
   BASE
   FROM
   TO
   STEP
   I - the current iteration value
   (from 'FROM' to 'TO' with stepsize 'STEP')
  (default: 'pow(BASE,I)')
 -filter <filter specification>
  The filter to use (on X axis). Full classname of filter to include, 
  followed by scheme options.
  (default: weka.filters.supervised.attribute.PLSFilter)
 -x-property <option>
  The X option to test (without leading dash).
  (default: filter.numComponents)
 -x-min <num>
  The minimum for X.
  (default: +5)
 -x-max <num>
  The maximum for X.
  (default: +20)
 -x-step <num>
  The step size for X.
  (default: 1)
 -x-base <num>
  The base for X.
  (default: 10)
 -x-expression <expr>
  The expression for the X value.
  Available parameters:
   BASE
   MIN
   MAX
   STEP
   I - the current iteration value
   (from 'FROM' to 'TO' with stepsize 'STEP')
  (default: 'pow(BASE,I)')
 -extend-grid
  Whether the grid can be extended.
  (default: no)
 -max-grid-extensions <num>
  The maximum number of grid extensions (-1 is unlimited).
  (default: 3)
 -sample-size <num>
  The size (in percent) of the sample to search the inital grid with.
  (default: 100)
 -traversal <ROW-WISE|COLUMN-WISE>
  The type of traversal for the grid.
  (default: COLUMN-WISE)
 -log-file <filename>
  The log file to log the messages to.
  (default: none)
 -num-slots <num>
  Number of execution slots.
  (default 1 - i.e. no parallelism)
 -S <num>
  Random number seed.
  (default 1)
 -D
  If set, classifier is run in debug mode and
  may output additional info to the console
 -W
  Full name of base classifier.
  (default: weka.classifiers.functions.LinearRegression)
 
 Options specific to classifier weka.classifiers.functions.LinearRegression:
 
 -D
  Produce debugging output.
  (default no debugging output)
 -S <number of selection method>
  Set the attribute selection method to use. 1 = None, 2 = Greedy.
  (default 0 = M5' method)
 -C
  Do not try to eliminate colinear attributes.
 
 -R <double>
  Set ridge parameter (default 1.0e-8).
 
 
 Options specific to filter weka.filters.supervised.attribute.PLSFilter ('-filter'):
 
 -D
  Turns on output of debugging information.
 -C <num>
  The number of components to compute.
  (default: 20)
 -U
  Updates the class attribute as well.
  (default: off)
 -M
  Turns replacing of missing values on.
  (default: off)
 -A <SIMPLS|PLS1>
  The algorithm to use.
  (default: PLS1)
 -P <none|center|standardize>
  The type of preprocessing that is applied to the data.
  (default: center)
Examples:
  • Optimizing SMO with RBFKernel (C and gamma)
    • Set the evaluation to Accuracy.
    • Set the filter to weka.filters.AllFilter since we don't need any special data processing and we don't optimize the filter in this case (data gets always passed through filter!).
    • Set weka.classifiers.functions.SMO as classifier with weka.classifiers.functions.supportVector.RBFKernel as kernel.
    • Set the XProperty to "classifier.c", XMin to "1", XMax to "16", XStep to "1" and the XExpression to "I". This will test the "C" parameter of SMO for the values from 1 to 16.
    • Set the YProperty to "classifier.kernel.gamma", YMin to "-5", YMax to "2", YStep to "1" YBase to "10" and YExpression to "pow(BASE,I)". This will test the gamma of the RBFKernel with the values 10^-5, 10^-4,..,10^2.
  • Optimizing PLSFilter with LinearRegression (# of components and ridge) - default setup
    • Set the evaluation to Correlation coefficient.
    • Set the filter to weka.filters.supervised.attribute.PLSFilter.
    • Set weka.classifiers.functions.LinearRegression as classifier and use no attribute selection and no elimination of colinear attributes.
    • Set the XProperty to "filter.numComponents", XMin to "5", XMax to "20" (this depends heavily on your dataset, should be no more than the number of attributes!), XStep to "1" and XExpression to "I". This will test the number of components the PLSFilter will produce from 5 to 20.
    • Set the YProperty to "classifier.ridge", XMin to "-10", XMax to "5", YStep to "1" and YExpression to "pow(BASE,I)". This will try ridge parameters from 10^-10 to 10^5.
General notes:
  • Turn the debug flag on in order to see some progress output in the console
  • If you want to view the fitness landscape that GridSearch explores, select a log file. This log will then contain Gnuplot data and script block for viewing the landscape. Just copy paste those blocks into files named accordingly and run Gnuplot with them.

Author(s):
Bernhard Pfahringer (bernhard at cs dot waikato dot ac dot nz)
Geoff Holmes (geoff at cs dot waikato dot ac dot nz)
fracpete (fracpete at waikato dot ac dot nz)
Version:
$Revision: 9734 $
See also:
weka.filters.supervised.attribute.PLSFilter
weka.classifiers.functions.LinearRegression
weka.filters.unsupervised.attribute.NumericCleaner
 
 public class GridSearch
   implements AdditionalMeasureProducerSummarizable {

  
a serializable version of Point2D.Double.

 
   protected static class PointDouble
     extends java.awt.geom.Point2D.Double
     implements SerializableRevisionHandler {

    
for serialization.
 
     private static final long serialVersionUID = 7151661776161898119L;
    
    
the default constructor.

Parameters:
x the x value of the point
y the y value of the point
 
     public PointDouble(double xdouble y) {
       super(xy);
     }

    
Determines whether or not two points are equal.

Parameters:
obj an object to be compared with this PointDouble
Returns:
true if the object to be compared has the same values; false otherwise.
 
     public boolean equals(Object obj) {
       PointDouble 	pd;
       
       pd = (PointDoubleobj;
       
       return (Utils.eq(this.getX(), pd.getX()) && Utils.eq(this.getY(), pd.getY()));
     }
    
    
returns a string representation of the Point.

Returns:
the point as string
 
     public String toString() {
       return super.toString().replaceAll(".*\\[""[");
     }
    
    
Returns the revision string.

Returns:
the revision
 
     public String getRevision() {
       return RevisionUtils.extract("$Revision: 9734 $");
     }
   }

  
a serializable version of Point.

See also:
java.awt.Point
 
   protected static class PointInt
     extends java.awt.Point
     implements SerializableRevisionHandler {

    
for serialization.
 
     private static final long serialVersionUID = -5900415163698021618L;

    
the default constructor.

Parameters:
x the x value of the point
y the y value of the point
 
     public PointInt(int xint y) {
       super(xy);
     }
    
    
returns a string representation of the Point.

Returns:
the point as string
 
     public String toString() {
       return super.toString().replaceAll(".*\\[""[");
     }
    
    
Returns the revision string.

Returns:
the revision
 
     public String getRevision() {
       return RevisionUtils.extract("$Revision: 9734 $");
     }
   }
  
  
for generating the parameter pairs in a grid.
 
   protected static class Grid
     implements SerializableRevisionHandler {

    
for serialization.
 
     private static final long serialVersionUID = 7290732613611243139L;
    
    
the minimum on the X axis.
 
     protected double m_MinX;
    
    
the maximum on the X axis.
 
     protected double m_MaxX;
    
    
the step size for the X axis.
 
     protected double m_StepX;

    
the label for the X axis.
 
     protected String m_LabelX;
    
    
the minimum on the Y axis.
 
     protected double m_MinY;
    
    
the maximum on the Y axis.
 
     protected double m_MaxY;
    
    
the step size for the Y axis.
 
     protected double m_StepY;

    
the label for the Y axis.
 
     protected String m_LabelY;
    
    
the number of points on the X axis.
 
     protected int m_Width;
    
    
the number of points on the Y axis.
 
     protected int m_Height;
    
    
initializes the grid.

Parameters:
minX the minimum on the X axis
maxX the maximum on the X axis
stepX the step size for the X axis
minY the minimum on the Y axis
maxY the maximum on the Y axis
stepY the step size for the Y axis
 
     public Grid(double minXdouble maxXdouble stepX
 	        double minYdouble maxYdouble stepY) {
       this(minXmaxXstepX""minYmaxYstepY"");
     }

    
    
initializes the grid.

Parameters:
minX the minimum on the X axis
maxX the maximum on the X axis
stepX the step size for the X axis
labelX the label for the X axis
minY the minimum on the Y axis
maxY the maximum on the Y axis
stepY the step size for the Y axis
labelY the label for the Y axis
 
     public Grid(double minXdouble maxXdouble stepXString labelX,
 	        double minYdouble maxYdouble stepYString labelY) {
 
       super();
       
          = minX;
          = maxX;
         = stepX;
        = labelX;
          = minY;
          = maxY;
         = stepY;
        = labelY;
        = (int) StrictMath.round(( - ) / ) + 1;
         = (int) StrictMath.round(( - ) / ) + 1;
       
       // is min < max?
       if ( >= )
 	throw new IllegalArgumentException("XMin must be smaller than XMax!");
       if ( >= )
 	throw new IllegalArgumentException("YMin must be smaller than YMax!");
       
       // steps positive?
       if ( <= 0)
 	throw new IllegalArgumentException("XStep must be a positive number!");
       if ( <= 0)
 	throw new IllegalArgumentException("YStep must be a positive number!");
       
       // check borders
       if (!Utils.eq( + (-1)*))
 	    "X axis doesn't match! Provided max: " +  
 	    + ", calculated max via min and step size: " 
 	    + ( + (-1)*));
       if (!Utils.eq( + (-1)*))
 	    "Y axis doesn't match! Provided max: " +  
 	    + ", calculated max via min and step size: " 
 	    + ( + (-1)*));
     }

    
Tests itself against the provided grid object.

Parameters:
o the grid object to compare against
Returns:
if the two grids have the same setup
 
     public boolean equals(Object o) {
       boolean	result;
       Grid	g;
       
       g = (Grido;
       
       result =    (width() == g.width())
                && (height() == g.height())
                && (getMinX() == g.getMinX())
                && (getMinY() == g.getMinY())
                && (getStepX() == g.getStepX())
                && (getStepY() == g.getStepY())
                && getLabelX().equals(g.getLabelX())
                && getLabelY().equals(g.getLabelY());
       
       return result;
     }
    
    
returns the left border.

Returns:
the left border
 
     public double getMinX() {
       return ;
     }
    
    
returns the right border.

Returns:
the right border
 
     public double getMaxX() {
       return ;
     }
    
    
returns the step size on the X axis.

Returns:
the step size
 
     public double getStepX() {
       return ;
     }
    
    
returns the label for the X axis.

Returns:
the label
 
     public String getLabelX() {
       return ;
     }
    
    
returns the bottom border.

Returns:
the bottom border
 
     public double getMinY() {
       return ;
     }
    
    
returns the top border.

Returns:
the top border
 
     public double getMaxY() {
       return ;
     }
    
    
returns the step size on the Y axis.

Returns:
the step size
 
     public double getStepY() {
       return ;
     }
    
    
returns the label for the Y axis.

Returns:
the label
 
     public String getLabelY() {
       return ;
     }
    
    
returns the number of points in the grid on the Y axis (incl. borders)

Returns:
the number of points in the grid on the Y axis
 
     public int height() {
       return ;
     }
    
    
returns the number of points in the grid on the X axis (incl. borders)

Returns:
the number of points in the grid on the X axis
 
     public int width() {
       return ;
     }

    
returns the values at the given point in the grid.

Parameters:
x the x-th point on the X axis
y the y-th point on the Y axis
Returns:
the value pair at the given position
 
     public PointDouble getValues(int xint y) {
       if (x >= width())
 	throw new IllegalArgumentException("Index out of scope on X axis (" + x + " >= " + width() + ")!");
       if (y >= height())
 	throw new IllegalArgumentException("Index out of scope on Y axis (" + y + " >= " + height() + ")!");
       
       return new PointDouble( + *x + *y);
     }

    
returns the closest index pair for the given value pair in the grid.

Parameters:
values the values to get the indices for
Returns:
the closest indices in the grid
 
     public PointInt getLocation(PointDouble values) {
       PointInt	result;
       int	x;
       int	y;
       double	distance;
       double	currDistance;
       int	i;
 
       // determine x
       x        = 0;
       distance = ;
       for (i = 0; i < width(); i++) {
 	currDistance = StrictMath.abs(values.getX() - getValues(i, 0).getX());
 	if (Utils.sm(currDistancedistance)) {
 	  distance = currDistance;
 	  x        = i;
 	}
       }
       
       // determine y
       y        = 0;
       distance = ;
       for (i = 0; i < height(); i++) {
 	currDistance = StrictMath.abs(values.getY() - getValues(0, i).getY());
 	if (Utils.sm(currDistancedistance)) {
 	  distance = currDistance;
 	  y        = i;
 	}
       }
       
       result = new PointInt(xy);
       return result;
     }

    
checks whether the given values are on the border of the grid.

Parameters:
values the values to check
Returns:
true if the the values are on the border
 
     public boolean isOnBorder(PointDouble values) {
       return isOnBorder(getLocation(values));
     }

    
checks whether the given location is on the border of the grid.

Parameters:
location the location to check
Returns:
true if the the location is on the border
 
     public boolean isOnBorder(PointInt location) {
       if (location.getX() == 0)
 	return true;
       else if (location.getX() == width() - 1)
 	return true;
       if (location.getY() == 0)
 	return true;
       else if (location.getY() == height() - 1)
 	return true;
       else
 	return false;
     }
    
    
returns a subgrid with the same step sizes, but different borders.

Parameters:
top the top index
left the left index
bottom the bottom index
right the right index
Returns:
the Sub-Grid
 
     public Grid subgrid(int topint leftint bottomint right) {
       return new Grid(
                    getValues(lefttop).getX(), getValues(righttop).getX(), getStepX(), getLabelX(),
                    getValues(leftbottom).getY(), getValues(lefttop).getY(), getStepY(), getLabelY());
     }
    
    
returns an extended grid that encompasses the given point (won't be on the border of the grid).

Parameters:
values the point that the grid should contain
Returns:
the extended grid
 
     public Grid extend(PointDouble values) {
       double	minX;
       double	maxX;
       double	minY;
       double	maxY;
       double	distance;
       Grid	result;
       
       // left
       if (Utils.smOrEq(values.getX(), getMinX())) {
 	distance = getMinX() - values.getX();
 	// exactly on grid point?
 	if (Utils.eq(distance, 0))
 	  minX = getMinX() - getStepX() * (StrictMath.round(distance / getStepX()) + 1);
 	else
 	  minX = getMinX() - getStepX() * (StrictMath.round(distance / getStepX()));
       }
       else {
 	minX = getMinX();
       }
       
       // right
       if (Utils.grOrEq(values.getX(), getMaxX())) {
 	distance = values.getX() - getMaxX();
 	// exactly on grid point?
 	if (Utils.eq(distance, 0))
 	  maxX = getMaxX() + getStepX() * (StrictMath.round(distance / getStepX()) + 1);
 	else
 	  maxX = getMaxX() + getStepX() * (StrictMath.round(distance / getStepX()));
       }
       else {
 	maxX = getMaxX();
       }
       
       // bottom
       if (Utils.smOrEq(values.getY(), getMinY())) {
 	distance = getMinY() - values.getY();
 	// exactly on grid point?
 	if (Utils.eq(distance, 0))
 	  minY = getMinY() - getStepY() * (StrictMath.round(distance / getStepY()) + 1);
 	else
 	  minY = getMinY() - getStepY() * (StrictMath.round(distance / getStepY()));
       }
       else {
 	minY = getMinY();
       }
       
       // top
       if (Utils.grOrEq(values.getY(), getMaxY())) {
 	distance = values.getY() - getMaxY();
 	// exactly on grid point?
 	if (Utils.eq(distance, 0))
 	  maxY = getMaxY() + getStepY() * (StrictMath.round(distance / getStepY()) + 1);
 	else
 	  maxY = getMaxY() + getStepY() * (StrictMath.round(distance / getStepY()));
       }
       else {
 	maxY = getMaxY();
       }
       
       result = new Grid(minXmaxXgetStepX(), getLabelX(), minYmaxYgetStepY(), getLabelY());
       
       // did the grid really extend?
       if (equals(result))
 	throw new IllegalStateException("Grid extension failed!");
       
       return result;
     }
    
    
returns an Enumeration over all pairs in the given row.

Parameters:
y the row to retrieve
Returns:
an Enumeration over all pairs
See also:
getValues(int,int)
 
     public Enumeration<PointDoublerow(int y) {
       Vector	result;
       int	i;
       
       result = new Vector();
       
       for (i = 0; i < width(); i++)
 	result.add(getValues(iy));
       
       return result.elements();
     }
    
    
returns an Enumeration over all pairs in the given column.

Parameters:
x the column to retrieve
Returns:
an Enumeration over all pairs
See also:
getValues(int,int)
 
     public Enumeration<PointDoublecolumn(int x) {
       Vector	result;
       int	i;
       
       result = new Vector();
       
       for (i = 0; i < height(); i++)
 	result.add(getValues(xi));
       
       return result.elements();
     }
    
    
returns a string representation of the grid.

Returns:
a string representation
 
     public String toString() {
       String	result;
       
       result  = "X: " +  + " - " +  + ", Step " + ;
       if (.length() != 0)
 	result += " (" +  + ")";
       result += "\n";
       
       result += "Y: " +  + " - " +  + ", Step " + ;
       if (.length() != 0)
 	result += " (" +  + ")";
       result += "\n";
 
       result += "Dimensions (Rows x Columns): " + height() + " x " + width();
       
       return result;
     }
    
    
Returns the revision string.

Returns:
the revision
 
     public String getRevision() {
       return RevisionUtils.extract("$Revision: 9734 $");
     }
   }
  
  
  
A helper class for storing the performance of a values-pair. Can be sorted with the PerformanceComparator class.

 
   protected static class Performance
     implements SerializableRevisionHandler {

    
for serialization.
 
     private static final long serialVersionUID = -4374706475277588755L;
    
    
the value pair the classifier was built with.
 
     protected PointDouble m_Values;
    
    
the Correlation coefficient.
 
     protected double m_CC;
    
    
the Root mean squared error.
 
     protected double m_RMSE;
    
    
the Root relative squared error.
 
     protected double m_RRSE;
    
    
the Mean absolute error.
 
     protected double m_MAE;
    
    
the Relative absolute error.
 
     protected double m_RAE;
    
    
the Accuracy.
 
     protected double m_ACC;

    
The weighted AUC value.
 
     protected double m_wAUC;
    
    
the kappa value.
 
     protected double m_Kappa;
    
    
initializes the performance container.

Parameters:
values the values-pair
evaluation the evaluation to extract the performance measures from
Throws:
java.lang.Exception if retrieving of measures fails
 
     public Performance(PointDouble valuesEvaluation evaluationthrows Exception {
       super();
       
        = values;
       
         = evaluation.rootMeanSquaredError();
         = evaluation.rootRelativeSquaredError();
          = evaluation.meanAbsoluteError();
          = evaluation.relativeAbsoluteError();
 
       try {
          = evaluation.weightedAreaUnderROC();
       }
       catch (Exception e) {
          = .;
       }
       try {
 	 = evaluation.correlationCoefficient();
       }
       catch (Exception e) {
       }
       try {
 	 = evaluation.pctCorrect();
       }
       catch (Exception e) {
       }
       try {
 	 = evaluation.kappa();
       }
       catch (Exception e) {
       }
     }
    
    
returns the performance measure.

Parameters:
evaluation the type of measure to return
Returns:
the performance measure
 
     public double getPerformance(int evaluation) {
       double	result;
       
       result = .;
       
       switch (evaluation) {
 	  result = ;
 	  break;
 	  result = ;
 	  break;
 	  result = ;
 	  break;
 	  result = ;
 	  break;
 	  result = ;
 	  break;
 	  result = (1 - StrictMath.abs()) +  + ;
 	  break;
 	  result = ;
 	  break;
	  result = ;
	  break;
      case :
          result = ;
          break;
	default:
	  throw new IllegalArgumentException("Evaluation type '" + evaluation + "' not supported!");
      }
      
      return result;
    }
    
    
returns the values-pair for this performance.

Returns:
the values-pair
    public PointDouble getValues() {
      return ;
    }
    
    
returns a string representation of this performance object.

Parameters:
evaluation the type of performance to return
Returns:
a string representation
    public String toString(int evaluation) {
      String	result;
      
      result =   "Performance (" + getValues() + "): " 
      	       + getPerformance(evaluation
      	       + " (" + new SelectedTag(evaluation) + ")";
      
      return result;
    }
    
    
returns a Gnuplot string of this performance object.

Parameters:
evaluation the type of performance to return
Returns:
the gnuplot string (x, y, z)
    public String toGnuplot(int evaluation) {
      String	result;
      
      result =   getValues().getX() + "\t" 
      	       + getValues().getY() + "\t"
      	       + getPerformance(evaluation);
      
      return result;
    }
    
    
returns a string representation of this performance object.

Returns:
a string representation
    public String toString() {
      String	result;
      int	i;
      
      result = "Performance (" + getValues() + "): ";
      
      for (i = 0; i < .i++) {
	if (i > 0)
	  result += ", ";
        result +=   getPerformance([i].getID()) 
        	  + " (" + new SelectedTag([i].getID(), ) + ")";
      }
      
      return result;
    }
    
    
Returns the revision string.

Returns:
the revision
    public String getRevision() {
      return RevisionUtils.extract("$Revision: 9734 $");
    }
  }
  
  
A concrete Comparator for the Performance class.

  protected static class PerformanceComparator
    implements Comparator<Performance>, SerializableRevisionHandler {
    
    
for serialization.
    private static final long serialVersionUID = 6507592831825393847L;
    
    
the performance measure to use for comparison.

    protected int m_Evaluation;
    
    
initializes the comparator with the given performance measure.

Parameters:
evaluation the performance measure to use
See also:
GridSearch.TAGS_EVALUATION
    public PerformanceComparator(int evaluation) {
      super();
      
       = evaluation;
    }
    
    
returns the performance measure that's used to compare the objects.

Returns:
the performance measure
See also:
GridSearch.TAGS_EVALUATION
    public int getEvaluation() {
      return ;
    }
    
    
Compares its two arguments for order. Returns a negative integer, zero, or a positive integer as the first argument is less than, equal to, or greater than the second.

Parameters:
o1 the first performance
o2 the second performance
Returns:
the order
    public int compare(Performance o1Performance o2) {
      int	result;
      double	p1;
      double	p2;
      
      p1 = o1.getPerformance(getEvaluation());
      p2 = o2.getPerformance(getEvaluation());
      
      if (Utils.sm(p1p2))
	result = -1;
      else if (Utils.gr(p1p2))
	result = 1;
      else
	result = 0;
      // only correlation coefficient/accuracy/kappa obey to this order, for the
      // errors (and the combination of all three), the smaller the number the
      // better -> hence invert them
      if (    (getEvaluation() != 
           && (getEvaluation() != 
           && (getEvaluation() != 
           && (getEvaluation() != ) )
	result = -result;
      return result;
    }
    
    
Indicates whether some other object is "equal to" this Comparator.

Parameters:
obj the object to compare with
Returns:
true if the same evaluation type is used
    public boolean equals(Object obj) {
      if (!(obj instanceof PerformanceComparator))
	throw new IllegalArgumentException("Must be PerformanceComparator!");
      
      return ( == ((PerformanceComparatorobj).);
    }
    
    
Returns the revision string.

Returns:
the revision
    public String getRevision() {
      return RevisionUtils.extract("$Revision: 9734 $");
    }
  }
  
  
Generates a 2-dim array for the performances from a grid for a certain type. x-min/y-min is in the bottom-left corner, i.e., getTable()[0][0] returns the performance for the x-min/y-max pair.
 x-min     x-max
 |-------------|
                - y-max
                |
                |
                - y-min
 
  protected static class PerformanceTable 
    implements SerializableRevisionHandler {
    
    
for serialization.
    private static final long serialVersionUID = 5486491313460338379L;

    
the owning classifier.
    protected GridSearch m_Owner;
    
    
the corresponding grid.
    protected Grid m_Grid;
    
    
the performances.
    protected Vector<Performancem_Performances;
    
    
the type of performance the table was generated for.
    protected int m_Type;
    
    
the table with the values.
    protected double[][] m_Table;
    
    
the minimum performance.
    protected double m_Min;
    
    
the maximum performance.
    protected double m_Max;
    
    
initializes the table.

Parameters:
owner the owning GridSearch
grid the underlying grid
performances the performances
type the type of performance
    public PerformanceTable(GridSearch ownerGrid gridVector<Performanceperformancesint type) {
      super();
      
              = owner;
               = grid;
               = type;
       = performances;
      
      generate();
    }
    
    
generates the table.
    protected void generate() {
      Performance 	perf;
      int 		i;
      PointInt 		location;
      
       = new double[getGrid().height()][getGrid().width()];
         = 0;
         = 0;
      
      for (i = 0; i < getPerformances().size(); i++) {
	perf     = (PerformancegetPerformances().get(i);
	location = getGrid().getLocation(perf.getValues());
	[getGrid().height() - (intlocation.getY() - 1][(intlocation.getX()] = perf.getPerformance(getType());
	// determine min/max
	if (i == 0) {
	   = ;
	else {
	  if (perf.getPerformance() < )
	     = perf.getPerformance();
	  if (perf.getPerformance() > )
	     = perf.getPerformance();
      }
    }
    
    
returns the corresponding grid.

Returns:
the underlying grid
    public Grid getGrid() {
      return ;
    }

    
returns the underlying performances.

Returns:
the underlying performances
    public Vector<PerformancegetPerformances() {
      return ;
    }
    
    
returns the type of performance.

Returns:
the type of performance
    public int getType() {
      return ;
    }
    
    
returns the generated table.

Returns:
the performance table
See also:
m_Table
generate()
    public double[][] getTable() {
      return ;
    }
    
    
the minimum performance.

Returns:
the performance
    public double getMin() {
      return ;
    }
    
    
the maximum performance.

Returns:
the performance
    public double getMax() {
      return ;
    }
    
    
returns the table as string.

Returns:
the table as string
    public String toString() {
      String	result;
      int	i;
      int	n;
      
      result =   "Table (" 
               + ") - "
               + "X: " + getGrid().getLabelX() + ", Y: " + getGrid().getLabelY()
               + ":\n";
      
      for (i = 0; i < getTable().lengthi++) {
	if (i > 0)
	  result += "\n";
	for (n = 0; n < getTable()[i].lengthn++) {
	  if (n > 0)
	    result += ",";
	  result += getTable()[i][n];
      }
      
      return result;
    }
    
    
returns a string containing a gnuplot script+data file.

Returns:
the data in gnuplot format
    public String toGnuplot() {
      StringBuffer	result;
      Tag		type;
      int		i;
      
      result = new StringBuffer();
      type   = new SelectedTag(getType(), ).getSelectedTag();
      
      result.append("Gnuplot (" + type.getReadable() + "):\n");
      result.append("# begin 'gridsearch.data'\n");
      result.append("# " + type.getReadable() + "\n");
      for (i = 0; i < getPerformances().size(); i++)
        result.append(getPerformances().get(i).toGnuplot(type.getID()) + "\n");
      result.append("# end 'gridsearch.data'\n\n");
      
      result.append("# begin 'gridsearch.plot'\n");
      result.append("# " + type.getReadable() + "\n");
      result.append("set data style lines\n");
      result.append("set contour base\n");
      result.append("set surface\n");
      result.append("set title '" + .getData().relationName() + "'\n");
      result.append("set xrange [" + getGrid().getMinX() + ":" + getGrid().getMaxX() + "]\n");
      result.append("set xlabel 'x (" + .getFilter().getClass().getName() + ": " + .getXProperty() + ")'\n");
      result.append("set yrange [" + getGrid().getMinY() + ":" + getGrid().getMaxY() + "]\n");
      result.append("set ylabel 'y - (" + .getClassifier().getClass().getName() + ": " + .getYProperty() + ")'\n");
      result.append("set zrange [" + (getMin() - (getMax() - getMin())*0.1) + ":" + (getMax() + (getMax() - getMin())*0.1) + "]\n");
      result.append("set zlabel 'z - " + type.getReadable() + "'\n");
      result.append("set dgrid3d " + getGrid().height() + "," + getGrid().width() + ",1\n");
      result.append("show contour\n");
      result.append("splot 'gridsearch.data'\n");
      result.append("pause -1\n");
      result.append("# end 'gridsearch.plot'");
      return result.toString();
    }
    
    
Returns the revision string.

Returns:
the revision
    public String getRevision() {
      return RevisionUtils.extract("$Revision: 9734 $");
    }
  }
  
  
Represents a simple cache for performance objects.
  protected static class PerformanceCache
    implements SerializableRevisionHandler {

    
for serialization.
    private static final long serialVersionUID = 5838863230451530252L;
    
    
the cache for points in the grid that got calculated.
    protected Hashtable m_Cache = new Hashtable();
    
    
returns the ID string for a cache item.

Parameters:
cv the number of folds in the cross-validation
values the point in the grid
Returns:
the ID string
    protected String getID(int cvPointDouble values) {
      return cv + "\t" + values.getX() + "\t" + values.getY();
    }
    
    
checks whether the point was already calculated ones.

Parameters:
cv the number of folds in the cross-validation
values the point in the grid
Returns:
true if the value is already cached
    public boolean isCached(int cvPointDouble values) {
      return (get(cvvalues) != null);
    }
    
    
returns a cached performance object, null if not yet in the cache.

Parameters:
cv the number of folds in the cross-validation
values the point in the grid
Returns:
the cached performance item, null if not in cache
    public Performance get(int cvPointDouble values) {
      return (Performance.get(getID(cvvalues));
    }
    
    
adds the performance to the cache.

Parameters:
cv the number of folds in the cross-validation
p the performance object to store
    public void add(int cvPerformance p) {
      .put(getID(cvp.getValues()), p);
    }
    
    
returns a string representation of the cache.

Returns:
the string representation of the cache
    public String toString() {
      return .toString();
    }
    
    
Returns the revision string.

Returns:
the revision
    public String getRevision() {
      return RevisionUtils.extract("$Revision: 9734 $");
    }
  }
  
  
Helper class for generating the setups.
  protected static class SetupGenerator
    implements SerializableRevisionHandler {
    
    
for serialization.
    private static final long serialVersionUID = -2517395033342543417L;
    
    
the owner.
    protected GridSearch m_Owner;

    
the Y option to work on.
    protected String m_Y_Property;
    
    
the minimum of Y.
    protected double m_Y_Min;
    
    
the maximum of Y.
    protected double m_Y_Max;
    
    
the step size of Y.
    protected double m_Y_Step;
    
    
the base for Y.
    protected double m_Y_Base;
    
    
The expression for the Y property.
    protected String m_Y_Expression;

    
the X option to work on.
    protected String m_X_Property;
    
    
the minimum of X.
    protected double m_X_Min;
    
    
the maximum of X.
    protected double m_X_Max;
    
    
the step size of X.
    protected double m_X_Step;
    
    
the base for X.
    protected double m_X_Base;
    
    
The expression for the X property.
    protected String m_X_Expression;
    
    
Initializes the setup generator.

Parameters:
owner the owning classifier
    public SetupGenerator(GridSearch owner) {
      super();
      
              = owner;
         = .getYProperty();
              = .getYMin();
              = .getYMax();
             = .getYStep();
             = .getYBase();
         = .getXProperty();
              = .getXMin();
              = .getXMax();
             = .getXStep();
             = .getXBase();
    }
    
    
evalutes the expression for the current iteration.

Parameters:
value the current iteration value (from 'min' to 'max' with stepsize 'step')
isX true if X is to be evaluated otherwise Y
Returns:
the generated value, NaN if the evaluation fails
    public double evaluate(double valueboolean isX) {
      double	result;
      HashMap	symbols;
      String	expr;
      double	base;
      double	min;
      double	max;
      double	step;
      if (isX) {
        expr = ;
        base = ;
        min  = ;
        max  = ;
        step = ;
      }
      else {
        expr = ;
        base = ;
        min  = ;
        max  = ;
        step = ;
      }
      try {
        symbols = new HashMap();
        symbols.put("BASE"new Double(base));
        symbols.put("FROM"new Double(min));
        symbols.put("TO",   new Double(max));
        symbols.put("STEP"new Double(step));
        symbols.put("I",    new Double(value));
        result = MathematicalExpression.evaluate(exprsymbols);
      }
      catch (Exception e) {
        result = .;
      }
      
      return result;
    }

    
tries to set the value as double, integer (just casts it to int!) or boolean (false if 0, otherwise true) in the object according to the specified path. float, char and long are also supported.

Parameters:
o the object to modify
path the property path
value the value to set
Returns:
the modified object
Throws:
java.lang.Exception if neither double nor int could be set
    public Object setValue(Object oString pathdouble valuethrows Exception {
      PropertyDescriptor	desc;
      Class		c;
      
      desc = PropertyPath.getPropertyDescriptor(opath);
      c    = desc.getPropertyType();
      // float
      if ((c == Float.class) || (c == .))
        PropertyPath.setValue(opathnew Float((floatvalue));
      // double
      else if ((c == Double.class) || (c == .))
        PropertyPath.setValue(opathnew Double(value));
      // char
      else if ((c == Character.class) || (c == .))
        PropertyPath.setValue(opathnew Integer((charvalue));
      // int
      else if ((c == Integer.class) || (c == .))
        PropertyPath.setValue(opathnew Integer((intvalue));
      // long
      else if ((c == Long.class) || (c == .))
        PropertyPath.setValue(opathnew Long((longvalue));
      // boolean
      else if ((c == Boolean.class) || (c == .))
        PropertyPath.setValue(opath, (value == 0 ? new Boolean(false) : new Boolean(true)));
      else throw new Exception(
  	"Could neither set double nor integer nor boolean value for '" + path + "'!");
      
      return o;
    }
    
    
returns a fully configures object (a copy of the provided one).

Parameters:
original the object to create a copy from and set the parameters
valueX the current iteration value for X
valueY the current iteration value for Y
Returns:
the configured classifier
Throws:
java.lang.Exception if setup fails
    public Object setup(Object originaldouble valueXdouble valueYthrows Exception {
      Object	result;
      result = new SerializedObject(original).getObject();
      if (original instanceof Classifier) {
	      result,
	      valueX);
	      result,
	      valueY);
      }
      else if (original instanceof Filter) {
	      result,
	      valueX);
	      result,
	      valueY);
      }
      else {
	throw new IllegalArgumentException("Object must be either classifier or filter!");
      }
      return result;
    }
    
    
Returns the revision string.

Returns:
the revision
    public String getRevision() {
      return RevisionUtils.extract("$Revision: 9734 $");
    }
  }

  
Helper class for evaluating a setup.
  protected static class EvaluationTask
    implements RunnableRevisionHandler {

    
the owner.
    protected GridSearch m_Owner;

    
for generating the setups.
    protected SetupGenerator m_Generator;
    
    
the classifier to use.
    protected Classifier m_Classifier;
    
    
the filter to use.
    protected Filter m_Filter;
    
    
the data to use for training.
    protected Instances m_Data;

    
the values to use.
    protected PointDouble m_Values;

    
the number of folds for cross-validation.
    protected int m_Folds;

    
the type of evaluation.
    protected int m_Evaluation;

    
Initializes the task.

Parameters:
owner the owning GridSearch classifier
generator the generator for the setips
inst the data
values the values in the grid
folds the number of cross-validation folds
eval the type of evaluation
    public EvaluationTask(GridSearch ownerSetupGenerator generator
	Instances instPointDouble valuesint foldsint eval) {
      super();
            = owner;
        = generator;
           = .getFilter();
             = inst;
           = values;
            = folds;
       = eval;
    }

    
Performs the evaluation.
    public void