00001 #include <gtk/gtk.h>
00002 #include <stdio.h>
00003 #include <stdlib.h>
00004 #include <ctype.h>
00005 #include <list>
00006 #include "callbacks.h"
00007 #include "interface.h"
00008 #include "../../svm.h"
00009 using namespace std;
00010
00011 #define DEFAULT_PARAM "-t 2 -c 100"
00012 #define XLEN 500
00013 #define YLEN 500
00014
00015 GdkColor colors[] =
00016 {
00017 {0,0,0,0},
00018 {0,0,120<<8,120<<8},
00019 {0,120<<8,120<<8,0},
00020 {0,120<<8,0,120<<8},
00021 {0,0,200<<8,200<<8},
00022 {0,200<<8,200<<8,0},
00023 {0,200<<8,0,200<<8},
00024 };
00025
00026 GdkGC *gc;
00027 GdkPixmap *pixmap;
00028 extern "C" GtkWidget *draw_main;
00029 GtkWidget *draw_main;
00030 extern "C" GtkWidget *entry_option;
00031 GtkWidget *entry_option;
00032
00033 typedef struct {
00034 double x, y;
00035 signed char value;
00036 } point;
00037
00038 list<point> point_list;
00039 int current_value = 1;
00040
00041 extern "C" void svm_toy_initialize()
00042 {
00043 gboolean success[7];
00044
00045 gdk_colormap_alloc_colors(
00046 gdk_colormap_get_system(),
00047 colors,
00048 7,
00049 FALSE,
00050 TRUE,
00051 success);
00052
00053 gc = gdk_gc_new(draw_main->window);
00054 pixmap = gdk_pixmap_new(draw_main->window,XLEN,YLEN,-1);
00055 gdk_gc_set_foreground(gc,&colors[0]);
00056 gdk_draw_rectangle(pixmap,gc,TRUE,0,0,XLEN,YLEN);
00057 gtk_entry_set_text(GTK_ENTRY(entry_option),DEFAULT_PARAM);
00058 }
00059
00060 void redraw_area(GtkWidget* widget, int x, int y, int w, int h)
00061 {
00062 gdk_draw_pixmap(widget->window,
00063 gc,
00064 pixmap,
00065 x,y,x,y,w,h);
00066 }
00067
00068 void draw_point(const point& p)
00069 {
00070 gdk_gc_set_foreground(gc,&colors[p.value+3]);
00071 gdk_draw_rectangle(pixmap, gc, TRUE,int(p.x*XLEN),int(p.y*YLEN),4,4);
00072 gdk_draw_rectangle(draw_main->window, gc, TRUE,int(p.x*XLEN),int(p.y*YLEN),4,4);
00073 }
00074
00075 void draw_all_points()
00076 {
00077 for(list<point>::iterator p = point_list.begin(); p != point_list.end();p++)
00078 draw_point(*p);
00079 }
00080
00081 void clear_all()
00082 {
00083 point_list.clear();
00084 gdk_gc_set_foreground(gc,&colors[0]);
00085 gdk_draw_rectangle(pixmap,gc,TRUE,0,0,XLEN,YLEN);
00086 redraw_area(draw_main,0,0,XLEN,YLEN);
00087 }
00088
00089 void
00090 on_button_change_clicked (GtkButton *button,
00091 gpointer user_data)
00092 {
00093 ++current_value;
00094 if(current_value > 3) current_value = 1;
00095 }
00096
00097 void
00098 on_button_run_clicked (GtkButton *button,
00099 gpointer user_data)
00100 {
00101
00102 if(point_list.empty()) return;
00103
00104 svm_parameter param;
00105 int i,j;
00106
00107
00108 param.svm_type = C_SVC;
00109 param.kernel_type = RBF;
00110 param.degree = 3;
00111 param.gamma = 0;
00112 param.coef0 = 0;
00113 param.nu = 0.5;
00114 param.cache_size = 100;
00115 param.C = 1;
00116 param.eps = 1e-3;
00117 param.p = 0.1;
00118 param.shrinking = 1;
00119 param.probability = 0;
00120 param.nr_weight = 0;
00121 param.weight_label = NULL;
00122 param.weight = NULL;
00123
00124
00125 const char *p = gtk_entry_get_text(GTK_ENTRY(entry_option));
00126
00127 while (1) {
00128 while (*p && *p != '-')
00129 p++;
00130
00131 if (*p == '\0')
00132 break;
00133
00134 p++;
00135 switch (*p++) {
00136 case 's':
00137 param.svm_type = atoi(p);
00138 break;
00139 case 't':
00140 param.kernel_type = atoi(p);
00141 break;
00142 case 'd':
00143 param.degree = atoi(p);
00144 break;
00145 case 'g':
00146 param.gamma = atof(p);
00147 break;
00148 case 'r':
00149 param.coef0 = atof(p);
00150 break;
00151 case 'n':
00152 param.nu = atof(p);
00153 break;
00154 case 'm':
00155 param.cache_size = atof(p);
00156 break;
00157 case 'c':
00158 param.C = atof(p);
00159 break;
00160 case 'e':
00161 param.eps = atof(p);
00162 break;
00163 case 'p':
00164 param.p = atof(p);
00165 break;
00166 case 'h':
00167 param.shrinking = atoi(p);
00168 break;
00169 case 'b':
00170 param.probability = atoi(p);
00171 break;
00172 case 'w':
00173 ++param.nr_weight;
00174 param.weight_label = (int *)realloc(param.weight_label,sizeof(int)*param.nr_weight);
00175 param.weight = (double *)realloc(param.weight,sizeof(double)*param.nr_weight);
00176 param.weight_label[param.nr_weight-1] = atoi(p);
00177 while(*p && !isspace(*p)) ++p;
00178 param.weight[param.nr_weight-1] = atof(p);
00179 break;
00180 }
00181 }
00182
00183
00184 svm_problem prob;
00185
00186 prob.l = point_list.size();
00187 prob.y = new double[prob.l];
00188
00189 if(param.kernel_type == PRECOMPUTED)
00190 {
00191 }
00192 else if(param.svm_type == EPSILON_SVR ||
00193 param.svm_type == NU_SVR)
00194 {
00195 if(param.gamma == 0) param.gamma = 1;
00196 svm_node *x_space = new svm_node[2 * prob.l];
00197 prob.x = new svm_node *[prob.l];
00198
00199 i = 0;
00200 for (list <point>::iterator q = point_list.begin(); q != point_list.end(); q++, i++)
00201 {
00202 x_space[2 * i].index = 1;
00203 x_space[2 * i].value = q->x;
00204 x_space[2 * i + 1].index = -1;
00205 prob.x[i] = &x_space[2 * i];
00206 prob.y[i] = q->y;
00207 }
00208
00209
00210 svm_model *model = svm_train(&prob, ¶m);
00211 svm_node x[2];
00212 x[0].index = 1;
00213 x[1].index = -1;
00214 int *j = new int[XLEN];
00215
00216 for (i = 0; i < XLEN; i++)
00217 {
00218 x[0].value = (double) i / XLEN;
00219 j[i] = (int)(YLEN*svm_predict(model, x));
00220 }
00221
00222 gdk_gc_set_foreground(gc,&colors[0]);
00223 gdk_draw_line(pixmap,gc,0,0,0,YLEN-1);
00224 gdk_draw_line(draw_main->window,gc,0,0,0,YLEN-1);
00225
00226 int p = (int)(param.p * YLEN);
00227 for(i = 1; i < XLEN; i++)
00228 {
00229 gdk_gc_set_foreground(gc,&colors[0]);
00230 gdk_draw_line(pixmap,gc,i,0,i,YLEN-1);
00231 gdk_draw_line(draw_main->window,gc,i,0,i,YLEN-1);
00232
00233 gdk_gc_set_foreground(gc,&colors[5]);
00234 gdk_draw_line(pixmap,gc,i-1,j[i-1],i,j[i]);
00235 gdk_draw_line(draw_main->window,gc,i-1,j[i-1],i,j[i]);
00236
00237 if(param.svm_type == EPSILON_SVR)
00238 {
00239 gdk_gc_set_foreground(gc,&colors[2]);
00240 gdk_draw_line(pixmap,gc,i-1,j[i-1]+p,i,j[i]+p);
00241 gdk_draw_line(draw_main->window,gc,i-1,j[i-1]+p,i,j[i]+p);
00242
00243 gdk_gc_set_foreground(gc,&colors[2]);
00244 gdk_draw_line(pixmap,gc,i-1,j[i-1]-p,i,j[i]-p);
00245 gdk_draw_line(draw_main->window,gc,i-1,j[i-1]-p,i,j[i]-p);
00246 }
00247 }
00248
00249 svm_free_and_destroy_model(&model);
00250 delete[] j;
00251 delete[] x_space;
00252 delete[] prob.x;
00253 delete[] prob.y;
00254 }
00255 else
00256 {
00257 if(param.gamma == 0) param.gamma = 0.5;
00258 svm_node *x_space = new svm_node[3 * prob.l];
00259 prob.x = new svm_node *[prob.l];
00260
00261 i = 0;
00262 for (list <point>::iterator q = point_list.begin(); q != point_list.end(); q++, i++)
00263 {
00264 x_space[3 * i].index = 1;
00265 x_space[3 * i].value = q->x;
00266 x_space[3 * i + 1].index = 2;
00267 x_space[3 * i + 1].value = q->y;
00268 x_space[3 * i + 2].index = -1;
00269 prob.x[i] = &x_space[3 * i];
00270 prob.y[i] = q->value;
00271 }
00272
00273
00274 svm_model *model = svm_train(&prob, ¶m);
00275 svm_node x[3];
00276 x[0].index = 1;
00277 x[1].index = 2;
00278 x[2].index = -1;
00279
00280 for (i = 0; i < XLEN; i++)
00281 for (j = 0; j < YLEN; j++) {
00282 x[0].value = (double) i / XLEN;
00283 x[1].value = (double) j / YLEN;
00284 double d = svm_predict(model, x);
00285 if (param.svm_type == ONE_CLASS && d<0) d=2;
00286 gdk_gc_set_foreground(gc,&colors[(int)d]);
00287 gdk_draw_point(pixmap,gc,i,j);
00288 gdk_draw_point(draw_main->window,gc,i,j);
00289 }
00290
00291 svm_free_and_destroy_model(&model);
00292 delete[] x_space;
00293 delete[] prob.x;
00294 delete[] prob.y;
00295 }
00296 free(param.weight_label);
00297 free(param.weight);
00298 draw_all_points();
00299 }
00300
00301 void
00302 on_button_clear_clicked (GtkButton *button,
00303 gpointer user_data)
00304 {
00305 clear_all();
00306 }
00307
00308 void
00309 on_window1_destroy (GtkObject *object,
00310 gpointer user_data)
00311 {
00312 gtk_exit(0);
00313 }
00314
00315 gboolean
00316 on_draw_main_button_press_event (GtkWidget *widget,
00317 GdkEventButton *event,
00318 gpointer user_data)
00319 {
00320 point p = {(double)event->x/XLEN, (double)event->y/YLEN, current_value};
00321 point_list.push_back(p);
00322 draw_point(p);
00323 return FALSE;
00324 }
00325
00326 gboolean
00327 on_draw_main_expose_event (GtkWidget *widget,
00328 GdkEventExpose *event,
00329 gpointer user_data)
00330 {
00331 redraw_area(widget,
00332 event->area.x, event->area.y,
00333 event->area.width, event->area.height);
00334 return FALSE;
00335 }
00336
00337 GtkWidget *fileselection;
00338 static enum { SAVE, LOAD } fileselection_flag;
00339
00340 void show_fileselection()
00341 {
00342 fileselection = create_fileselection();
00343 gtk_signal_connect_object(
00344 GTK_OBJECT(GTK_FILE_SELECTION(fileselection)->ok_button),
00345 "clicked", GTK_SIGNAL_FUNC(gtk_widget_destroy),
00346 (GtkObject *) fileselection);
00347
00348 gtk_signal_connect_object (GTK_OBJECT
00349 (GTK_FILE_SELECTION(fileselection)->cancel_button),
00350 "clicked", GTK_SIGNAL_FUNC(gtk_widget_destroy),
00351 (GtkObject *) fileselection);
00352
00353 gtk_widget_show(fileselection);
00354 }
00355
00356 void
00357 on_button_save_clicked (GtkButton *button,
00358 gpointer user_data)
00359 {
00360 fileselection_flag = SAVE;
00361 show_fileselection();
00362 }
00363
00364
00365 void
00366 on_button_load_clicked (GtkButton *button,
00367 gpointer user_data)
00368 {
00369 fileselection_flag = LOAD;
00370 show_fileselection();
00371 }
00372
00373 void
00374 on_filesel_ok_clicked (GtkButton *button,
00375 gpointer user_data)
00376 {
00377 gtk_widget_hide(fileselection);
00378 const char *filename = gtk_file_selection_get_filename(GTK_FILE_SELECTION(fileselection));
00379
00380 if(fileselection_flag == SAVE)
00381 {
00382 FILE *fp = fopen(filename,"w");
00383 if(fp)
00384 {
00385 for(list<point>::iterator p = point_list.begin(); p != point_list.end();p++)
00386 fprintf(fp,"%d 1:%f 2:%f\n", p->value, p->x, p->y);
00387 fclose(fp);
00388 }
00389
00390 }
00391 else if(fileselection_flag == LOAD)
00392 {
00393 FILE *fp = fopen(filename,"r");
00394 if(fp)
00395 {
00396 clear_all();
00397 char buf[4096];
00398 while(fgets(buf,sizeof(buf),fp))
00399 {
00400 int v;
00401 double x,y;
00402 if(sscanf(buf,"%d%*d:%lf%*d:%lf",&v,&x,&y)!=3)
00403 break;
00404 point p = {x,y,v};
00405 point_list.push_back(p);
00406 }
00407 fclose(fp);
00408 draw_all_points();
00409 }
00410 }
00411 }
00412
00413 void
00414 on_fileselection_destroy (GtkObject *object,
00415 gpointer user_data)
00416 {
00417 }
00418
00419 void
00420 on_filesel_cancel_clicked (GtkButton *button,
00421 gpointer user_data)
00422 {
00423 }