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


ml_classifiers
Author(s): Scott Niekum
autogenerated on Fri Jan 3 2014 11:30:23