Options.java
Upload User: rhdiban
Upload Date: 2013-08-09
Package Size: 15085k
Code Size: 11k
Category:

Windows Develop

Development Platform:

Java

  1. /*
  2.  *    This program is free software; you can redistribute it and/or modify
  3.  *    it under the terms of the GNU General Public License as published by
  4.  *    the Free Software Foundation; either version 2 of the License, or
  5.  *    (at your option) any later version.
  6.  *
  7.  *    This program is distributed in the hope that it will be useful,
  8.  *    but WITHOUT ANY WARRANTY; without even the implied warranty of
  9.  *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  10.  *    GNU General Public License for more details.
  11.  *
  12.  *    You should have received a copy of the GNU General Public License
  13.  *    along with this program; if not, write to the Free Software
  14.  *    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  15.  */
  16. /*
  17.  *    Options.java
  18.  *    Copyright (C) 1999 Yong Wang
  19.  *
  20.  */
  21. package weka.classifiers.m5;
  22. import java.lang.*;
  23. import java.io.*;
  24. import weka.core.*;
  25.  
  26. /**
  27.  * Class for handing options 
  28.  * @author Yong Wang (yongwang@cs.waikato.ac.nz)
  29.  * @version $Revision: 1.4 $
  30.  */
  31. public final class Options implements Serializable {
  32.   boolean smooth;           // =true, smoothed result; otherwise, unsmoothed result
  33.   int     randomSeed;       // random seed for cross-validation 
  34.   int     classcol;         // class column 
  35.   int     verbosity;        // verbosity level, 0-2 
  36.   int     model;            // output model type could be linearRegression (1),
  37.                             // regressionTree (2), modelTree (3) 
  38.   int     numFolds;         // the number of folds for cross-validation
  39.   double  pruningFactor;    // pruning factor a in (n+ak)/(n-k)
  40.   String  trainFile;        // name of the training file
  41.   String  testFile ;        // name of the test file
  42.   int     lmNo;             // linear model number,  falling into which tested instances will be printed 
  43.   double  deviation;        // the global standard deviation of the class attribute of the instances, used for splitting stopping, scale determination of the class attribute
  44.  
  45.   final static String   VERSION="v1.1";
  46.   public Options(Instances inst) {
  47.     smooth = true;         
  48.     randomSeed = 1;     
  49.     classcol = inst.classIndex();       
  50.     verbosity = 0;      
  51.     model = Node.MODEL_TREE;          
  52.     numFolds =10;     
  53.     pruningFactor =2;
  54.     trainFile = null;    
  55.     testFile =null;    
  56.     lmNo = 0;         
  57.   }
  58.   /**
  59.    * Constructs an object to store command line options and other necessary 
  60.    *     information
  61.    * @param argv command line auguments
  62.    */
  63.   public Options(String [] argv){
  64.     int count;
  65.     char sw;
  66.     String rest = new String();
  67.     
  68.     rest=null;
  69.     classcol=-3;
  70.     pruningFactor=1.0;
  71.     randomSeed=1;
  72.     model=Node.MODEL_TREE;
  73.     trainFile=null;
  74.     testFile=null;
  75.     numFolds=0;
  76.     lmNo = 0;
  77.     if(argv.length<1 || argv[0].startsWith("-") != true)
  78.       M5Utils.errorMsg("no training file specified. See -help.");
  79.     count=0;
  80.     while (count < argv.length && argv[count].startsWith("-") == true && 
  81.    argv[count].length() >=2) {
  82.       sw  = argv[count].charAt(1);            // sw = switch 
  83.       if(argv[count].length()>2)
  84. rest= argv[count].substring(2);       // rest = rest of string after sw
  85.       else if(argv[count].length()==2 && count+1 < argv.length) {
  86. if (argv[count+1].startsWith("-") == false) { 
  87.   count++; rest = argv[count].toString(); 
  88. }
  89.       }
  90.       switch (sw) {
  91.       case 'c':
  92. if(rest!=null){
  93.   if(rest.charAt(0)>48 && rest.charAt(0)<58)
  94.     classcol = Integer.parseInt(rest) - 1;
  95.   else if(rest.charAt(0)=='f')classcol=0;
  96.   else if(rest.charAt(0)=='l')classcol=-1;
  97.   else classcol=-2;
  98. }
  99. break;
  100.       case 'f':
  101. if(rest!=null){
  102.   pruningFactor = Double.valueOf(rest).doubleValue();
  103.   if(pruningFactor<-0.01 || pruningFactor>10.01)
  104.     M5Utils.errorMsg("pruning factor out of limit (0.0 - 10.0).n" +
  105.      "Default value 1.0. (0.0 - 3.0) is the " + 
  106.      "recommended range.");
  107. }
  108. break;
  109.       case 'h':printValidOptions();
  110.       case 'L': 
  111. lmNo = Integer.parseInt(rest);
  112. break;
  113.       case 'o':
  114. model=Node.MODEL_TREE;
  115. if(rest!=null){
  116.   switch(rest.charAt(0)){
  117.   case '1':
  118.   case 'l':
  119.   case 'L':model=Node.LINEAR_REGRESSION; break;
  120.   case '2':
  121.   case 'r':
  122.   case 'R':model=Node.REGRESSION_TREE; break;
  123.   case '3':
  124.   case 'm':
  125.   case 'M':model=Node.MODEL_TREE; break;
  126.   default: M5Utils.errorMsg("unknown model type -o " + rest + 
  127.     " . See -help");System.exit(1);
  128.   }
  129. }
  130. break;   
  131.       case 's': numFolds=10;
  132. if(rest!=null){
  133.   randomSeed = Integer.parseInt(rest);
  134.   if(randomSeed<0){
  135.     M5Utils.errorMsg("randomization seed must be >= 0. " + 
  136.      "Default value is 1.");
  137.   }
  138. }
  139. break;
  140.       case 't': 
  141. if(rest!=null){trainFile=rest.substring(0);} 
  142. else trainFile=null;
  143. break;
  144.       case 'T': 
  145. if(rest!=null){testFile=rest.substring(0);} 
  146. else testFile=null;
  147.         break;
  148.       case 'v':
  149. if(rest!=null){
  150.   verbosity = Integer.parseInt(rest); 
  151.   if(verbosity<0 || verbosity>2)
  152.     M5Utils.errorMsg("verbosity level should range within (0-2). " + 
  153.      "See -help.");
  154. }
  155. break;
  156.     case 'x': numFolds=10;
  157.       if (rest!=null) {
  158. numFolds=Integer.parseInt(rest); 
  159. if (numFolds<=1 || numFolds>100) {
  160.   M5Utils.errorMsg("fold number for cross-validation must be within" + 
  161.    " (2 - 100). See -help.");
  162. }
  163.       }
  164.       break;
  165.       default:  if(rest==null)
  166. System.out.println("M5' error: Invalid option -" + sw);
  167.       else M5Utils.errorMsg("invalid option -" + sw + " " + rest);
  168.       System.exit(1);
  169.       }
  170.       rest=null;
  171.       count++;
  172.     }
  173.     if(trainFile==null)
  174.       M5Utils.errorMsg("no training file specified. See -help.");
  175.   }
  176.   
  177.   /**
  178.    * Initializes for constucting model trees
  179.    * @param dataset a dataset 
  180.    * @exception Exception if something goes wrong
  181.    */
  182.   public final void  initialize(Instances inst) throws Exception {
  183.     
  184.     FileInputStream inputStream;
  185.     int i,j;
  186.     int [] index = null;
  187.     if(numFolds > inst.numInstances())
  188.       M5Utils.errorMsg("fold number for cross-validation greater than the " +
  189.        "number of instances.");
  190.     if(classcol==-3 || classcol==-1)classcol=inst.numAttributes()-1;
  191.     if(inst.classAttribute().isNominal()==true)
  192.       M5Utils.errorMsg("class column must be real or integer attribute.");
  193.     if(verbosity<0 && (testFile==null || numFolds>=1))verbosity=0;
  194.   }
  195.   
  196.   /**
  197.    * Prints information stored in an 'Options' object, basically containing 
  198.    *     command line options
  199.    * @param dataset a dataset
  200.    * @exception Exception if something goes wrong
  201.    */
  202.   public final String  toString(Instances inst) throws Exception {
  203.     
  204.     StringBuffer text = new StringBuffer();
  205.     
  206.     text.append("    Options:nn");
  207.     text.append("        Training file   :     " + trainFile + "n");
  208.     if(testFile!=null) text.append("        Test file       :     "+testFile + 
  209.    "n");
  210.     text.append("        Class attribute :     "+inst.classAttribute().name()
  211. + " (column "+ (classcol+1) +")n");
  212.     if(numFolds>1) text.append("        Cross-Validation:     "+numFolds+
  213.       "-fold with random seed "+randomSeed + "n");
  214.     text.append("        Verbosity level :     "+verbosity + "n"); 
  215.     if(model==Node.LINEAR_REGRESSION) 
  216.       text.append("        Output model    :     linear regression" + "n");
  217.     if(model==Node.REGRESSION_TREE) 
  218.       text.append("        Output model    :     regression tree" + "n");
  219.     if(model==Node.MODEL_TREE){
  220.       text.append("        Pruning factor  :     "+pruningFactor + "n"); 
  221.       text.append("        Output model    :     model treen");
  222.     }  
  223.     text.append("n");
  224.     return text.toString();
  225.   }
  226.   /**
  227.    * Prints valid command line options and simply explains the output
  228.    */
  229.   public final void printValidOptions()
  230.   {
  231.     System.out.println("Usage:");
  232.     System.out.println("      M5Java [-options]n");
  233.     System.out.println("Options:");
  234.     System.out.println("  -c (<num>|first|last)  column to predict values "+
  235.        "(default last)");
  236.     System.out.println("  -f <num>               pruning factor 0.0 - 10.0 "+
  237.        "(default 1.0)");
  238.     System.out.println("  -h                     displays this help");
  239.     System.out.println("  -o <l|m|r>             output model: linear, "+
  240.        "model tree, or regression tree");
  241.     System.out.println("  -s <num>               random seed for "+
  242.        "cross-validation only. No randomization");
  243.     System.out.println("                         while 0 (default 1)");
  244.     System.out.println("  -t <file>              training set file ");
  245.     System.out.println("  -T <file>              test set file");
  246.     System.out.println("  -v <num>               verbosity level 0,1,2 "+
  247.        "(default 0)");
  248.     System.out.println("  -x <num>               cross validation "+
  249.        "(default 10-fold)n");
  250.     System.out.println("Definitions:");
  251.     System.out.println("  Correlation coefficient: correlation between actual "+
  252.        "values and predictions");
  253.     System.out.println("  Mean absolute error: average absolute prediction "+
  254.        "error");
  255.     System.out.println("  Root mean squared error: square root of the average "+
  256.        "squared prediction error");
  257.     System.out.println("  Relative absolute error: ratio of the mean absolute "+
  258.        "residuals to the absolute");
  259.     System.out.println("      deviation of the target values");
  260.     System.out.println("  Root relative squared error: square root of the "+
  261.        "ratio of the variance of the ");
  262.     System.out.println("      residuals to the variance of the target valuesn");
  263.     System.out.println("  Note: 100% relative error is the same as would be "+
  264.        "obtained by predicting a");
  265.     System.out.println("      simple averagen");
  266.     System.out.println("Description:");
  267.     System.out.println("  An unsmoothed prediction is calculated directly by "+
  268.        "the function at the leaf.");
  269.     System.out.println("  A smoothed prediction uses the value calculated at "+
  270.        "the leaf of the tree,");
  271.     System.out.println("  and passes it back up the tree, smoothing at each "+
  272.        "higher node.n");
  273.     System.out.println("  Let");
  274.     System.out.println("tp' be the model passed up to the next higher node,");
  275.     System.out.println("tp be the model passed to this node from below,");
  276.     System.out.println("tq be the model at this node,");
  277.     System.out.println("tn be the number of training instances that reach "+
  278.        "the node below,");
  279.     System.out.println("tk be a constant (default value 15),n");
  280.     System.out.println("  then the smoothed model at this node is:n");
  281.     System.out.println("tp' = (n*p+k*q) / (n+k)n");
  282.     System.out.println("Version:");
  283.     System.out.println("t" + Options.VERSION);
  284.     System.exit(1);
  285.   }
  286. }