00001 // Copyright (C) 2010-2011 Institut de Robotica i Informatica Industrial, CSIC-UPC. 00002 // Author 00003 // All rights reserved. 00004 // 00005 // This file is part of iri-ros-pkg 00006 // iri-ros-pkg is free software: you can redistribute it and/or modify 00007 // it under the terms of the GNU Lesser General Public License as published by 00008 // the Free Software Foundation, either version 3 of the License, or 00009 // at your option) any later version. 00010 // 00011 // This program is distributed in the hope that it will be useful, 00012 // but WITHOUT ANY WARRANTY; without even the implied warranty of 00013 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00014 // GNU Lesser General Public License for more details. 00015 // 00016 // You should have received a copy of the GNU Lesser General Public License 00017 // along with this program. If not, see <http://www.gnu.org/licenses/>. 00018 // 00019 // IMPORTANT NOTE: This code has been generated through a script from the 00020 // iri_ros_scripts. Please do NOT delete any comments to guarantee the correctness 00021 // of the scripts. ROS topics can be easly add by using those scripts. Please 00022 // refer to the IRI wiki page for more information: 00023 // http://wikiri.upc.es/index.php/Robotics_Lab 00024 00025 #ifndef _svmsgd_alg_h_ 00026 #define _svmsgd_alg_h_ 00027 00028 #include <iri_svmsgd/SvmsgdConfig.h> 00029 #include "mutex.h" 00030 #include <vector> 00031 //#include <iri_svmsgd/learn_example.h> 00032 00033 //include svmsgd_alg main library 00034 00035 /* Available losses */ 00036 #define HINGELOSS 1 00037 #define SMOOTHHINGELOSS 2 00038 #define SQUAREDHINGELOSS 3 00039 #define LOGLOSS 10 00040 #define LOGLOSSMARGIN 11 00041 00042 /* One when bias is regularized */ 00043 #define REGULARIZEBIAS 0 00044 00045 00051 class SvmsgdAlgorithm 00052 { 00053 protected: 00060 CMutex alg_mutex_; 00061 00062 // private attributes and methods 00063 int num_iters; 00064 int loss_type; 00065 int d; 00066 double *w; 00067 double bias; 00068 00069 double lambda; 00070 int use_bias; 00071 int use_reg_bias; 00072 double bscale; 00073 int count; 00074 int skip; 00075 double t; 00076 double wscale; 00077 bool calibrated; 00078 00079 inline void add_to_dense_vec(const std::vector<float> &vec1, double alpha, double* vec2) 00080 { 00081 for (int i=0; i<this->d; i++) 00082 vec2[i]+=alpha*vec1[i]; 00083 } 00084 00085 inline void add_to_dense_vec_abs(const std::vector<float> &vec1, double alpha, double* vec2) 00086 { 00087 for (int i=0; i<this->d; i++) 00088 vec2[i]+=alpha*abs(vec1[i]); 00089 } 00090 00091 inline double dot(const std::vector<float> &v1, const double* v2, int n) 00092 { 00093 double r=0; 00094 for(int i=0; i<n; i++) 00095 r+=v1[i]*v2[i]; 00096 return r; 00097 } 00098 00099 //todo define how to calibrate 00100 double dloss(double z); 00101 double loss(double z); 00102 00103 public: 00110 typedef iri_svmsgd::SvmsgdConfig Config; 00111 00118 Config config_; 00119 00128 SvmsgdAlgorithm(void); 00129 00135 void lock(void) { alg_mutex_.enter(); }; 00136 00142 void unlock(void) { alg_mutex_.exit(); }; 00143 00151 bool try_enter(void) { return alg_mutex_.try_enter(); }; 00152 00164 void config_update(Config& new_cfg, uint32_t level=0); 00165 00166 // here define all svmsgd_alg interface methods to retrieve and set 00167 // the driver parameters 00168 00175 ~SvmsgdAlgorithm(void); 00176 //arnau 20/9/12 to be reviewed! void calibrate(const std::vector<iri_svmsgd::learn_example> &train_matrix); 00177 void process_new_example(std::vector<float> &new_example, double label); 00178 inline float get_bias(){return this->bias;} 00179 inline std::vector<float> get_w() 00180 { 00181 std::vector<float> wout(this->d); 00182 for(int i=0;i<this->d;i++) 00183 wout[i]=this->w[i]; 00184 return wout; 00185 } 00186 }; 00187 00188 #endif