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


haf_grasping
Author(s): David Fischinger
autogenerated on Wed Jan 11 2017 03:48:49