callbacks.cpp
上传用户:xgw_05
上传日期:2014-12-08
资源大小:2726k
文件大小:10k
源码类别:

.net编程

开发平台:

Java

  1. #include <gtk/gtk.h>
  2. #include <stdio.h>
  3. #include <ctype.h>
  4. #include <list>
  5. #include "callbacks.h"
  6. #include "interface.h"
  7. #include "../../svm.h"
  8. #define DEFAULT_PARAM "-t 2 -c 100"
  9. #define XLEN 500
  10. #define YLEN 500
  11. GdkColor colors[] = 
  12. {
  13. {0,0,0,0},
  14. {0,0,120<<8,120<<8},
  15. {0,120<<8,120<<8,0},
  16. {0,120<<8,0,120<<8},
  17. {0,0,200<<8,200<<8},
  18. {0,200<<8,200<<8,0},
  19. {0,200<<8,0,200<<8},
  20. };
  21. GdkGC *gc;
  22. GdkPixmap *pixmap;
  23. extern "C" GtkWidget *draw_main;
  24. GtkWidget *draw_main;
  25. extern "C" GtkWidget *entry_option;
  26. GtkWidget *entry_option;
  27. typedef struct {
  28. double x, y;
  29. signed char value;
  30. } point;
  31. list<point> point_list;
  32. int current_value = 1;
  33. extern "C" void svm_toy_initialize()
  34. {
  35. gboolean success[7];
  36. gdk_colormap_alloc_colors(
  37. gdk_colormap_get_system(),
  38. colors,
  39. 7,
  40. FALSE,
  41. TRUE,
  42. success);
  43. gc = gdk_gc_new(draw_main->window);
  44. pixmap = gdk_pixmap_new(draw_main->window,XLEN,YLEN,-1);
  45. gdk_gc_set_foreground(gc,&colors[0]);
  46. gdk_draw_rectangle(pixmap,gc,TRUE,0,0,XLEN,YLEN);
  47. gtk_entry_set_text(GTK_ENTRY(entry_option),DEFAULT_PARAM);
  48. }
  49. void redraw_area(GtkWidget* widget, int x, int y, int w, int h)
  50. {
  51. gdk_draw_pixmap(widget->window,
  52. gc,
  53. pixmap,
  54. x,y,x,y,w,h);
  55. }
  56. void draw_point(const point& p)
  57. {
  58. gdk_gc_set_foreground(gc,&colors[p.value+3]);
  59. gdk_draw_rectangle(pixmap, gc, TRUE,int(p.x*XLEN),int(p.y*YLEN),4,4);
  60. gdk_draw_rectangle(draw_main->window, gc, TRUE,int(p.x*XLEN),int(p.y*YLEN),4,4);
  61. }
  62. void draw_all_points()
  63. {
  64. for(list<point>::iterator p = point_list.begin(); p != point_list.end();p++)
  65. draw_point(*p);
  66. }
  67. void clear_all()
  68. {
  69. point_list.clear();
  70. gdk_gc_set_foreground(gc,&colors[0]);
  71. gdk_draw_rectangle(pixmap,gc,TRUE,0,0,XLEN,YLEN);
  72. redraw_area(draw_main,0,0,XLEN,YLEN);
  73. }
  74. void
  75. on_button_change_clicked               (GtkButton       *button,
  76.                                         gpointer         user_data)
  77. {
  78. ++current_value;
  79. if(current_value > 3) current_value = 1;
  80. }
  81. void
  82. on_button_run_clicked                  (GtkButton       *button,
  83.                                         gpointer         user_data)
  84. {
  85. // guard
  86. if(point_list.empty()) return;
  87. svm_parameter param;
  88. int i,j;
  89. // default values
  90. param.svm_type = C_SVC;
  91. param.kernel_type = RBF;
  92. param.degree = 3;
  93. param.gamma = 0;
  94. param.coef0 = 0;
  95. param.nu = 0.5;
  96. param.cache_size = 40;
  97. param.C = 1;
  98. param.eps = 1e-3;
  99. param.p = 0.1;
  100. param.shrinking = 1;
  101. param.nr_weight = 0;
  102. param.weight_label = NULL;
  103. param.weight = NULL;
  104. // parse options
  105. const char *p = gtk_entry_get_text(GTK_ENTRY(entry_option));
  106. while (1) {
  107. while (*p && *p != '-')
  108. p++;
  109. if (*p == '')
  110. break;
  111. p++;
  112. switch (*p++) {
  113. case 's':
  114. param.svm_type = atoi(p);
  115. break;
  116. case 't':
  117. param.kernel_type = atoi(p);
  118. break;
  119. case 'd':
  120. param.degree = atof(p);
  121. break;
  122. case 'g':
  123. param.gamma = atof(p);
  124. break;
  125. case 'r':
  126. param.coef0 = atof(p);
  127. break;
  128. case 'n':
  129. param.nu = atof(p);
  130. break;
  131. case 'm':
  132. param.cache_size = atof(p);
  133. break;
  134. case 'c':
  135. param.C = atof(p);
  136. break;
  137. case 'e':
  138. param.eps = atof(p);
  139. break;
  140. case 'p':
  141. param.p = atof(p);
  142. break;
  143. case 'h':
  144. param.shrinking = atoi(p);
  145. break;
  146. case 'w':
  147. ++param.nr_weight;
  148. param.weight_label = (int *)realloc(param.weight_label,sizeof(int)*param.nr_weight);
  149. param.weight = (double *)realloc(param.weight,sizeof(double)*param.nr_weight);
  150. param.weight_label[param.nr_weight-1] = atoi(p);
  151. while(*p && !isspace(*p)) ++p;
  152. param.weight[param.nr_weight-1] = atof(p);
  153. break;
  154. }
  155. }
  156. // build problem
  157. svm_problem prob;
  158. prob.l = point_list.size();
  159. prob.y = new double[prob.l];
  160. if(param.svm_type == EPSILON_SVR ||
  161.    param.svm_type == NU_SVR)
  162. {
  163. if(param.gamma == 0) param.gamma = 1;
  164. svm_node *x_space = new svm_node[2 * prob.l];
  165. prob.x = new svm_node *[prob.l];
  166. i = 0;
  167. for (list <point>::iterator q = point_list.begin(); q != point_list.end(); q++, i++)
  168. {
  169. x_space[2 * i].index = 1;
  170. x_space[2 * i].value = q->x;
  171. x_space[2 * i + 1].index = -1;
  172. prob.x[i] = &x_space[2 * i];
  173. prob.y[i] = q->y;
  174. }
  175. // build model & classify
  176. svm_model *model = svm_train(&prob, &param);
  177. svm_node x[2];
  178. x[0].index = 1;
  179. x[1].index = -1;
  180. int *j = new int[XLEN];
  181. for (i = 0; i < XLEN; i++) 
  182. {
  183. x[0].value = (double) i / XLEN;
  184. j[i] = (int)(YLEN*svm_predict(model, x));
  185. }
  186. gdk_gc_set_foreground(gc,&colors[0]);
  187. gdk_draw_line(pixmap,gc,0,0,0,YLEN-1);
  188. gdk_draw_line(draw_main->window,gc,0,0,0,YLEN-1);
  189. int p = (int)(param.p * YLEN);
  190. for(i = 1; i < XLEN; i++)
  191. {
  192. gdk_gc_set_foreground(gc,&colors[0]);
  193. gdk_draw_line(pixmap,gc,i,0,i,YLEN-1);
  194. gdk_draw_line(draw_main->window,gc,i,0,i,YLEN-1);
  195. gdk_gc_set_foreground(gc,&colors[5]);
  196. gdk_draw_line(pixmap,gc,i-1,j[i-1],i,j[i]);
  197. gdk_draw_line(draw_main->window,gc,i-1,j[i-1],i,j[i]);
  198. if(param.svm_type == EPSILON_SVR)
  199. {
  200. gdk_gc_set_foreground(gc,&colors[2]);
  201. gdk_draw_line(pixmap,gc,i-1,j[i-1]+p,i,j[i]+p);
  202. gdk_draw_line(draw_main->window,gc,i-1,j[i-1]+p,i,j[i]+p);
  203. gdk_gc_set_foreground(gc,&colors[2]);
  204. gdk_draw_line(pixmap,gc,i-1,j[i-1]-p,i,j[i]-p);
  205. gdk_draw_line(draw_main->window,gc,i-1,j[i-1]-p,i,j[i]-p);
  206. }
  207. }
  208. svm_destroy_model(model);
  209. delete[] j;
  210. delete[] x_space;
  211. delete[] prob.x;
  212. delete[] prob.y;
  213. }
  214. else
  215. {
  216. if(param.gamma == 0) param.gamma = 0.5;
  217. svm_node *x_space = new svm_node[3 * prob.l];
  218. prob.x = new svm_node *[prob.l];
  219. i = 0;
  220. for (list <point>::iterator q = point_list.begin(); q != point_list.end(); q++, i++)
  221. {
  222. x_space[3 * i].index = 1;
  223. x_space[3 * i].value = q->x;
  224. x_space[3 * i + 1].index = 2;
  225. x_space[3 * i + 1].value = q->y;
  226. x_space[3 * i + 2].index = -1;
  227. prob.x[i] = &x_space[3 * i];
  228. prob.y[i] = q->value;
  229. }
  230. // build model & classify
  231. svm_model *model = svm_train(&prob, &param);
  232. svm_node x[3];
  233. x[0].index = 1;
  234. x[1].index = 2;
  235. x[2].index = -1;
  236. for (i = 0; i < XLEN; i++) 
  237. for (j = 0; j < YLEN; j++) {
  238. x[0].value = (double) i / XLEN;
  239. x[1].value = (double) j / YLEN;
  240. double d = svm_predict(model, x);
  241. gdk_gc_set_foreground(gc,&colors[(int)d]);
  242. gdk_draw_point(pixmap,gc,i,j);
  243. gdk_draw_point(draw_main->window,gc,i,j);
  244. }
  245. svm_destroy_model(model);
  246. delete[] x_space;
  247. delete[] prob.x;
  248. delete[] prob.y;
  249. }
  250. free(param.weight_label);
  251. free(param.weight);
  252. draw_all_points();
  253. }
  254. void
  255. on_button_clear_clicked                (GtkButton       *button,
  256.                                         gpointer         user_data)
  257. {
  258. clear_all();
  259. }
  260. void
  261. on_window1_destroy                     (GtkObject       *object,
  262.                                         gpointer         user_data)
  263. {
  264. gtk_exit(0);
  265. }
  266. gboolean
  267. on_draw_main_button_press_event        (GtkWidget       *widget,
  268.                                         GdkEventButton  *event,
  269.                                         gpointer         user_data)
  270. {
  271. point p = {(double)event->x/XLEN, (double)event->y/YLEN, current_value};
  272. point_list.push_back(p);
  273. draw_point(p);
  274. return FALSE;
  275. }
  276. gboolean
  277. on_draw_main_expose_event              (GtkWidget       *widget,
  278.                                         GdkEventExpose  *event,
  279.                                         gpointer         user_data)
  280. {
  281. redraw_area(widget,
  282.     event->area.x, event->area.y,
  283.     event->area.width, event->area.height);
  284. return FALSE;
  285. }
  286. GtkWidget *fileselection;
  287. enum { SAVE, LOAD } fileselection_flag;
  288. void show_fileselection()
  289. {
  290. fileselection = create_fileselection();
  291. gtk_signal_connect_object(
  292. GTK_OBJECT(GTK_FILE_SELECTION(fileselection)->ok_button),
  293. "clicked", GTK_SIGNAL_FUNC(gtk_widget_destroy),
  294. (GtkObject *) fileselection);
  295. gtk_signal_connect_object (GTK_OBJECT
  296. (GTK_FILE_SELECTION(fileselection)->cancel_button),
  297. "clicked", GTK_SIGNAL_FUNC(gtk_widget_destroy),
  298. (GtkObject *) fileselection);
  299. gtk_widget_show(fileselection);
  300. }
  301. void
  302. on_button_save_clicked                 (GtkButton       *button,
  303.                                         gpointer         user_data)
  304. {
  305. fileselection_flag = SAVE;
  306. show_fileselection();
  307. }
  308. void
  309. on_button_load_clicked                 (GtkButton       *button,
  310.                                         gpointer         user_data)
  311. {
  312. fileselection_flag = LOAD;
  313. show_fileselection();
  314. }
  315. void
  316. on_filesel_ok_clicked                  (GtkButton       *button,
  317.                                         gpointer         user_data)
  318. {
  319. gtk_widget_hide(fileselection);
  320. const char *filename = gtk_file_selection_get_filename(GTK_FILE_SELECTION(fileselection));
  321. if(fileselection_flag == SAVE)
  322. {
  323. FILE *fp = fopen(filename,"w");
  324. if(fp)
  325. {
  326. for(list<point>::iterator p = point_list.begin(); p != point_list.end();p++)
  327. fprintf(fp,"%d 1:%f 2:%fn", p->value, p->x, p->y);
  328. fclose(fp);
  329. }
  330. }
  331. else if(fileselection_flag == LOAD)
  332. {
  333. FILE *fp = fopen(filename,"r");
  334. if(fp)
  335. {
  336. clear_all();
  337. char buf[4096];
  338. while(fgets(buf,sizeof(buf),fp))
  339. {
  340. int v;
  341. double x,y;
  342. if(sscanf(buf,"%d%*d:%lf%*d:%lf",&v,&x,&y)!=3)
  343. break;
  344. point p = {x,y,v};
  345. point_list.push_back(p);
  346. }
  347. fclose(fp);
  348. draw_all_points();
  349. }
  350. }
  351. }
  352. void
  353. on_fileselection_destroy               (GtkObject       *object,
  354.                                         gpointer         user_data)
  355. {
  356. }
  357. void
  358. on_filesel_cancel_clicked              (GtkButton       *button,
  359.                                         gpointer         user_data)
  360. {
  361. }