00001 #include <windows.h>
00002 #include <windowsx.h>
00003 #include <stdio.h>
00004 #include <ctype.h>
00005 #include <list>
00006 #include "../../svm.h"
00007 using namespace std;
00008
00009 #define DEFAULT_PARAM "-t 2 -c 100"
00010 #define XLEN 500
00011 #define YLEN 500
00012 #define DrawLine(dc,x1,y1,x2,y2,c) \
00013 do { \
00014 HPEN hpen = CreatePen(PS_SOLID,0,c); \
00015 HPEN horig = SelectPen(dc,hpen); \
00016 MoveToEx(dc,x1,y1,NULL); \
00017 LineTo(dc,x2,y2); \
00018 SelectPen(dc,horig); \
00019 DeletePen(hpen); \
00020 } while(0)
00021
00022 using namespace std;
00023
00024 COLORREF colors[] =
00025 {
00026 RGB(0,0,0),
00027 RGB(0,120,120),
00028 RGB(120,120,0),
00029 RGB(120,0,120),
00030 RGB(0,200,200),
00031 RGB(200,200,0),
00032 RGB(200,0,200)
00033 };
00034
00035 HWND main_window;
00036 HBITMAP buffer;
00037 HDC window_dc;
00038 HDC buffer_dc;
00039 HBRUSH brush1, brush2, brush3;
00040 HWND edit;
00041
00042 enum {
00043 ID_BUTTON_CHANGE, ID_BUTTON_RUN, ID_BUTTON_CLEAR,
00044 ID_BUTTON_LOAD, ID_BUTTON_SAVE, ID_EDIT
00045 };
00046
00047 struct point {
00048 double x, y;
00049 signed char value;
00050 };
00051
00052 list<point> point_list;
00053 int current_value = 1;
00054
00055 LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);
00056
00057 int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
00058 PSTR szCmdLine, int iCmdShow)
00059 {
00060 static char szAppName[] = "SvmToy";
00061 MSG msg;
00062 WNDCLASSEX wndclass;
00063
00064 wndclass.cbSize = sizeof(wndclass);
00065 wndclass.style = CS_HREDRAW | CS_VREDRAW;
00066 wndclass.lpfnWndProc = WndProc;
00067 wndclass.cbClsExtra = 0;
00068 wndclass.cbWndExtra = 0;
00069 wndclass.hInstance = hInstance;
00070 wndclass.hIcon = LoadIcon(NULL, IDI_APPLICATION);
00071 wndclass.hCursor = LoadCursor(NULL, IDC_ARROW);
00072 wndclass.hbrBackground = (HBRUSH) GetStockObject(BLACK_BRUSH);
00073 wndclass.lpszMenuName = NULL;
00074 wndclass.lpszClassName = szAppName;
00075 wndclass.hIconSm = LoadIcon(NULL, IDI_APPLICATION);
00076
00077 RegisterClassEx(&wndclass);
00078
00079 main_window = CreateWindow(szAppName,
00080 "SVM Toy",
00081 WS_OVERLAPPEDWINDOW,
00082 CW_USEDEFAULT,
00083 CW_USEDEFAULT,
00084 XLEN,
00085 YLEN+52,
00086 NULL,
00087 NULL,
00088 hInstance,
00089 NULL);
00090
00091 ShowWindow(main_window, iCmdShow);
00092 UpdateWindow(main_window);
00093
00094 CreateWindow("button", "Change", WS_CHILD | WS_VISIBLE | BS_PUSHBUTTON,
00095 0, YLEN, 50, 25, main_window, (HMENU) ID_BUTTON_CHANGE, hInstance, NULL);
00096 CreateWindow("button", "Run", WS_CHILD | WS_VISIBLE | BS_PUSHBUTTON,
00097 50, YLEN, 50, 25, main_window, (HMENU) ID_BUTTON_RUN, hInstance, NULL);
00098 CreateWindow("button", "Clear", WS_CHILD | WS_VISIBLE | BS_PUSHBUTTON,
00099 100, YLEN, 50, 25, main_window, (HMENU) ID_BUTTON_CLEAR, hInstance, NULL);
00100 CreateWindow("button", "Save", WS_CHILD | WS_VISIBLE | BS_PUSHBUTTON,
00101 150, YLEN, 50, 25, main_window, (HMENU) ID_BUTTON_SAVE, hInstance, NULL);
00102 CreateWindow("button", "Load", WS_CHILD | WS_VISIBLE | BS_PUSHBUTTON,
00103 200, YLEN, 50, 25, main_window, (HMENU) ID_BUTTON_LOAD, hInstance, NULL);
00104
00105 edit = CreateWindow("edit", NULL, WS_CHILD | WS_VISIBLE,
00106 250, YLEN, 250, 25, main_window, (HMENU) ID_EDIT, hInstance, NULL);
00107
00108 Edit_SetText(edit,DEFAULT_PARAM);
00109
00110 brush1 = CreateSolidBrush(colors[4]);
00111 brush2 = CreateSolidBrush(colors[5]);
00112 brush3 = CreateSolidBrush(colors[6]);
00113
00114 window_dc = GetDC(main_window);
00115 buffer = CreateCompatibleBitmap(window_dc, XLEN, YLEN);
00116 buffer_dc = CreateCompatibleDC(window_dc);
00117 SelectObject(buffer_dc, buffer);
00118 PatBlt(buffer_dc, 0, 0, XLEN, YLEN, BLACKNESS);
00119
00120 while (GetMessage(&msg, NULL, 0, 0)) {
00121 TranslateMessage(&msg);
00122 DispatchMessage(&msg);
00123 }
00124 return msg.wParam;
00125 }
00126
00127 int getfilename( HWND hWnd , char *filename, int len, int save)
00128 {
00129 OPENFILENAME OpenFileName;
00130 memset(&OpenFileName,0,sizeof(OpenFileName));
00131 filename[0]='\0';
00132
00133 OpenFileName.lStructSize = sizeof(OPENFILENAME);
00134 OpenFileName.hwndOwner = hWnd;
00135 OpenFileName.lpstrFile = filename;
00136 OpenFileName.nMaxFile = len;
00137 OpenFileName.Flags = 0;
00138
00139 return save?GetSaveFileName(&OpenFileName):GetOpenFileName(&OpenFileName);
00140 }
00141
00142 void clear_all()
00143 {
00144 point_list.clear();
00145 PatBlt(buffer_dc, 0, 0, XLEN, YLEN, BLACKNESS);
00146 InvalidateRect(main_window, 0, 0);
00147 }
00148
00149 HBRUSH choose_brush(int v)
00150 {
00151 if(v==1) return brush1;
00152 else if(v==2) return brush2;
00153 else return brush3;
00154 }
00155
00156 void draw_point(const point & p)
00157 {
00158 RECT rect;
00159 rect.left = int(p.x*XLEN);
00160 rect.top = int(p.y*YLEN);
00161 rect.right = int(p.x*XLEN) + 3;
00162 rect.bottom = int(p.y*YLEN) + 3;
00163 FillRect(window_dc, &rect, choose_brush(p.value));
00164 FillRect(buffer_dc, &rect, choose_brush(p.value));
00165 }
00166
00167 void draw_all_points()
00168 {
00169 for(list<point>::iterator p = point_list.begin(); p != point_list.end(); p++)
00170 draw_point(*p);
00171 }
00172
00173 void button_run_clicked()
00174 {
00175
00176 if(point_list.empty()) return;
00177
00178 svm_parameter param;
00179 int i,j;
00180
00181
00182 param.svm_type = C_SVC;
00183 param.kernel_type = RBF;
00184 param.degree = 3;
00185 param.gamma = 0;
00186 param.coef0 = 0;
00187 param.nu = 0.5;
00188 param.cache_size = 100;
00189 param.C = 1;
00190 param.eps = 1e-3;
00191 param.p = 0.1;
00192 param.shrinking = 1;
00193 param.probability = 0;
00194 param.nr_weight = 0;
00195 param.weight_label = NULL;
00196 param.weight = NULL;
00197
00198
00199 char str[1024];
00200 Edit_GetLine(edit, 0, str, sizeof(str));
00201 const char *p = str;
00202
00203 while (1) {
00204 while (*p && *p != '-')
00205 p++;
00206
00207 if (*p == '\0')
00208 break;
00209
00210 p++;
00211 switch (*p++) {
00212 case 's':
00213 param.svm_type = atoi(p);
00214 break;
00215 case 't':
00216 param.kernel_type = atoi(p);
00217 break;
00218 case 'd':
00219 param.degree = atoi(p);
00220 break;
00221 case 'g':
00222 param.gamma = atof(p);
00223 break;
00224 case 'r':
00225 param.coef0 = atof(p);
00226 break;
00227 case 'n':
00228 param.nu = atof(p);
00229 break;
00230 case 'm':
00231 param.cache_size = atof(p);
00232 break;
00233 case 'c':
00234 param.C = atof(p);
00235 break;
00236 case 'e':
00237 param.eps = atof(p);
00238 break;
00239 case 'p':
00240 param.p = atof(p);
00241 break;
00242 case 'h':
00243 param.shrinking = atoi(p);
00244 break;
00245 case 'b':
00246 param.probability = atoi(p);
00247 break;
00248 case 'w':
00249 ++param.nr_weight;
00250 param.weight_label = (int *)realloc(param.weight_label,sizeof(int)*param.nr_weight);
00251 param.weight = (double *)realloc(param.weight,sizeof(double)*param.nr_weight);
00252 param.weight_label[param.nr_weight-1] = atoi(p);
00253 while(*p && !isspace(*p)) ++p;
00254 param.weight[param.nr_weight-1] = atof(p);
00255 break;
00256 }
00257 }
00258
00259
00260 svm_problem prob;
00261
00262 prob.l = point_list.size();
00263 prob.y = new double[prob.l];
00264
00265 if(param.kernel_type == PRECOMPUTED)
00266 {
00267 }
00268 else if(param.svm_type == EPSILON_SVR ||
00269 param.svm_type == NU_SVR)
00270 {
00271 if(param.gamma == 0) param.gamma = 1;
00272 svm_node *x_space = new svm_node[2 * prob.l];
00273 prob.x = new svm_node *[prob.l];
00274
00275 i = 0;
00276 for (list<point>::iterator q = point_list.begin(); q != point_list.end(); q++, i++)
00277 {
00278 x_space[2 * i].index = 1;
00279 x_space[2 * i].value = q->x;
00280 x_space[2 * i + 1].index = -1;
00281 prob.x[i] = &x_space[2 * i];
00282 prob.y[i] = q->y;
00283 }
00284
00285
00286 svm_model *model = svm_train(&prob, ¶m);
00287 svm_node x[2];
00288 x[0].index = 1;
00289 x[1].index = -1;
00290 int *j = new int[XLEN];
00291
00292 for (i = 0; i < XLEN; i++)
00293 {
00294 x[0].value = (double) i / XLEN;
00295 j[i] = (int)(YLEN*svm_predict(model, x));
00296 }
00297
00298 DrawLine(buffer_dc,0,0,0,YLEN,colors[0]);
00299 DrawLine(window_dc,0,0,0,YLEN,colors[0]);
00300
00301 int p = (int)(param.p * YLEN);
00302 for(int i=1; i < XLEN; i++)
00303 {
00304 DrawLine(buffer_dc,i,0,i,YLEN,colors[0]);
00305 DrawLine(window_dc,i,0,i,YLEN,colors[0]);
00306
00307 DrawLine(buffer_dc,i-1,j[i-1],i,j[i],colors[5]);
00308 DrawLine(window_dc,i-1,j[i-1],i,j[i],colors[5]);
00309
00310 if(param.svm_type == EPSILON_SVR)
00311 {
00312 DrawLine(buffer_dc,i-1,j[i-1]+p,i,j[i]+p,colors[2]);
00313 DrawLine(window_dc,i-1,j[i-1]+p,i,j[i]+p,colors[2]);
00314
00315 DrawLine(buffer_dc,i-1,j[i-1]-p,i,j[i]-p,colors[2]);
00316 DrawLine(window_dc,i-1,j[i-1]-p,i,j[i]-p,colors[2]);
00317 }
00318 }
00319
00320 svm_free_and_destroy_model(&model);
00321 delete[] j;
00322 delete[] x_space;
00323 delete[] prob.x;
00324 delete[] prob.y;
00325 }
00326 else
00327 {
00328 if(param.gamma == 0) param.gamma = 0.5;
00329 svm_node *x_space = new svm_node[3 * prob.l];
00330 prob.x = new svm_node *[prob.l];
00331
00332 i = 0;
00333 for (list<point>::iterator q = point_list.begin(); q != point_list.end(); q++, i++)
00334 {
00335 x_space[3 * i].index = 1;
00336 x_space[3 * i].value = q->x;
00337 x_space[3 * i + 1].index = 2;
00338 x_space[3 * i + 1].value = q->y;
00339 x_space[3 * i + 2].index = -1;
00340 prob.x[i] = &x_space[3 * i];
00341 prob.y[i] = q->value;
00342 }
00343
00344
00345 svm_model *model = svm_train(&prob, ¶m);
00346 svm_node x[3];
00347 x[0].index = 1;
00348 x[1].index = 2;
00349 x[2].index = -1;
00350
00351 for (i = 0; i < XLEN; i++)
00352 for (j = 0; j < YLEN; j++) {
00353 x[0].value = (double) i / XLEN;
00354 x[1].value = (double) j / YLEN;
00355 double d = svm_predict(model, x);
00356 if (param.svm_type == ONE_CLASS && d<0) d=2;
00357 SetPixel(window_dc, i, j, colors[(int)d]);
00358 SetPixel(buffer_dc, i, j, colors[(int)d]);
00359 }
00360
00361 svm_free_and_destroy_model(&model);
00362 delete[] x_space;
00363 delete[] prob.x;
00364 delete[] prob.y;
00365 }
00366 free(param.weight_label);
00367 free(param.weight);
00368 draw_all_points();
00369 }
00370
00371 LRESULT CALLBACK WndProc(HWND hwnd, UINT iMsg, WPARAM wParam, LPARAM lParam)
00372 {
00373 HDC hdc;
00374 PAINTSTRUCT ps;
00375
00376 switch (iMsg) {
00377 case WM_LBUTTONDOWN:
00378 {
00379 int x = LOWORD(lParam);
00380 int y = HIWORD(lParam);
00381 point p = {(double)x/XLEN, (double)y/YLEN, current_value};
00382 point_list.push_back(p);
00383 draw_point(p);
00384 }
00385 return 0;
00386 case WM_PAINT:
00387 {
00388 hdc = BeginPaint(hwnd, &ps);
00389 BitBlt(hdc, 0, 0, XLEN, YLEN, buffer_dc, 0, 0, SRCCOPY);
00390 EndPaint(hwnd, &ps);
00391 }
00392 return 0;
00393 case WM_COMMAND:
00394 {
00395 int id = LOWORD(wParam);
00396 switch (id) {
00397 case ID_BUTTON_CHANGE:
00398 ++current_value;
00399 if(current_value > 3) current_value = 1;
00400 break;
00401 case ID_BUTTON_RUN:
00402 button_run_clicked();
00403 break;
00404 case ID_BUTTON_CLEAR:
00405 clear_all();
00406 break;
00407 case ID_BUTTON_SAVE:
00408 {
00409 char filename[1024];
00410 if(getfilename(hwnd,filename,1024,1))
00411 {
00412 FILE *fp = fopen(filename,"w");
00413 if(fp)
00414 {
00415 for (list<point>::iterator p = point_list.begin(); p != point_list.end(); p++)
00416 fprintf(fp,"%d 1:%f 2:%f\n",p->value,p->x,p->y);
00417 fclose(fp);
00418 }
00419 }
00420 }
00421 break;
00422 case ID_BUTTON_LOAD:
00423 {
00424 char filename[1024];
00425 if(getfilename(hwnd,filename,1024,0))
00426 {
00427 FILE *fp = fopen(filename,"r");
00428 if(fp)
00429 {
00430 clear_all();
00431 char buf[4096];
00432 while(fgets(buf,sizeof(buf),fp))
00433 {
00434 int v;
00435 double x,y;
00436 if(sscanf(buf,"%d%*d:%lf%*d:%lf",&v,&x,&y)!=3)
00437 break;
00438 point p = {x,y,v};
00439 point_list.push_back(p);
00440 }
00441 fclose(fp);
00442 draw_all_points();
00443 }
00444 }
00445 }
00446 break;
00447 }
00448 }
00449 return 0;
00450 case WM_DESTROY:
00451 PostQuitMessage(0);
00452 return 0;
00453 }
00454
00455 return DefWindowProc(hwnd, iMsg, wParam, lParam);
00456 }