00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013 package de.picana.generator;
00014
00015 import de.picana.control.*;
00016 import de.picana.logging.*;
00017
00018 import java.util.*;
00019 import java.io.*;
00020
00021
00031 public class Random2ARFF extends Task {
00032
00033 private String out_filename;
00034 private String relation;
00035 private int num_instances;
00036 private boolean normal;
00037 private boolean invert;
00038 private Vector attributes;
00039
00040
00042 class Attr {
00043 public String name;
00044 public double min;
00045 public double max;
00046 public double sigma;
00047
00048 Attr(String name, double min, double max, double sigma) {
00049 this.name = name;
00050 this.min = min;
00051 this.max = max;
00052 this.sigma = sigma;
00053 }
00054 }
00055
00056
00058 public Random2ARFF() {
00059 }
00060
00061 public void init(ParameterSet params, Logger logger) {
00062
00063 super.init(params, logger);
00064
00065 attributes = new Vector();
00066
00067 out_filename = (String)params.getParameter("out");
00068 relation = (String)params.getParameter("relation");
00069
00070 num_instances = 1000;
00071 try {
00072 num_instances = Integer.parseInt((String)params.getParameter("num_instances"));
00073 } catch (NumberFormatException nfe) {}
00074
00075 normal = false;
00076 String str_normal = (String)params.getParameter("normal");
00077 if (str_normal != null)
00078 if (str_normal.equals("true"))
00079 normal = true;
00080
00081 invert = false;
00082 String str_invert = (String)params.getParameter("invert");
00083 if (str_invert != null)
00084 if (str_invert.equals("true"))
00085 invert = true;
00086
00087 ParameterSet attribute_set = (ParameterSet)params.getParameter("attributes");
00088 Enumeration attribute_names = attribute_set.getParameterNames();
00089 while (attribute_names.hasMoreElements()) {
00090 String name = (String)attribute_names.nextElement();
00091 ParameterSet attribute = (ParameterSet)attribute_set.getParameter(name);
00092
00093 double min = 0.0;
00094 try {
00095 min = Double.parseDouble((String)attribute.getParameter("min"));
00096 } catch (NumberFormatException nfe) {}
00097
00098 double max = 0.0;
00099 try {
00100 max = Double.parseDouble((String)attribute.getParameter("max"));
00101 } catch (NumberFormatException nfe) {}
00102
00103 double sigma = 0.0;
00104 try {
00105 sigma = Double.parseDouble((String)attribute.getParameter("sigma"));
00106 } catch (NumberFormatException nfe) {}
00107
00108 attributes.addElement(new Attr(attribute.getName(), min, max, sigma));
00109 }
00110 }
00111
00112 public void start() throws TaskException {
00113
00114 try {
00115 logger.info(LOGSRC, "Started.");
00116
00117 Random rand = new Random();
00118
00119 FileOutputStream output = new FileOutputStream(new File(out_filename));
00120 PrintWriter pw = new PrintWriter(output);
00121
00122 pw.println("% ARFF file for picture data from PNG files");
00123 pw.println("%");
00124 pw.println("@relation " + relation);
00125 pw.println();
00126 for (int i=0; i < attributes.size(); i++) {
00127 Attr a = (Attr)attributes.elementAt(i);
00128 pw.println("@attribute " + a.name + " numeric");
00129 }
00130 pw.println();
00131 pw.println("@data");
00132 pw.println("%");
00133 pw.println("% x instances");
00134 pw.println("%");
00135
00136 for (int n=0; n < num_instances; n++) {
00137 for (int i=0; i < attributes.size(); i++) {
00138 Attr a = (Attr)attributes.elementAt(i);
00139 if (normal)
00140 pw.print(getRandom(a.min, a.max, a.sigma,invert));
00141 else
00142 pw.print(getRandom(a.min, a.max));
00143
00144 if (i != attributes.size()-1) {
00145 pw.print(",");
00146 }
00147 }
00148 pw.println();
00149 }
00150
00151 pw.close();
00152
00153 logger.info(LOGSRC, "Stopped.");
00154
00155 } catch (Exception e) {
00156 throw new TaskException(e.toString());
00157 }
00158 }
00159
00160 public void stop() {
00161 }
00162 public void pause() {
00163 }
00164 public void resume() {
00165 }
00166
00175 public double getRandom(double min, double max, double sigma, boolean invert) {
00176 double r = 0.0;
00177 boolean found = false;
00178
00179 if (invert) {
00180
00181 while (!found) {
00182 r = rand.nextGaussian() * Math.sqrt(sigma);
00183 if (r < 0) {
00184 r = 1.0 - Math.abs(r);
00185 if ((r >= 0.5) && (r <= max))
00186 found = true;
00187 } else {
00188 if ((r < 0.5) && (r >= min))
00189 found = true;
00190 }
00191 }
00192
00193 } else {
00194 while (!found) {
00195 r = rand.nextGaussian() * Math.sqrt(sigma) + 0.5;
00196 if ((r >= min) && (r <= max))
00197 found = true;
00198 }
00199 }
00200 return r;
00201 }
00202
00209 public double getRandom(double min, double max) {
00210 return (rand.nextDouble() / (max-min)) + min;
00211 }
00212 }