5 import java.awt.event.*;
10 static final String DEFAULT_PARAM=
"-t 2 -c 100";
21 final static Color colors[] =
33 point(
double x,
double y, byte value)
43 Vector<point>
point_list =
new Vector<point>();
50 final Button button_change =
new Button(
"Change");
51 Button button_run =
new Button(
"Run");
52 Button button_clear =
new Button(
"Clear");
53 Button button_save =
new Button(
"Save");
54 Button button_load =
new Button(
"Load");
55 final TextField input_line =
new TextField(DEFAULT_PARAM);
57 BorderLayout layout =
new BorderLayout();
58 this.setLayout(layout);
60 Panel p =
new Panel();
61 GridBagLayout gridbag =
new GridBagLayout();
64 GridBagConstraints
c =
new GridBagConstraints();
65 c.fill = GridBagConstraints.HORIZONTAL;
68 gridbag.setConstraints(button_change,c);
69 gridbag.setConstraints(button_run,c);
70 gridbag.setConstraints(button_clear,c);
71 gridbag.setConstraints(button_save,c);
72 gridbag.setConstraints(button_load,c);
75 gridbag.setConstraints(input_line,c);
77 button_change.setBackground(colors[current_value]);
85 this.
add(p,BorderLayout.SOUTH);
87 button_change.addActionListener(
new ActionListener()
88 {
public void actionPerformed (ActionEvent e)
89 { button_change_clicked(); button_change.setBackground(colors[current_value]); }});
91 button_run.addActionListener(
new ActionListener()
92 {
public void actionPerformed (ActionEvent e)
95 button_clear.addActionListener(
new ActionListener()
96 {
public void actionPerformed (ActionEvent e)
97 { button_clear_clicked(); }});
99 button_save.addActionListener(
new ActionListener()
100 {
public void actionPerformed (ActionEvent e)
101 { button_save_clicked(input_line.getText()); }});
103 button_load.addActionListener(
new ActionListener()
104 {
public void actionPerformed (ActionEvent e)
105 { button_load_clicked(); }});
107 input_line.addActionListener(
new ActionListener()
108 {
public void actionPerformed (ActionEvent e)
111 this.enableEvents(AWTEvent.MOUSE_EVENT_MASK);
116 Color
c = colors[p.value+3];
118 Graphics window_gc = getGraphics();
119 buffer_gc.setColor(c);
120 buffer_gc.fillRect((
int)(p.x*XLEN),(
int)(p.y*YLEN),4,4);
121 window_gc.setColor(c);
122 window_gc.fillRect((
int)(p.x*XLEN),(
int)(p.y*YLEN),4,4);
127 point_list.removeAllElements();
130 buffer_gc.setColor(colors[0]);
131 buffer_gc.fillRect(0,0,XLEN,YLEN);
138 int n = point_list.size();
143 void button_change_clicked()
146 if(current_value > 3) current_value = 1;
149 private static double atof(String s)
151 return Double.valueOf(s).doubleValue();
154 private static int atoi(String s)
156 return Integer.parseInt(s);
162 if(point_list.isEmpty())
return;
181 param.
weight =
new double[0];
184 StringTokenizer st =
new StringTokenizer(args);
185 String[] argv =
new String[st.countTokens()];
186 for(
int i=0;i<argv.length;i++)
187 argv[i] = st.nextToken();
189 for(
int i=0;i<argv.length;i++)
191 if(argv[i].charAt(0) !=
'-')
break;
194 System.err.print(
"unknown option\n");
197 switch(argv[i-1].charAt(1))
221 param.
C =
atof(argv[i]);
227 param.
p =
atof(argv[i]);
244 double[] old = param.
weight;
253 System.err.print(
"unknown option\n");
259 prob.
l = point_list.size();
260 prob.
y =
new double[prob.
l];
270 for(
int i=0;i<prob.
l;i++)
272 point p = point_list.elementAt(i);
284 int[] j =
new int[XLEN];
286 Graphics window_gc = getGraphics();
287 for (
int i = 0; i < XLEN; i++)
289 x[0].
value = (double) i / XLEN;
290 j[i] = (int)(YLEN*
svm.svm_predict(model, x));
293 buffer_gc.setColor(colors[0]);
294 buffer_gc.drawLine(0,0,0,YLEN-1);
295 window_gc.setColor(colors[0]);
296 window_gc.drawLine(0,0,0,YLEN-1);
298 int p = (int)(param.
p * YLEN);
299 for(
int i=1;i<XLEN;i++)
301 buffer_gc.setColor(colors[0]);
302 buffer_gc.drawLine(i,0,i,YLEN-1);
303 window_gc.setColor(colors[0]);
304 window_gc.drawLine(i,0,i,YLEN-1);
306 buffer_gc.setColor(colors[5]);
307 window_gc.setColor(colors[5]);
308 buffer_gc.drawLine(i-1,j[i-1],i,j[i]);
309 window_gc.drawLine(i-1,j[i-1],i,j[i]);
313 buffer_gc.setColor(colors[2]);
314 window_gc.setColor(colors[2]);
315 buffer_gc.drawLine(i-1,j[i-1]+p,i,j[i]+p);
316 window_gc.drawLine(i-1,j[i-1]+p,i,j[i]+p);
318 buffer_gc.setColor(colors[2]);
319 window_gc.setColor(colors[2]);
320 buffer_gc.drawLine(i-1,j[i-1]-p,i,j[i]-p);
321 window_gc.drawLine(i-1,j[i-1]-p,i,j[i]-p);
329 for(
int i=0;i<prob.
l;i++)
331 point p = point_list.elementAt(i);
349 Graphics window_gc = getGraphics();
350 for (
int i = 0; i < XLEN; i++)
351 for (
int j = 0; j < YLEN ; j++) {
352 x[0].
value = (double) i / XLEN;
353 x[1].
value = (double) j / YLEN;
354 double d =
svm.svm_predict(model, x);
356 buffer_gc.setColor(colors[(
int)d]);
357 window_gc.setColor(colors[(
int)d]);
358 buffer_gc.drawLine(i,j,i,j);
359 window_gc.drawLine(i,j,i,j);
366 void button_clear_clicked()
371 void button_save_clicked(String args)
373 FileDialog dialog =
new FileDialog(
new Frame(),
"Save",FileDialog.SAVE);
374 dialog.setVisible(
true);
375 String
filename = dialog.getDirectory() + dialog.getFile();
376 if (filename == null)
return;
378 DataOutputStream fp =
new DataOutputStream(
new BufferedOutputStream(
new FileOutputStream(filename)));
381 int svm_type_idx = args.indexOf(
"-s ");
382 if(svm_type_idx != -1)
384 StringTokenizer svm_str_st =
new StringTokenizer(args.substring(svm_type_idx+2).trim());
385 svm_type =
atoi(svm_str_st.nextToken());
388 int n = point_list.size();
393 point p = point_list.elementAt(i);
394 fp.writeBytes(p.y+
" 1:"+p.x+
"\n");
401 point p = point_list.elementAt(i);
402 fp.writeBytes(p.value+
" 1:"+p.x+
" 2:"+p.y+
"\n");
406 }
catch (IOException e) { System.err.print(e); }
409 void button_load_clicked()
411 FileDialog dialog =
new FileDialog(
new Frame(),
"Load",FileDialog.LOAD);
412 dialog.setVisible(
true);
413 String
filename = dialog.getDirectory() + dialog.getFile();
414 if (filename == null)
return;
417 BufferedReader fp =
new BufferedReader(
new FileReader(filename));
419 while((line = fp.readLine()) != null)
421 StringTokenizer st =
new StringTokenizer(line,
" \t\n\r\f:");
422 if(st.countTokens() == 5)
424 byte value = (byte)
atoi(st.nextToken());
426 double x =
atof(st.nextToken());
428 double y =
atof(st.nextToken());
429 point_list.addElement(
new point(x,y,value));
431 else if(st.countTokens() == 3)
433 double y =
atof(st.nextToken());
435 double x =
atof(st.nextToken());
436 point_list.addElement(
new point(x,y,current_value));
441 }
catch (IOException e) { System.err.print(e); }
447 if(e.getID() == MouseEvent.MOUSE_PRESSED)
449 if(e.getX() >= XLEN || e.getY() >= YLEN)
return;
450 point p =
new point((
double)e.getX()/XLEN,
451 (double)e.getY()/YLEN,
453 point_list.addElement(p);
462 buffer = this.createImage(XLEN,YLEN);
463 buffer_gc = buffer.getGraphics();
464 buffer_gc.setColor(colors[0]);
465 buffer_gc.fillRect(0,0,XLEN,YLEN);
467 g.drawImage(buffer,0,0,
this);
480 public static void main(String[] argv)
482 new AppletFrame(
"svm_toy",
new svm_toy(),500,500+50);
486 class AppletFrame
extends Frame {
487 AppletFrame(String title, Applet applet,
int width,
int height)
490 this.addWindowListener(
new WindowAdapter() {
491 public void windowClosing(WindowEvent e) {
496 applet.setSize(width,height);
500 this.setVisible(
true);
void button_run_clicked()
struct svm_parameter param
static double atof(String s)
bool add(const actionlib::TwoIntsGoal &req, actionlib::TwoIntsResult &res)
TFSIMD_FORCE_INLINE const tfScalar & y() const
static int atoi(String s)
void draw_point(const point &p)
Dimension getPreferredSize()
static void main(String[] argv)
void processMouseEvent(MouseEvent e)
void setSize(int w, int h)
static final int PRECOMPUTED
void setSize(Dimension d)
static final int ONE_CLASS
static final int EPSILON_SVR