svm-toy.cpp
Go to the documentation of this file.
00001 #include <QtGui>
00002 #include <stdio.h>
00003 #include <stdlib.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 
00013 QRgb colors[] =
00014 {
00015         qRgb(0,0,0),
00016         qRgb(0,120,120),
00017         qRgb(120,120,0),
00018         qRgb(120,0,120),
00019         qRgb(0,200,200),
00020         qRgb(200,200,0),
00021         qRgb(200,0,200)
00022 };
00023 
00024 class SvmToyWindow : public QWidget
00025 {
00026 
00027 Q_OBJECT
00028 
00029 public:
00030         SvmToyWindow();
00031         ~SvmToyWindow();
00032 protected:
00033         virtual void mousePressEvent( QMouseEvent* );
00034         virtual void paintEvent( QPaintEvent* );
00035 
00036 private:
00037         QPixmap buffer;
00038         QPixmap icon1;
00039         QPixmap icon2;
00040         QPixmap icon3;
00041         QPushButton button_change_icon;
00042         QPushButton button_run;
00043         QPushButton button_clear;
00044         QPushButton button_save;
00045         QPushButton button_load;
00046         QLineEdit input_line;
00047         QPainter buffer_painter;
00048         struct point {
00049                 double x, y;
00050                 signed char value;
00051         };
00052         list<point> point_list;
00053         int current_value;
00054         const QPixmap& choose_icon(int v)
00055         {
00056                 if(v==1) return icon1;
00057                 else if(v==2) return icon2;
00058                 else return icon3;
00059         }
00060         void clear_all()
00061         {
00062                 point_list.clear();
00063                 buffer.fill(Qt::black);
00064                 repaint();
00065         }
00066         void draw_point(const point& p)
00067         {
00068                 const QPixmap& icon = choose_icon(p.value);
00069                 buffer_painter.drawPixmap((int)(p.x*XLEN),(int)(p.y*YLEN),icon);
00070                 repaint();
00071         }
00072         void draw_all_points()
00073         {
00074                 for(list<point>::iterator p = point_list.begin(); p != point_list.end();p++)
00075                         draw_point(*p); 
00076         }
00077 private slots: 
00078         void button_change_icon_clicked()
00079         {
00080                 ++current_value;
00081                 if(current_value > 3) current_value = 1;
00082                 button_change_icon.setIcon(choose_icon(current_value));
00083         }
00084         void button_run_clicked()
00085         {
00086                 // guard
00087                 if(point_list.empty()) return;
00088 
00089                 svm_parameter param;
00090                 int i,j;        
00091 
00092                 // default values
00093                 param.svm_type = C_SVC;
00094                 param.kernel_type = RBF;
00095                 param.degree = 3;
00096                 param.gamma = 0;
00097                 param.coef0 = 0;
00098                 param.nu = 0.5;
00099                 param.cache_size = 100;
00100                 param.C = 1;
00101                 param.eps = 1e-3;
00102                 param.p = 0.1;
00103                 param.shrinking = 1;
00104                 param.probability = 0;
00105                 param.nr_weight = 0;
00106                 param.weight_label = NULL;
00107                 param.weight = NULL;
00108 
00109                 // parse options
00110                 const char *p = input_line.text().toAscii().constData();
00111 
00112                 while (1) {
00113                         while (*p && *p != '-')
00114                                 p++;
00115 
00116                         if (*p == '\0')
00117                                 break;
00118 
00119                         p++;
00120                         switch (*p++) {
00121                                 case 's':
00122                                         param.svm_type = atoi(p);
00123                                         break;
00124                                 case 't':
00125                                         param.kernel_type = atoi(p);
00126                                         break;
00127                                 case 'd':
00128                                         param.degree = atoi(p);
00129                                         break;
00130                                 case 'g':
00131                                         param.gamma = atof(p);
00132                                         break;
00133                                 case 'r':
00134                                         param.coef0 = atof(p);
00135                                         break;
00136                                 case 'n':
00137                                         param.nu = atof(p);
00138                                         break;
00139                                 case 'm':
00140                                         param.cache_size = atof(p);
00141                                         break;
00142                                 case 'c':
00143                                         param.C = atof(p);
00144                                         break;
00145                                 case 'e':
00146                                         param.eps = atof(p);
00147                                         break;
00148                                 case 'p':
00149                                         param.p = atof(p);
00150                                         break;
00151                                 case 'h':
00152                                         param.shrinking = atoi(p);
00153                                         break;
00154                                 case 'b':
00155                                         param.probability = atoi(p);
00156                                         break;
00157                                 case 'w':
00158                                         ++param.nr_weight;
00159                                         param.weight_label = (int *)realloc(param.weight_label,sizeof(int)*param.nr_weight);
00160                                         param.weight = (double *)realloc(param.weight,sizeof(double)*param.nr_weight);
00161                                         param.weight_label[param.nr_weight-1] = atoi(p);
00162                                         while(*p && !isspace(*p)) ++p;
00163                                         param.weight[param.nr_weight-1] = atof(p);
00164                                         break;
00165                         }
00166                 }
00167         
00168                 // build problem
00169                 svm_problem prob;
00170 
00171                 prob.l = point_list.size();
00172                 prob.y = new double[prob.l];
00173 
00174                 if(param.kernel_type == PRECOMPUTED)
00175                 {
00176                 }
00177                 else if(param.svm_type == EPSILON_SVR ||
00178                         param.svm_type == NU_SVR)
00179                 {
00180                         if(param.gamma == 0) param.gamma = 1;
00181                         svm_node *x_space = new svm_node[2 * prob.l];
00182                         prob.x = new svm_node *[prob.l];
00183 
00184                         i = 0;
00185                         for (list <point>::iterator q = point_list.begin(); q != point_list.end(); q++, i++)
00186                         {
00187                                 x_space[2 * i].index = 1;
00188                                 x_space[2 * i].value = q->x;
00189                                 x_space[2 * i + 1].index = -1;
00190                                 prob.x[i] = &x_space[2 * i];
00191                                 prob.y[i] = q->y;
00192                         }
00193 
00194                         // build model & classify
00195                         svm_model *model = svm_train(&prob, &param);
00196                         svm_node x[2];
00197                         x[0].index = 1;
00198                         x[1].index = -1;
00199                         int *j = new int[XLEN];
00200 
00201                         for (i = 0; i < XLEN; i++)
00202                         {
00203                                 x[0].value = (double) i / XLEN;
00204                                 j[i] = (int)(YLEN*svm_predict(model, x));
00205                         }
00206                         
00207                         buffer_painter.setPen(colors[0]);
00208                         buffer_painter.drawLine(0,0,0,YLEN-1);
00209 
00210                         int p = (int)(param.p * YLEN);
00211                         for(i = 1; i < XLEN; i++)
00212                         {
00213                                 buffer_painter.setPen(colors[0]);
00214                                 buffer_painter.drawLine(i,0,i,YLEN-1);
00215                         
00216                                 buffer_painter.setPen(colors[5]);
00217                                 buffer_painter.drawLine(i-1,j[i-1],i,j[i]);
00218                                 
00219                                 if(param.svm_type == EPSILON_SVR)
00220                                 {
00221                                         buffer_painter.setPen(colors[2]);
00222                                         buffer_painter.drawLine(i-1,j[i-1]+p,i,j[i]+p);
00223 
00224                                         buffer_painter.setPen(colors[2]);
00225                                         buffer_painter.drawLine(i-1,j[i-1]-p,i,j[i]-p);
00226                                 }
00227                         }
00228 
00229                         svm_free_and_destroy_model(&model);
00230                         delete[] j;
00231                         delete[] x_space;
00232                         delete[] prob.x;
00233                         delete[] prob.y;
00234                 }
00235                 else
00236                 {
00237                         if(param.gamma == 0) param.gamma = 0.5;
00238                         svm_node *x_space = new svm_node[3 * prob.l];
00239                         prob.x = new svm_node *[prob.l];
00240 
00241                         i = 0;
00242                         for (list <point>::iterator q = point_list.begin(); q != point_list.end(); q++, i++)
00243                         {
00244                                 x_space[3 * i].index = 1;
00245                                 x_space[3 * i].value = q->x;
00246                                 x_space[3 * i + 1].index = 2;
00247                                 x_space[3 * i + 1].value = q->y;
00248                                 x_space[3 * i + 2].index = -1;
00249                                 prob.x[i] = &x_space[3 * i];
00250                                 prob.y[i] = q->value;
00251                         }
00252 
00253                         // build model & classify
00254                         svm_model *model = svm_train(&prob, &param);
00255                         svm_node x[3];
00256                         x[0].index = 1;
00257                         x[1].index = 2;
00258                         x[2].index = -1;
00259 
00260                         for (i = 0; i < XLEN; i++)
00261                                 for (j = 0; j < YLEN ; j++) {
00262                                         x[0].value = (double) i / XLEN;
00263                                         x[1].value = (double) j / YLEN;
00264                                         double d = svm_predict(model, x);
00265                                         if (param.svm_type == ONE_CLASS && d<0) d=2;
00266                                         buffer_painter.setPen(colors[(int)d]);
00267                                         buffer_painter.drawPoint(i,j);
00268                         }
00269 
00270                         svm_free_and_destroy_model(&model);
00271                         delete[] x_space;
00272                         delete[] prob.x;
00273                         delete[] prob.y;
00274                 }
00275                 free(param.weight_label);
00276                 free(param.weight);
00277                 draw_all_points();
00278         }
00279         void button_clear_clicked()
00280         {
00281                 clear_all();
00282         }
00283         void button_save_clicked()
00284         {
00285                 QString filename = QFileDialog::getSaveFileName();
00286                 if(!filename.isNull())
00287                 {
00288                         FILE *fp = fopen(filename.toAscii().constData(),"w");
00289                         if(fp)
00290                         {
00291                                 for(list<point>::iterator p = point_list.begin(); p != point_list.end();p++)
00292                                         fprintf(fp,"%d 1:%f 2:%f\n", p->value, p->x, p->y);
00293                                 fclose(fp);
00294                         }
00295                 }
00296         }
00297         void button_load_clicked()
00298         {
00299                 QString filename = QFileDialog::getOpenFileName();
00300                 if(!filename.isNull())
00301                 {
00302                         FILE *fp = fopen(filename.toAscii().constData(),"r");
00303                         if(fp)
00304                         {
00305                                 clear_all();
00306                                 char buf[4096];
00307                                 while(fgets(buf,sizeof(buf),fp))
00308                                 {
00309                                         int v;
00310                                         double x,y;
00311                                         if(sscanf(buf,"%d%*d:%lf%*d:%lf",&v,&x,&y)!=3)
00312                                                 break;                                                                                                  
00313                                         point p = {x,y,v};
00314                                         point_list.push_back(p);
00315                                 }
00316                                 fclose(fp);
00317                                 draw_all_points();
00318                         }                               
00319                 }
00320                 
00321         }
00322 };
00323 
00324 #include "svm-toy.moc"
00325 
00326 SvmToyWindow::SvmToyWindow()
00327 :button_change_icon(this)
00328 ,button_run("Run",this)
00329 ,button_clear("Clear",this)
00330 ,button_save("Save",this)
00331 ,button_load("Load",this)
00332 ,input_line(this)
00333 ,current_value(1)
00334 {
00335         buffer = QPixmap(XLEN,YLEN);
00336         buffer.fill(Qt::black);
00337 
00338         buffer_painter.begin(&buffer);
00339 
00340         QObject::connect(&button_change_icon, SIGNAL(clicked()), this,
00341                          SLOT(button_change_icon_clicked()));
00342         QObject::connect(&button_run, SIGNAL(clicked()), this,
00343                          SLOT(button_run_clicked()));
00344         QObject::connect(&button_clear, SIGNAL(clicked()), this,
00345                          SLOT(button_clear_clicked()));
00346         QObject::connect(&button_save, SIGNAL(clicked()), this,
00347                          SLOT(button_save_clicked()));
00348         QObject::connect(&button_load, SIGNAL(clicked()), this,
00349                          SLOT(button_load_clicked()));
00350         QObject::connect(&input_line, SIGNAL(returnPressed()), this,
00351                          SLOT(button_run_clicked()));
00352 
00353         // don't blank the window before repainting
00354         setAttribute(Qt::WA_NoBackground);
00355   
00356         icon1 = QPixmap(4,4);
00357         icon2 = QPixmap(4,4);
00358         icon3 = QPixmap(4,4);
00359         
00360         
00361         QPainter painter;
00362         painter.begin(&icon1);
00363         painter.fillRect(0,0,4,4,QBrush(colors[4]));
00364         painter.end();
00365 
00366         painter.begin(&icon2);
00367         painter.fillRect(0,0,4,4,QBrush(colors[5]));
00368         painter.end();
00369 
00370         painter.begin(&icon3);
00371         painter.fillRect(0,0,4,4,QBrush(colors[6]));
00372         painter.end();
00373 
00374         button_change_icon.setGeometry( 0, YLEN, 50, 25 );
00375         button_run.setGeometry( 50, YLEN, 50, 25 );
00376         button_clear.setGeometry( 100, YLEN, 50, 25 );
00377         button_save.setGeometry( 150, YLEN, 50, 25);
00378         button_load.setGeometry( 200, YLEN, 50, 25);
00379         input_line.setGeometry( 250, YLEN, 250, 25);
00380         
00381         input_line.setText(DEFAULT_PARAM);
00382         button_change_icon.setIcon(icon1);
00383 }
00384 
00385 SvmToyWindow::~SvmToyWindow()
00386 {
00387         buffer_painter.end();
00388 }
00389 
00390 void SvmToyWindow::mousePressEvent( QMouseEvent* event )
00391 {
00392         point p = {(double)event->x()/XLEN, (double)event->y()/YLEN, current_value};
00393         point_list.push_back(p);
00394         draw_point(p);
00395 }
00396 
00397 void SvmToyWindow::paintEvent( QPaintEvent* )
00398 {
00399         // copy the image from the buffer pixmap to the window
00400         QPainter p(this);
00401         p.drawPixmap(0, 0, buffer);
00402 }
00403 
00404 int main( int argc, char* argv[] )
00405 {
00406         QApplication myapp( argc, argv );
00407 
00408         SvmToyWindow* mywidget = new SvmToyWindow();
00409         mywidget->setGeometry( 100, 100, XLEN, YLEN+25 );
00410 
00411         mywidget->show();
00412         return myapp.exec();
00413 }


libsvm3
Author(s): various
autogenerated on Wed Nov 27 2013 11:36:23